From 7d90c3852406c2882b1e5ebca4ac8e3c2f03fe97 Mon Sep 17 00:00:00 2001 From: artisticMink Date: Mon, 23 Oct 2023 17:30:07 +0200 Subject: [PATCH] Sync Bulk Edit and CharacterGroupOverlay state --- public/scripts/CharacterGroupOverlay.js | 33 ++++++++++--------- public/scripts/bulk-edit.js | 43 ++++++++++++++++--------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/public/scripts/CharacterGroupOverlay.js b/public/scripts/CharacterGroupOverlay.js index e5c4e9993..918bf88b6 100644 --- a/public/scripts/CharacterGroupOverlay.js +++ b/public/scripts/CharacterGroupOverlay.js @@ -10,7 +10,7 @@ import {event_types, eventSource} from "../script.js"; */ let characterGroupOverlayInstance = null; -class State { +class CharacterGroupOverlayState { static browse = 0; static select = 1; } @@ -22,9 +22,9 @@ class CharacterGroupOverlay { static selectModeClass = 'group_overlay_mode_select'; static selectedClass = 'character_selected'; - #state = State.browse; + #state = CharacterGroupOverlayState.browse; #longPress = false; - #longPressEndCallbacks = []; + #stateChangeCallbacks = []; #selectedCharacters = []; /** @@ -54,8 +54,8 @@ class CharacterGroupOverlay { this.#longPress = longPress; } - get longPressEndCallbacks() { - return this.#longPressEndCallbacks; + get stateChangeCallbacks() { + return this.#stateChangeCallbacks; } get selectedCharacters() { @@ -73,14 +73,13 @@ class CharacterGroupOverlay { characterGroupOverlayInstance = Object.freeze(this); } - browseState = () => this.state = State.browse; - selectState = () => this.state = State.select; + browseState = () => this.state = CharacterGroupOverlayState.browse; + selectState = () => this.state = CharacterGroupOverlayState.select; /** * Set up a Sortable grid for the loaded page */ onPageLoad = () => { - let touchState = false; const grid = document.getElementById(CharacterGroupOverlay.containerId); const sortable = new Sortable(grid, { @@ -129,7 +128,7 @@ class CharacterGroupOverlay { this.isLongPress = true; setTimeout(() => { if (this.isLongPress) { - this.state = State.select; + this.state = CharacterGroupOverlayState.select; } }, 3000); } @@ -139,25 +138,27 @@ class CharacterGroupOverlay { } handleCancelClick = () => { - this.state = State.browse; + this.state = CharacterGroupOverlayState.browse; } handleStateChange = () => { switch (this.state) { - case State.browse: + case CharacterGroupOverlayState.browse: this.container.classList.remove(CharacterGroupOverlay.selectModeClass); [...this.container.getElementsByClassName(CharacterGroupOverlay.characterClass)] .forEach(element => element.removeEventListener('click', this.toggleCharacterSelected)); - this.longPressEndCallbacks.forEach(callback => callback()); this.clearSelectedCharacters(); this.disableContextMenu(); break; - case State.select: + case CharacterGroupOverlayState.select: this.container.classList.add(CharacterGroupOverlay.selectModeClass); [...this.container.getElementsByClassName(CharacterGroupOverlay.characterClass)] .forEach(element => element.addEventListener('click', this.toggleCharacterSelected)); this.enableContextMenu(); + break; } + + this.stateChangeCallbacks.forEach(callback => callback(this.state)); } toggleCharacterSelected = event => { @@ -194,7 +195,9 @@ class CharacterGroupOverlay { } } - addLongPressEndCallback = callback => this.longPressEndCallbacks.push(callback); + handleContextMenuFavorite = () => this.selectedCharacters.forEach(characterId => CharacterContextMenu.toggleFavorite(characterId)); + + addStateChangeCallback = callback => this.stateChangeCallbacks.push(callback); selectCharacter = characterId => this.selectedCharacters.push(String(characterId)); @@ -206,4 +209,4 @@ class CharacterGroupOverlay { } } -export {CharacterGroupOverlay}; +export {CharacterGroupOverlayState, CharacterContextMenu, CharacterGroupOverlay}; diff --git a/public/scripts/bulk-edit.js b/public/scripts/bulk-edit.js index 062ac1b8b..83de126bc 100644 --- a/public/scripts/bulk-edit.js +++ b/public/scripts/bulk-edit.js @@ -1,12 +1,36 @@ import { characters, getCharacters, handleDeleteCharacter, callPopup } from "../script.js"; -import {CharacterGroupOverlay} from "../../CharacterGroupOverlay.js"; +import {CharacterGroupOverlay, CharacterGroupOverlayState} from "./CharacterGroupOverlay.js"; + let is_bulk_edit = false; -(new CharacterGroupOverlay()).addLongPressEndCallback(() => { +const enableBulkEdit = () => { + enableBulkSelect(); + (new CharacterGroupOverlay()).selectState(); + // show the delete button + $("#bulkDeleteButton").show(); + is_bulk_edit = true; +} + +const disableBulkEdit = () => { disableBulkSelect(); + (new CharacterGroupOverlay()).browseState(); + // hide the delete button $("#bulkDeleteButton").hide(); is_bulk_edit = false; +} + +const toggleBulkEditMode = (isBulkEdit) => { + if (isBulkEdit) { + disableBulkEdit(); + } else { + enableBulkEdit(); + } +} + +(new CharacterGroupOverlay()).addStateChangeCallback((state) => { + if (state === CharacterGroupOverlayState.select) enableBulkEdit(); + if (state === CharacterGroupOverlayState.browse) disableBulkEdit(); }); /** @@ -14,20 +38,7 @@ let is_bulk_edit = false; */ function onEditButtonClick() { console.log("Edit button clicked"); - // toggle bulk edit mode - if (is_bulk_edit) { - disableBulkSelect(); - (new CharacterGroupOverlay()).browseState(); - // hide the delete button - $("#bulkDeleteButton").hide(); - is_bulk_edit = false; - } else { - enableBulkSelect(); - (new CharacterGroupOverlay()).selectState(); - // show the delete button - $("#bulkDeleteButton").show(); - is_bulk_edit = true; - } + toggleBulkEditMode(is_bulk_edit); } /**