From c713106948e4700a8701cbc3d32e9c66c499cabe Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Wed, 11 Dec 2024 13:35:09 +0200 Subject: [PATCH] close all dialogs on `navigate` --- src/common/navigate.ts | 56 +++++++++++++++++------------- src/dialogs/make-dialog-manager.ts | 3 ++ 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/common/navigate.ts b/src/common/navigate.ts index 2e211715aa..46bf0d9b69 100644 --- a/src/common/navigate.ts +++ b/src/common/navigate.ts @@ -1,3 +1,4 @@ +import { closeAllDialogs } from "../dialogs/make-dialog-manager"; import { fireEvent } from "./dom/fire_event"; import { mainWindow } from "./dom/get_main_window"; @@ -13,35 +14,40 @@ export interface NavigateOptions { data?: any; } -export const navigate = (path: string, options?: NavigateOptions) => { - const replace = options?.replace || false; +export const navigate = async (path: string, options?: NavigateOptions) => { + const { history } = mainWindow; + let replace = options?.replace || false; - setTimeout(() => { - if (__DEMO__) { - if (replace) { - mainWindow.history.replaceState( - mainWindow.history.state?.root - ? { root: true } - : (options?.data ?? null), - "", - `${mainWindow.location.pathname}#${path}` - ); - } else { - mainWindow.location.hash = path; - } - } else if (replace) { - mainWindow.history.replaceState( - mainWindow.history.state?.root - ? { root: true } - : (options?.data ?? null), + if (history.state?.dialog) { + const closed = await closeAllDialogs(); + if (!closed) { + // block navigation if dialogs refuse to close + return; + } + // if there were open dialogs, we discard the current state + replace = true; + } + + if (__DEMO__) { + if (replace) { + history.replaceState( + history.state?.root ? { root: true } : (options?.data ?? null), "", - path + `${mainWindow.location.pathname}#${path}` ); } else { - mainWindow.history.pushState(options?.data ?? null, "", path); + mainWindow.location.hash = path; } - fireEvent(mainWindow, "location-changed", { - replace, - }); + } else if (replace) { + history.replaceState( + history.state?.root ? { root: true } : (options?.data ?? null), + "", + path + ); + } else { + history.pushState(options?.data ?? null, "", path); + } + fireEvent(mainWindow, "location-changed", { + replace, }); }; diff --git a/src/dialogs/make-dialog-manager.ts b/src/dialogs/make-dialog-manager.ts index a0fc6883d9..dd96d79cda 100644 --- a/src/dialogs/make-dialog-manager.ts +++ b/src/dialogs/make-dialog-manager.ts @@ -174,6 +174,9 @@ export const closeLastDialog = async () => { } }; +export const closeAllDialogs = () => + Promise.all(OPEN_DIALOG_STACK.map((state) => closeDialog(state.dialogTag))); + const _handleClosed = async (ev: HASSDomEvent) => { // If not closed by navigating back, remove the open state from history const dialogIndex = OPEN_DIALOG_STACK.findIndex(