Retrospring/app/assets/javascripts/answerbox/comment.coffee

69 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-12-08 06:42:34 -08:00
# Toggle button
$(document).on "click", "button[name=ab-comments]", ->
btn = $(this)
aid = btn[0].dataset.aId
state = btn[0].dataset.state
2014-12-14 22:47:56 -08:00
commentBox = $("#ab-comments-section-#{aid}")
2014-12-08 06:42:34 -08:00
switch state
when 'hidden'
commentBox.slideDown()
btn[0].dataset.state = 'shown'
when 'shown'
commentBox.slideUp()
btn[0].dataset.state = 'hidden'
$(document).on "keyup", "input[name=ab-comment-new]", (evt) ->
input = $(this)
aid = input[0].dataset.aId
ctr = $("span#ab-comment-charcount-#{aid}")
cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]")
if evt.which == 13 # return key
evt.preventDefault()
return cbox.addClass "has-error" if input.val().length > 160 || input.val().trim().length == 0
input.attr 'disabled', 'disabled'
$.ajax
url: '/ajax/create_comment'
type: 'POST'
data:
answer: aid
comment: input.val()
dataType: 'json' # jQuery can't guess the datatype correctly here...
success: (data, status, jqxhr) ->
console.log data
if data.success
2014-12-14 22:47:56 -08:00
$("#ab-comments-#{aid}").html data.render
2014-12-08 06:42:34 -08:00
input.val ''
ctr.html 160
$("span#ab-comment-count-#{aid}").html data.count
2015-04-20 18:12:11 -07:00
subs = $("a[data-action=ab-submarine][data-a-id=#{aid}]")[0]
subs.dataset.torpedo = "no"
2015-06-08 11:35:16 -07:00
subs.children[0].nextSibling.textContent = translate('views.actions.unsubscribe')
2014-12-08 06:42:34 -08:00
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) ->
input.removeAttr 'disabled'
# character count
$(document).on "input", "input[name=ab-comment-new]", (evt) ->
input = $(this)
aid = input[0].dataset.aId
ctr = $("span#ab-comment-charcount-#{aid}")
cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]")
cbox.removeClass "has-error" if cbox.hasClass "has-error"
ctr.html 160 - input.val().length
if Number(ctr.html()) < 0
ctr.removeClass 'text-muted'
ctr.addClass 'text-danger'
else
ctr.removeClass 'text-danger'
2014-12-14 22:47:56 -08:00
ctr.addClass 'text-muted'