added options parameter to navigate

This commit is contained in:
Miodec 2022-07-20 15:56:23 +02:00
parent d548a9bb84
commit 30ea71158c

View file

@ -15,6 +15,11 @@ import { Auth } from "../firebase";
//source: https://www.youtube.com/watch?v=OstALBk-jTc
// https://www.youtube.com/watch?v=OstALBk-jTc
//this will be used in tribe
interface NavigateOptions {
empty?: boolean;
}
function pathToRegex(path: string): RegExp {
return new RegExp(
"^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"
@ -34,7 +39,10 @@ function getParams(match: { route: Route; result: RegExpMatchArray }): {
interface Route {
path: string;
load: (params: { [key: string]: string }) => void;
load: (
params: { [key: string]: string },
navigateOptions: NavigateOptions
) => void;
}
const routes: Route[] = [
@ -101,7 +109,10 @@ const routes: Route[] = [
},
];
export function navigate(url = window.location.pathname): void {
export function navigate(
url = window.location.pathname,
options = {} as NavigateOptions
): void {
if (
TestUI.testRestarting ||
TestUI.resultCalculating ||
@ -112,10 +123,10 @@ export function navigate(url = window.location.pathname): void {
url = url.replace(/\/$/, "");
if (url === "") url = "/";
history.pushState(null, "", url);
router();
router(options);
}
async function router(): Promise<void> {
async function router(options = {} as NavigateOptions): Promise<void> {
const matches = routes.map((r) => {
return {
route: r,
@ -133,10 +144,12 @@ async function router(): Promise<void> {
return;
}
match.route.load(getParams(match));
match.route.load(getParams(match), options);
}
window.addEventListener("popstate", router);
window.addEventListener("popstate", () => {
router();
});
document.addEventListener("DOMContentLoaded", () => {
document.body.addEventListener("click", (e) => {