Files
nairobi/app/assets/javascripts/comments.js
Javi Martín 41b9d2ba01 Simplify code to show/collapse comment replies
Tests are also a bit easier to read, even though we need to use the
`text:` option to find links because otherwise the text in the hidden
`<span>` tags will cause `click_link` to miss the link we want to click.

Here's an explanation by one of Capybara's authors:
https://github.com/teamcapybara/capybara/issues/2347#issuecomment-626373440
2020-05-12 23:23:01 +02:00

57 lines
1.9 KiB
JavaScript

(function() {
"use strict";
App.Comments = {
add_comment: function(parent_id, response_html) {
$(response_html).insertAfter($("#js-comment-form-" + parent_id));
this.update_comments_count();
},
add_reply: function(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: 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_and_hide_form: function(id) {
var form_container, input;
form_container = $("#js-comment-form-" + id);
input = form_container.find("form textarea");
input.val("");
form_container.hide();
},
reset_form: function(id) {
var input;
input = $("#js-comment-form-" + id + " form textarea");
input.val("");
},
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).data().id + "_children").toggle("slow");
$(this).children(".far").toggleClass("fa-minus-square fa-plus-square");
$(this).children(".js-child-toggle").toggle();
return false;
});
}
};
}).call(this);