forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
304 lines (275 loc) · 6.16 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var Recognizer = require('route-recognizer')
var hasPushState = typeof history !== 'undefined' && history.pushState
/**
* Router constructor
*
* @param {Object} [options]
* - {String} root
* - {Boolean} hashbang (default: true)
* - {Boolean} pushstate (default: false)
*/
function VueRouter (options) {
this._recognizer = new Recognizer()
this._started = false
this._vm = null
this._currentPath = null
this._notfoundHandler = null
this._root = null
this._hasPushState = hasPushState
var root = options && options.root
if (root) {
// make sure there's the starting slash
if (root.charAt(0) !== '/') {
root = '/' + root
}
// remove trailing slash
this._root = root.replace(/\/$/, '')
}
this._hashbang = !(options && options.hashbang === false)
this._pushstate = !!(hasPushState && options && options.pushstate)
}
var p = VueRouter.prototype
//
// Public API
//
//
/**
* Register a map of top-level paths.
*/
p.map = function (map) {
for (var route in map) {
this.on(route, map[route])
}
}
/**
* Register a single root-level path
*
* @param {String} rootPath
* @param {Object} config
* - {String} component
* - {Object} [subRoutes]
* - {Boolean} [forceRefresh]
* - {Function} [before]
* - {Function} [after]
*/
p.on = function (rootPath, config) {
if (rootPath === '*') {
this.notfound(config)
} else {
this._addRoute(rootPath, config, [])
}
}
/**
* Set the notfound route config.
*
* @param {Object} config
*/
p.notfound = function (config) {
this._notfoundHandler = [{ handler: config }]
}
/**
* Set redirects.
*
* @param {Object} map
*/
p.redirect = function (map) {
// TODO
// use another recognizer to recognize redirects
}
/**
* Navigate to a given path.
* The path is assumed to be already decoded, and will
* be resolved against root (if provided)
*
* @param {String} path
* @param {Object} [options]
*/
p.go = function (path, options) {
var replace = options && options.replace
if (this._pushstate) {
// make it relative to root
path = this._root
? this._root + '/' + path.replace(/^\//, '')
: path
if (replace) {
history.replaceState({}, '', path)
} else {
history.pushState({}, '', path)
}
this._match(path)
} else {
path = path.replace(/^#!?/, '')
var hash = this._hashbang
? '!' + path
: path
setHash(hash, replace)
}
}
/**
* Start the router.
*
* @param {Vue} vm
*/
p.start = function (vm) {
if (this._started) {
return
}
this._started = true
this._vm = this._vm || vm.$root
if (!this._vm) {
throw new Error(
'vue-router must be started with a root Vue instance.'
)
}
if (this._pushstate) {
this.initHistoryMode()
} else {
this.initHashMode()
}
}
/**
* Initialize hash mode.
*/
p.initHashMode = function () {
var self = this
this.onRouteChange = function () {
// format hashbang
var hash = location.hash
if (self._hashbang && hash && hash.charAt(1) !== '!') {
setHash('!' + hash.slice(1), true)
return
}
if (!self._hashbang && hash && hash.charAt(1) === '!') {
setHash(hash.slice(2), true)
return
}
hash = hash.replace(/^#!?/, '')
var url = hash + location.search
url = decodeURI(url)
self._match(url)
}
window.addEventListener('hashchange', this.onRouteChange)
this.onRouteChange()
}
/**
* Initialize HTML5 history mode.
*/
p.initHistoryMode = function () {
var self = this
this.onRouteChange = function () {
var url = location.pathname + location.search
url = decodeURI(url)
self._match(url)
}
window.addEventListener('popstate', this.onRouteChange)
this.onRouteChange()
}
/**
* Stop listening to route changes.
*/
p.stop = function () {
var event = this._pushstate
? 'popstate'
: 'hashchange'
window.removeEventListener(event, this.onRouteChange)
this._vm.route = null
this._started = false
}
//
// Private Methods
//
/**
* Add a route containing a list of segments to the internal
* route recognizer. Will be called recursively to add all
* possible sub-routes.
*
* @param {String} path
* @param {Object} config
* @param {Array} segments
*/
p._addRoute = function (path, config, segments) {
segments.push({
path: path,
handler: config
})
this._recognizer.add(segments)
if (config.subRoutes) {
for (var subPath in config.subRoutes) {
// recursively walk all sub routes
this._addRoute(
subPath,
config.subRoutes[subPath],
// pass a copy in recursion to avoid mutating
// across branches
segments.slice()
)
}
}
}
/**
* Match a URL path and set the route context on vm,
* triggering view updates.
*
* @param {String} path
*/
p._match = function (path) {
if (path === this._currentPath) {
return
}
this._currentPath = path
// normalize against root
if (
this._pushstate &&
this._root &&
path.indexOf(this._root) === 0
) {
path = path.slice(this._root.length)
}
var matched = this._recognizer.recognize(path)
// aggregate params
var params
if (matched) {
params = [].reduce.call(matched, function (prev, cur) {
if (cur.params) {
for (var key in cur.params) {
prev[key] = cur.params[key]
}
}
return prev
}, {})
}
// construct route context
var context = {
path: path,
params: params,
query: matched && matched.queryParams,
_matched: matched || this._notfoundHandler,
_matchedCount: 0,
_router: this
}
this._vm.$set('route', context)
}
/**
* Installation interface.
* Install the necessary directives.
*/
VueRouter.install = function (Vue) {
require('./view')(Vue)
require('./link')(Vue)
}
/**
* Set current hash
*
* @param {String} hash
* @param {Boolean} replace
*/
function setHash (hash, replace) {
if (replace) {
var urlLength = location.href.length - location.hash.length
var fullURL = location.href.slice(0, urlLength) + '#' + hash
location.replace(fullURL)
} else {
location.hash = hash
}
}
module.exports = VueRouter