Files
nairobi/app/assets/javascripts/comments.js
2020-05-12 23:57:57 +02:00

48 lines
1.5 KiB
JavaScript

(function() {
"use strict";
App.Comments = {
add_comment: function(parent_selector, response_html) {
$(parent_selector + " .comment-list:first").prepend($(response_html));
this.update_comments_count();
},
update_comments_count: function() {
$(".js-comments-count").each(function() {
var new_val;
new_val = $(this).text().trim().replace(/\d+/, function(match) {
return parseInt(match, 10) + 1;
});
$(this).text(new_val);
});
},
display_error: function(field_with_errors, error_html) {
$(error_html).insertAfter($("" + field_with_errors));
},
reset_form: function(parent_selector) {
var form_container;
form_container = $(parent_selector + " .comment-form:first");
form_container.find("textarea").val("");
if (parent_selector !== "") {
form_container.hide();
}
},
toggle_form: function(id) {
$("#js-comment-form-" + id).toggle();
},
initialize: function() {
$("body").on("click", ".js-add-comment-link", function() {
App.Comments.toggle_form($(this).data().id);
return false;
});
$("body").on("click", ".js-toggle-children", function() {
$(this).closest(".comment").find(".comment-list:first").toggle("slow");
$(this).children(".far").toggleClass("fa-minus-square fa-plus-square");
$(this).children(".js-child-toggle").toggle();
return false;
});
}
};
}).call(this);