-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
134 lines (113 loc) · 2.9 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
let loc = location
/**
* Change event
*/
let change = Symbol()
/**
* Event changes a path
*/
let routerChanged = Symbol()
/**
* Navigate event
*/
let routerNavigate = Symbol()
/**
* Router key on store
*/
let routerKey = Symbol('route')
/**
* Storeon module for URL routing
* @param {Route[]} routes
*/
function createRouter (routes = []) {
return store => {
store.on('@init', () => {
store.dispatch(change, parse(loc.pathname, routes))
})
store.on(routerChanged, () => {})
store.on(routerNavigate, (state, path) => {
if (state[routerKey].path !== path) {
history.pushState(null, null, path)
}
store.dispatch(change, parse(path, routes))
store.dispatch(routerChanged, store.get()[routerKey])
})
store.on(change, (state, [path, index, params = []]) => {
let route = routes[index]
let newState = {}
newState[routerKey] = {
match: false,
path,
params
}
if (route) {
newState[routerKey].match = route[1](...params)
}
return newState
})
document.documentElement.addEventListener('click', event => {
let link = event.target.closest('a')
if (
!event.defaultPrevented &&
link != null &&
link.href.indexOf(loc.origin) === 0 &&
link.target !== '_blank' &&
link.dataset.ignoreRouter == null &&
event.button === 0 &&
!event.metaKey &&
!event.ctrlKey &&
!event.shiftKey &&
!event.altKey
) {
event.preventDefault()
store.dispatch(routerNavigate, link.href.slice(loc.origin.length))
}
})
window.addEventListener('popstate', () => {
if (store.get()[routerKey].path !== loc.pathname) {
store.dispatch(change, parse(loc.pathname, routes))
store.dispatch(routerChanged, store.get()[routerKey])
}
})
}
}
/**
* @private
* @param {string} path
* @param {Route[]} routes
* @return {[string, number, string[]]}
*/
function parse (path, routes) {
let normalized = path.replace(/(^\/|\/$)/g, '')
for (let [index, [itemPath]] of routes.entries()) {
if (typeof itemPath === 'string') {
let checkPath = itemPath.replace(/(^\/|\/$)/g, '')
if (checkPath === normalized) {
return [path, index]
}
if (checkPath.includes('*')) {
let prepareRe = checkPath
.replace(/[\s!#$()+,.:<=?[\\\]^{|}]/g, '\\$&')
.replace(/\*/g, '([^/]*)')
let re = RegExp('^' + prepareRe + '$', 'i')
let match = normalized.match(re)
if (match) {
return [path, index, match.slice(1)]
}
}
}
if (itemPath instanceof RegExp) {
let matchRE = normalized.match(itemPath)
if (matchRE) {
return [path, index, matchRE.slice(1)]
}
}
}
return [path]
}
module.exports = {
routerNavigate,
routerChanged,
routerKey,
createRouter
}