|
| 1 | +//@ts-check |
| 2 | + |
| 3 | +export const ModalEvents = Object.freeze({ |
| 4 | + ADD: 'modal:add', |
| 5 | + DELETE: 'modal:delete', |
| 6 | + OPEN: 'modal:open', |
| 7 | + CLOSE: 'modal:close', |
| 8 | + CLOSE_ALL: 'modal:closeAll', |
| 9 | + DESTROY: 'modal:destroy', |
| 10 | +}); |
| 11 | + |
| 12 | +/** @typedef {string} ModalId */ |
| 13 | + |
| 14 | +/** @typedef {import('../blocks/Modal/Modal.js').Modal} ModalNode */ |
| 15 | + |
| 16 | +/** @typedef {(data: { id: ModalId; modal: ModalNode }) => void} ModalCb */ |
| 17 | + |
| 18 | +/** @typedef {(typeof ModalEvents)[keyof ModalEvents]} ModalEventType */ |
| 19 | + |
| 20 | +export class ModalManager { |
| 21 | + /** |
| 22 | + * @private |
| 23 | + * @type {Map<ModalId, ModalNode>} |
| 24 | + */ |
| 25 | + _modals = new Map(); |
| 26 | + |
| 27 | + /** |
| 28 | + * @private |
| 29 | + * @type {Set<ModalId>} |
| 30 | + */ |
| 31 | + _activeModals = new Set(); |
| 32 | + |
| 33 | + /** |
| 34 | + * @private |
| 35 | + * @type {Map<ModalEventType, Set<ModalCb>>} |
| 36 | + */ |
| 37 | + _subscribers = new Map(); |
| 38 | + |
| 39 | + /** @param {import('./Block.js').Block} block */ |
| 40 | + constructor(block) { |
| 41 | + this._block = block; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @private |
| 46 | + * @param {unknown[]} args |
| 47 | + */ |
| 48 | + _debugPrint(...args) { |
| 49 | + this._block.debugPrint('[modal-manager]', ...args); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Register a modal with the manager |
| 54 | + * |
| 55 | + * @param {ModalId} id - Unique identifier for the modal |
| 56 | + * @param {ModalNode} modal - Modal component instance |
| 57 | + */ |
| 58 | + registerModal(id, modal) { |
| 59 | + this._modals.set(id, modal); |
| 60 | + this._notify(ModalEvents.ADD, { id, modal }); |
| 61 | + } |
| 62 | + |
| 63 | + /** @param {ModalId} id - Unique identifier for the modal */ |
| 64 | + deleteModal(id) { |
| 65 | + if (!this._modals.has(id)) return false; |
| 66 | + |
| 67 | + const modal = this._modals.get(id); |
| 68 | + this._modals.delete(id); |
| 69 | + this._activeModals.delete(id); |
| 70 | + this._notify(ModalEvents.DELETE, { id, modal }); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Open a modal by its ID |
| 76 | + * |
| 77 | + * @param {ModalId} id - The ID of the modal to open |
| 78 | + * @returns {boolean} - Success status |
| 79 | + */ |
| 80 | + open(id) { |
| 81 | + if (!this._modals.has(id)) { |
| 82 | + this._debugPrint(`Modal with ID "${id}" not found`); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + const modal = this._modals.get(id); |
| 87 | + |
| 88 | + this._activeModals.add(id); |
| 89 | + this._notify(ModalEvents.OPEN, { modal, id }); |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Close a specific modal by ID |
| 95 | + * |
| 96 | + * @param {ModalId} id - The ID of the modal to close |
| 97 | + * @returns {boolean} - Success status |
| 98 | + */ |
| 99 | + close(id) { |
| 100 | + if (!this._modals.has(id) || !this._activeModals.has(id)) { |
| 101 | + this._debugPrint(`Modal with ID "${id}" not found or not active`); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + const modal = this._modals.get(id); |
| 106 | + |
| 107 | + this._activeModals.delete(id); |
| 108 | + this._notify(ModalEvents.CLOSE, { id, modal }); |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Toggle a modal - open if closed, close if open |
| 114 | + * |
| 115 | + * @param {ModalId} id - The ID of the modal to toggle |
| 116 | + * @returns {boolean} - Success status |
| 117 | + */ |
| 118 | + toggle(id) { |
| 119 | + if (!this._modals.has(id)) { |
| 120 | + this._debugPrint(`Modal with ID "${id}" not found`); |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + if (this._activeModals.has(id)) { |
| 125 | + return this.close(id); |
| 126 | + } else { |
| 127 | + return this.open(id); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Check if any modals are currently active/open |
| 133 | + * |
| 134 | + * @returns {boolean} - True if there are any active modals |
| 135 | + */ |
| 136 | + get hasActiveModals() { |
| 137 | + return this._activeModals.size > 0; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Close the most recently opened modal and return to the previous one |
| 142 | + * |
| 143 | + * @returns {boolean} - Success status |
| 144 | + */ |
| 145 | + back() { |
| 146 | + if (this._activeModals.size === 0) { |
| 147 | + this._debugPrint('No active modals to go back from'); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // Get the last opened modal |
| 152 | + const lastModalId = Array.from(this._activeModals).pop(); |
| 153 | + return this.close(/** @type {string} */ (lastModalId)); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Close all open modals |
| 158 | + * |
| 159 | + * @returns {number} - Number of modals closed |
| 160 | + */ |
| 161 | + closeAll() { |
| 162 | + const count = this._activeModals.size; |
| 163 | + |
| 164 | + this._activeModals.clear(); |
| 165 | + this._notify(ModalEvents.CLOSE_ALL, {}); |
| 166 | + return count; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Subscribe to modal events |
| 171 | + * |
| 172 | + * @param {ModalEventType} event |
| 173 | + * @param {ModalCb} callback |
| 174 | + * @returns {() => void} |
| 175 | + */ |
| 176 | + subscribe(event, callback) { |
| 177 | + if (!this._subscribers.has(event)) { |
| 178 | + this._subscribers.set(event, new Set()); |
| 179 | + } |
| 180 | + this._subscribers.get(event)?.add(callback); |
| 181 | + |
| 182 | + return () => this.unsubscribe(event, callback); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Unsubscribe from modal events |
| 187 | + * |
| 188 | + * @param {ModalEventType} event |
| 189 | + * @param {ModalCb | undefined} callback |
| 190 | + */ |
| 191 | + unsubscribe(event, callback) { |
| 192 | + if (this._subscribers.has(event)) { |
| 193 | + this._subscribers.get(event)?.delete(/** @type {ModalCb} */ (callback)); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Notify all subscribers of a modal event |
| 199 | + * |
| 200 | + * @private |
| 201 | + * @param {ModalEventType} event - Event name |
| 202 | + * @param {{ |
| 203 | + * id: ModalId; |
| 204 | + * modal: ModalNode; |
| 205 | + * } |
| 206 | + * | object} data |
| 207 | + */ |
| 208 | + _notify(event, data) { |
| 209 | + if (this._subscribers.has(event)) { |
| 210 | + for (const callback of this._subscribers.get(event) ?? new Set()) { |
| 211 | + try { |
| 212 | + callback( |
| 213 | + /** |
| 214 | + * @type{{ |
| 215 | + * id: ModalId; |
| 216 | + * modal: ModalNode; |
| 217 | + * }} |
| 218 | + */ (data), |
| 219 | + ); |
| 220 | + } catch (error) { |
| 221 | + this._debugPrint('Error in modal subscriber:', error); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** Destroy the modal manager, clean up resources */ |
| 228 | + destroy() { |
| 229 | + this.closeAll(); |
| 230 | + this._modals.clear(); |
| 231 | + this._subscribers.clear(); |
| 232 | + this._notify(ModalEvents.DESTROY, {}); |
| 233 | + } |
| 234 | +} |
0 commit comments