forked from nanostores/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (139 loc) · 4.38 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
import { atom, onMount } from 'nanostores'
export function createRouter(routes, opts = {}) {
let router = atom()
router.routes = Object.keys(routes).map(name => {
let value = routes[name]
if (typeof value === 'string') {
value = value.replace(/\/$/g, '') || '/'
let names = value.match(/(?<=\/:)\w+/g) || []
let pattern = value
.replace(/[\s!#$()+,.:<=?[\\\]^{|}]/g, '\\$&')
.replace(/\/\\:\w+\\\?/g, '/?([^/]*)')
.replace(/\/\\:\w+/g, '/([^/]+)')
return [
name,
RegExp('^' + pattern + '$', 'i'),
(...matches) =>
matches.reduce((params, match, index) => {
params[names[index]] = decodeURIComponent(match)
return params
}, {}),
value
]
} else {
return [name, ...value]
}
})
let prev
let parse = path => {
path = path.replace(/\/($|\?)/, '$1') || '/'
if (prev === path) return false
prev = path
let url = new URL(path, 'http://a')
if (!opts.search) path = url.pathname
let search = Object.fromEntries(url.searchParams)
for (let [route, pattern, cb] of router.routes) {
let match = path.match(pattern)
if (match) {
return { params: cb(...match.slice(1)), path, route, search }
}
}
}
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 changed = location.hash !== link.hash
router.open(link.pathname + link.search)
if (changed) {
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 popstate = () => {
let page = parse(location.pathname + location.search)
if (page !== false) set(page)
}
if (typeof window !== 'undefined' && typeof location !== 'undefined') {
onMount(router, () => {
let page = parse(location.pathname + location.search)
if (page !== false) set(page)
if (opts.links !== false) document.body.addEventListener('click', click)
window.addEventListener('popstate', popstate)
return () => {
prev = undefined
document.body.removeEventListener('click', click)
window.removeEventListener('popstate', popstate)
}
})
} 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).slice(0, -1)] : null
if (param) {
return '/' + encodeURIComponent(param)
} else {
return ''
}
})
.replace(/\/:\w+/g, i => '/' + encodeURIComponent(params[i.slice(2)]))
let postfix = ''
if (search) {
postfix = '' + new URLSearchParams(search)
if (postfix) postfix = '?' + postfix
}
return (path || '/') + postfix
}
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)
}