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

95 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-12-28 15:50:14 -08:00
# Toggle button
$(document).on "click", "button[name=mod-comments]", ->
btn = $(this)
id = btn[0].dataset.id
state = btn[0].dataset.state
commentBox = $("#mod-comments-section-#{id}")
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=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
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/mod/create_comment'
type: 'POST'
data:
id: id
comment: input.val()
dataType: 'json' # jQuery can't guess the datatype correctly here...
success: (data, status, jqxhr) ->
console.log data
if data.success
$("#mod-comments-#{id}").html data.render
input.val ''
ctr.html 160
$("span#mod-comment-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) ->
input.removeAttr 'disabled'
# character count
$(document).on "input", "input[name=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
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'
ctr.addClass 'text-muted'
2014-12-28 16:05:30 -08:00
# destroy
$(document).on "click", "a[data-action=mod-comment-destroy]", (ev) ->
ev.preventDefault()
2015-01-03 10:49:47 -08:00
btn = $(this)
cid = btn[0].dataset.id
2015-01-03 08:36:55 -08:00
swal
title: "Really delete?"
text: "You will not be able to recover this comment."
2015-01-03 08:36:55 -08:00
type: "warning"
showCancelButton: true
confirmButtonColor: "#DD6B55"
confirmButtonText: "Delete"
2015-01-03 10:49:47 -08:00
closeOnConfirm: true
2015-01-03 08:36:55 -08:00
, ->
2014-12-28 16:05:30 -08:00
$.ajax
url: '/ajax/mod/destroy_comment'
type: 'POST'
data:
comment: cid
success: (data, status, jqxhr) ->
if data.success
$("li[data-comment-id=#{cid}]").slideUp()
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) ->