26 lines
475 B
JavaScript
26 lines
475 B
JavaScript
'use strict';
|
|
|
|
const routes = [];
|
|
|
|
export const route = (pattern, handler) => {
|
|
routes.push({
|
|
pattern: pattern,
|
|
handler: handler
|
|
});
|
|
};
|
|
|
|
export const go = (dest) => {
|
|
window.location.hash = '#' + dest;
|
|
};
|
|
|
|
export const start = () => {
|
|
const dest = window.location.hash.slice(1);
|
|
for (let route of routes) {
|
|
const match = route.pattern.exec(dest);
|
|
if (!match) continue;
|
|
return route.handler(match.slice(1));
|
|
}
|
|
};
|
|
|
|
window.addEventListener('hashchange', start);
|