Files
boc/aamos-ledger/ui/js/router.js
T

37 lines
1.2 KiB
JavaScript
Raw Normal View History

// ═══════════════════════════════════════════════════════════════════════════
// LandveX Finance — Simple Router
// ═══════════════════════════════════════════════════════════════════════════
export class Router {
constructor(opts = {}) {
this.routes = opts.routes || {};
this.default = opts.default || 'dashboard';
this.current = null;
}
start() {
// Handle browser back/forward
window.addEventListener('popstate', () => this.handle());
// Initial route
this.handle();
}
handle() {
const hash = window.location.hash.replace('#/', '') || this.default;
this.navigate(hash, false);
}
navigate(route, pushState = true) {
if (this.current === route) return;
this.current = route;
if (pushState) {
window.history.pushState(null, '', `#/${route}`);
}
const handler = this.routes[route] || this.routes[this.default];
if (handler) handler();
}
}