From 2beac62fc3964c220293d084b7087c8675dc1a9a Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 21 Jan 2021 20:08:14 -0600 Subject: [PATCH] comments: move Javascript into component. This is a minor breaking changing for userscripts that may have tried to hook into our comment Javascript. --- .../comment_component/comment_component.js | 34 ++++++++++++++++++ app/javascript/packs/application.js | 2 +- app/javascript/src/javascripts/comments.js | 35 ------------------- 3 files changed, 35 insertions(+), 36 deletions(-) create mode 100644 app/components/comment_component/comment_component.js delete mode 100644 app/javascript/src/javascripts/comments.js diff --git a/app/components/comment_component/comment_component.js b/app/components/comment_component/comment_component.js new file mode 100644 index 000000000..875cab5da --- /dev/null +++ b/app/components/comment_component/comment_component.js @@ -0,0 +1,34 @@ +class CommentComponent { + static initialize() { + if ($("#c-posts #a-show, #c-comments").length) { + $(document).on("click.danbooru.comment", ".edit_comment_link", CommentComponent.showEditForm); + $(document).on("click.danbooru.comment", ".expand-comment-response", CommentComponent.showNewCommentForm); + $(document).on("click.danbooru.comment", ".unhide-comment-link", CommentComponent.unhideComment); + } + } + + static showNewCommentForm(e) { + $(e.target).hide(); + var $form = $(e.target).closest("div.new-comment").find("form"); + $form.show(); + $form[0].scrollIntoView(false); + e.preventDefault(); + } + + static showEditForm(e) { + $(this).closest(".comment").find(".edit_comment").show(); + e.preventDefault(); + } + + static unhideComment(e) { + let $comment = $(this).closest(".comment"); + $comment.find(".unhide-comment-link").hide(); + $comment.find(".body").show(); + e.preventDefault(); + } +} + + +$(document).ready(CommentComponent.initialize); + +export default CommentComponent; diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index ed816dae2..81bde903b 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -34,7 +34,7 @@ importAll(require.context('../../components', true, /\.s?css(?:\.erb)?$/)); export { default as jQuery } from "jquery"; export { default as Autocomplete } from '../src/javascripts/autocomplete.js.erb'; export { default as Blacklist } from '../src/javascripts/blacklists.js'; -export { default as Comment } from '../src/javascripts/comments.js'; +export { default as CommentComponent } from "../../components/comment_component/comment_component.js"; export { default as CurrentUser } from '../src/javascripts/current_user.js'; export { default as Dtext } from '../src/javascripts/dtext.js'; export { default as IqdbQuery } from '../src/javascripts/iqdb_queries.js'; diff --git a/app/javascript/src/javascripts/comments.js b/app/javascript/src/javascripts/comments.js deleted file mode 100644 index 968c7888e..000000000 --- a/app/javascript/src/javascripts/comments.js +++ /dev/null @@ -1,35 +0,0 @@ -let Comment = {}; - -Comment.initialize_all = function() { - if ($("#c-posts").length || $("#c-comments").length) { - $(document).on("click.danbooru.comment", ".edit_comment_link", Comment.show_edit_form); - $(document).on("click.danbooru.comment", ".expand-comment-response", Comment.show_new_comment_form); - $(document).on("click.danbooru.comment", ".unhide-comment-link", Comment.unhide_comment); - } -} - -Comment.show_new_comment_form = function(e) { - $(e.target).hide(); - var $form = $(e.target).closest("div.new-comment").find("form"); - $form.show(); - $form[0].scrollIntoView(false); - e.preventDefault(); -} - -Comment.show_edit_form = function(e) { - $(this).closest(".comment").find(".edit_comment").show(); - e.preventDefault(); -} - -Comment.unhide_comment = function(e) { - let $comment = $(this).closest(".comment"); - $comment.find(".unhide-comment-link").hide(); - $comment.find(".body").show(); - e.preventDefault(); -} - -$(document).ready(function() { - Comment.initialize_all(); -}); - -export default Comment