Extract the needed portion of code to a new partial to be able to update only the elements needed when a new comment is added keeping UI properly updated.
51 lines
1.6 KiB
JavaScript
51 lines
1.6 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);
|
|
});
|
|
},
|
|
update_responses_count: function(comment_id, responses_count_html) {
|
|
$(comment_id + "_reply .responses-count").html(responses_count_html);
|
|
},
|
|
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);
|