Router navigate now uses it's own internal state array. Router now has a back function which allows going backwards on different platforms.

This commit is contained in:
Matt Mo 2021-07-28 08:40:52 -07:00
parent f4171fdca1
commit 020e6a3ae8

@ -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);