mirror of
https://github.com/JeremyLikness/vanillajs-deck
synced 2024-11-15 01:54:57 +00:00
30 lines
771 B
JavaScript
30 lines
771 B
JavaScript
export class Router {
|
|
|
|
constructor() {
|
|
this._eventSource = document.createElement("div");
|
|
this._routeChanged = new CustomEvent("routechanged", {
|
|
bubbles: true,
|
|
cancelable: false
|
|
});
|
|
this._route = null;
|
|
window.addEventListener("popstate", () => {
|
|
if (this.getRoute() !== this._route) {
|
|
this._route = this.getRoute();
|
|
this._eventSource.dispatchEvent(this._routeChanged);
|
|
}
|
|
});
|
|
}
|
|
|
|
get eventSource() {
|
|
return this._eventSource;
|
|
}
|
|
|
|
setRoute(route) {
|
|
window.location.hash = route;
|
|
this._route = route;
|
|
}
|
|
|
|
getRoute() {
|
|
return window.location.hash.substr(1).replace(/\//ig, "/");
|
|
}
|
|
} |