1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | (function (global, factory) {
|
7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
8 | typeof define === 'function' && define.amd ? define(factory) :
|
9 | (global.VueRouter = factory());
|
10 | }(this, (function () { 'use strict';
|
11 |
|
12 |
|
13 |
|
14 | function assert (condition, message) {
|
15 | if (!condition) {
|
16 | throw new Error(("[vue-router] " + message))
|
17 | }
|
18 | }
|
19 |
|
20 | function warn (condition, message) {
|
21 | if ("development" !== 'production' && !condition) {
|
22 | typeof console !== 'undefined' && console.warn(("[vue-router] " + message));
|
23 | }
|
24 | }
|
25 |
|
26 | function isError (err) {
|
27 | return Object.prototype.toString.call(err).indexOf('Error') > -1
|
28 | }
|
29 |
|
30 | function extend (a, b) {
|
31 | for (var key in b) {
|
32 | a[key] = b[key];
|
33 | }
|
34 | return a
|
35 | }
|
36 |
|
37 | var View = {
|
38 | name: 'RouterView',
|
39 | functional: true,
|
40 | props: {
|
41 | name: {
|
42 | type: String,
|
43 | default: 'default'
|
44 | }
|
45 | },
|
46 | render: function render (_, ref) {
|
47 | var props = ref.props;
|
48 | var children = ref.children;
|
49 | var parent = ref.parent;
|
50 | var data = ref.data;
|
51 |
|
52 |
|
53 | data.routerView = true;
|
54 |
|
55 |
|
56 |
|
57 | var h = parent.$createElement;
|
58 | var name = props.name;
|
59 | var route = parent.$route;
|
60 | var cache = parent._routerViewCache || (parent._routerViewCache = {});
|
61 |
|
62 |
|
63 |
|
64 | var depth = 0;
|
65 | var inactive = false;
|
66 | while (parent && parent._routerRoot !== parent) {
|
67 | var vnodeData = parent.$vnode && parent.$vnode.data;
|
68 | if (vnodeData) {
|
69 | if (vnodeData.routerView) {
|
70 | depth++;
|
71 | }
|
72 | if (vnodeData.keepAlive && parent._inactive) {
|
73 | inactive = true;
|
74 | }
|
75 | }
|
76 | parent = parent.$parent;
|
77 | }
|
78 | data.routerViewDepth = depth;
|
79 |
|
80 |
|
81 | if (inactive) {
|
82 | return h(cache[name], data, children)
|
83 | }
|
84 |
|
85 | var matched = route.matched[depth];
|
86 |
|
87 | if (!matched) {
|
88 | cache[name] = null;
|
89 | return h()
|
90 | }
|
91 |
|
92 | var component = cache[name] = matched.components[name];
|
93 |
|
94 |
|
95 |
|
96 | data.registerRouteInstance = function (vm, val) {
|
97 |
|
98 | var current = matched.instances[name];
|
99 | if (
|
100 | (val && current !== vm) ||
|
101 | (!val && current === vm)
|
102 | ) {
|
103 | matched.instances[name] = val;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | ;(data.hook || (data.hook = {})).prepatch = function (_, vnode) {
|
110 | matched.instances[name] = vnode.componentInstance;
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 | data.hook.init = function (vnode) {
|
116 | if (vnode.data.keepAlive &&
|
117 | vnode.componentInstance &&
|
118 | vnode.componentInstance !== matched.instances[name]
|
119 | ) {
|
120 | matched.instances[name] = vnode.componentInstance;
|
121 | }
|
122 | };
|
123 |
|
124 |
|
125 | var propsToPass = data.props = resolveProps(route, matched.props && matched.props[name]);
|
126 | if (propsToPass) {
|
127 |
|
128 | propsToPass = data.props = extend({}, propsToPass);
|
129 |
|
130 | var attrs = data.attrs = data.attrs || {};
|
131 | for (var key in propsToPass) {
|
132 | if (!component.props || !(key in component.props)) {
|
133 | attrs[key] = propsToPass[key];
|
134 | delete propsToPass[key];
|
135 | }
|
136 | }
|
137 | }
|
138 |
|
139 | return h(component, data, children)
|
140 | }
|
141 | }
|
142 |
|
143 | function resolveProps (route, config) {
|
144 | switch (typeof config) {
|
145 | case 'undefined':
|
146 | return
|
147 | case 'object':
|
148 | return config
|
149 | case 'function':
|
150 | return config(route)
|
151 | case 'boolean':
|
152 | return config ? route.params : undefined
|
153 | default:
|
154 | {
|
155 | warn(
|
156 | false,
|
157 | "props in \"" + (route.path) + "\" is a " + (typeof config) + ", " +
|
158 | "expecting an object, function or boolean."
|
159 | );
|
160 | }
|
161 | }
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 | var encodeReserveRE = /[!'()*]/g;
|
167 | var encodeReserveReplacer = function (c) { return '%' + c.charCodeAt(0).toString(16); };
|
168 | var commaRE = /%2C/g;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | var encode = function (str) { return encodeURIComponent(str)
|
174 | .replace(encodeReserveRE, encodeReserveReplacer)
|
175 | .replace(commaRE, ','); };
|
176 |
|
177 | var decode = decodeURIComponent;
|
178 |
|
179 | function resolveQuery (
|
180 | query,
|
181 | extraQuery,
|
182 | _parseQuery
|
183 | ) {
|
184 | if ( extraQuery === void 0 ) extraQuery = {};
|
185 |
|
186 | var parse = _parseQuery || parseQuery;
|
187 | var parsedQuery;
|
188 | try {
|
189 | parsedQuery = parse(query || '');
|
190 | } catch (e) {
|
191 | "development" !== 'production' && warn(false, e.message);
|
192 | parsedQuery = {};
|
193 | }
|
194 | for (var key in extraQuery) {
|
195 | parsedQuery[key] = extraQuery[key];
|
196 | }
|
197 | return parsedQuery
|
198 | }
|
199 |
|
200 | function parseQuery (query) {
|
201 | var res = {};
|
202 |
|
203 | query = query.trim().replace(/^(\?|#|&)/, '');
|
204 |
|
205 | if (!query) {
|
206 | return res
|
207 | }
|
208 |
|
209 | query.split('&').forEach(function (param) {
|
210 | var parts = param.replace(/\+/g, ' ').split('=');
|
211 | var key = decode(parts.shift());
|
212 | var val = parts.length > 0
|
213 | ? decode(parts.join('='))
|
214 | : null;
|
215 |
|
216 | if (res[key] === undefined) {
|
217 | res[key] = val;
|
218 | } else if (Array.isArray(res[key])) {
|
219 | res[key].push(val);
|
220 | } else {
|
221 | res[key] = [res[key], val];
|
222 | }
|
223 | });
|
224 |
|
225 | return res
|
226 | }
|
227 |
|
228 | function stringifyQuery (obj) {
|
229 | var res = obj ? Object.keys(obj).map(function (key) {
|
230 | var val = obj[key];
|
231 |
|
232 | if (val === undefined) {
|
233 | return ''
|
234 | }
|
235 |
|
236 | if (val === null) {
|
237 | return encode(key)
|
238 | }
|
239 |
|
240 | if (Array.isArray(val)) {
|
241 | var result = [];
|
242 | val.forEach(function (val2) {
|
243 | if (val2 === undefined) {
|
244 | return
|
245 | }
|
246 | if (val2 === null) {
|
247 | result.push(encode(key));
|
248 | } else {
|
249 | result.push(encode(key) + '=' + encode(val2));
|
250 | }
|
251 | });
|
252 | return result.join('&')
|
253 | }
|
254 |
|
255 | return encode(key) + '=' + encode(val)
|
256 | }).filter(function (x) { return x.length > 0; }).join('&') : null;
|
257 | return res ? ("?" + res) : ''
|
258 | }
|
259 |
|
260 |
|
261 |
|
262 | var trailingSlashRE = /\/?$/;
|
263 |
|
264 | function createRoute (
|
265 | record,
|
266 | location,
|
267 | redirectedFrom,
|
268 | router
|
269 | ) {
|
270 | var stringifyQuery$$1 = router && router.options.stringifyQuery;
|
271 |
|
272 | var query = location.query || {};
|
273 | try {
|
274 | query = clone(query);
|
275 | } catch (e) {}
|
276 |
|
277 | var route = {
|
278 | name: location.name || (record && record.name),
|
279 | meta: (record && record.meta) || {},
|
280 | path: location.path || '/',
|
281 | hash: location.hash || '',
|
282 | query: query,
|
283 | params: location.params || {},
|
284 | fullPath: getFullPath(location, stringifyQuery$$1),
|
285 | matched: record ? formatMatch(record) : []
|
286 | };
|
287 | if (redirectedFrom) {
|
288 | route.redirectedFrom = getFullPath(redirectedFrom, stringifyQuery$$1);
|
289 | }
|
290 | return Object.freeze(route)
|
291 | }
|
292 |
|
293 | function clone (value) {
|
294 | if (Array.isArray(value)) {
|
295 | return value.map(clone)
|
296 | } else if (value && typeof value === 'object') {
|
297 | var res = {};
|
298 | for (var key in value) {
|
299 | res[key] = clone(value[key]);
|
300 | }
|
301 | return res
|
302 | } else {
|
303 | return value
|
304 | }
|
305 | }
|
306 |
|
307 |
|
308 | var START = createRoute(null, {
|
309 | path: '/'
|
310 | });
|
311 |
|
312 | function formatMatch (record) {
|
313 | var res = [];
|
314 | while (record) {
|
315 | res.unshift(record);
|
316 | record = record.parent;
|
317 | }
|
318 | return res
|
319 | }
|
320 |
|
321 | function getFullPath (
|
322 | ref,
|
323 | _stringifyQuery
|
324 | ) {
|
325 | var path = ref.path;
|
326 | var query = ref.query; if ( query === void 0 ) query = {};
|
327 | var hash = ref.hash; if ( hash === void 0 ) hash = '';
|
328 |
|
329 | var stringify = _stringifyQuery || stringifyQuery;
|
330 | return (path || '/') + stringify(query) + hash
|
331 | }
|
332 |
|
333 | function isSameRoute (a, b) {
|
334 | if (b === START) {
|
335 | return a === b
|
336 | } else if (!b) {
|
337 | return false
|
338 | } else if (a.path && b.path) {
|
339 | return (
|
340 | a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') &&
|
341 | a.hash === b.hash &&
|
342 | isObjectEqual(a.query, b.query)
|
343 | )
|
344 | } else if (a.name && b.name) {
|
345 | return (
|
346 | a.name === b.name &&
|
347 | a.hash === b.hash &&
|
348 | isObjectEqual(a.query, b.query) &&
|
349 | isObjectEqual(a.params, b.params)
|
350 | )
|
351 | } else {
|
352 | return false
|
353 | }
|
354 | }
|
355 |
|
356 | function isObjectEqual (a, b) {
|
357 | if ( a === void 0 ) a = {};
|
358 | if ( b === void 0 ) b = {};
|
359 |
|
360 |
|
361 | if (!a || !b) { return a === b }
|
362 | var aKeys = Object.keys(a);
|
363 | var bKeys = Object.keys(b);
|
364 | if (aKeys.length !== bKeys.length) {
|
365 | return false
|
366 | }
|
367 | return aKeys.every(function (key) {
|
368 | var aVal = a[key];
|
369 | var bVal = b[key];
|
370 |
|
371 | if (typeof aVal === 'object' && typeof bVal === 'object') {
|
372 | return isObjectEqual(aVal, bVal)
|
373 | }
|
374 | return String(aVal) === String(bVal)
|
375 | })
|
376 | }
|
377 |
|
378 | function isIncludedRoute (current, target) {
|
379 | return (
|
380 | current.path.replace(trailingSlashRE, '/').indexOf(
|
381 | target.path.replace(trailingSlashRE, '/')
|
382 | ) === 0 &&
|
383 | (!target.hash || current.hash === target.hash) &&
|
384 | queryIncludes(current.query, target.query)
|
385 | )
|
386 | }
|
387 |
|
388 | function queryIncludes (current, target) {
|
389 | for (var key in target) {
|
390 | if (!(key in current)) {
|
391 | return false
|
392 | }
|
393 | }
|
394 | return true
|
395 | }
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | var toTypes = [String, Object];
|
401 | var eventTypes = [String, Array];
|
402 |
|
403 | var Link = {
|
404 | name: 'RouterLink',
|
405 | props: {
|
406 | to: {
|
407 | type: toTypes,
|
408 | required: true
|
409 | },
|
410 | tag: {
|
411 | type: String,
|
412 | default: 'a'
|
413 | },
|
414 | exact: Boolean,
|
415 | append: Boolean,
|
416 | replace: Boolean,
|
417 | activeClass: String,
|
418 | exactActiveClass: String,
|
419 | event: {
|
420 | type: eventTypes,
|
421 | default: 'click'
|
422 | }
|
423 | },
|
424 | render: function render (h) {
|
425 | var this$1 = this;
|
426 |
|
427 | var router = this.$router;
|
428 | var current = this.$route;
|
429 | var ref = router.resolve(this.to, current, this.append);
|
430 | var location = ref.location;
|
431 | var route = ref.route;
|
432 | var href = ref.href;
|
433 |
|
434 | var classes = {};
|
435 | var globalActiveClass = router.options.linkActiveClass;
|
436 | var globalExactActiveClass = router.options.linkExactActiveClass;
|
437 |
|
438 | var activeClassFallback = globalActiveClass == null
|
439 | ? 'router-link-active'
|
440 | : globalActiveClass;
|
441 | var exactActiveClassFallback = globalExactActiveClass == null
|
442 | ? 'router-link-exact-active'
|
443 | : globalExactActiveClass;
|
444 | var activeClass = this.activeClass == null
|
445 | ? activeClassFallback
|
446 | : this.activeClass;
|
447 | var exactActiveClass = this.exactActiveClass == null
|
448 | ? exactActiveClassFallback
|
449 | : this.exactActiveClass;
|
450 | var compareTarget = location.path
|
451 | ? createRoute(null, location, null, router)
|
452 | : route;
|
453 |
|
454 | classes[exactActiveClass] = isSameRoute(current, compareTarget);
|
455 | classes[activeClass] = this.exact
|
456 | ? classes[exactActiveClass]
|
457 | : isIncludedRoute(current, compareTarget);
|
458 |
|
459 | var handler = function (e) {
|
460 | if (guardEvent(e)) {
|
461 | if (this$1.replace) {
|
462 | router.replace(location);
|
463 | } else {
|
464 | router.push(location);
|
465 | }
|
466 | }
|
467 | };
|
468 |
|
469 | var on = { click: guardEvent };
|
470 | if (Array.isArray(this.event)) {
|
471 | this.event.forEach(function (e) { on[e] = handler; });
|
472 | } else {
|
473 | on[this.event] = handler;
|
474 | }
|
475 |
|
476 | var data = {
|
477 | class: classes
|
478 | };
|
479 |
|
480 | if (this.tag === 'a') {
|
481 | data.on = on;
|
482 | data.attrs = { href: href };
|
483 | } else {
|
484 |
|
485 | var a = findAnchor(this.$slots.default);
|
486 | if (a) {
|
487 |
|
488 | a.isStatic = false;
|
489 | var aData = a.data = extend({}, a.data);
|
490 | aData.on = on;
|
491 | var aAttrs = a.data.attrs = extend({}, a.data.attrs);
|
492 | aAttrs.href = href;
|
493 | } else {
|
494 |
|
495 | data.on = on;
|
496 | }
|
497 | }
|
498 |
|
499 | return h(this.tag, data, this.$slots.default)
|
500 | }
|
501 | }
|
502 |
|
503 | function guardEvent (e) {
|
504 |
|
505 | if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) { return }
|
506 |
|
507 | if (e.defaultPrevented) { return }
|
508 |
|
509 | if (e.button !== undefined && e.button !== 0) { return }
|
510 |
|
511 | if (e.currentTarget && e.currentTarget.getAttribute) {
|
512 | var target = e.currentTarget.getAttribute('target');
|
513 | if (/\b_blank\b/i.test(target)) { return }
|
514 | }
|
515 |
|
516 | if (e.preventDefault) {
|
517 | e.preventDefault();
|
518 | }
|
519 | return true
|
520 | }
|
521 |
|
522 | function findAnchor (children) {
|
523 | if (children) {
|
524 | var child;
|
525 | for (var i = 0; i < children.length; i++) {
|
526 | child = children[i];
|
527 | if (child.tag === 'a') {
|
528 | return child
|
529 | }
|
530 | if (child.children && (child = findAnchor(child.children))) {
|
531 | return child
|
532 | }
|
533 | }
|
534 | }
|
535 | }
|
536 |
|
537 | var _Vue;
|
538 |
|
539 | function install (Vue) {
|
540 | if (install.installed && _Vue === Vue) { return }
|
541 | install.installed = true;
|
542 |
|
543 | _Vue = Vue;
|
544 |
|
545 | var isDef = function (v) { return v !== undefined; };
|
546 |
|
547 | var registerInstance = function (vm, callVal) {
|
548 | var i = vm.$options._parentVnode;
|
549 | if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
|
550 | i(vm, callVal);
|
551 | }
|
552 | };
|
553 |
|
554 | Vue.mixin({
|
555 | beforeCreate: function beforeCreate () {
|
556 | if (isDef(this.$options.router)) {
|
557 | this._routerRoot = this;
|
558 | this._router = this.$options.router;
|
559 | this._router.init(this);
|
560 | Vue.util.defineReactive(this, '_route', this._router.history.current);
|
561 | } else {
|
562 | this._routerRoot = (this.$parent && this.$parent._routerRoot) || this;
|
563 | }
|
564 | registerInstance(this, this);
|
565 | },
|
566 | destroyed: function destroyed () {
|
567 | registerInstance(this);
|
568 | }
|
569 | });
|
570 |
|
571 | Object.defineProperty(Vue.prototype, '$router', {
|
572 | get: function get () { return this._routerRoot._router }
|
573 | });
|
574 |
|
575 | Object.defineProperty(Vue.prototype, '$route', {
|
576 | get: function get () { return this._routerRoot._route }
|
577 | });
|
578 |
|
579 | Vue.component('RouterView', View);
|
580 | Vue.component('RouterLink', Link);
|
581 |
|
582 | var strats = Vue.config.optionMergeStrategies;
|
583 |
|
584 | strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created;
|
585 | }
|
586 |
|
587 |
|
588 |
|
589 | var inBrowser = typeof window !== 'undefined';
|
590 |
|
591 |
|
592 |
|
593 | function resolvePath (
|
594 | relative,
|
595 | base,
|
596 | append
|
597 | ) {
|
598 | var firstChar = relative.charAt(0);
|
599 | if (firstChar === '/') {
|
600 | return relative
|
601 | }
|
602 |
|
603 | if (firstChar === '?' || firstChar === '#') {
|
604 | return base + relative
|
605 | }
|
606 |
|
607 | var stack = base.split('/');
|
608 |
|
609 |
|
610 |
|
611 |
|
612 | if (!append || !stack[stack.length - 1]) {
|
613 | stack.pop();
|
614 | }
|
615 |
|
616 |
|
617 | var segments = relative.replace(/^\//, '').split('/');
|
618 | for (var i = 0; i < segments.length; i++) {
|
619 | var segment = segments[i];
|
620 | if (segment === '..') {
|
621 | stack.pop();
|
622 | } else if (segment !== '.') {
|
623 | stack.push(segment);
|
624 | }
|
625 | }
|
626 |
|
627 |
|
628 | if (stack[0] !== '') {
|
629 | stack.unshift('');
|
630 | }
|
631 |
|
632 | return stack.join('/')
|
633 | }
|
634 |
|
635 | function parsePath (path) {
|
636 | var hash = '';
|
637 | var query = '';
|
638 |
|
639 | var hashIndex = path.indexOf('#');
|
640 | if (hashIndex >= 0) {
|
641 | hash = path.slice(hashIndex);
|
642 | path = path.slice(0, hashIndex);
|
643 | }
|
644 |
|
645 | var queryIndex = path.indexOf('?');
|
646 | if (queryIndex >= 0) {
|
647 | query = path.slice(queryIndex + 1);
|
648 | path = path.slice(0, queryIndex);
|
649 | }
|
650 |
|
651 | return {
|
652 | path: path,
|
653 | query: query,
|
654 | hash: hash
|
655 | }
|
656 | }
|
657 |
|
658 | function cleanPath (path) {
|
659 | return path.replace(/\/\//g, '/')
|
660 | }
|
661 |
|
662 | var isarray = Array.isArray || function (arr) {
|
663 | return Object.prototype.toString.call(arr) == '[object Array]';
|
664 | };
|
665 |
|
666 |
|
667 |
|
668 |
|
669 | var pathToRegexp_1 = pathToRegexp;
|
670 | var parse_1 = parse;
|
671 | var compile_1 = compile;
|
672 | var tokensToFunction_1 = tokensToFunction;
|
673 | var tokensToRegExp_1 = tokensToRegExp;
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 | var PATH_REGEXP = new RegExp([
|
681 |
|
682 |
|
683 | '(\\\\.)',
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 | '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
|
691 | ].join('|'), 'g');
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 | function parse (str, options) {
|
701 | var tokens = [];
|
702 | var key = 0;
|
703 | var index = 0;
|
704 | var path = '';
|
705 | var defaultDelimiter = options && options.delimiter || '/';
|
706 | var res;
|
707 |
|
708 | while ((res = PATH_REGEXP.exec(str)) != null) {
|
709 | var m = res[0];
|
710 | var escaped = res[1];
|
711 | var offset = res.index;
|
712 | path += str.slice(index, offset);
|
713 | index = offset + m.length;
|
714 |
|
715 |
|
716 | if (escaped) {
|
717 | path += escaped[1];
|
718 | continue
|
719 | }
|
720 |
|
721 | var next = str[index];
|
722 | var prefix = res[2];
|
723 | var name = res[3];
|
724 | var capture = res[4];
|
725 | var group = res[5];
|
726 | var modifier = res[6];
|
727 | var asterisk = res[7];
|
728 |
|
729 |
|
730 | if (path) {
|
731 | tokens.push(path);
|
732 | path = '';
|
733 | }
|
734 |
|
735 | var partial = prefix != null && next != null && next !== prefix;
|
736 | var repeat = modifier === '+' || modifier === '*';
|
737 | var optional = modifier === '?' || modifier === '*';
|
738 | var delimiter = res[2] || defaultDelimiter;
|
739 | var pattern = capture || group;
|
740 |
|
741 | tokens.push({
|
742 | name: name || key++,
|
743 | prefix: prefix || '',
|
744 | delimiter: delimiter,
|
745 | optional: optional,
|
746 | repeat: repeat,
|
747 | partial: partial,
|
748 | asterisk: !!asterisk,
|
749 | pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
|
750 | });
|
751 | }
|
752 |
|
753 |
|
754 | if (index < str.length) {
|
755 | path += str.substr(index);
|
756 | }
|
757 |
|
758 |
|
759 | if (path) {
|
760 | tokens.push(path);
|
761 | }
|
762 |
|
763 | return tokens
|
764 | }
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 | function compile (str, options) {
|
774 | return tokensToFunction(parse(str, options))
|
775 | }
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 | function encodeURIComponentPretty (str) {
|
784 | return encodeURI(str).replace(/[\/?#]/g, function (c) {
|
785 | return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
786 | })
|
787 | }
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 | function encodeAsterisk (str) {
|
796 | return encodeURI(str).replace(/[?#]/g, function (c) {
|
797 | return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
798 | })
|
799 | }
|
800 |
|
801 |
|
802 |
|
803 |
|
804 | function tokensToFunction (tokens) {
|
805 |
|
806 | var matches = new Array(tokens.length);
|
807 |
|
808 |
|
809 | for (var i = 0; i < tokens.length; i++) {
|
810 | if (typeof tokens[i] === 'object') {
|
811 | matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$');
|
812 | }
|
813 | }
|
814 |
|
815 | return function (obj, opts) {
|
816 | var path = '';
|
817 | var data = obj || {};
|
818 | var options = opts || {};
|
819 | var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent;
|
820 |
|
821 | for (var i = 0; i < tokens.length; i++) {
|
822 | var token = tokens[i];
|
823 |
|
824 | if (typeof token === 'string') {
|
825 | path += token;
|
826 |
|
827 | continue
|
828 | }
|
829 |
|
830 | var value = data[token.name];
|
831 | var segment;
|
832 |
|
833 | if (value == null) {
|
834 | if (token.optional) {
|
835 |
|
836 | if (token.partial) {
|
837 | path += token.prefix;
|
838 | }
|
839 |
|
840 | continue
|
841 | } else {
|
842 | throw new TypeError('Expected "' + token.name + '" to be defined')
|
843 | }
|
844 | }
|
845 |
|
846 | if (isarray(value)) {
|
847 | if (!token.repeat) {
|
848 | throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
|
849 | }
|
850 |
|
851 | if (value.length === 0) {
|
852 | if (token.optional) {
|
853 | continue
|
854 | } else {
|
855 | throw new TypeError('Expected "' + token.name + '" to not be empty')
|
856 | }
|
857 | }
|
858 |
|
859 | for (var j = 0; j < value.length; j++) {
|
860 | segment = encode(value[j]);
|
861 |
|
862 | if (!matches[i].test(segment)) {
|
863 | throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
|
864 | }
|
865 |
|
866 | path += (j === 0 ? token.prefix : token.delimiter) + segment;
|
867 | }
|
868 |
|
869 | continue
|
870 | }
|
871 |
|
872 | segment = token.asterisk ? encodeAsterisk(value) : encode(value);
|
873 |
|
874 | if (!matches[i].test(segment)) {
|
875 | throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
|
876 | }
|
877 |
|
878 | path += token.prefix + segment;
|
879 | }
|
880 |
|
881 | return path
|
882 | }
|
883 | }
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 | function escapeString (str) {
|
892 | return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1')
|
893 | }
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 | function escapeGroup (group) {
|
902 | return group.replace(/([=!:$\/()])/g, '\\$1')
|
903 | }
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 | function attachKeys (re, keys) {
|
913 | re.keys = keys;
|
914 | return re
|
915 | }
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 | function flags (options) {
|
924 | return options.sensitive ? '' : 'i'
|
925 | }
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 | function regexpToRegexp (path, keys) {
|
935 |
|
936 | var groups = path.source.match(/\((?!\?)/g);
|
937 |
|
938 | if (groups) {
|
939 | for (var i = 0; i < groups.length; i++) {
|
940 | keys.push({
|
941 | name: i,
|
942 | prefix: null,
|
943 | delimiter: null,
|
944 | optional: false,
|
945 | repeat: false,
|
946 | partial: false,
|
947 | asterisk: false,
|
948 | pattern: null
|
949 | });
|
950 | }
|
951 | }
|
952 |
|
953 | return attachKeys(path, keys)
|
954 | }
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | function arrayToRegexp (path, keys, options) {
|
965 | var parts = [];
|
966 |
|
967 | for (var i = 0; i < path.length; i++) {
|
968 | parts.push(pathToRegexp(path[i], keys, options).source);
|
969 | }
|
970 |
|
971 | var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options));
|
972 |
|
973 | return attachKeys(regexp, keys)
|
974 | }
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 | function stringToRegexp (path, keys, options) {
|
985 | return tokensToRegExp(parse(path, options), keys, options)
|
986 | }
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 | function tokensToRegExp (tokens, keys, options) {
|
997 | if (!isarray(keys)) {
|
998 | options = (keys || options);
|
999 | keys = [];
|
1000 | }
|
1001 |
|
1002 | options = options || {};
|
1003 |
|
1004 | var strict = options.strict;
|
1005 | var end = options.end !== false;
|
1006 | var route = '';
|
1007 |
|
1008 |
|
1009 | for (var i = 0; i < tokens.length; i++) {
|
1010 | var token = tokens[i];
|
1011 |
|
1012 | if (typeof token === 'string') {
|
1013 | route += escapeString(token);
|
1014 | } else {
|
1015 | var prefix = escapeString(token.prefix);
|
1016 | var capture = '(?:' + token.pattern + ')';
|
1017 |
|
1018 | keys.push(token);
|
1019 |
|
1020 | if (token.repeat) {
|
1021 | capture += '(?:' + prefix + capture + ')*';
|
1022 | }
|
1023 |
|
1024 | if (token.optional) {
|
1025 | if (!token.partial) {
|
1026 | capture = '(?:' + prefix + '(' + capture + '))?';
|
1027 | } else {
|
1028 | capture = prefix + '(' + capture + ')?';
|
1029 | }
|
1030 | } else {
|
1031 | capture = prefix + '(' + capture + ')';
|
1032 | }
|
1033 |
|
1034 | route += capture;
|
1035 | }
|
1036 | }
|
1037 |
|
1038 | var delimiter = escapeString(options.delimiter || '/');
|
1039 | var endsWithDelimiter = route.slice(-delimiter.length) === delimiter;
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 | if (!strict) {
|
1046 | route = (endsWithDelimiter ? route.slice(0, -delimiter.length) : route) + '(?:' + delimiter + '(?=$))?';
|
1047 | }
|
1048 |
|
1049 | if (end) {
|
1050 | route += '$';
|
1051 | } else {
|
1052 |
|
1053 |
|
1054 | route += strict && endsWithDelimiter ? '' : '(?=' + delimiter + '|$)';
|
1055 | }
|
1056 |
|
1057 | return attachKeys(new RegExp('^' + route, flags(options)), keys)
|
1058 | }
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 | function pathToRegexp (path, keys, options) {
|
1073 | if (!isarray(keys)) {
|
1074 | options = (keys || options);
|
1075 | keys = [];
|
1076 | }
|
1077 |
|
1078 | options = options || {};
|
1079 |
|
1080 | if (path instanceof RegExp) {
|
1081 | return regexpToRegexp(path, (keys))
|
1082 | }
|
1083 |
|
1084 | if (isarray(path)) {
|
1085 | return arrayToRegexp( (path), (keys), options)
|
1086 | }
|
1087 |
|
1088 | return stringToRegexp( (path), (keys), options)
|
1089 | }
|
1090 | pathToRegexp_1.parse = parse_1;
|
1091 | pathToRegexp_1.compile = compile_1;
|
1092 | pathToRegexp_1.tokensToFunction = tokensToFunction_1;
|
1093 | pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 | var regexpCompileCache = Object.create(null);
|
1099 |
|
1100 | function fillParams (
|
1101 | path,
|
1102 | params,
|
1103 | routeMsg
|
1104 | ) {
|
1105 | params = params || {};
|
1106 | try {
|
1107 | var filler =
|
1108 | regexpCompileCache[path] ||
|
1109 | (regexpCompileCache[path] = pathToRegexp_1.compile(path));
|
1110 |
|
1111 |
|
1112 | if (params.pathMatch) { params[0] = params.pathMatch; }
|
1113 |
|
1114 | return filler(params, { pretty: true })
|
1115 | } catch (e) {
|
1116 | {
|
1117 | warn(false, ("missing param for " + routeMsg + ": " + (e.message)));
|
1118 | }
|
1119 | return ''
|
1120 | } finally {
|
1121 |
|
1122 | delete params[0];
|
1123 | }
|
1124 | }
|
1125 |
|
1126 |
|
1127 |
|
1128 | function createRouteMap (
|
1129 | routes,
|
1130 | oldPathList,
|
1131 | oldPathMap,
|
1132 | oldNameMap
|
1133 | ) {
|
1134 |
|
1135 | var pathList = oldPathList || [];
|
1136 |
|
1137 | var pathMap = oldPathMap || Object.create(null);
|
1138 |
|
1139 | var nameMap = oldNameMap || Object.create(null);
|
1140 |
|
1141 | routes.forEach(function (route) {
|
1142 | addRouteRecord(pathList, pathMap, nameMap, route);
|
1143 | });
|
1144 |
|
1145 |
|
1146 | for (var i = 0, l = pathList.length; i < l; i++) {
|
1147 | if (pathList[i] === '*') {
|
1148 | pathList.push(pathList.splice(i, 1)[0]);
|
1149 | l--;
|
1150 | i--;
|
1151 | }
|
1152 | }
|
1153 |
|
1154 | return {
|
1155 | pathList: pathList,
|
1156 | pathMap: pathMap,
|
1157 | nameMap: nameMap
|
1158 | }
|
1159 | }
|
1160 |
|
1161 | function addRouteRecord (
|
1162 | pathList,
|
1163 | pathMap,
|
1164 | nameMap,
|
1165 | route,
|
1166 | parent,
|
1167 | matchAs
|
1168 | ) {
|
1169 | var path = route.path;
|
1170 | var name = route.name;
|
1171 | {
|
1172 | assert(path != null, "\"path\" is required in a route configuration.");
|
1173 | assert(
|
1174 | typeof route.component !== 'string',
|
1175 | "route config \"component\" for path: " + (String(path || name)) + " cannot be a " +
|
1176 | "string id. Use an actual component instead."
|
1177 | );
|
1178 | }
|
1179 |
|
1180 | var pathToRegexpOptions = route.pathToRegexpOptions || {};
|
1181 | var normalizedPath = normalizePath(
|
1182 | path,
|
1183 | parent,
|
1184 | pathToRegexpOptions.strict
|
1185 | );
|
1186 |
|
1187 | if (typeof route.caseSensitive === 'boolean') {
|
1188 | pathToRegexpOptions.sensitive = route.caseSensitive;
|
1189 | }
|
1190 |
|
1191 | var record = {
|
1192 | path: normalizedPath,
|
1193 | regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
|
1194 | components: route.components || { default: route.component },
|
1195 | instances: {},
|
1196 | name: name,
|
1197 | parent: parent,
|
1198 | matchAs: matchAs,
|
1199 | redirect: route.redirect,
|
1200 | beforeEnter: route.beforeEnter,
|
1201 | meta: route.meta || {},
|
1202 | props: route.props == null
|
1203 | ? {}
|
1204 | : route.components
|
1205 | ? route.props
|
1206 | : { default: route.props }
|
1207 | };
|
1208 |
|
1209 | if (route.children) {
|
1210 |
|
1211 |
|
1212 |
|
1213 | {
|
1214 | if (route.name && !route.redirect && route.children.some(function (child) { return /^\/?$/.test(child.path); })) {
|
1215 | warn(
|
1216 | false,
|
1217 | "Named Route '" + (route.name) + "' has a default child route. " +
|
1218 | "When navigating to this named route (:to=\"{name: '" + (route.name) + "'\"), " +
|
1219 | "the default child route will not be rendered. Remove the name from " +
|
1220 | "this route and use the name of the default child route for named " +
|
1221 | "links instead."
|
1222 | );
|
1223 | }
|
1224 | }
|
1225 | route.children.forEach(function (child) {
|
1226 | var childMatchAs = matchAs
|
1227 | ? cleanPath((matchAs + "/" + (child.path)))
|
1228 | : undefined;
|
1229 | addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs);
|
1230 | });
|
1231 | }
|
1232 |
|
1233 | if (route.alias !== undefined) {
|
1234 | var aliases = Array.isArray(route.alias)
|
1235 | ? route.alias
|
1236 | : [route.alias];
|
1237 |
|
1238 | aliases.forEach(function (alias) {
|
1239 | var aliasRoute = {
|
1240 | path: alias,
|
1241 | children: route.children
|
1242 | };
|
1243 | addRouteRecord(
|
1244 | pathList,
|
1245 | pathMap,
|
1246 | nameMap,
|
1247 | aliasRoute,
|
1248 | parent,
|
1249 | record.path || '/'
|
1250 | );
|
1251 | });
|
1252 | }
|
1253 |
|
1254 | if (!pathMap[record.path]) {
|
1255 | pathList.push(record.path);
|
1256 | pathMap[record.path] = record;
|
1257 | }
|
1258 |
|
1259 | if (name) {
|
1260 | if (!nameMap[name]) {
|
1261 | nameMap[name] = record;
|
1262 | } else if ("development" !== 'production' && !matchAs) {
|
1263 | warn(
|
1264 | false,
|
1265 | "Duplicate named routes definition: " +
|
1266 | "{ name: \"" + name + "\", path: \"" + (record.path) + "\" }"
|
1267 | );
|
1268 | }
|
1269 | }
|
1270 | }
|
1271 |
|
1272 | function compileRouteRegex (path, pathToRegexpOptions) {
|
1273 | var regex = pathToRegexp_1(path, [], pathToRegexpOptions);
|
1274 | {
|
1275 | var keys = Object.create(null);
|
1276 | regex.keys.forEach(function (key) {
|
1277 | warn(!keys[key.name], ("Duplicate param keys in route with path: \"" + path + "\""));
|
1278 | keys[key.name] = true;
|
1279 | });
|
1280 | }
|
1281 | return regex
|
1282 | }
|
1283 |
|
1284 | function normalizePath (path, parent, strict) {
|
1285 | if (!strict) { path = path.replace(/\/$/, ''); }
|
1286 | if (path[0] === '/') { return path }
|
1287 | if (parent == null) { return path }
|
1288 | return cleanPath(((parent.path) + "/" + path))
|
1289 | }
|
1290 |
|
1291 |
|
1292 |
|
1293 | function normalizeLocation (
|
1294 | raw,
|
1295 | current,
|
1296 | append,
|
1297 | router
|
1298 | ) {
|
1299 | var next = typeof raw === 'string' ? { path: raw } : raw;
|
1300 |
|
1301 | if (next._normalized) {
|
1302 | return next
|
1303 | } else if (next.name) {
|
1304 | return extend({}, raw)
|
1305 | }
|
1306 |
|
1307 |
|
1308 | if (!next.path && next.params && current) {
|
1309 | next = extend({}, next);
|
1310 | next._normalized = true;
|
1311 | var params = extend(extend({}, current.params), next.params);
|
1312 | if (current.name) {
|
1313 | next.name = current.name;
|
1314 | next.params = params;
|
1315 | } else if (current.matched.length) {
|
1316 | var rawPath = current.matched[current.matched.length - 1].path;
|
1317 | next.path = fillParams(rawPath, params, ("path " + (current.path)));
|
1318 | } else {
|
1319 | warn(false, "relative params navigation requires a current route.");
|
1320 | }
|
1321 | return next
|
1322 | }
|
1323 |
|
1324 | var parsedPath = parsePath(next.path || '');
|
1325 | var basePath = (current && current.path) || '/';
|
1326 | var path = parsedPath.path
|
1327 | ? resolvePath(parsedPath.path, basePath, append || next.append)
|
1328 | : basePath;
|
1329 |
|
1330 | var query = resolveQuery(
|
1331 | parsedPath.query,
|
1332 | next.query,
|
1333 | router && router.options.parseQuery
|
1334 | );
|
1335 |
|
1336 | var hash = next.hash || parsedPath.hash;
|
1337 | if (hash && hash.charAt(0) !== '#') {
|
1338 | hash = "#" + hash;
|
1339 | }
|
1340 |
|
1341 | return {
|
1342 | _normalized: true,
|
1343 | path: path,
|
1344 | query: query,
|
1345 | hash: hash
|
1346 | }
|
1347 | }
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 | function createMatcher (
|
1354 | routes,
|
1355 | router
|
1356 | ) {
|
1357 | var ref = createRouteMap(routes);
|
1358 | var pathList = ref.pathList;
|
1359 | var pathMap = ref.pathMap;
|
1360 | var nameMap = ref.nameMap;
|
1361 |
|
1362 | function addRoutes (routes) {
|
1363 | createRouteMap(routes, pathList, pathMap, nameMap);
|
1364 | }
|
1365 |
|
1366 | function match (
|
1367 | raw,
|
1368 | currentRoute,
|
1369 | redirectedFrom
|
1370 | ) {
|
1371 | var location = normalizeLocation(raw, currentRoute, false, router);
|
1372 | var name = location.name;
|
1373 |
|
1374 | if (name) {
|
1375 | var record = nameMap[name];
|
1376 | {
|
1377 | warn(record, ("Route with name '" + name + "' does not exist"));
|
1378 | }
|
1379 | if (!record) { return _createRoute(null, location) }
|
1380 | var paramNames = record.regex.keys
|
1381 | .filter(function (key) { return !key.optional; })
|
1382 | .map(function (key) { return key.name; });
|
1383 |
|
1384 | if (typeof location.params !== 'object') {
|
1385 | location.params = {};
|
1386 | }
|
1387 |
|
1388 | if (currentRoute && typeof currentRoute.params === 'object') {
|
1389 | for (var key in currentRoute.params) {
|
1390 | if (!(key in location.params) && paramNames.indexOf(key) > -1) {
|
1391 | location.params[key] = currentRoute.params[key];
|
1392 | }
|
1393 | }
|
1394 | }
|
1395 |
|
1396 | if (record) {
|
1397 | location.path = fillParams(record.path, location.params, ("named route \"" + name + "\""));
|
1398 | return _createRoute(record, location, redirectedFrom)
|
1399 | }
|
1400 | } else if (location.path) {
|
1401 | location.params = {};
|
1402 | for (var i = 0; i < pathList.length; i++) {
|
1403 | var path = pathList[i];
|
1404 | var record$1 = pathMap[path];
|
1405 | if (matchRoute(record$1.regex, location.path, location.params)) {
|
1406 | return _createRoute(record$1, location, redirectedFrom)
|
1407 | }
|
1408 | }
|
1409 | }
|
1410 |
|
1411 | return _createRoute(null, location)
|
1412 | }
|
1413 |
|
1414 | function redirect (
|
1415 | record,
|
1416 | location
|
1417 | ) {
|
1418 | var originalRedirect = record.redirect;
|
1419 | var redirect = typeof originalRedirect === 'function'
|
1420 | ? originalRedirect(createRoute(record, location, null, router))
|
1421 | : originalRedirect;
|
1422 |
|
1423 | if (typeof redirect === 'string') {
|
1424 | redirect = { path: redirect };
|
1425 | }
|
1426 |
|
1427 | if (!redirect || typeof redirect !== 'object') {
|
1428 | {
|
1429 | warn(
|
1430 | false, ("invalid redirect option: " + (JSON.stringify(redirect)))
|
1431 | );
|
1432 | }
|
1433 | return _createRoute(null, location)
|
1434 | }
|
1435 |
|
1436 | var re = redirect;
|
1437 | var name = re.name;
|
1438 | var path = re.path;
|
1439 | var query = location.query;
|
1440 | var hash = location.hash;
|
1441 | var params = location.params;
|
1442 | query = re.hasOwnProperty('query') ? re.query : query;
|
1443 | hash = re.hasOwnProperty('hash') ? re.hash : hash;
|
1444 | params = re.hasOwnProperty('params') ? re.params : params;
|
1445 |
|
1446 | if (name) {
|
1447 |
|
1448 | var targetRecord = nameMap[name];
|
1449 | {
|
1450 | assert(targetRecord, ("redirect failed: named route \"" + name + "\" not found."));
|
1451 | }
|
1452 | return match({
|
1453 | _normalized: true,
|
1454 | name: name,
|
1455 | query: query,
|
1456 | hash: hash,
|
1457 | params: params
|
1458 | }, undefined, location)
|
1459 | } else if (path) {
|
1460 |
|
1461 | var rawPath = resolveRecordPath(path, record);
|
1462 |
|
1463 | var resolvedPath = fillParams(rawPath, params, ("redirect route with path \"" + rawPath + "\""));
|
1464 |
|
1465 | return match({
|
1466 | _normalized: true,
|
1467 | path: resolvedPath,
|
1468 | query: query,
|
1469 | hash: hash
|
1470 | }, undefined, location)
|
1471 | } else {
|
1472 | {
|
1473 | warn(false, ("invalid redirect option: " + (JSON.stringify(redirect))));
|
1474 | }
|
1475 | return _createRoute(null, location)
|
1476 | }
|
1477 | }
|
1478 |
|
1479 | function alias (
|
1480 | record,
|
1481 | location,
|
1482 | matchAs
|
1483 | ) {
|
1484 | var aliasedPath = fillParams(matchAs, location.params, ("aliased route with path \"" + matchAs + "\""));
|
1485 | var aliasedMatch = match({
|
1486 | _normalized: true,
|
1487 | path: aliasedPath
|
1488 | });
|
1489 | if (aliasedMatch) {
|
1490 | var matched = aliasedMatch.matched;
|
1491 | var aliasedRecord = matched[matched.length - 1];
|
1492 | location.params = aliasedMatch.params;
|
1493 | return _createRoute(aliasedRecord, location)
|
1494 | }
|
1495 | return _createRoute(null, location)
|
1496 | }
|
1497 |
|
1498 | function _createRoute (
|
1499 | record,
|
1500 | location,
|
1501 | redirectedFrom
|
1502 | ) {
|
1503 | if (record && record.redirect) {
|
1504 | return redirect(record, redirectedFrom || location)
|
1505 | }
|
1506 | if (record && record.matchAs) {
|
1507 | return alias(record, location, record.matchAs)
|
1508 | }
|
1509 | return createRoute(record, location, redirectedFrom, router)
|
1510 | }
|
1511 |
|
1512 | return {
|
1513 | match: match,
|
1514 | addRoutes: addRoutes
|
1515 | }
|
1516 | }
|
1517 |
|
1518 | function matchRoute (
|
1519 | regex,
|
1520 | path,
|
1521 | params
|
1522 | ) {
|
1523 | var m = path.match(regex);
|
1524 |
|
1525 | if (!m) {
|
1526 | return false
|
1527 | } else if (!params) {
|
1528 | return true
|
1529 | }
|
1530 |
|
1531 | for (var i = 1, len = m.length; i < len; ++i) {
|
1532 | var key = regex.keys[i - 1];
|
1533 | var val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i];
|
1534 | if (key) {
|
1535 |
|
1536 | params[key.name || 'pathMatch'] = val;
|
1537 | }
|
1538 | }
|
1539 |
|
1540 | return true
|
1541 | }
|
1542 |
|
1543 | function resolveRecordPath (path, record) {
|
1544 | return resolvePath(path, record.parent ? record.parent.path : '/', true)
|
1545 | }
|
1546 |
|
1547 |
|
1548 |
|
1549 | var positionStore = Object.create(null);
|
1550 |
|
1551 | function setupScroll () {
|
1552 |
|
1553 |
|
1554 | window.history.replaceState({ key: getStateKey() }, '', window.location.href.replace(window.location.origin, ''));
|
1555 | window.addEventListener('popstate', function (e) {
|
1556 | saveScrollPosition();
|
1557 | if (e.state && e.state.key) {
|
1558 | setStateKey(e.state.key);
|
1559 | }
|
1560 | });
|
1561 | }
|
1562 |
|
1563 | function handleScroll (
|
1564 | router,
|
1565 | to,
|
1566 | from,
|
1567 | isPop
|
1568 | ) {
|
1569 | if (!router.app) {
|
1570 | return
|
1571 | }
|
1572 |
|
1573 | var behavior = router.options.scrollBehavior;
|
1574 | if (!behavior) {
|
1575 | return
|
1576 | }
|
1577 |
|
1578 | {
|
1579 | assert(typeof behavior === 'function', "scrollBehavior must be a function");
|
1580 | }
|
1581 |
|
1582 |
|
1583 | router.app.$nextTick(function () {
|
1584 | var position = getScrollPosition();
|
1585 | var shouldScroll = behavior.call(router, to, from, isPop ? position : null);
|
1586 |
|
1587 | if (!shouldScroll) {
|
1588 | return
|
1589 | }
|
1590 |
|
1591 | if (typeof shouldScroll.then === 'function') {
|
1592 | shouldScroll.then(function (shouldScroll) {
|
1593 | scrollToPosition((shouldScroll), position);
|
1594 | }).catch(function (err) {
|
1595 | {
|
1596 | assert(false, err.toString());
|
1597 | }
|
1598 | });
|
1599 | } else {
|
1600 | scrollToPosition(shouldScroll, position);
|
1601 | }
|
1602 | });
|
1603 | }
|
1604 |
|
1605 | function saveScrollPosition () {
|
1606 | var key = getStateKey();
|
1607 | if (key) {
|
1608 | positionStore[key] = {
|
1609 | x: window.pageXOffset,
|
1610 | y: window.pageYOffset
|
1611 | };
|
1612 | }
|
1613 | }
|
1614 |
|
1615 | function getScrollPosition () {
|
1616 | var key = getStateKey();
|
1617 | if (key) {
|
1618 | return positionStore[key]
|
1619 | }
|
1620 | }
|
1621 |
|
1622 | function getElementPosition (el, offset) {
|
1623 | var docEl = document.documentElement;
|
1624 | var docRect = docEl.getBoundingClientRect();
|
1625 | var elRect = el.getBoundingClientRect();
|
1626 | return {
|
1627 | x: elRect.left - docRect.left - offset.x,
|
1628 | y: elRect.top - docRect.top - offset.y
|
1629 | }
|
1630 | }
|
1631 |
|
1632 | function isValidPosition (obj) {
|
1633 | return isNumber(obj.x) || isNumber(obj.y)
|
1634 | }
|
1635 |
|
1636 | function normalizePosition (obj) {
|
1637 | return {
|
1638 | x: isNumber(obj.x) ? obj.x : window.pageXOffset,
|
1639 | y: isNumber(obj.y) ? obj.y : window.pageYOffset
|
1640 | }
|
1641 | }
|
1642 |
|
1643 | function normalizeOffset (obj) {
|
1644 | return {
|
1645 | x: isNumber(obj.x) ? obj.x : 0,
|
1646 | y: isNumber(obj.y) ? obj.y : 0
|
1647 | }
|
1648 | }
|
1649 |
|
1650 | function isNumber (v) {
|
1651 | return typeof v === 'number'
|
1652 | }
|
1653 |
|
1654 | function scrollToPosition (shouldScroll, position) {
|
1655 | var isObject = typeof shouldScroll === 'object';
|
1656 | if (isObject && typeof shouldScroll.selector === 'string') {
|
1657 | var el = document.querySelector(shouldScroll.selector);
|
1658 | if (el) {
|
1659 | var offset = shouldScroll.offset && typeof shouldScroll.offset === 'object' ? shouldScroll.offset : {};
|
1660 | offset = normalizeOffset(offset);
|
1661 | position = getElementPosition(el, offset);
|
1662 | } else if (isValidPosition(shouldScroll)) {
|
1663 | position = normalizePosition(shouldScroll);
|
1664 | }
|
1665 | } else if (isObject && isValidPosition(shouldScroll)) {
|
1666 | position = normalizePosition(shouldScroll);
|
1667 | }
|
1668 |
|
1669 | if (position) {
|
1670 | window.scrollTo(position.x, position.y);
|
1671 | }
|
1672 | }
|
1673 |
|
1674 |
|
1675 |
|
1676 | var supportsPushState = inBrowser && (function () {
|
1677 | var ua = window.navigator.userAgent;
|
1678 |
|
1679 | if (
|
1680 | (ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
|
1681 | ua.indexOf('Mobile Safari') !== -1 &&
|
1682 | ua.indexOf('Chrome') === -1 &&
|
1683 | ua.indexOf('Windows Phone') === -1
|
1684 | ) {
|
1685 | return false
|
1686 | }
|
1687 |
|
1688 | return window.history && 'pushState' in window.history
|
1689 | })();
|
1690 |
|
1691 |
|
1692 | var Time = inBrowser && window.performance && window.performance.now
|
1693 | ? window.performance
|
1694 | : Date;
|
1695 |
|
1696 | var _key = genKey();
|
1697 |
|
1698 | function genKey () {
|
1699 | return Time.now().toFixed(3)
|
1700 | }
|
1701 |
|
1702 | function getStateKey () {
|
1703 | return _key
|
1704 | }
|
1705 |
|
1706 | function setStateKey (key) {
|
1707 | _key = key;
|
1708 | }
|
1709 |
|
1710 | function pushState (url, replace) {
|
1711 | saveScrollPosition();
|
1712 |
|
1713 |
|
1714 | var history = window.history;
|
1715 | try {
|
1716 | if (replace) {
|
1717 | history.replaceState({ key: _key }, '', url);
|
1718 | } else {
|
1719 | _key = genKey();
|
1720 | history.pushState({ key: _key }, '', url);
|
1721 | }
|
1722 | } catch (e) {
|
1723 | window.location[replace ? 'replace' : 'assign'](url);
|
1724 | }
|
1725 | }
|
1726 |
|
1727 | function replaceState (url) {
|
1728 | pushState(url, true);
|
1729 | }
|
1730 |
|
1731 |
|
1732 |
|
1733 | function runQueue (queue, fn, cb) {
|
1734 | var step = function (index) {
|
1735 | if (index >= queue.length) {
|
1736 | cb();
|
1737 | } else {
|
1738 | if (queue[index]) {
|
1739 | fn(queue[index], function () {
|
1740 | step(index + 1);
|
1741 | });
|
1742 | } else {
|
1743 | step(index + 1);
|
1744 | }
|
1745 | }
|
1746 | };
|
1747 | step(0);
|
1748 | }
|
1749 |
|
1750 |
|
1751 |
|
1752 | function resolveAsyncComponents (matched) {
|
1753 | return function (to, from, next) {
|
1754 | var hasAsync = false;
|
1755 | var pending = 0;
|
1756 | var error = null;
|
1757 |
|
1758 | flatMapComponents(matched, function (def, _, match, key) {
|
1759 |
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 | if (typeof def === 'function' && def.cid === undefined) {
|
1765 | hasAsync = true;
|
1766 | pending++;
|
1767 |
|
1768 | var resolve = once(function (resolvedDef) {
|
1769 | if (isESModule(resolvedDef)) {
|
1770 | resolvedDef = resolvedDef.default;
|
1771 | }
|
1772 |
|
1773 | def.resolved = typeof resolvedDef === 'function'
|
1774 | ? resolvedDef
|
1775 | : _Vue.extend(resolvedDef);
|
1776 | match.components[key] = resolvedDef;
|
1777 | pending--;
|
1778 | if (pending <= 0) {
|
1779 | next();
|
1780 | }
|
1781 | });
|
1782 |
|
1783 | var reject = once(function (reason) {
|
1784 | var msg = "Failed to resolve async component " + key + ": " + reason;
|
1785 | "development" !== 'production' && warn(false, msg);
|
1786 | if (!error) {
|
1787 | error = isError(reason)
|
1788 | ? reason
|
1789 | : new Error(msg);
|
1790 | next(error);
|
1791 | }
|
1792 | });
|
1793 |
|
1794 | var res;
|
1795 | try {
|
1796 | res = def(resolve, reject);
|
1797 | } catch (e) {
|
1798 | reject(e);
|
1799 | }
|
1800 | if (res) {
|
1801 | if (typeof res.then === 'function') {
|
1802 | res.then(resolve, reject);
|
1803 | } else {
|
1804 |
|
1805 | var comp = res.component;
|
1806 | if (comp && typeof comp.then === 'function') {
|
1807 | comp.then(resolve, reject);
|
1808 | }
|
1809 | }
|
1810 | }
|
1811 | }
|
1812 | });
|
1813 |
|
1814 | if (!hasAsync) { next(); }
|
1815 | }
|
1816 | }
|
1817 |
|
1818 | function flatMapComponents (
|
1819 | matched,
|
1820 | fn
|
1821 | ) {
|
1822 | return flatten(matched.map(function (m) {
|
1823 | return Object.keys(m.components).map(function (key) { return fn(
|
1824 | m.components[key],
|
1825 | m.instances[key],
|
1826 | m, key
|
1827 | ); })
|
1828 | }))
|
1829 | }
|
1830 |
|
1831 | function flatten (arr) {
|
1832 | return Array.prototype.concat.apply([], arr)
|
1833 | }
|
1834 |
|
1835 | var hasSymbol =
|
1836 | typeof Symbol === 'function' &&
|
1837 | typeof Symbol.toStringTag === 'symbol';
|
1838 |
|
1839 | function isESModule (obj) {
|
1840 | return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
|
1841 | }
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 | function once (fn) {
|
1848 | var called = false;
|
1849 | return function () {
|
1850 | var args = [], len = arguments.length;
|
1851 | while ( len-- ) args[ len ] = arguments[ len ];
|
1852 |
|
1853 | if (called) { return }
|
1854 | called = true;
|
1855 | return fn.apply(this, args)
|
1856 | }
|
1857 | }
|
1858 |
|
1859 |
|
1860 |
|
1861 | var History = function History (router, base) {
|
1862 | this.router = router;
|
1863 | this.base = normalizeBase(base);
|
1864 |
|
1865 | this.current = START;
|
1866 | this.pending = null;
|
1867 | this.ready = false;
|
1868 | this.readyCbs = [];
|
1869 | this.readyErrorCbs = [];
|
1870 | this.errorCbs = [];
|
1871 | };
|
1872 |
|
1873 | History.prototype.listen = function listen (cb) {
|
1874 | this.cb = cb;
|
1875 | };
|
1876 |
|
1877 | History.prototype.onReady = function onReady (cb, errorCb) {
|
1878 | if (this.ready) {
|
1879 | cb();
|
1880 | } else {
|
1881 | this.readyCbs.push(cb);
|
1882 | if (errorCb) {
|
1883 | this.readyErrorCbs.push(errorCb);
|
1884 | }
|
1885 | }
|
1886 | };
|
1887 |
|
1888 | History.prototype.onError = function onError (errorCb) {
|
1889 | this.errorCbs.push(errorCb);
|
1890 | };
|
1891 |
|
1892 | History.prototype.transitionTo = function transitionTo (location, onComplete, onAbort) {
|
1893 | var this$1 = this;
|
1894 |
|
1895 | var route = this.router.match(location, this.current);
|
1896 | this.confirmTransition(route, function () {
|
1897 | this$1.updateRoute(route);
|
1898 | onComplete && onComplete(route);
|
1899 | this$1.ensureURL();
|
1900 |
|
1901 |
|
1902 | if (!this$1.ready) {
|
1903 | this$1.ready = true;
|
1904 | this$1.readyCbs.forEach(function (cb) { cb(route); });
|
1905 | }
|
1906 | }, function (err) {
|
1907 | if (onAbort) {
|
1908 | onAbort(err);
|
1909 | }
|
1910 | if (err && !this$1.ready) {
|
1911 | this$1.ready = true;
|
1912 | this$1.readyErrorCbs.forEach(function (cb) { cb(err); });
|
1913 | }
|
1914 | });
|
1915 | };
|
1916 |
|
1917 | History.prototype.confirmTransition = function confirmTransition (route, onComplete, onAbort) {
|
1918 | var this$1 = this;
|
1919 |
|
1920 | var current = this.current;
|
1921 | var abort = function (err) {
|
1922 | if (isError(err)) {
|
1923 | if (this$1.errorCbs.length) {
|
1924 | this$1.errorCbs.forEach(function (cb) { cb(err); });
|
1925 | } else {
|
1926 | warn(false, 'uncaught error during route navigation:');
|
1927 | console.error(err);
|
1928 | }
|
1929 | }
|
1930 | onAbort && onAbort(err);
|
1931 | };
|
1932 | if (
|
1933 | isSameRoute(route, current) &&
|
1934 |
|
1935 | route.matched.length === current.matched.length
|
1936 | ) {
|
1937 | this.ensureURL();
|
1938 | return abort()
|
1939 | }
|
1940 |
|
1941 | var ref = resolveQueue(this.current.matched, route.matched);
|
1942 | var updated = ref.updated;
|
1943 | var deactivated = ref.deactivated;
|
1944 | var activated = ref.activated;
|
1945 |
|
1946 | var queue = [].concat(
|
1947 |
|
1948 | extractLeaveGuards(deactivated),
|
1949 |
|
1950 | this.router.beforeHooks,
|
1951 |
|
1952 | extractUpdateHooks(updated),
|
1953 |
|
1954 | activated.map(function (m) { return m.beforeEnter; }),
|
1955 |
|
1956 | resolveAsyncComponents(activated)
|
1957 | );
|
1958 |
|
1959 | this.pending = route;
|
1960 | var iterator = function (hook, next) {
|
1961 | if (this$1.pending !== route) {
|
1962 | return abort()
|
1963 | }
|
1964 | try {
|
1965 | hook(route, current, function (to) {
|
1966 | if (to === false || isError(to)) {
|
1967 |
|
1968 | this$1.ensureURL(true);
|
1969 | abort(to);
|
1970 | } else if (
|
1971 | typeof to === 'string' ||
|
1972 | (typeof to === 'object' && (
|
1973 | typeof to.path === 'string' ||
|
1974 | typeof to.name === 'string'
|
1975 | ))
|
1976 | ) {
|
1977 |
|
1978 | abort();
|
1979 | if (typeof to === 'object' && to.replace) {
|
1980 | this$1.replace(to);
|
1981 | } else {
|
1982 | this$1.push(to);
|
1983 | }
|
1984 | } else {
|
1985 |
|
1986 | next(to);
|
1987 | }
|
1988 | });
|
1989 | } catch (e) {
|
1990 | abort(e);
|
1991 | }
|
1992 | };
|
1993 |
|
1994 | runQueue(queue, iterator, function () {
|
1995 | var postEnterCbs = [];
|
1996 | var isValid = function () { return this$1.current === route; };
|
1997 |
|
1998 |
|
1999 | var enterGuards = extractEnterGuards(activated, postEnterCbs, isValid);
|
2000 | var queue = enterGuards.concat(this$1.router.resolveHooks);
|
2001 | runQueue(queue, iterator, function () {
|
2002 | if (this$1.pending !== route) {
|
2003 | return abort()
|
2004 | }
|
2005 | this$1.pending = null;
|
2006 | onComplete(route);
|
2007 | if (this$1.router.app) {
|
2008 | this$1.router.app.$nextTick(function () {
|
2009 | postEnterCbs.forEach(function (cb) { cb(); });
|
2010 | });
|
2011 | }
|
2012 | });
|
2013 | });
|
2014 | };
|
2015 |
|
2016 | History.prototype.updateRoute = function updateRoute (route) {
|
2017 | var prev = this.current;
|
2018 | this.current = route;
|
2019 | this.cb && this.cb(route);
|
2020 | this.router.afterHooks.forEach(function (hook) {
|
2021 | hook && hook(route, prev);
|
2022 | });
|
2023 | };
|
2024 |
|
2025 | function normalizeBase (base) {
|
2026 | if (!base) {
|
2027 | if (inBrowser) {
|
2028 |
|
2029 | var baseEl = document.querySelector('base');
|
2030 | base = (baseEl && baseEl.getAttribute('href')) || '/';
|
2031 |
|
2032 | base = base.replace(/^https?:\/\/[^\/]+/, '');
|
2033 | } else {
|
2034 | base = '/';
|
2035 | }
|
2036 | }
|
2037 |
|
2038 | if (base.charAt(0) !== '/') {
|
2039 | base = '/' + base;
|
2040 | }
|
2041 |
|
2042 | return base.replace(/\/$/, '')
|
2043 | }
|
2044 |
|
2045 | function resolveQueue (
|
2046 | current,
|
2047 | next
|
2048 | ) {
|
2049 | var i;
|
2050 | var max = Math.max(current.length, next.length);
|
2051 | for (i = 0; i < max; i++) {
|
2052 | if (current[i] !== next[i]) {
|
2053 | break
|
2054 | }
|
2055 | }
|
2056 | return {
|
2057 | updated: next.slice(0, i),
|
2058 | activated: next.slice(i),
|
2059 | deactivated: current.slice(i)
|
2060 | }
|
2061 | }
|
2062 |
|
2063 | function extractGuards (
|
2064 | records,
|
2065 | name,
|
2066 | bind,
|
2067 | reverse
|
2068 | ) {
|
2069 | var guards = flatMapComponents(records, function (def, instance, match, key) {
|
2070 | var guard = extractGuard(def, name);
|
2071 | if (guard) {
|
2072 | return Array.isArray(guard)
|
2073 | ? guard.map(function (guard) { return bind(guard, instance, match, key); })
|
2074 | : bind(guard, instance, match, key)
|
2075 | }
|
2076 | });
|
2077 | return flatten(reverse ? guards.reverse() : guards)
|
2078 | }
|
2079 |
|
2080 | function extractGuard (
|
2081 | def,
|
2082 | key
|
2083 | ) {
|
2084 | if (typeof def !== 'function') {
|
2085 |
|
2086 | def = _Vue.extend(def);
|
2087 | }
|
2088 | return def.options[key]
|
2089 | }
|
2090 |
|
2091 | function extractLeaveGuards (deactivated) {
|
2092 | return extractGuards(deactivated, 'beforeRouteLeave', bindGuard, true)
|
2093 | }
|
2094 |
|
2095 | function extractUpdateHooks (updated) {
|
2096 | return extractGuards(updated, 'beforeRouteUpdate', bindGuard)
|
2097 | }
|
2098 |
|
2099 | function bindGuard (guard, instance) {
|
2100 | if (instance) {
|
2101 | return function boundRouteGuard () {
|
2102 | return guard.apply(instance, arguments)
|
2103 | }
|
2104 | }
|
2105 | }
|
2106 |
|
2107 | function extractEnterGuards (
|
2108 | activated,
|
2109 | cbs,
|
2110 | isValid
|
2111 | ) {
|
2112 | return extractGuards(activated, 'beforeRouteEnter', function (guard, _, match, key) {
|
2113 | return bindEnterGuard(guard, match, key, cbs, isValid)
|
2114 | })
|
2115 | }
|
2116 |
|
2117 | function bindEnterGuard (
|
2118 | guard,
|
2119 | match,
|
2120 | key,
|
2121 | cbs,
|
2122 | isValid
|
2123 | ) {
|
2124 | return function routeEnterGuard (to, from, next) {
|
2125 | return guard(to, from, function (cb) {
|
2126 | next(cb);
|
2127 | if (typeof cb === 'function') {
|
2128 | cbs.push(function () {
|
2129 |
|
2130 |
|
2131 |
|
2132 |
|
2133 |
|
2134 | poll(cb, match.instances, key, isValid);
|
2135 | });
|
2136 | }
|
2137 | })
|
2138 | }
|
2139 | }
|
2140 |
|
2141 | function poll (
|
2142 | cb, // somehow flow cannot infer this is a function
|
2143 | instances,
|
2144 | key,
|
2145 | isValid
|
2146 | ) {
|
2147 | if (
|
2148 | instances[key] &&
|
2149 | !instances[key]._isBeingDestroyed
|
2150 | ) {
|
2151 | cb(instances[key]);
|
2152 | } else if (isValid()) {
|
2153 | setTimeout(function () {
|
2154 | poll(cb, instances, key, isValid);
|
2155 | }, 16);
|
2156 | }
|
2157 | }
|
2158 |
|
2159 |
|
2160 |
|
2161 | var HTML5History = (function (History$$1) {
|
2162 | function HTML5History (router, base) {
|
2163 | var this$1 = this;
|
2164 |
|
2165 | History$$1.call(this, router, base);
|
2166 |
|
2167 | var expectScroll = router.options.scrollBehavior;
|
2168 | var supportsScroll = supportsPushState && expectScroll;
|
2169 |
|
2170 | if (supportsScroll) {
|
2171 | setupScroll();
|
2172 | }
|
2173 |
|
2174 | var initLocation = getLocation(this.base);
|
2175 | window.addEventListener('popstate', function (e) {
|
2176 | var current = this$1.current;
|
2177 |
|
2178 |
|
2179 |
|
2180 | var location = getLocation(this$1.base);
|
2181 | if (this$1.current === START && location === initLocation) {
|
2182 | return
|
2183 | }
|
2184 |
|
2185 | this$1.transitionTo(location, function (route) {
|
2186 | if (supportsScroll) {
|
2187 | handleScroll(router, route, current, true);
|
2188 | }
|
2189 | });
|
2190 | });
|
2191 | }
|
2192 |
|
2193 | if ( History$$1 ) HTML5History.__proto__ = History$$1;
|
2194 | HTML5History.prototype = Object.create( History$$1 && History$$1.prototype );
|
2195 | HTML5History.prototype.constructor = HTML5History;
|
2196 |
|
2197 | HTML5History.prototype.go = function go (n) {
|
2198 | window.history.go(n);
|
2199 | };
|
2200 |
|
2201 | HTML5History.prototype.push = function push (location, onComplete, onAbort) {
|
2202 | var this$1 = this;
|
2203 |
|
2204 | var ref = this;
|
2205 | var fromRoute = ref.current;
|
2206 | this.transitionTo(location, function (route) {
|
2207 | pushState(cleanPath(this$1.base + route.fullPath));
|
2208 | handleScroll(this$1.router, route, fromRoute, false);
|
2209 | onComplete && onComplete(route);
|
2210 | }, onAbort);
|
2211 | };
|
2212 |
|
2213 | HTML5History.prototype.replace = function replace (location, onComplete, onAbort) {
|
2214 | var this$1 = this;
|
2215 |
|
2216 | var ref = this;
|
2217 | var fromRoute = ref.current;
|
2218 | this.transitionTo(location, function (route) {
|
2219 | replaceState(cleanPath(this$1.base + route.fullPath));
|
2220 | handleScroll(this$1.router, route, fromRoute, false);
|
2221 | onComplete && onComplete(route);
|
2222 | }, onAbort);
|
2223 | };
|
2224 |
|
2225 | HTML5History.prototype.ensureURL = function ensureURL (push) {
|
2226 | if (getLocation(this.base) !== this.current.fullPath) {
|
2227 | var current = cleanPath(this.base + this.current.fullPath);
|
2228 | push ? pushState(current) : replaceState(current);
|
2229 | }
|
2230 | };
|
2231 |
|
2232 | HTML5History.prototype.getCurrentLocation = function getCurrentLocation () {
|
2233 | return getLocation(this.base)
|
2234 | };
|
2235 |
|
2236 | return HTML5History;
|
2237 | }(History));
|
2238 |
|
2239 | function getLocation (base) {
|
2240 | var path = decodeURI(window.location.pathname);
|
2241 | if (base && path.indexOf(base) === 0) {
|
2242 | path = path.slice(base.length);
|
2243 | }
|
2244 | return (path || '/') + window.location.search + window.location.hash
|
2245 | }
|
2246 |
|
2247 |
|
2248 |
|
2249 | var HashHistory = (function (History$$1) {
|
2250 | function HashHistory (router, base, fallback) {
|
2251 | History$$1.call(this, router, base);
|
2252 |
|
2253 | if (fallback && checkFallback(this.base)) {
|
2254 | return
|
2255 | }
|
2256 | ensureSlash();
|
2257 | }
|
2258 |
|
2259 | if ( History$$1 ) HashHistory.__proto__ = History$$1;
|
2260 | HashHistory.prototype = Object.create( History$$1 && History$$1.prototype );
|
2261 | HashHistory.prototype.constructor = HashHistory;
|
2262 |
|
2263 |
|
2264 |
|
2265 | HashHistory.prototype.setupListeners = function setupListeners () {
|
2266 | var this$1 = this;
|
2267 |
|
2268 | var router = this.router;
|
2269 | var expectScroll = router.options.scrollBehavior;
|
2270 | var supportsScroll = supportsPushState && expectScroll;
|
2271 |
|
2272 | if (supportsScroll) {
|
2273 | setupScroll();
|
2274 | }
|
2275 |
|
2276 | window.addEventListener(supportsPushState ? 'popstate' : 'hashchange', function () {
|
2277 | var current = this$1.current;
|
2278 | if (!ensureSlash()) {
|
2279 | return
|
2280 | }
|
2281 | this$1.transitionTo(getHash(), function (route) {
|
2282 | if (supportsScroll) {
|
2283 | handleScroll(this$1.router, route, current, true);
|
2284 | }
|
2285 | if (!supportsPushState) {
|
2286 | replaceHash(route.fullPath);
|
2287 | }
|
2288 | });
|
2289 | });
|
2290 | };
|
2291 |
|
2292 | HashHistory.prototype.push = function push (location, onComplete, onAbort) {
|
2293 | var this$1 = this;
|
2294 |
|
2295 | var ref = this;
|
2296 | var fromRoute = ref.current;
|
2297 | this.transitionTo(location, function (route) {
|
2298 | pushHash(route.fullPath);
|
2299 | handleScroll(this$1.router, route, fromRoute, false);
|
2300 | onComplete && onComplete(route);
|
2301 | }, onAbort);
|
2302 | };
|
2303 |
|
2304 | HashHistory.prototype.replace = function replace (location, onComplete, onAbort) {
|
2305 | var this$1 = this;
|
2306 |
|
2307 | var ref = this;
|
2308 | var fromRoute = ref.current;
|
2309 | this.transitionTo(location, function (route) {
|
2310 | replaceHash(route.fullPath);
|
2311 | handleScroll(this$1.router, route, fromRoute, false);
|
2312 | onComplete && onComplete(route);
|
2313 | }, onAbort);
|
2314 | };
|
2315 |
|
2316 | HashHistory.prototype.go = function go (n) {
|
2317 | window.history.go(n);
|
2318 | };
|
2319 |
|
2320 | HashHistory.prototype.ensureURL = function ensureURL (push) {
|
2321 | var current = this.current.fullPath;
|
2322 | if (getHash() !== current) {
|
2323 | push ? pushHash(current) : replaceHash(current);
|
2324 | }
|
2325 | };
|
2326 |
|
2327 | HashHistory.prototype.getCurrentLocation = function getCurrentLocation () {
|
2328 | return getHash()
|
2329 | };
|
2330 |
|
2331 | return HashHistory;
|
2332 | }(History));
|
2333 |
|
2334 | function checkFallback (base) {
|
2335 | var location = getLocation(base);
|
2336 | if (!/^\/#/.test(location)) {
|
2337 | window.location.replace(
|
2338 | cleanPath(base + '/#' + location)
|
2339 | );
|
2340 | return true
|
2341 | }
|
2342 | }
|
2343 |
|
2344 | function ensureSlash () {
|
2345 | var path = getHash();
|
2346 | if (path.charAt(0) === '/') {
|
2347 | return true
|
2348 | }
|
2349 | replaceHash('/' + path);
|
2350 | return false
|
2351 | }
|
2352 |
|
2353 | function getHash () {
|
2354 |
|
2355 |
|
2356 | var href = window.location.href;
|
2357 | var index = href.indexOf('#');
|
2358 |
|
2359 | if (index < 0) { return '' }
|
2360 |
|
2361 | href = href.slice(index + 1);
|
2362 |
|
2363 |
|
2364 |
|
2365 | var searchIndex = href.indexOf('?');
|
2366 | if (searchIndex < 0) {
|
2367 | var hashIndex = href.indexOf('#');
|
2368 | if (hashIndex > -1) { href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex); }
|
2369 | else { href = decodeURI(href); }
|
2370 | } else {
|
2371 | if (searchIndex > -1) { href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex); }
|
2372 | }
|
2373 |
|
2374 | return href
|
2375 | }
|
2376 |
|
2377 | function getUrl (path) {
|
2378 | var href = window.location.href;
|
2379 | var i = href.indexOf('#');
|
2380 | var base = i >= 0 ? href.slice(0, i) : href;
|
2381 | return (base + "#" + path)
|
2382 | }
|
2383 |
|
2384 | function pushHash (path) {
|
2385 | if (supportsPushState) {
|
2386 | pushState(getUrl(path));
|
2387 | } else {
|
2388 | window.location.hash = path;
|
2389 | }
|
2390 | }
|
2391 |
|
2392 | function replaceHash (path) {
|
2393 | if (supportsPushState) {
|
2394 | replaceState(getUrl(path));
|
2395 | } else {
|
2396 | window.location.replace(getUrl(path));
|
2397 | }
|
2398 | }
|
2399 |
|
2400 |
|
2401 |
|
2402 | var AbstractHistory = (function (History$$1) {
|
2403 | function AbstractHistory (router, base) {
|
2404 | History$$1.call(this, router, base);
|
2405 | this.stack = [];
|
2406 | this.index = -1;
|
2407 | }
|
2408 |
|
2409 | if ( History$$1 ) AbstractHistory.__proto__ = History$$1;
|
2410 | AbstractHistory.prototype = Object.create( History$$1 && History$$1.prototype );
|
2411 | AbstractHistory.prototype.constructor = AbstractHistory;
|
2412 |
|
2413 | AbstractHistory.prototype.push = function push (location, onComplete, onAbort) {
|
2414 | var this$1 = this;
|
2415 |
|
2416 | this.transitionTo(location, function (route) {
|
2417 | this$1.stack = this$1.stack.slice(0, this$1.index + 1).concat(route);
|
2418 | this$1.index++;
|
2419 | onComplete && onComplete(route);
|
2420 | }, onAbort);
|
2421 | };
|
2422 |
|
2423 | AbstractHistory.prototype.replace = function replace (location, onComplete, onAbort) {
|
2424 | var this$1 = this;
|
2425 |
|
2426 | this.transitionTo(location, function (route) {
|
2427 | this$1.stack = this$1.stack.slice(0, this$1.index).concat(route);
|
2428 | onComplete && onComplete(route);
|
2429 | }, onAbort);
|
2430 | };
|
2431 |
|
2432 | AbstractHistory.prototype.go = function go (n) {
|
2433 | var this$1 = this;
|
2434 |
|
2435 | var targetIndex = this.index + n;
|
2436 | if (targetIndex < 0 || targetIndex >= this.stack.length) {
|
2437 | return
|
2438 | }
|
2439 | var route = this.stack[targetIndex];
|
2440 | this.confirmTransition(route, function () {
|
2441 | this$1.index = targetIndex;
|
2442 | this$1.updateRoute(route);
|
2443 | });
|
2444 | };
|
2445 |
|
2446 | AbstractHistory.prototype.getCurrentLocation = function getCurrentLocation () {
|
2447 | var current = this.stack[this.stack.length - 1];
|
2448 | return current ? current.fullPath : '/'
|
2449 | };
|
2450 |
|
2451 | AbstractHistory.prototype.ensureURL = function ensureURL () {
|
2452 |
|
2453 | };
|
2454 |
|
2455 | return AbstractHistory;
|
2456 | }(History));
|
2457 |
|
2458 |
|
2459 |
|
2460 |
|
2461 |
|
2462 | var VueRouter = function VueRouter (options) {
|
2463 | if ( options === void 0 ) options = {};
|
2464 |
|
2465 | this.app = null;
|
2466 | this.apps = [];
|
2467 | this.options = options;
|
2468 | this.beforeHooks = [];
|
2469 | this.resolveHooks = [];
|
2470 | this.afterHooks = [];
|
2471 | this.matcher = createMatcher(options.routes || [], this);
|
2472 |
|
2473 | var mode = options.mode || 'hash';
|
2474 | this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false;
|
2475 | if (this.fallback) {
|
2476 | mode = 'hash';
|
2477 | }
|
2478 | if (!inBrowser) {
|
2479 | mode = 'abstract';
|
2480 | }
|
2481 | this.mode = mode;
|
2482 |
|
2483 | switch (mode) {
|
2484 | case 'history':
|
2485 | this.history = new HTML5History(this, options.base);
|
2486 | break
|
2487 | case 'hash':
|
2488 | this.history = new HashHistory(this, options.base, this.fallback);
|
2489 | break
|
2490 | case 'abstract':
|
2491 | this.history = new AbstractHistory(this, options.base);
|
2492 | break
|
2493 | default:
|
2494 | {
|
2495 | assert(false, ("invalid mode: " + mode));
|
2496 | }
|
2497 | }
|
2498 | };
|
2499 |
|
2500 | var prototypeAccessors = { currentRoute: { configurable: true } };
|
2501 |
|
2502 | VueRouter.prototype.match = function match (
|
2503 | raw,
|
2504 | current,
|
2505 | redirectedFrom
|
2506 | ) {
|
2507 | return this.matcher.match(raw, current, redirectedFrom)
|
2508 | };
|
2509 |
|
2510 | prototypeAccessors.currentRoute.get = function () {
|
2511 | return this.history && this.history.current
|
2512 | };
|
2513 |
|
2514 | VueRouter.prototype.init = function init (app /* Vue component instance */) {
|
2515 | var this$1 = this;
|
2516 |
|
2517 | "development" !== 'production' && assert(
|
2518 | install.installed,
|
2519 | "not installed. Make sure to call `Vue.use(VueRouter)` " +
|
2520 | "before creating root instance."
|
2521 | );
|
2522 |
|
2523 | this.apps.push(app);
|
2524 |
|
2525 |
|
2526 |
|
2527 | app.$once('hook:destroyed', function () {
|
2528 |
|
2529 | var index = this$1.apps.indexOf(app);
|
2530 | if (index > -1) { this$1.apps.splice(index, 1); }
|
2531 |
|
2532 |
|
2533 | if (this$1.app === app) { this$1.app = this$1.apps[0] || null; }
|
2534 | });
|
2535 |
|
2536 |
|
2537 |
|
2538 | if (this.app) {
|
2539 | return
|
2540 | }
|
2541 |
|
2542 | this.app = app;
|
2543 |
|
2544 | var history = this.history;
|
2545 |
|
2546 | if (history instanceof HTML5History) {
|
2547 | history.transitionTo(history.getCurrentLocation());
|
2548 | } else if (history instanceof HashHistory) {
|
2549 | var setupHashListener = function () {
|
2550 | history.setupListeners();
|
2551 | };
|
2552 | history.transitionTo(
|
2553 | history.getCurrentLocation(),
|
2554 | setupHashListener,
|
2555 | setupHashListener
|
2556 | );
|
2557 | }
|
2558 |
|
2559 | history.listen(function (route) {
|
2560 | this$1.apps.forEach(function (app) {
|
2561 | app._route = route;
|
2562 | });
|
2563 | });
|
2564 | };
|
2565 |
|
2566 | VueRouter.prototype.beforeEach = function beforeEach (fn) {
|
2567 | return registerHook(this.beforeHooks, fn)
|
2568 | };
|
2569 |
|
2570 | VueRouter.prototype.beforeResolve = function beforeResolve (fn) {
|
2571 | return registerHook(this.resolveHooks, fn)
|
2572 | };
|
2573 |
|
2574 | VueRouter.prototype.afterEach = function afterEach (fn) {
|
2575 | return registerHook(this.afterHooks, fn)
|
2576 | };
|
2577 |
|
2578 | VueRouter.prototype.onReady = function onReady (cb, errorCb) {
|
2579 | this.history.onReady(cb, errorCb);
|
2580 | };
|
2581 |
|
2582 | VueRouter.prototype.onError = function onError (errorCb) {
|
2583 | this.history.onError(errorCb);
|
2584 | };
|
2585 |
|
2586 | VueRouter.prototype.push = function push (location, onComplete, onAbort) {
|
2587 | this.history.push(location, onComplete, onAbort);
|
2588 | };
|
2589 |
|
2590 | VueRouter.prototype.replace = function replace (location, onComplete, onAbort) {
|
2591 | this.history.replace(location, onComplete, onAbort);
|
2592 | };
|
2593 |
|
2594 | VueRouter.prototype.go = function go (n) {
|
2595 | this.history.go(n);
|
2596 | };
|
2597 |
|
2598 | VueRouter.prototype.back = function back () {
|
2599 | this.go(-1);
|
2600 | };
|
2601 |
|
2602 | VueRouter.prototype.forward = function forward () {
|
2603 | this.go(1);
|
2604 | };
|
2605 |
|
2606 | VueRouter.prototype.getMatchedComponents = function getMatchedComponents (to) {
|
2607 | var route = to
|
2608 | ? to.matched
|
2609 | ? to
|
2610 | : this.resolve(to).route
|
2611 | : this.currentRoute;
|
2612 | if (!route) {
|
2613 | return []
|
2614 | }
|
2615 | return [].concat.apply([], route.matched.map(function (m) {
|
2616 | return Object.keys(m.components).map(function (key) {
|
2617 | return m.components[key]
|
2618 | })
|
2619 | }))
|
2620 | };
|
2621 |
|
2622 | VueRouter.prototype.resolve = function resolve (
|
2623 | to,
|
2624 | current,
|
2625 | append
|
2626 | ) {
|
2627 | current = current || this.history.current;
|
2628 | var location = normalizeLocation(
|
2629 | to,
|
2630 | current,
|
2631 | append,
|
2632 | this
|
2633 | );
|
2634 | var route = this.match(location, current);
|
2635 | var fullPath = route.redirectedFrom || route.fullPath;
|
2636 | var base = this.history.base;
|
2637 | var href = createHref(base, fullPath, this.mode);
|
2638 | return {
|
2639 | location: location,
|
2640 | route: route,
|
2641 | href: href,
|
2642 |
|
2643 | normalizedTo: location,
|
2644 | resolved: route
|
2645 | }
|
2646 | };
|
2647 |
|
2648 | VueRouter.prototype.addRoutes = function addRoutes (routes) {
|
2649 | this.matcher.addRoutes(routes);
|
2650 | if (this.history.current !== START) {
|
2651 | this.history.transitionTo(this.history.getCurrentLocation());
|
2652 | }
|
2653 | };
|
2654 |
|
2655 | Object.defineProperties( VueRouter.prototype, prototypeAccessors );
|
2656 |
|
2657 | function registerHook (list, fn) {
|
2658 | list.push(fn);
|
2659 | return function () {
|
2660 | var i = list.indexOf(fn);
|
2661 | if (i > -1) { list.splice(i, 1); }
|
2662 | }
|
2663 | }
|
2664 |
|
2665 | function createHref (base, fullPath, mode) {
|
2666 | var path = mode === 'hash' ? '#' + fullPath : fullPath;
|
2667 | return base ? cleanPath(base + '/' + path) : path
|
2668 | }
|
2669 |
|
2670 | VueRouter.install = install;
|
2671 | VueRouter.version = '3.0.6';
|
2672 |
|
2673 | if (inBrowser && window.Vue) {
|
2674 | window.Vue.use(VueRouter);
|
2675 | }
|
2676 |
|
2677 | return VueRouter;
|
2678 |
|
2679 | })));
|