-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
159 lines (142 loc) · 4.58 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { atom, onMount } from 'nanostores'
export function createRouter(routes, opts = {}) {
let router = atom()
router.routes = Object.keys(routes).map(name => {
let pattern = routes[name]
if (typeof pattern !== 'string') {
return [name, ...[pattern].flat()]
}
pattern = pattern.replace(/\/$/g, '') || '/'
let regexp = pattern
.replace(/[\s!#$()+,.:<=?[\\\]^{|}]/g, '\\$&')
.replace(/\/\\:(\w+)\\\?/g, '(?:/(?<$1>(?<=/)[^/]+))?')
.replace(/\/\\:(\w+)/g, '/(?<$1>[^/]+)')
return [name, RegExp('^' + regexp + '$', 'i'), null, pattern]
})
let prev
let parse = href => {
let url = new URL(href.replace(/#$/, ''), 'http://a')
let cache = url.pathname + url.search + url.hash
if (prev === cache) return false
prev = cache
let path = opts.search ? url.pathname + url.search : url.pathname
path = path.replace(/\/($|\?)/, '$1') || '/'
for (let [route, regexp, callback] of router.routes) {
let match = path.match(regexp)
if (match) {
return {
hash: url.hash,
params: callback
? callback(...match.slice(1))
: Object.keys({ ...match.groups }).reduce((params, key) => {
params[key] = match.groups[key]
? decodeURIComponent(match.groups[key])
: ''
return params
}, {}),
path,
route,
search: Object.fromEntries(url.searchParams)
}
}
}
}
let click = event => {
let link = event.target.closest('a')
if (
link &&
event.button === 0 && // Left mouse button
link.target !== '_blank' && // Not for new tab
link.origin === location.origin && // Not external link
link.rel !== 'external' && // Not external link
link.target !== '_self' && // Now manually disabled
!link.download && // Not download link
!event.altKey && // Not download link by user
!event.metaKey && // Not open in new tab by user
!event.ctrlKey && // Not open in new tab by user
!event.shiftKey && // Not open in new window by user
!event.defaultPrevented // Click was not cancelled
) {
event.preventDefault()
let hashChanged = location.hash !== link.hash
router.open(link.href)
if (hashChanged) {
location.hash = link.hash
if (link.hash === '' || link.hash === '#') {
window.dispatchEvent(new HashChangeEvent('hashchange'))
}
}
}
}
let set = router.set
if (process.env.NODE_ENV !== 'production') {
delete router.set
}
let change = () => {
let page = parse(location.href)
if (page !== false) set(page)
}
if (typeof window !== 'undefined' && typeof location !== 'undefined') {
onMount(router, () => {
let page = parse(location.href)
if (page !== false) set(page)
if (opts.links !== false) document.body.addEventListener('click', click)
window.addEventListener('popstate', change)
window.addEventListener('hashchange', change)
return () => {
prev = undefined
document.body.removeEventListener('click', click)
window.removeEventListener('popstate', change)
window.removeEventListener('hashchange', change)
}
})
} else {
set(parse('/'))
}
router.open = (path, redirect) => {
let page = parse(path)
if (page !== false) {
if (typeof history !== 'undefined') {
if (redirect) {
history.replaceState(null, null, path)
} else {
history.pushState(null, null, path)
}
}
set(page)
}
}
return router
}
export function getPagePath(router, name, params, search) {
if (typeof name === 'object') {
search = params
params = name.params
name = name.route
}
let route = router.routes.find(i => i[0] === name)
if (process.env.NODE_ENV !== 'production') {
if (!route[3]) throw new Error('RegExp routes are not supported')
}
let path = route[3]
.replace(/\/:\w+\?/g, i => {
let param = params && params[i.slice(2, -1)]
if (param) {
return '/' + encodeURIComponent(param)
} else {
return ''
}
})
.replace(/\/:\w+/g, i => '/' + encodeURIComponent(params[i.slice(2)]))
if (search) {
let postfix = '' + new URLSearchParams(search)
if (postfix) return path + '?' + postfix
}
return path
}
export function openPage(router, name, params, search) {
router.open(getPagePath(router, name, params, search))
}
export function redirectPage(router, name, params, search) {
router.open(getPagePath(router, name, params, search), true)
}