From 020e6a3ae87710369161081af97cffdf12a64368 Mon Sep 17 00:00:00 2001 From: Matt Mo Date: Wed, 28 Jul 2021 08:40:52 -0700 Subject: [PATCH] Router navigate now uses it's own internal state array. Router now has a back function which allows going backwards on different platforms. --- ignite-html-router.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ignite-html-router.js b/ignite-html-router.js index 565eee9..fed362f 100644 --- a/ignite-html-router.js +++ b/ignite-html-router.js @@ -172,6 +172,11 @@ class RouterViewTemplate extends IgniteTemplate { } class Router { + /** + * Navigates to a given route and refreshes the page if requested. + * @param {String} route The route to navigate to. + * @param {Boolean} refresh Whether or not to refresh the page, default is false. + */ static navigate(route, refresh = false) { if (refresh) { if (Router.hashMode) { @@ -183,14 +188,34 @@ class Router { } else { if (Router.hashMode) { window.location.hash = route; + Router.states.push(route); } else { window.history.pushState(route, route, route); + Router.states.push(route); } window.dispatchEvent(new Event("pushstate")); } } + /** + * Navigates back one with the ability to have a fallback route or refresh the page. + * @param {String} fallback The fallback route incase there is no history, default is null. + * @param {Boolean} refresh Whether or not to refresh the page, default is false. + */ + static back(fallback = null, refresh = false) { + if (Router.states && Router.states.length > 1) { + Router.states.pop(); //Pop the current state. + Router.navigate(Router.states.pop(), refresh); //Navigate to the previous state. + } else { + if (fallback) { + Router.navigate(fallback, refresh); + } else { + window.history.back(); + } + } + } + static matches(route) { //Get the path parts from the window var pathParts = []; @@ -285,6 +310,7 @@ class Router { } } +Router.states = []; Router.hashMode = false; customElements.define("router-link", RouterLink);