ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
const routes = [];
let currentRoute;

export const route = (pattern, handler) => {
	routes.push({
		pattern: pattern,
		handler: handler
	});
};

export const go = (dest) => {
	window.location.hash = '#' + dest;
};

export const start = async () => {
	const dest = window.location.hash.slice(1);

	if (currentRoute && currentRoute.unload) currentRoute.unload();

	for (const route of routes) {
		const match = route.pattern.exec(dest);

		if (!match) continue;

		currentRoute = await route.handler(match.slice(1));

		return;
	}
};

window.addEventListener('hashchange', start);