Files
grecia/app/assets/javascripts/comments.js.coffee
Javi Martín cc7e0d586b Reduce local variables usage in CoffeeScript
Local variables are one of the things CoffeeScript doesn't compile to
modern JavaScript automatically: it uses `var` instead of `const` or
`let`.

Besides, using `$this = $(this)` is usually done to reference the
current object in another function where the current object is a
different one. Here we were using it with no clear purpose.
2019-09-11 03:14:17 +02:00

59 lines
1.9 KiB
CoffeeScript

"use strict"
App.Comments =
add_comment: (parent_id, response_html) ->
$(response_html).insertAfter($("#js-comment-form-#{parent_id}"))
this.update_comments_count()
add_reply: (parent_id, response_html) ->
if $("##{parent_id} .comment-children").length == 0
$("##{parent_id}").append("<li><ul id='#{parent_id}_children' class='no-bullet comment-children'></ul></li>")
$("##{parent_id} .comment-children:first").prepend($(response_html))
this.update_comments_count()
update_comments_count: ->
$(".js-comments-count").each ->
new_val = $(this).text().trim().replace /\d+/, (match) -> parseInt(match, 10) + 1
$(this).text(new_val)
display_error: (field_with_errors, error_html) ->
$(error_html).insertAfter($("#{field_with_errors}"))
reset_and_hide_form: (id) ->
form_container = $("#js-comment-form-#{id}")
input = form_container.find("form textarea")
input.val("")
form_container.hide()
reset_form: (id) ->
input = $("#js-comment-form-#{id} form textarea")
input.val("")
toggle_form: (id) ->
$("#js-comment-form-#{id}").toggle()
toggle_arrow: (id) ->
arrow = "span##{id}_arrow"
if $(arrow).hasClass("icon-arrow-right")
$(arrow).removeClass("icon-arrow-right").addClass("icon-arrow-down")
else
$(arrow).removeClass("icon-arrow-down").addClass("icon-arrow-right")
initialize: ->
$("body .js-add-comment-link").each ->
unless $(this).data("initialized") is "yes"
$(this).on("click", ->
App.Comments.toggle_form($(this).data().id)
false
).data "initialized", "yes"
$("body .js-toggle-children").each ->
$(this).on("click", ->
children_container_id = "#{$(this).data().id}_children"
$("##{children_container_id}").toggle("slow")
App.Comments.toggle_arrow(children_container_id)
$(this).children(".js-child-toggle").toggle()
false
)