close all dialogs on navigate

This commit is contained in:
Petar Petrov 2024-12-11 13:35:09 +02:00
parent 142e674020
commit c713106948
2 changed files with 34 additions and 25 deletions

View File

@ -1,3 +1,4 @@
import { closeAllDialogs } from "../dialogs/make-dialog-manager";
import { fireEvent } from "./dom/fire_event"; import { fireEvent } from "./dom/fire_event";
import { mainWindow } from "./dom/get_main_window"; import { mainWindow } from "./dom/get_main_window";
@ -13,35 +14,40 @@ export interface NavigateOptions {
data?: any; data?: any;
} }
export const navigate = (path: string, options?: NavigateOptions) => { export const navigate = async (path: string, options?: NavigateOptions) => {
const replace = options?.replace || false; const { history } = mainWindow;
let replace = options?.replace || false;
setTimeout(() => { if (history.state?.dialog) {
if (__DEMO__) { const closed = await closeAllDialogs();
if (replace) { if (!closed) {
mainWindow.history.replaceState( // block navigation if dialogs refuse to close
mainWindow.history.state?.root return;
? { root: true } }
: (options?.data ?? null), // if there were open dialogs, we discard the current state
"", replace = true;
`${mainWindow.location.pathname}#${path}` }
);
} else { if (__DEMO__) {
mainWindow.location.hash = path; if (replace) {
} history.replaceState(
} else if (replace) { history.state?.root ? { root: true } : (options?.data ?? null),
mainWindow.history.replaceState(
mainWindow.history.state?.root
? { root: true }
: (options?.data ?? null),
"", "",
path `${mainWindow.location.pathname}#${path}`
); );
} else { } else {
mainWindow.history.pushState(options?.data ?? null, "", path); mainWindow.location.hash = path;
} }
fireEvent(mainWindow, "location-changed", { } else if (replace) {
replace, history.replaceState(
}); history.state?.root ? { root: true } : (options?.data ?? null),
"",
path
);
} else {
history.pushState(options?.data ?? null, "", path);
}
fireEvent(mainWindow, "location-changed", {
replace,
}); });
}; };

View File

@ -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<DialogClosedParams>) => { const _handleClosed = async (ev: HASSDomEvent<DialogClosedParams>) => {
// If not closed by navigating back, remove the open state from history // If not closed by navigating back, remove the open state from history
const dialogIndex = OPEN_DIALOG_STACK.findIndex( const dialogIndex = OPEN_DIALOG_STACK.findIndex(