Files
frontend/src/common/navigate.ts
Bram Kragten 4b9ff641ba Wait with navigate until history.back is done (#11152)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2022-01-10 15:30:21 -08:00

47 lines
1.1 KiB
TypeScript

import { historyPromise } from "../state/url-sync-mixin";
import { fireEvent } from "./dom/fire_event";
import { mainWindow } from "./dom/get_main_window";
declare global {
// for fire event
interface HASSDomEvents {
"location-changed": NavigateOptions;
}
}
export interface NavigateOptions {
replace?: boolean;
}
export const navigate = (path: string, options?: NavigateOptions) => {
const replace = options?.replace || false;
if (historyPromise) {
historyPromise.then(() => navigate(path, options));
return;
}
if (__DEMO__) {
if (replace) {
mainWindow.history.replaceState(
mainWindow.history.state?.root ? { root: true } : null,
"",
`${mainWindow.location.pathname}#${path}`
);
} else {
mainWindow.location.hash = path;
}
} else if (replace) {
mainWindow.history.replaceState(
mainWindow.history.state?.root ? { root: true } : null,
"",
path
);
} else {
mainWindow.history.pushState(null, "", path);
}
fireEvent(mainWindow, "location-changed", {
replace,
});
};