2014-12-08 06:42:34 -08:00
|
|
|
$(document).on "click", "button[name=ab-smile]", ->
|
|
|
|
btn = $(this)
|
|
|
|
aid = btn[0].dataset.aId
|
|
|
|
action = btn[0].dataset.action
|
|
|
|
count = Number $("span#ab-smile-count-#{aid}").html()
|
2020-05-02 11:35:28 -07:00
|
|
|
btn[0].dataset.loadingText = "<i class=\"fa fa-fw fa-meh-o fa-spin\"></i> <span id=\"ab-smile-count-#{aid}\">#{count}</span>"
|
2014-12-08 06:42:34 -08:00
|
|
|
btn.button "loading"
|
|
|
|
|
|
|
|
target_url = switch action
|
|
|
|
when 'smile'
|
|
|
|
count++
|
|
|
|
'/ajax/create_smile'
|
|
|
|
when 'unsmile'
|
|
|
|
count--
|
|
|
|
'/ajax/destroy_smile'
|
|
|
|
|
|
|
|
success = false
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: target_url
|
|
|
|
type: 'POST'
|
|
|
|
data:
|
|
|
|
id: aid
|
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
success = data.success
|
|
|
|
if success
|
|
|
|
$("span#ab-smile-count-#{aid}").html(count)
|
|
|
|
showNotification data.message, data.success
|
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
2015-06-08 11:35:16 -07:00
|
|
|
showNotification translate('frontend.error.message'), false
|
2014-12-08 06:42:34 -08:00
|
|
|
complete: (jqxhr, status) ->
|
|
|
|
btn.button "reset"
|
2020-04-18 14:44:59 -07:00
|
|
|
window.setTimeout ->
|
|
|
|
if success
|
|
|
|
switch action
|
|
|
|
when 'smile'
|
|
|
|
btn[0].dataset.action = 'unsmile'
|
2020-05-02 11:35:28 -07:00
|
|
|
btn.html "<i class=\"fa fa-fw fa-frown-o\"></i> <span id=\"ab-smile-count-#{aid}\">#{count}</span>"
|
2020-04-18 14:44:59 -07:00
|
|
|
when 'unsmile'
|
|
|
|
btn[0].dataset.action = 'smile'
|
2020-05-02 11:35:28 -07:00
|
|
|
btn.html "<i class=\"fa fa-fw fa-smile-o\"></i> <span id=\"ab-smile-count-#{aid}\">#{count}</span>"
|
2020-04-18 14:44:59 -07:00
|
|
|
, 20
|