2014-12-28 14:26:16 -08:00
|
|
|
($ document).on "click", "button[name=mod-vote]", ->
|
|
|
|
btn = $(this)
|
|
|
|
id = btn[0].dataset.id
|
|
|
|
action = btn[0].dataset.action
|
|
|
|
upvote = btn[0].dataset.voteType == 'upvote'
|
|
|
|
btn.attr 'disabled', 'disabled'
|
|
|
|
|
|
|
|
target_url = switch action
|
|
|
|
when 'vote'
|
|
|
|
'/ajax/mod/create_vote'
|
|
|
|
when 'unvote'
|
|
|
|
'/ajax/mod/destroy_vote'
|
|
|
|
|
|
|
|
success = false
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: target_url
|
|
|
|
type: 'POST'
|
|
|
|
data:
|
|
|
|
id: id
|
|
|
|
upvote: upvote
|
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
success = data.success
|
|
|
|
if success
|
|
|
|
($ "span#mod-count-#{id}").html(data.count)
|
|
|
|
showNotification data.message, data.success
|
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
|
|
|
showNotification "An error occurred, a developer should check the console for details", false
|
|
|
|
complete: (jqxhr, status) ->
|
|
|
|
if success
|
|
|
|
switch action
|
|
|
|
when 'vote'
|
2014-12-28 14:57:07 -08:00
|
|
|
btn.attr 'disabled', 'disabled'
|
2014-12-28 14:26:16 -08:00
|
|
|
btn[0].dataset.action = 'unvote'
|
2014-12-28 14:57:07 -08:00
|
|
|
console.log("vote for #{upvote ? 'downvote' : 'upvote'}")
|
|
|
|
other_btn = $ "button[name=mod-vote][data-id=#{id}][data-vote-type=#{if upvote then 'downvote' else 'upvote'}]"
|
|
|
|
other_btn.removeAttr 'disabled'
|
2014-12-28 14:26:16 -08:00
|
|
|
other_btn[0].dataset.action = 'unvote'
|
|
|
|
when 'unvote'
|
2014-12-28 14:57:07 -08:00
|
|
|
btn.removeAttr 'disabled'
|
2014-12-28 14:26:16 -08:00
|
|
|
btn[0].dataset.action = 'vote'
|
2014-12-28 14:57:07 -08:00
|
|
|
console.log("vote for #{upvote ? 'downvote' : 'upvote'}")
|
|
|
|
other_btn = $ "button[name=mod-vote][data-id=#{id}][data-vote-type=#{if upvote then 'downvote' else 'upvote'}]"
|
|
|
|
other_btn.removeAttr 'disabled', 'disabled'
|
2014-12-28 14:26:16 -08:00
|
|
|
other_btn[0].dataset.action = 'vote'
|