blob: eb5b57f6e1c5725320d776443dfa51297dda57ac (
plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import * as router from 'lib/router'
// Model
export type Route =
| Login
| Maps
| Map;
type Login = {
state: 'login'
}
export const login: Login = { state: 'login' }
type Maps = {
state: 'maps'
}
export const maps: Maps = { state: 'maps' }
type Map = {
state: 'map',
id: string
}
export function map({ id }: { id: string }): Map {
return { state: 'map', id }
}
// Serialization
export function get(url: string): Route | undefined {
let res;
if (url == '/login') {
return { state: 'login' }
} else if (url == '/') {
return { state: 'maps' }
} else if (res = router.matches(url, '/:id')) {
return {
state: 'map',
id: res.data['id']
}
}
}
export function toString(route: Route): string {
if (route.state == 'login') return '/login'
else if (route.state == 'map') return `/${route.id}`
else return '/'
}
// History
export function push(route: Route): void {
history.pushState({}, '', toString(route))
// Trigger pop state
const popStateEvent = new PopStateEvent('popstate', {})
dispatchEvent(popStateEvent)
}
|