From 7a235c25dfd2c486fa76b4a7225348b8791918b9 Mon Sep 17 00:00:00 2001 From: Volodymyr Shvydkyi Date: Fri, 5 Jul 2024 12:59:17 +0300 Subject: [PATCH] Fix picture selector behavior (#2964) Fixes a few issues: 1. Checkbox becomes invisible after selecting/unselecting an image 2. Selection toolbar is not visible when one or multiple images are selected Updates pictures select all behavior: - when no pictures selected then all pictures selected - when one/many pictures selected then all pictures selected instead of toggling of picture selection state Closes #2960 --- .../alchemy_admin/picture_selector.js | 48 +++++++++++--- .../alchemy/admin/pictures/index.html.erb | 10 --- .../alchemy_admin/picture_selector.spec.js | 63 +++++++++++++++++++ 3 files changed, 101 insertions(+), 20 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..3088efff26 100644 --- a/app/javascript/alchemy_admin/picture_selector.js +++ b/app/javascript/alchemy_admin/picture_selector.js @@ -1,34 +1,62 @@ import { on } from "alchemy_admin/utils/events" +function toggleCheckboxes(state) { + document + .querySelectorAll(".picture_tool.select input[type='checkbox']") + .forEach((checkbox) => { + checkbox.checked = state + checkbox.closest(".picture_thumbnail").classList.toggle("active", state) + }) +} + +function checkedInputs() { + return document.querySelectorAll("#picture_archive input:checked") +} + +function editMultiplePicturesUrl(href) { + const searchParameters = new URLSearchParams() + checkedInputs().forEach((entry) => + searchParameters.append(entry.name, entry.value) + ) + const url = href + "?" + searchParameters.toString() + + return url +} + /** * 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") + + toggleCheckboxes(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 on("click", ".selected_item_tools", "a#edit_multiple_pictures", (event) => { event.preventDefault() - const searchParameters = new URLSearchParams() - checkedInputs().forEach((entry) => - searchParameters.append(entry.name, entry.value) - ) - const url = event.target.href + "?" + searchParameters.toString() + const url = editMultiplePicturesUrl(event.target.href) Alchemy.openDialog(url, { title: event.target.title, 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 @@