From 509e024b0a538ec468bdf470f224ae933d3c731a Mon Sep 17 00:00:00 2001 From: Volodymyr Shvydkyi Date: Tue, 2 Jul 2024 15:04:29 +0300 Subject: [PATCH 1/2] Fix picture selector behavior --- .../alchemy_admin/picture_selector.js | 22 ++++++- .../alchemy/admin/pictures/index.html.erb | 10 --- .../alchemy_admin/picture_selector.spec.js | 63 +++++++++++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 spec/javascript/alchemy_admin/picture_selector.spec.js diff --git a/app/javascript/alchemy_admin/picture_selector.js b/app/javascript/alchemy_admin/picture_selector.js index dace00271f..f3938b4ad3 100644 --- a/app/javascript/alchemy_admin/picture_selector.js +++ b/app/javascript/alchemy_admin/picture_selector.js @@ -4,20 +4,36 @@ import { on } from "alchemy_admin/utils/events" * Multiple picture select handler for the picture archive. */ export default function PictureSelector() { + const selectAllButton = document.querySelector("#select_all_pictures") const selectedItemTools = document.querySelector(".selected_item_tools") const checkedInputs = () => document.querySelectorAll("#picture_archive input:checked") + on("click", ".toolbar_buttons", "a#select_all_pictures", (event) => { + event.preventDefault() + + selectAllButton.classList.toggle("active") + + const state = selectAllButton.classList.contains("active") + + document + .querySelectorAll(".picture_tool.select input[type='checkbox']") + .forEach(function (checkbox) { + checkbox.checked = state + checkbox.closest(".picture_thumbnail").classList.toggle("active", state) + }) + + selectedItemTools.classList.toggle("hidden", !state) + }) + // make the item toolbar visible and show the checkbox also if it is not hovered anymore on("change", ".picture_tool.select", "input", (event) => { - selectedItemTools.style.display = - checkedInputs().length > 0 ? "block" : "none" + selectedItemTools.classList.toggle("hidden", checkedInputs().length === 0) const parentElementClassList = event.target.parentElement.classList const checked = event.target.checked parentElementClassList.toggle("visible", checked) - parentElementClassList.toggle("hidden", !checked) }) // open the edit view in a dialog modal diff --git a/app/views/alchemy/admin/pictures/index.html.erb b/app/views/alchemy/admin/pictures/index.html.erb index a73d23d87c..c25c2f5420 100644 --- a/app/views/alchemy/admin/pictures/index.html.erb +++ b/app/views/alchemy/admin/pictures/index.html.erb @@ -89,16 +89,6 @@