From 4966139fd1011c054980baba41a0008e2ea51692 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 5 Aug 2024 00:09:50 +0200 Subject: [PATCH] Improve performance of expand/close all WI button --- public/script.js | 18 +++++++++++++----- public/scripts/utils.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/public/script.js b/public/script.js index 2f3cc6e62..6cddb1ae6 100644 --- a/public/script.js +++ b/public/script.js @@ -156,6 +156,7 @@ import { ensureImageFormatSupported, flashHighlight, isTrueBoolean, + toggleDrawer, } from './scripts/utils.js'; import { debounce_timeout } from './scripts/constants.js'; @@ -10591,12 +10592,19 @@ jQuery(async function () { } }); - $(document).on('click', '#OpenAllWIEntries', function () { - $('#world_popup_entries_list').children().find('.down').click(); - }); - $(document).on('click', '#CloseAllWIEntries', function () { - $('#world_popup_entries_list').children().find('.up').click(); + document.addEventListener('click', function (e) { + if (!(e.target instanceof HTMLElement)) return; + if (e.target.matches('#OpenAllWIEntries')) { + document.querySelectorAll('#world_popup_entries_list .inline-drawer').forEach((/** @type {HTMLElement} */ drawer) => { + toggleDrawer(drawer, true); + }); + } else if (e.target.matches('#CloseAllWIEntries')) { + document.querySelectorAll('#world_popup_entries_list .inline-drawer').forEach((/** @type {HTMLElement} */ drawer) => { + toggleDrawer(drawer, false); + }); + } }); + $(document).on('click', '.open_alternate_greetings', openAlternateGreetings); /* $('#set_character_world').on('click', openCharacterWorldPopup); */ diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 7995a5987..adb29dd0c 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -1929,3 +1929,31 @@ export function getFreeName(name, list, numberFormatter = (n) => ` #${n}`) { } return `${name}${numberFormatter(counter)}`; } + + +/** + * Toggles the visibility of a drawer by changing the display style of its content. + * This function skips the usual drawer animation. + * + * @param {HTMLElement} drawer - The drawer element to toggle + * @param {boolean} [expand=true] - Whether to expand or collapse the drawer + */ +export function toggleDrawer(drawer, expand = true) { + /** @type {HTMLElement} */ + const icon = drawer.querySelector('.inline-drawer-icon'); + /** @type {HTMLElement} */ + const content = drawer.querySelector('.inline-drawer-content'); + + if (expand) { + icon.classList.remove('up', 'fa-circle-chevron-up'); + icon.classList.add('down', 'fa-circle-chevron-down'); + content.style.display = 'block'; + } else { + icon.classList.remove('down', 'fa-circle-chevron-down'); + icon.classList.add('up', 'fa-circle-chevron-up'); + content.style.display = 'none'; + } + + // Set the height of "autoSetHeight" textareas within the inline-drawer to their scroll height + content.querySelectorAll('textarea.autoSetHeight').forEach(resetScrollHeight); +}