From a7791635aa98f012d35f97c3c5288494bbb28e38 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 13 Mar 2024 18:03:43 +0100 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- app/javascript/controllers/comment_controller.js | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 app/javascript/controllers/comment_controller.js (limited to 'app/javascript/controllers/comment_controller.js') diff --git a/app/javascript/controllers/comment_controller.js b/app/javascript/controllers/comment_controller.js new file mode 100644 index 0000000..22167c8 --- /dev/null +++ b/app/javascript/controllers/comment_controller.js @@ -0,0 +1,36 @@ +import { Controller } from "@hotwired/stimulus" + +// Controller that handles actions for adding some dynamism like showing/hiding +// comments. +export default class extends Controller { + // Cancel the current operation for comments. + toggle(event) { + event.preventDefault(); + + const parent = event.target.closest('.comment'); + const regl_body = parent.getElementsByClassName('comment-body'); + const edit_body = parent.getElementsByClassName('comment-edit-body'); + if (regl_body.length !== 1 || edit_body.length !== 1) { + return; + } + + regl_body[0].classList.toggle('hidden'); + edit_body[0].classList.toggle('hidden'); + } + + // Hide/show the "New comment" button & form properly, while also focusing on + // the text area when shown. + toggleNewComment(event) { + event.preventDefault(); + + document.getElementById("comment-new-button").classList.toggle('hidden'); + + let body = document.getElementById("comment-new-body"); + body.classList.toggle('hidden'); + + // BUG (minor): this is not working! (bug in Trix?) + if (!body.classList.contains("hidden")) { + document.getElementById("comment_content").focus(); + } + } +} -- cgit v1.2.3