From 30ea71158c73a00c6debf0a04939d7721864411a Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 20 Jul 2022 15:56:23 +0200 Subject: [PATCH] added options parameter to navigate --- .../src/ts/controllers/route-controller.ts | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/frontend/src/ts/controllers/route-controller.ts b/frontend/src/ts/controllers/route-controller.ts index 7838bb937..a8fa6b771 100644 --- a/frontend/src/ts/controllers/route-controller.ts +++ b/frontend/src/ts/controllers/route-controller.ts @@ -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 { +async function router(options = {} as NavigateOptions): Promise { const matches = routes.map((r) => { return { route: r, @@ -133,10 +144,12 @@ async function router(): Promise { 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) => {