diff --git a/frontend/js/main.js b/frontend/js/main.js index ef29427..1be53df 100644 --- a/frontend/js/main.js +++ b/frontend/js/main.js @@ -806,15 +806,21 @@ function showModal(html, onSubmit) { `; + // Suspend parent modal if sub-modal opening on top + const parentOverlay = document.querySelector(".modal-overlay"); + if (parentOverlay && parentOverlay._suspend) parentOverlay._suspend(); + document.body.appendChild(overlay); // Simple history stack: each open = pushState, each back = popState pops it. // Modal = 1 entry. Fullscreen map/chart inside modal = 1 more entry each. // Back always pops the top entry. No re-pushing, no flags. let _historyDepth = 1; // how many entries this modal owns + let _suspended = false; // true when sub-modal is open on top history.pushState({ modal: true }, ""); const popHandler = () => { + if (_suspended) return; // sub-modal handles this popstate _historyDepth--; // Check what's on top and collapse it @@ -855,13 +861,19 @@ function showModal(html, onSubmit) { history.pushState({ modalFs: true }, ""); }; + // Suspend/resume this modal's popHandler when a sub-modal opens on top + overlay._suspend = function() { _suspended = true; }; + overlay._resume = function() { _suspended = false; }; + function closeModal() { window.removeEventListener("popstate", popHandler); overlay.remove(); - // Pop all entries this modal owns - if (_historyDepth > 0) { - for (let i = 0; i < _historyDepth; i++) history.back(); - } + // Resume parent modal if this was a sub-modal + const parent = document.querySelector(".modal-overlay"); + if (parent && parent._resume) parent._resume(); + // Pop this modal's history entry + _modalPopHandled = true; + history.back(); } overlay.querySelector("#modal-back").onclick = () => closeModal();