kdu-router 3.4.0-beta.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +8 -6
  3. package/dist/kdu-router.cjs.js +2848 -0
  4. package/dist/kdu-router.cjs.prod.js +2610 -0
  5. package/dist/kdu-router.d.ts +1255 -0
  6. package/dist/kdu-router.esm-browser.js +3332 -0
  7. package/dist/kdu-router.esm-bundler.js +3343 -0
  8. package/dist/kdu-router.global.js +3354 -0
  9. package/dist/kdu-router.global.prod.js +6 -0
  10. package/ketur/attributes.json +8 -14
  11. package/ketur/tags.json +2 -12
  12. package/package.json +64 -92
  13. package/dist/kdu-router.common.js +0 -3040
  14. package/dist/kdu-router.esm.browser.js +0 -3005
  15. package/dist/kdu-router.esm.browser.min.js +0 -11
  16. package/dist/kdu-router.esm.js +0 -3038
  17. package/dist/kdu-router.js +0 -3046
  18. package/dist/kdu-router.min.js +0 -11
  19. package/src/components/link.js +0 -197
  20. package/src/components/view.js +0 -149
  21. package/src/create-matcher.js +0 -200
  22. package/src/create-route-map.js +0 -205
  23. package/src/history/abstract.js +0 -68
  24. package/src/history/base.js +0 -400
  25. package/src/history/hash.js +0 -163
  26. package/src/history/html5.js +0 -94
  27. package/src/index.js +0 -277
  28. package/src/install.js +0 -52
  29. package/src/util/async.js +0 -18
  30. package/src/util/dom.js +0 -3
  31. package/src/util/errors.js +0 -85
  32. package/src/util/location.js +0 -69
  33. package/src/util/misc.js +0 -6
  34. package/src/util/params.js +0 -37
  35. package/src/util/path.js +0 -74
  36. package/src/util/push-state.js +0 -46
  37. package/src/util/query.js +0 -96
  38. package/src/util/resolve-components.js +0 -109
  39. package/src/util/route.js +0 -132
  40. package/src/util/scroll.js +0 -165
  41. package/src/util/state-key.js +0 -22
  42. package/src/util/warn.js +0 -14
  43. package/types/index.d.ts +0 -17
  44. package/types/kdu.d.ts +0 -22
  45. package/types/router.d.ts +0 -170
@@ -1,3005 +0,0 @@
1
- /*!
2
- * kdu-router v3.4.0-beta.0
3
- * (c) 2022 NKDuy
4
- * @license MIT
5
- */
6
- /* */
7
-
8
- function assert (condition, message) {
9
- if (!condition) {
10
- throw new Error(`[kdu-router] ${message}`)
11
- }
12
- }
13
-
14
- function warn (condition, message) {
15
- if ( !condition) {
16
- typeof console !== 'undefined' && console.warn(`[kdu-router] ${message}`);
17
- }
18
- }
19
-
20
- function extend (a, b) {
21
- for (const key in b) {
22
- a[key] = b[key];
23
- }
24
- return a
25
- }
26
-
27
- var View = {
28
- name: 'RouterView',
29
- functional: true,
30
- props: {
31
- name: {
32
- type: String,
33
- default: 'default'
34
- }
35
- },
36
- render (_, { props, children, parent, data }) {
37
- // used by devtools to display a router-view badge
38
- data.routerView = true;
39
-
40
- // directly use parent context's createElement() function
41
- // so that components rendered by router-view can resolve named slots
42
- const h = parent.$createElement;
43
- const name = props.name;
44
- const route = parent.$route;
45
- const cache = parent._routerViewCache || (parent._routerViewCache = {});
46
-
47
- // determine current view depth, also check to see if the tree
48
- // has been toggled inactive but kept-alive.
49
- let depth = 0;
50
- let inactive = false;
51
- while (parent && parent._routerRoot !== parent) {
52
- const knodeData = parent.$knode ? parent.$knode.data : {};
53
- if (knodeData.routerView) {
54
- depth++;
55
- }
56
- if (knodeData.keepAlive && parent._directInactive && parent._inactive) {
57
- inactive = true;
58
- }
59
- parent = parent.$parent;
60
- }
61
- data.routerViewDepth = depth;
62
-
63
- // render previous view if the tree is inactive and kept-alive
64
- if (inactive) {
65
- const cachedData = cache[name];
66
- const cachedComponent = cachedData && cachedData.component;
67
- if (cachedComponent) {
68
- // #2301
69
- // pass props
70
- if (cachedData.configProps) {
71
- fillPropsinData(cachedComponent, data, cachedData.route, cachedData.configProps);
72
- }
73
- return h(cachedComponent, data, children)
74
- } else {
75
- // render previous empty view
76
- return h()
77
- }
78
- }
79
-
80
- const matched = route.matched[depth];
81
- const component = matched && matched.components[name];
82
-
83
- // render empty node if no matched route or no config component
84
- if (!matched || !component) {
85
- cache[name] = null;
86
- return h()
87
- }
88
-
89
- // cache component
90
- cache[name] = { component };
91
-
92
- // attach instance registration hook
93
- // this will be called in the instance's injected lifecycle hooks
94
- data.registerRouteInstance = (vm, val) => {
95
- // val could be undefined for unregistration
96
- const current = matched.instances[name];
97
- if (
98
- (val && current !== vm) ||
99
- (!val && current === vm)
100
- ) {
101
- matched.instances[name] = val;
102
- }
103
- }
104
-
105
- // also register instance in prepatch hook
106
- // in case the same component instance is reused across different routes
107
- ;(data.hook || (data.hook = {})).prepatch = (_, knode) => {
108
- matched.instances[name] = knode.componentInstance;
109
- };
110
-
111
- // register instance in init hook
112
- // in case kept-alive component be actived when routes changed
113
- data.hook.init = (knode) => {
114
- if (knode.data.keepAlive &&
115
- knode.componentInstance &&
116
- knode.componentInstance !== matched.instances[name]
117
- ) {
118
- matched.instances[name] = knode.componentInstance;
119
- }
120
- };
121
-
122
- const configProps = matched.props && matched.props[name];
123
- // save route and configProps in cache
124
- if (configProps) {
125
- extend(cache[name], {
126
- route,
127
- configProps
128
- });
129
- fillPropsinData(component, data, route, configProps);
130
- }
131
-
132
- return h(component, data, children)
133
- }
134
- };
135
-
136
- function fillPropsinData (component, data, route, configProps) {
137
- // resolve props
138
- let propsToPass = data.props = resolveProps(route, configProps);
139
- if (propsToPass) {
140
- // clone to prevent mutation
141
- propsToPass = data.props = extend({}, propsToPass);
142
- // pass non-declared props as attrs
143
- const attrs = data.attrs = data.attrs || {};
144
- for (const key in propsToPass) {
145
- if (!component.props || !(key in component.props)) {
146
- attrs[key] = propsToPass[key];
147
- delete propsToPass[key];
148
- }
149
- }
150
- }
151
- }
152
-
153
- function resolveProps (route, config) {
154
- switch (typeof config) {
155
- case 'undefined':
156
- return
157
- case 'object':
158
- return config
159
- case 'function':
160
- return config(route)
161
- case 'boolean':
162
- return config ? route.params : undefined
163
- default:
164
- {
165
- warn(
166
- false,
167
- `props in "${route.path}" is a ${typeof config}, ` +
168
- `expecting an object, function or boolean.`
169
- );
170
- }
171
- }
172
- }
173
-
174
- /* */
175
-
176
- const encodeReserveRE = /[!'()*]/g;
177
- const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16);
178
- const commaRE = /%2C/g;
179
-
180
- // fixed encodeURIComponent which is more conformant to RFC3986:
181
- // - escapes [!'()*]
182
- // - preserve commas
183
- const encode = str => encodeURIComponent(str)
184
- .replace(encodeReserveRE, encodeReserveReplacer)
185
- .replace(commaRE, ',');
186
-
187
- const decode = decodeURIComponent;
188
-
189
- function resolveQuery (
190
- query,
191
- extraQuery = {},
192
- _parseQuery
193
- ) {
194
- const parse = _parseQuery || parseQuery;
195
- let parsedQuery;
196
- try {
197
- parsedQuery = parse(query || '');
198
- } catch (e) {
199
- warn(false, e.message);
200
- parsedQuery = {};
201
- }
202
- for (const key in extraQuery) {
203
- const value = extraQuery[key];
204
- parsedQuery[key] = Array.isArray(value) ? value.map(v => '' + v) : '' + value;
205
- }
206
- return parsedQuery
207
- }
208
-
209
- function parseQuery (query) {
210
- const res = {};
211
-
212
- query = query.trim().replace(/^(\?|#|&)/, '');
213
-
214
- if (!query) {
215
- return res
216
- }
217
-
218
- query.split('&').forEach(param => {
219
- const parts = param.replace(/\+/g, ' ').split('=');
220
- const key = decode(parts.shift());
221
- const val = parts.length > 0
222
- ? decode(parts.join('='))
223
- : null;
224
-
225
- if (res[key] === undefined) {
226
- res[key] = val;
227
- } else if (Array.isArray(res[key])) {
228
- res[key].push(val);
229
- } else {
230
- res[key] = [res[key], val];
231
- }
232
- });
233
-
234
- return res
235
- }
236
-
237
- function stringifyQuery (obj) {
238
- const res = obj ? Object.keys(obj).map(key => {
239
- const val = obj[key];
240
-
241
- if (val === undefined) {
242
- return ''
243
- }
244
-
245
- if (val === null) {
246
- return encode(key)
247
- }
248
-
249
- if (Array.isArray(val)) {
250
- const result = [];
251
- val.forEach(val2 => {
252
- if (val2 === undefined) {
253
- return
254
- }
255
- if (val2 === null) {
256
- result.push(encode(key));
257
- } else {
258
- result.push(encode(key) + '=' + encode(val2));
259
- }
260
- });
261
- return result.join('&')
262
- }
263
-
264
- return encode(key) + '=' + encode(val)
265
- }).filter(x => x.length > 0).join('&') : null;
266
- return res ? `?${res}` : ''
267
- }
268
-
269
- /* */
270
-
271
- const trailingSlashRE = /\/?$/;
272
-
273
- function createRoute (
274
- record,
275
- location,
276
- redirectedFrom,
277
- router
278
- ) {
279
- const stringifyQuery = router && router.options.stringifyQuery;
280
-
281
- let query = location.query || {};
282
- try {
283
- query = clone(query);
284
- } catch (e) {}
285
-
286
- const route = {
287
- name: location.name || (record && record.name),
288
- meta: (record && record.meta) || {},
289
- path: location.path || '/',
290
- hash: location.hash || '',
291
- query,
292
- params: location.params || {},
293
- fullPath: getFullPath(location, stringifyQuery),
294
- matched: record ? formatMatch(record) : []
295
- };
296
- if (redirectedFrom) {
297
- route.redirectedFrom = getFullPath(redirectedFrom, stringifyQuery);
298
- }
299
- return Object.freeze(route)
300
- }
301
-
302
- function clone (value) {
303
- if (Array.isArray(value)) {
304
- return value.map(clone)
305
- } else if (value && typeof value === 'object') {
306
- const res = {};
307
- for (const key in value) {
308
- res[key] = clone(value[key]);
309
- }
310
- return res
311
- } else {
312
- return value
313
- }
314
- }
315
-
316
- // the starting route that represents the initial state
317
- const START = createRoute(null, {
318
- path: '/'
319
- });
320
-
321
- function formatMatch (record) {
322
- const res = [];
323
- while (record) {
324
- res.unshift(record);
325
- record = record.parent;
326
- }
327
- return res
328
- }
329
-
330
- function getFullPath (
331
- { path, query = {}, hash = '' },
332
- _stringifyQuery
333
- ) {
334
- const stringify = _stringifyQuery || stringifyQuery;
335
- return (path || '/') + stringify(query) + hash
336
- }
337
-
338
- function isSameRoute (a, b) {
339
- if (b === START) {
340
- return a === b
341
- } else if (!b) {
342
- return false
343
- } else if (a.path && b.path) {
344
- return (
345
- a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') &&
346
- a.hash === b.hash &&
347
- isObjectEqual(a.query, b.query)
348
- )
349
- } else if (a.name && b.name) {
350
- return (
351
- a.name === b.name &&
352
- a.hash === b.hash &&
353
- isObjectEqual(a.query, b.query) &&
354
- isObjectEqual(a.params, b.params)
355
- )
356
- } else {
357
- return false
358
- }
359
- }
360
-
361
- function isObjectEqual (a = {}, b = {}) {
362
- // handle null value #1566
363
- if (!a || !b) return a === b
364
- const aKeys = Object.keys(a);
365
- const bKeys = Object.keys(b);
366
- if (aKeys.length !== bKeys.length) {
367
- return false
368
- }
369
- return aKeys.every(key => {
370
- const aVal = a[key];
371
- const bVal = b[key];
372
- // check nested equality
373
- if (typeof aVal === 'object' && typeof bVal === 'object') {
374
- return isObjectEqual(aVal, bVal)
375
- }
376
- return String(aVal) === String(bVal)
377
- })
378
- }
379
-
380
- function isIncludedRoute (current, target) {
381
- return (
382
- current.path.replace(trailingSlashRE, '/').indexOf(
383
- target.path.replace(trailingSlashRE, '/')
384
- ) === 0 &&
385
- (!target.hash || current.hash === target.hash) &&
386
- queryIncludes(current.query, target.query)
387
- )
388
- }
389
-
390
- function queryIncludes (current, target) {
391
- for (const key in target) {
392
- if (!(key in current)) {
393
- return false
394
- }
395
- }
396
- return true
397
- }
398
-
399
- /* */
400
-
401
- function resolvePath (
402
- relative,
403
- base,
404
- append
405
- ) {
406
- const firstChar = relative.charAt(0);
407
- if (firstChar === '/') {
408
- return relative
409
- }
410
-
411
- if (firstChar === '?' || firstChar === '#') {
412
- return base + relative
413
- }
414
-
415
- const stack = base.split('/');
416
-
417
- // remove trailing segment if:
418
- // - not appending
419
- // - appending to trailing slash (last segment is empty)
420
- if (!append || !stack[stack.length - 1]) {
421
- stack.pop();
422
- }
423
-
424
- // resolve relative path
425
- const segments = relative.replace(/^\//, '').split('/');
426
- for (let i = 0; i < segments.length; i++) {
427
- const segment = segments[i];
428
- if (segment === '..') {
429
- stack.pop();
430
- } else if (segment !== '.') {
431
- stack.push(segment);
432
- }
433
- }
434
-
435
- // ensure leading slash
436
- if (stack[0] !== '') {
437
- stack.unshift('');
438
- }
439
-
440
- return stack.join('/')
441
- }
442
-
443
- function parsePath (path) {
444
- let hash = '';
445
- let query = '';
446
-
447
- const hashIndex = path.indexOf('#');
448
- if (hashIndex >= 0) {
449
- hash = path.slice(hashIndex);
450
- path = path.slice(0, hashIndex);
451
- }
452
-
453
- const queryIndex = path.indexOf('?');
454
- if (queryIndex >= 0) {
455
- query = path.slice(queryIndex + 1);
456
- path = path.slice(0, queryIndex);
457
- }
458
-
459
- return {
460
- path,
461
- query,
462
- hash
463
- }
464
- }
465
-
466
- function cleanPath (path) {
467
- return path.replace(/\/\//g, '/')
468
- }
469
-
470
- var isarray = Array.isArray || function (arr) {
471
- return Object.prototype.toString.call(arr) == '[object Array]';
472
- };
473
-
474
- /**
475
- * Expose `pathToRegexp`.
476
- */
477
- var pathToRegexp_1 = pathToRegexp;
478
- var parse_1 = parse;
479
- var compile_1 = compile;
480
- var tokensToFunction_1 = tokensToFunction;
481
- var tokensToRegExp_1 = tokensToRegExp;
482
-
483
- /**
484
- * The main path matching regexp utility.
485
- *
486
- * @type {RegExp}
487
- */
488
- var PATH_REGEXP = new RegExp([
489
- // Match escaped characters that would otherwise appear in future matches.
490
- // This allows the user to escape special characters that won't transform.
491
- '(\\\\.)',
492
- // Match Express-style parameters and un-named parameters with a prefix
493
- // and optional suffixes. Matches appear as:
494
- //
495
- // "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined]
496
- // "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined]
497
- // "/*" => ["/", undefined, undefined, undefined, undefined, "*"]
498
- '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
499
- ].join('|'), 'g');
500
-
501
- /**
502
- * Parse a string for the raw tokens.
503
- *
504
- * @param {string} str
505
- * @param {Object=} options
506
- * @return {!Array}
507
- */
508
- function parse (str, options) {
509
- var tokens = [];
510
- var key = 0;
511
- var index = 0;
512
- var path = '';
513
- var defaultDelimiter = options && options.delimiter || '/';
514
- var res;
515
-
516
- while ((res = PATH_REGEXP.exec(str)) != null) {
517
- var m = res[0];
518
- var escaped = res[1];
519
- var offset = res.index;
520
- path += str.slice(index, offset);
521
- index = offset + m.length;
522
-
523
- // Ignore already escaped sequences.
524
- if (escaped) {
525
- path += escaped[1];
526
- continue
527
- }
528
-
529
- var next = str[index];
530
- var prefix = res[2];
531
- var name = res[3];
532
- var capture = res[4];
533
- var group = res[5];
534
- var modifier = res[6];
535
- var asterisk = res[7];
536
-
537
- // Push the current path onto the tokens.
538
- if (path) {
539
- tokens.push(path);
540
- path = '';
541
- }
542
-
543
- var partial = prefix != null && next != null && next !== prefix;
544
- var repeat = modifier === '+' || modifier === '*';
545
- var optional = modifier === '?' || modifier === '*';
546
- var delimiter = res[2] || defaultDelimiter;
547
- var pattern = capture || group;
548
-
549
- tokens.push({
550
- name: name || key++,
551
- prefix: prefix || '',
552
- delimiter: delimiter,
553
- optional: optional,
554
- repeat: repeat,
555
- partial: partial,
556
- asterisk: !!asterisk,
557
- pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
558
- });
559
- }
560
-
561
- // Match any characters still remaining.
562
- if (index < str.length) {
563
- path += str.substr(index);
564
- }
565
-
566
- // If the path exists, push it onto the end.
567
- if (path) {
568
- tokens.push(path);
569
- }
570
-
571
- return tokens
572
- }
573
-
574
- /**
575
- * Compile a string to a template function for the path.
576
- *
577
- * @param {string} str
578
- * @param {Object=} options
579
- * @return {!function(Object=, Object=)}
580
- */
581
- function compile (str, options) {
582
- return tokensToFunction(parse(str, options), options)
583
- }
584
-
585
- /**
586
- * Prettier encoding of URI path segments.
587
- *
588
- * @param {string}
589
- * @return {string}
590
- */
591
- function encodeURIComponentPretty (str) {
592
- return encodeURI(str).replace(/[\/?#]/g, function (c) {
593
- return '%' + c.charCodeAt(0).toString(16).toUpperCase()
594
- })
595
- }
596
-
597
- /**
598
- * Encode the asterisk parameter. Similar to `pretty`, but allows slashes.
599
- *
600
- * @param {string}
601
- * @return {string}
602
- */
603
- function encodeAsterisk (str) {
604
- return encodeURI(str).replace(/[?#]/g, function (c) {
605
- return '%' + c.charCodeAt(0).toString(16).toUpperCase()
606
- })
607
- }
608
-
609
- /**
610
- * Expose a method for transforming tokens into the path function.
611
- */
612
- function tokensToFunction (tokens, options) {
613
- // Compile all the tokens into regexps.
614
- var matches = new Array(tokens.length);
615
-
616
- // Compile all the patterns before compilation.
617
- for (var i = 0; i < tokens.length; i++) {
618
- if (typeof tokens[i] === 'object') {
619
- matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options));
620
- }
621
- }
622
-
623
- return function (obj, opts) {
624
- var path = '';
625
- var data = obj || {};
626
- var options = opts || {};
627
- var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent;
628
-
629
- for (var i = 0; i < tokens.length; i++) {
630
- var token = tokens[i];
631
-
632
- if (typeof token === 'string') {
633
- path += token;
634
-
635
- continue
636
- }
637
-
638
- var value = data[token.name];
639
- var segment;
640
-
641
- if (value == null) {
642
- if (token.optional) {
643
- // Prepend partial segment prefixes.
644
- if (token.partial) {
645
- path += token.prefix;
646
- }
647
-
648
- continue
649
- } else {
650
- throw new TypeError('Expected "' + token.name + '" to be defined')
651
- }
652
- }
653
-
654
- if (isarray(value)) {
655
- if (!token.repeat) {
656
- throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
657
- }
658
-
659
- if (value.length === 0) {
660
- if (token.optional) {
661
- continue
662
- } else {
663
- throw new TypeError('Expected "' + token.name + '" to not be empty')
664
- }
665
- }
666
-
667
- for (var j = 0; j < value.length; j++) {
668
- segment = encode(value[j]);
669
-
670
- if (!matches[i].test(segment)) {
671
- throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
672
- }
673
-
674
- path += (j === 0 ? token.prefix : token.delimiter) + segment;
675
- }
676
-
677
- continue
678
- }
679
-
680
- segment = token.asterisk ? encodeAsterisk(value) : encode(value);
681
-
682
- if (!matches[i].test(segment)) {
683
- throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
684
- }
685
-
686
- path += token.prefix + segment;
687
- }
688
-
689
- return path
690
- }
691
- }
692
-
693
- /**
694
- * Escape a regular expression string.
695
- *
696
- * @param {string} str
697
- * @return {string}
698
- */
699
- function escapeString (str) {
700
- return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1')
701
- }
702
-
703
- /**
704
- * Escape the capturing group by escaping special characters and meaning.
705
- *
706
- * @param {string} group
707
- * @return {string}
708
- */
709
- function escapeGroup (group) {
710
- return group.replace(/([=!:$\/()])/g, '\\$1')
711
- }
712
-
713
- /**
714
- * Attach the keys as a property of the regexp.
715
- *
716
- * @param {!RegExp} re
717
- * @param {Array} keys
718
- * @return {!RegExp}
719
- */
720
- function attachKeys (re, keys) {
721
- re.keys = keys;
722
- return re
723
- }
724
-
725
- /**
726
- * Get the flags for a regexp from the options.
727
- *
728
- * @param {Object} options
729
- * @return {string}
730
- */
731
- function flags (options) {
732
- return options && options.sensitive ? '' : 'i'
733
- }
734
-
735
- /**
736
- * Pull out keys from a regexp.
737
- *
738
- * @param {!RegExp} path
739
- * @param {!Array} keys
740
- * @return {!RegExp}
741
- */
742
- function regexpToRegexp (path, keys) {
743
- // Use a negative lookahead to match only capturing groups.
744
- var groups = path.source.match(/\((?!\?)/g);
745
-
746
- if (groups) {
747
- for (var i = 0; i < groups.length; i++) {
748
- keys.push({
749
- name: i,
750
- prefix: null,
751
- delimiter: null,
752
- optional: false,
753
- repeat: false,
754
- partial: false,
755
- asterisk: false,
756
- pattern: null
757
- });
758
- }
759
- }
760
-
761
- return attachKeys(path, keys)
762
- }
763
-
764
- /**
765
- * Transform an array into a regexp.
766
- *
767
- * @param {!Array} path
768
- * @param {Array} keys
769
- * @param {!Object} options
770
- * @return {!RegExp}
771
- */
772
- function arrayToRegexp (path, keys, options) {
773
- var parts = [];
774
-
775
- for (var i = 0; i < path.length; i++) {
776
- parts.push(pathToRegexp(path[i], keys, options).source);
777
- }
778
-
779
- var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options));
780
-
781
- return attachKeys(regexp, keys)
782
- }
783
-
784
- /**
785
- * Create a path regexp from string input.
786
- *
787
- * @param {string} path
788
- * @param {!Array} keys
789
- * @param {!Object} options
790
- * @return {!RegExp}
791
- */
792
- function stringToRegexp (path, keys, options) {
793
- return tokensToRegExp(parse(path, options), keys, options)
794
- }
795
-
796
- /**
797
- * Expose a function for taking tokens and returning a RegExp.
798
- *
799
- * @param {!Array} tokens
800
- * @param {(Array|Object)=} keys
801
- * @param {Object=} options
802
- * @return {!RegExp}
803
- */
804
- function tokensToRegExp (tokens, keys, options) {
805
- if (!isarray(keys)) {
806
- options = /** @type {!Object} */ (keys || options);
807
- keys = [];
808
- }
809
-
810
- options = options || {};
811
-
812
- var strict = options.strict;
813
- var end = options.end !== false;
814
- var route = '';
815
-
816
- // Iterate over the tokens and create our regexp string.
817
- for (var i = 0; i < tokens.length; i++) {
818
- var token = tokens[i];
819
-
820
- if (typeof token === 'string') {
821
- route += escapeString(token);
822
- } else {
823
- var prefix = escapeString(token.prefix);
824
- var capture = '(?:' + token.pattern + ')';
825
-
826
- keys.push(token);
827
-
828
- if (token.repeat) {
829
- capture += '(?:' + prefix + capture + ')*';
830
- }
831
-
832
- if (token.optional) {
833
- if (!token.partial) {
834
- capture = '(?:' + prefix + '(' + capture + '))?';
835
- } else {
836
- capture = prefix + '(' + capture + ')?';
837
- }
838
- } else {
839
- capture = prefix + '(' + capture + ')';
840
- }
841
-
842
- route += capture;
843
- }
844
- }
845
-
846
- var delimiter = escapeString(options.delimiter || '/');
847
- var endsWithDelimiter = route.slice(-delimiter.length) === delimiter;
848
-
849
- // In non-strict mode we allow a slash at the end of match. If the path to
850
- // match already ends with a slash, we remove it for consistency. The slash
851
- // is valid at the end of a path match, not in the middle. This is important
852
- // in non-ending mode, where "/test/" shouldn't match "/test//route".
853
- if (!strict) {
854
- route = (endsWithDelimiter ? route.slice(0, -delimiter.length) : route) + '(?:' + delimiter + '(?=$))?';
855
- }
856
-
857
- if (end) {
858
- route += '$';
859
- } else {
860
- // In non-ending mode, we need the capturing groups to match as much as
861
- // possible by using a positive lookahead to the end or next path segment.
862
- route += strict && endsWithDelimiter ? '' : '(?=' + delimiter + '|$)';
863
- }
864
-
865
- return attachKeys(new RegExp('^' + route, flags(options)), keys)
866
- }
867
-
868
- /**
869
- * Normalize the given path string, returning a regular expression.
870
- *
871
- * An empty array can be passed in for the keys, which will hold the
872
- * placeholder key descriptions. For example, using `/user/:id`, `keys` will
873
- * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
874
- *
875
- * @param {(string|RegExp|Array)} path
876
- * @param {(Array|Object)=} keys
877
- * @param {Object=} options
878
- * @return {!RegExp}
879
- */
880
- function pathToRegexp (path, keys, options) {
881
- if (!isarray(keys)) {
882
- options = /** @type {!Object} */ (keys || options);
883
- keys = [];
884
- }
885
-
886
- options = options || {};
887
-
888
- if (path instanceof RegExp) {
889
- return regexpToRegexp(path, /** @type {!Array} */ (keys))
890
- }
891
-
892
- if (isarray(path)) {
893
- return arrayToRegexp(/** @type {!Array} */ (path), /** @type {!Array} */ (keys), options)
894
- }
895
-
896
- return stringToRegexp(/** @type {string} */ (path), /** @type {!Array} */ (keys), options)
897
- }
898
- pathToRegexp_1.parse = parse_1;
899
- pathToRegexp_1.compile = compile_1;
900
- pathToRegexp_1.tokensToFunction = tokensToFunction_1;
901
- pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
902
-
903
- /* */
904
-
905
- // $flow-disable-line
906
- const regexpCompileCache = Object.create(null);
907
-
908
- function fillParams (
909
- path,
910
- params,
911
- routeMsg
912
- ) {
913
- params = params || {};
914
- try {
915
- const filler =
916
- regexpCompileCache[path] ||
917
- (regexpCompileCache[path] = pathToRegexp_1.compile(path));
918
-
919
- // Fix #2505 resolving asterisk routes { name: 'not-found', params: { pathMatch: '/not-found' }}
920
- // and fix #3106 so that you can work with location descriptor object having params.pathMatch equal to empty string
921
- if (typeof params.pathMatch === 'string') params[0] = params.pathMatch;
922
-
923
- return filler(params, { pretty: true })
924
- } catch (e) {
925
- {
926
- // Fix #3072 no warn if `pathMatch` is string
927
- warn(typeof params.pathMatch === 'string', `missing param for ${routeMsg}: ${e.message}`);
928
- }
929
- return ''
930
- } finally {
931
- // delete the 0 if it was added
932
- delete params[0];
933
- }
934
- }
935
-
936
- /* */
937
-
938
- function normalizeLocation (
939
- raw,
940
- current,
941
- append,
942
- router
943
- ) {
944
- let next = typeof raw === 'string' ? { path: raw } : raw;
945
- // named target
946
- if (next._normalized) {
947
- return next
948
- } else if (next.name) {
949
- next = extend({}, raw);
950
- const params = next.params;
951
- if (params && typeof params === 'object') {
952
- next.params = extend({}, params);
953
- }
954
- return next
955
- }
956
-
957
- // relative params
958
- if (!next.path && next.params && current) {
959
- next = extend({}, next);
960
- next._normalized = true;
961
- const params = extend(extend({}, current.params), next.params);
962
- if (current.name) {
963
- next.name = current.name;
964
- next.params = params;
965
- } else if (current.matched.length) {
966
- const rawPath = current.matched[current.matched.length - 1].path;
967
- next.path = fillParams(rawPath, params, `path ${current.path}`);
968
- } else {
969
- warn(false, `relative params navigation requires a current route.`);
970
- }
971
- return next
972
- }
973
-
974
- const parsedPath = parsePath(next.path || '');
975
- const basePath = (current && current.path) || '/';
976
- const path = parsedPath.path
977
- ? resolvePath(parsedPath.path, basePath, append || next.append)
978
- : basePath;
979
-
980
- const query = resolveQuery(
981
- parsedPath.query,
982
- next.query,
983
- router && router.options.parseQuery
984
- );
985
-
986
- let hash = next.hash || parsedPath.hash;
987
- if (hash && hash.charAt(0) !== '#') {
988
- hash = `#${hash}`;
989
- }
990
-
991
- return {
992
- _normalized: true,
993
- path,
994
- query,
995
- hash
996
- }
997
- }
998
-
999
- /* */
1000
-
1001
- // work around weird flow bug
1002
- const toTypes = [String, Object];
1003
- const eventTypes = [String, Array];
1004
-
1005
- const noop = () => {};
1006
-
1007
- var Link = {
1008
- name: 'RouterLink',
1009
- props: {
1010
- to: {
1011
- type: toTypes,
1012
- required: true
1013
- },
1014
- tag: {
1015
- type: String,
1016
- default: 'a'
1017
- },
1018
- exact: Boolean,
1019
- append: Boolean,
1020
- replace: Boolean,
1021
- activeClass: String,
1022
- exactActiveClass: String,
1023
- ariaCurrentValue: {
1024
- type: String,
1025
- default: 'page'
1026
- },
1027
- event: {
1028
- type: eventTypes,
1029
- default: 'click'
1030
- }
1031
- },
1032
- render (h) {
1033
- const router = this.$router;
1034
- const current = this.$route;
1035
- const { location, route, href } = router.resolve(
1036
- this.to,
1037
- current,
1038
- this.append
1039
- );
1040
-
1041
- const classes = {};
1042
- const globalActiveClass = router.options.linkActiveClass;
1043
- const globalExactActiveClass = router.options.linkExactActiveClass;
1044
- // Support global empty active class
1045
- const activeClassFallback =
1046
- globalActiveClass == null ? 'router-link-active' : globalActiveClass;
1047
- const exactActiveClassFallback =
1048
- globalExactActiveClass == null
1049
- ? 'router-link-exact-active'
1050
- : globalExactActiveClass;
1051
- const activeClass =
1052
- this.activeClass == null ? activeClassFallback : this.activeClass;
1053
- const exactActiveClass =
1054
- this.exactActiveClass == null
1055
- ? exactActiveClassFallback
1056
- : this.exactActiveClass;
1057
-
1058
- const compareTarget = route.redirectedFrom
1059
- ? createRoute(null, normalizeLocation(route.redirectedFrom), null, router)
1060
- : route;
1061
-
1062
- classes[exactActiveClass] = isSameRoute(current, compareTarget);
1063
- classes[activeClass] = this.exact
1064
- ? classes[exactActiveClass]
1065
- : isIncludedRoute(current, compareTarget);
1066
-
1067
- const ariaCurrentValue = classes[exactActiveClass] ? this.ariaCurrentValue : null;
1068
-
1069
- const handler = e => {
1070
- if (guardEvent(e)) {
1071
- if (this.replace) {
1072
- router.replace(location, noop);
1073
- } else {
1074
- router.push(location, noop);
1075
- }
1076
- }
1077
- };
1078
-
1079
- const on = { click: guardEvent };
1080
- if (Array.isArray(this.event)) {
1081
- this.event.forEach(e => {
1082
- on[e] = handler;
1083
- });
1084
- } else {
1085
- on[this.event] = handler;
1086
- }
1087
-
1088
- const data = { class: classes };
1089
-
1090
- const scopedSlot =
1091
- !this.$scopedSlots.$hasNormal &&
1092
- this.$scopedSlots.default &&
1093
- this.$scopedSlots.default({
1094
- href,
1095
- route,
1096
- navigate: handler,
1097
- isActive: classes[activeClass],
1098
- isExactActive: classes[exactActiveClass]
1099
- });
1100
-
1101
- if (scopedSlot) {
1102
- if (scopedSlot.length === 1) {
1103
- return scopedSlot[0]
1104
- } else if (scopedSlot.length > 1 || !scopedSlot.length) {
1105
- {
1106
- warn(
1107
- false,
1108
- `RouterLink with to="${
1109
- this.to
1110
- }" is trying to use a scoped slot but it didn't provide exactly one child. Wrapping the content with a span element.`
1111
- );
1112
- }
1113
- return scopedSlot.length === 0 ? h() : h('span', {}, scopedSlot)
1114
- }
1115
- }
1116
-
1117
- if (this.tag === 'a') {
1118
- data.on = on;
1119
- data.attrs = { href, 'aria-current': ariaCurrentValue };
1120
- } else {
1121
- // find the first <a> child and apply listener and href
1122
- const a = findAnchor(this.$slots.default);
1123
- if (a) {
1124
- // in case the <a> is a static node
1125
- a.isStatic = false;
1126
- const aData = (a.data = extend({}, a.data));
1127
- aData.on = aData.on || {};
1128
- // transform existing events in both objects into arrays so we can push later
1129
- for (const event in aData.on) {
1130
- const handler = aData.on[event];
1131
- if (event in on) {
1132
- aData.on[event] = Array.isArray(handler) ? handler : [handler];
1133
- }
1134
- }
1135
- // append new listeners for router-link
1136
- for (const event in on) {
1137
- if (event in aData.on) {
1138
- // on[event] is always a function
1139
- aData.on[event].push(on[event]);
1140
- } else {
1141
- aData.on[event] = handler;
1142
- }
1143
- }
1144
-
1145
- const aAttrs = (a.data.attrs = extend({}, a.data.attrs));
1146
- aAttrs.href = href;
1147
- aAttrs['aria-current'] = ariaCurrentValue;
1148
- } else {
1149
- // doesn't have <a> child, apply listener to self
1150
- data.on = on;
1151
- }
1152
- }
1153
-
1154
- return h(this.tag, data, this.$slots.default)
1155
- }
1156
- };
1157
-
1158
- function guardEvent (e) {
1159
- // don't redirect with control keys
1160
- if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return
1161
- // don't redirect when preventDefault called
1162
- if (e.defaultPrevented) return
1163
- // don't redirect on right click
1164
- if (e.button !== undefined && e.button !== 0) return
1165
- // don't redirect if `target="_blank"`
1166
- if (e.currentTarget && e.currentTarget.getAttribute) {
1167
- const target = e.currentTarget.getAttribute('target');
1168
- if (/\b_blank\b/i.test(target)) return
1169
- }
1170
- // this may be a Weex event which doesn't have this method
1171
- if (e.preventDefault) {
1172
- e.preventDefault();
1173
- }
1174
- return true
1175
- }
1176
-
1177
- function findAnchor (children) {
1178
- if (children) {
1179
- let child;
1180
- for (let i = 0; i < children.length; i++) {
1181
- child = children[i];
1182
- if (child.tag === 'a') {
1183
- return child
1184
- }
1185
- if (child.children && (child = findAnchor(child.children))) {
1186
- return child
1187
- }
1188
- }
1189
- }
1190
- }
1191
-
1192
- let _Kdu;
1193
-
1194
- function install (Kdu) {
1195
- if (install.installed && _Kdu === Kdu) return
1196
- install.installed = true;
1197
-
1198
- _Kdu = Kdu;
1199
-
1200
- const isDef = v => v !== undefined;
1201
-
1202
- const registerInstance = (vm, callVal) => {
1203
- let i = vm.$options._parentKnode;
1204
- if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
1205
- i(vm, callVal);
1206
- }
1207
- };
1208
-
1209
- Kdu.mixin({
1210
- beforeCreate () {
1211
- if (isDef(this.$options.router)) {
1212
- this._routerRoot = this;
1213
- this._router = this.$options.router;
1214
- this._router.init(this);
1215
- Kdu.util.defineReactive(this, '_route', this._router.history.current);
1216
- } else {
1217
- this._routerRoot = (this.$parent && this.$parent._routerRoot) || this;
1218
- }
1219
- registerInstance(this, this);
1220
- },
1221
- destroyed () {
1222
- registerInstance(this);
1223
- }
1224
- });
1225
-
1226
- Object.defineProperty(Kdu.prototype, '$router', {
1227
- get () { return this._routerRoot._router }
1228
- });
1229
-
1230
- Object.defineProperty(Kdu.prototype, '$route', {
1231
- get () { return this._routerRoot._route }
1232
- });
1233
-
1234
- Kdu.component('RouterView', View);
1235
- Kdu.component('RouterLink', Link);
1236
-
1237
- const strats = Kdu.config.optionMergeStrategies;
1238
- // use the same hook merging strategy for route hooks
1239
- strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created;
1240
- }
1241
-
1242
- /* */
1243
-
1244
- const inBrowser = typeof window !== 'undefined';
1245
-
1246
- /* */
1247
-
1248
- function createRouteMap (
1249
- routes,
1250
- oldPathList,
1251
- oldPathMap,
1252
- oldNameMap
1253
- ) {
1254
- // the path list is used to control path matching priority
1255
- const pathList = oldPathList || [];
1256
- // $flow-disable-line
1257
- const pathMap = oldPathMap || Object.create(null);
1258
- // $flow-disable-line
1259
- const nameMap = oldNameMap || Object.create(null);
1260
-
1261
- routes.forEach(route => {
1262
- addRouteRecord(pathList, pathMap, nameMap, route);
1263
- });
1264
-
1265
- // ensure wildcard routes are always at the end
1266
- for (let i = 0, l = pathList.length; i < l; i++) {
1267
- if (pathList[i] === '*') {
1268
- pathList.push(pathList.splice(i, 1)[0]);
1269
- l--;
1270
- i--;
1271
- }
1272
- }
1273
-
1274
- {
1275
- // warn if routes do not include leading slashes
1276
- const found = pathList
1277
- // check for missing leading slash
1278
- .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/');
1279
-
1280
- if (found.length > 0) {
1281
- const pathNames = found.map(path => `- ${path}`).join('\n');
1282
- warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`);
1283
- }
1284
- }
1285
-
1286
- return {
1287
- pathList,
1288
- pathMap,
1289
- nameMap
1290
- }
1291
- }
1292
-
1293
- function addRouteRecord (
1294
- pathList,
1295
- pathMap,
1296
- nameMap,
1297
- route,
1298
- parent,
1299
- matchAs
1300
- ) {
1301
- const { path, name } = route;
1302
- {
1303
- assert(path != null, `"path" is required in a route configuration.`);
1304
- assert(
1305
- typeof route.component !== 'string',
1306
- `route config "component" for path: ${String(
1307
- path || name
1308
- )} cannot be a ` + `string id. Use an actual component instead.`
1309
- );
1310
- }
1311
-
1312
- const pathToRegexpOptions =
1313
- route.pathToRegexpOptions || {};
1314
- const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict);
1315
-
1316
- if (typeof route.caseSensitive === 'boolean') {
1317
- pathToRegexpOptions.sensitive = route.caseSensitive;
1318
- }
1319
-
1320
- const record = {
1321
- path: normalizedPath,
1322
- regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
1323
- components: route.components || { default: route.component },
1324
- instances: {},
1325
- name,
1326
- parent,
1327
- matchAs,
1328
- redirect: route.redirect,
1329
- beforeEnter: route.beforeEnter,
1330
- meta: route.meta || {},
1331
- props:
1332
- route.props == null
1333
- ? {}
1334
- : route.components
1335
- ? route.props
1336
- : { default: route.props }
1337
- };
1338
-
1339
- if (route.children) {
1340
- // Warn if route is named, does not redirect and has a default child route.
1341
- // If users navigate to this route by name, the default child will
1342
- // not be rendered (GH Issue #629)
1343
- {
1344
- if (
1345
- route.name &&
1346
- !route.redirect &&
1347
- route.children.some(child => /^\/?$/.test(child.path))
1348
- ) {
1349
- warn(
1350
- false,
1351
- `Named Route '${route.name}' has a default child route. ` +
1352
- `When navigating to this named route (:to="{name: '${
1353
- route.name
1354
- }'"), ` +
1355
- `the default child route will not be rendered. Remove the name from ` +
1356
- `this route and use the name of the default child route for named ` +
1357
- `links instead.`
1358
- );
1359
- }
1360
- }
1361
- route.children.forEach(child => {
1362
- const childMatchAs = matchAs
1363
- ? cleanPath(`${matchAs}/${child.path}`)
1364
- : undefined;
1365
- addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs);
1366
- });
1367
- }
1368
-
1369
- if (!pathMap[record.path]) {
1370
- pathList.push(record.path);
1371
- pathMap[record.path] = record;
1372
- }
1373
-
1374
- if (route.alias !== undefined) {
1375
- const aliases = Array.isArray(route.alias) ? route.alias : [route.alias];
1376
- for (let i = 0; i < aliases.length; ++i) {
1377
- const alias = aliases[i];
1378
- if ( alias === path) {
1379
- warn(
1380
- false,
1381
- `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
1382
- );
1383
- // skip in dev to make it work
1384
- continue
1385
- }
1386
-
1387
- const aliasRoute = {
1388
- path: alias,
1389
- children: route.children
1390
- };
1391
- addRouteRecord(
1392
- pathList,
1393
- pathMap,
1394
- nameMap,
1395
- aliasRoute,
1396
- parent,
1397
- record.path || '/' // matchAs
1398
- );
1399
- }
1400
- }
1401
-
1402
- if (name) {
1403
- if (!nameMap[name]) {
1404
- nameMap[name] = record;
1405
- } else if ( !matchAs) {
1406
- warn(
1407
- false,
1408
- `Duplicate named routes definition: ` +
1409
- `{ name: "${name}", path: "${record.path}" }`
1410
- );
1411
- }
1412
- }
1413
- }
1414
-
1415
- function compileRouteRegex (
1416
- path,
1417
- pathToRegexpOptions
1418
- ) {
1419
- const regex = pathToRegexp_1(path, [], pathToRegexpOptions);
1420
- {
1421
- const keys = Object.create(null);
1422
- regex.keys.forEach(key => {
1423
- warn(
1424
- !keys[key.name],
1425
- `Duplicate param keys in route with path: "${path}"`
1426
- );
1427
- keys[key.name] = true;
1428
- });
1429
- }
1430
- return regex
1431
- }
1432
-
1433
- function normalizePath (
1434
- path,
1435
- parent,
1436
- strict
1437
- ) {
1438
- if (!strict) path = path.replace(/\/$/, '');
1439
- if (path[0] === '/') return path
1440
- if (parent == null) return path
1441
- return cleanPath(`${parent.path}/${path}`)
1442
- }
1443
-
1444
- /* */
1445
-
1446
-
1447
-
1448
- function createMatcher (
1449
- routes,
1450
- router
1451
- ) {
1452
- const { pathList, pathMap, nameMap } = createRouteMap(routes);
1453
-
1454
- function addRoutes (routes) {
1455
- createRouteMap(routes, pathList, pathMap, nameMap);
1456
- }
1457
-
1458
- function match (
1459
- raw,
1460
- currentRoute,
1461
- redirectedFrom
1462
- ) {
1463
- const location = normalizeLocation(raw, currentRoute, false, router);
1464
- const { name } = location;
1465
-
1466
- if (name) {
1467
- const record = nameMap[name];
1468
- {
1469
- warn(record, `Route with name '${name}' does not exist`);
1470
- }
1471
- if (!record) return _createRoute(null, location)
1472
- const paramNames = record.regex.keys
1473
- .filter(key => !key.optional)
1474
- .map(key => key.name);
1475
-
1476
- if (typeof location.params !== 'object') {
1477
- location.params = {};
1478
- }
1479
-
1480
- if (currentRoute && typeof currentRoute.params === 'object') {
1481
- for (const key in currentRoute.params) {
1482
- if (!(key in location.params) && paramNames.indexOf(key) > -1) {
1483
- location.params[key] = currentRoute.params[key];
1484
- }
1485
- }
1486
- }
1487
-
1488
- location.path = fillParams(record.path, location.params, `named route "${name}"`);
1489
- return _createRoute(record, location, redirectedFrom)
1490
- } else if (location.path) {
1491
- location.params = {};
1492
- for (let i = 0; i < pathList.length; i++) {
1493
- const path = pathList[i];
1494
- const record = pathMap[path];
1495
- if (matchRoute(record.regex, location.path, location.params)) {
1496
- return _createRoute(record, location, redirectedFrom)
1497
- }
1498
- }
1499
- }
1500
- // no match
1501
- return _createRoute(null, location)
1502
- }
1503
-
1504
- function redirect (
1505
- record,
1506
- location
1507
- ) {
1508
- const originalRedirect = record.redirect;
1509
- let redirect = typeof originalRedirect === 'function'
1510
- ? originalRedirect(createRoute(record, location, null, router))
1511
- : originalRedirect;
1512
-
1513
- if (typeof redirect === 'string') {
1514
- redirect = { path: redirect };
1515
- }
1516
-
1517
- if (!redirect || typeof redirect !== 'object') {
1518
- {
1519
- warn(
1520
- false, `invalid redirect option: ${JSON.stringify(redirect)}`
1521
- );
1522
- }
1523
- return _createRoute(null, location)
1524
- }
1525
-
1526
- const re = redirect;
1527
- const { name, path } = re;
1528
- let { query, hash, params } = location;
1529
- query = re.hasOwnProperty('query') ? re.query : query;
1530
- hash = re.hasOwnProperty('hash') ? re.hash : hash;
1531
- params = re.hasOwnProperty('params') ? re.params : params;
1532
-
1533
- if (name) {
1534
- // resolved named direct
1535
- const targetRecord = nameMap[name];
1536
- {
1537
- assert(targetRecord, `redirect failed: named route "${name}" not found.`);
1538
- }
1539
- return match({
1540
- _normalized: true,
1541
- name,
1542
- query,
1543
- hash,
1544
- params
1545
- }, undefined, location)
1546
- } else if (path) {
1547
- // 1. resolve relative redirect
1548
- const rawPath = resolveRecordPath(path, record);
1549
- // 2. resolve params
1550
- const resolvedPath = fillParams(rawPath, params, `redirect route with path "${rawPath}"`);
1551
- // 3. rematch with existing query and hash
1552
- return match({
1553
- _normalized: true,
1554
- path: resolvedPath,
1555
- query,
1556
- hash
1557
- }, undefined, location)
1558
- } else {
1559
- {
1560
- warn(false, `invalid redirect option: ${JSON.stringify(redirect)}`);
1561
- }
1562
- return _createRoute(null, location)
1563
- }
1564
- }
1565
-
1566
- function alias (
1567
- record,
1568
- location,
1569
- matchAs
1570
- ) {
1571
- const aliasedPath = fillParams(matchAs, location.params, `aliased route with path "${matchAs}"`);
1572
- const aliasedMatch = match({
1573
- _normalized: true,
1574
- path: aliasedPath
1575
- });
1576
- if (aliasedMatch) {
1577
- const matched = aliasedMatch.matched;
1578
- const aliasedRecord = matched[matched.length - 1];
1579
- location.params = aliasedMatch.params;
1580
- return _createRoute(aliasedRecord, location)
1581
- }
1582
- return _createRoute(null, location)
1583
- }
1584
-
1585
- function _createRoute (
1586
- record,
1587
- location,
1588
- redirectedFrom
1589
- ) {
1590
- if (record && record.redirect) {
1591
- return redirect(record, redirectedFrom || location)
1592
- }
1593
- if (record && record.matchAs) {
1594
- return alias(record, location, record.matchAs)
1595
- }
1596
- return createRoute(record, location, redirectedFrom, router)
1597
- }
1598
-
1599
- return {
1600
- match,
1601
- addRoutes
1602
- }
1603
- }
1604
-
1605
- function matchRoute (
1606
- regex,
1607
- path,
1608
- params
1609
- ) {
1610
- const m = path.match(regex);
1611
-
1612
- if (!m) {
1613
- return false
1614
- } else if (!params) {
1615
- return true
1616
- }
1617
-
1618
- for (let i = 1, len = m.length; i < len; ++i) {
1619
- const key = regex.keys[i - 1];
1620
- const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i];
1621
- if (key) {
1622
- // Fix #1994: using * with props: true generates a param named 0
1623
- params[key.name || 'pathMatch'] = val;
1624
- }
1625
- }
1626
-
1627
- return true
1628
- }
1629
-
1630
- function resolveRecordPath (path, record) {
1631
- return resolvePath(path, record.parent ? record.parent.path : '/', true)
1632
- }
1633
-
1634
- /* */
1635
-
1636
- // use User Timing api (if present) for more accurate key precision
1637
- const Time =
1638
- inBrowser && window.performance && window.performance.now
1639
- ? window.performance
1640
- : Date;
1641
-
1642
- function genStateKey () {
1643
- return Time.now().toFixed(3)
1644
- }
1645
-
1646
- let _key = genStateKey();
1647
-
1648
- function getStateKey () {
1649
- return _key
1650
- }
1651
-
1652
- function setStateKey (key) {
1653
- return (_key = key)
1654
- }
1655
-
1656
- /* */
1657
-
1658
- const positionStore = Object.create(null);
1659
-
1660
- function setupScroll () {
1661
- // Prevent browser scroll behavior on History popstate
1662
- if ('scrollRestoration' in window.history) {
1663
- window.history.scrollRestoration = 'manual';
1664
- }
1665
- // Fix for #1585 for Firefox
1666
- // Fix for #2195 Add optional third attribute to workaround a bug in safari https://bugs.webkit.org/show_bug.cgi?id=182678
1667
- // Fix for #2774 Support for apps loaded from Windows file shares not mapped to network drives: replaced location.origin with
1668
- // window.location.protocol + '//' + window.location.host
1669
- // location.host contains the port and location.hostname doesn't
1670
- const protocolAndPath = window.location.protocol + '//' + window.location.host;
1671
- const absolutePath = window.location.href.replace(protocolAndPath, '');
1672
- // preserve existing history state as it could be overriden by the user
1673
- const stateCopy = extend({}, window.history.state);
1674
- stateCopy.key = getStateKey();
1675
- window.history.replaceState(stateCopy, '', absolutePath);
1676
- window.addEventListener('popstate', handlePopState);
1677
- return () => {
1678
- window.removeEventListener('popstate', handlePopState);
1679
- }
1680
- }
1681
-
1682
- function handleScroll (
1683
- router,
1684
- to,
1685
- from,
1686
- isPop
1687
- ) {
1688
- if (!router.app) {
1689
- return
1690
- }
1691
-
1692
- const behavior = router.options.scrollBehavior;
1693
- if (!behavior) {
1694
- return
1695
- }
1696
-
1697
- {
1698
- assert(typeof behavior === 'function', `scrollBehavior must be a function`);
1699
- }
1700
-
1701
- // wait until re-render finishes before scrolling
1702
- router.app.$nextTick(() => {
1703
- const position = getScrollPosition();
1704
- const shouldScroll = behavior.call(
1705
- router,
1706
- to,
1707
- from,
1708
- isPop ? position : null
1709
- );
1710
-
1711
- if (!shouldScroll) {
1712
- return
1713
- }
1714
-
1715
- if (typeof shouldScroll.then === 'function') {
1716
- shouldScroll
1717
- .then(shouldScroll => {
1718
- scrollToPosition((shouldScroll), position);
1719
- })
1720
- .catch(err => {
1721
- {
1722
- assert(false, err.toString());
1723
- }
1724
- });
1725
- } else {
1726
- scrollToPosition(shouldScroll, position);
1727
- }
1728
- });
1729
- }
1730
-
1731
- function saveScrollPosition () {
1732
- const key = getStateKey();
1733
- if (key) {
1734
- positionStore[key] = {
1735
- x: window.pageXOffset,
1736
- y: window.pageYOffset
1737
- };
1738
- }
1739
- }
1740
-
1741
- function handlePopState (e) {
1742
- saveScrollPosition();
1743
- if (e.state && e.state.key) {
1744
- setStateKey(e.state.key);
1745
- }
1746
- }
1747
-
1748
- function getScrollPosition () {
1749
- const key = getStateKey();
1750
- if (key) {
1751
- return positionStore[key]
1752
- }
1753
- }
1754
-
1755
- function getElementPosition (el, offset) {
1756
- const docEl = document.documentElement;
1757
- const docRect = docEl.getBoundingClientRect();
1758
- const elRect = el.getBoundingClientRect();
1759
- return {
1760
- x: elRect.left - docRect.left - offset.x,
1761
- y: elRect.top - docRect.top - offset.y
1762
- }
1763
- }
1764
-
1765
- function isValidPosition (obj) {
1766
- return isNumber(obj.x) || isNumber(obj.y)
1767
- }
1768
-
1769
- function normalizePosition (obj) {
1770
- return {
1771
- x: isNumber(obj.x) ? obj.x : window.pageXOffset,
1772
- y: isNumber(obj.y) ? obj.y : window.pageYOffset
1773
- }
1774
- }
1775
-
1776
- function normalizeOffset (obj) {
1777
- return {
1778
- x: isNumber(obj.x) ? obj.x : 0,
1779
- y: isNumber(obj.y) ? obj.y : 0
1780
- }
1781
- }
1782
-
1783
- function isNumber (v) {
1784
- return typeof v === 'number'
1785
- }
1786
-
1787
- const hashStartsWithNumberRE = /^#\d/;
1788
-
1789
- function scrollToPosition (shouldScroll, position) {
1790
- const isObject = typeof shouldScroll === 'object';
1791
- if (isObject && typeof shouldScroll.selector === 'string') {
1792
- // getElementById would still fail if the selector contains a more complicated query like #main[data-attr]
1793
- // but at the same time, it doesn't make much sense to select an element with an id and an extra selector
1794
- const el = hashStartsWithNumberRE.test(shouldScroll.selector) // $flow-disable-line
1795
- ? document.getElementById(shouldScroll.selector.slice(1)) // $flow-disable-line
1796
- : document.querySelector(shouldScroll.selector);
1797
-
1798
- if (el) {
1799
- let offset =
1800
- shouldScroll.offset && typeof shouldScroll.offset === 'object'
1801
- ? shouldScroll.offset
1802
- : {};
1803
- offset = normalizeOffset(offset);
1804
- position = getElementPosition(el, offset);
1805
- } else if (isValidPosition(shouldScroll)) {
1806
- position = normalizePosition(shouldScroll);
1807
- }
1808
- } else if (isObject && isValidPosition(shouldScroll)) {
1809
- position = normalizePosition(shouldScroll);
1810
- }
1811
-
1812
- if (position) {
1813
- window.scrollTo(position.x, position.y);
1814
- }
1815
- }
1816
-
1817
- /* */
1818
-
1819
- const supportsPushState =
1820
- inBrowser &&
1821
- (function () {
1822
- const ua = window.navigator.userAgent;
1823
-
1824
- if (
1825
- (ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
1826
- ua.indexOf('Mobile Safari') !== -1 &&
1827
- ua.indexOf('Chrome') === -1 &&
1828
- ua.indexOf('Windows Phone') === -1
1829
- ) {
1830
- return false
1831
- }
1832
-
1833
- return window.history && typeof window.history.pushState === 'function'
1834
- })();
1835
-
1836
- function pushState (url, replace) {
1837
- saveScrollPosition();
1838
- // try...catch the pushState call to get around Safari
1839
- // DOM Exception 18 where it limits to 100 pushState calls
1840
- const history = window.history;
1841
- try {
1842
- if (replace) {
1843
- // preserve existing history state as it could be overriden by the user
1844
- const stateCopy = extend({}, history.state);
1845
- stateCopy.key = getStateKey();
1846
- history.replaceState(stateCopy, '', url);
1847
- } else {
1848
- history.pushState({ key: setStateKey(genStateKey()) }, '', url);
1849
- }
1850
- } catch (e) {
1851
- window.location[replace ? 'replace' : 'assign'](url);
1852
- }
1853
- }
1854
-
1855
- function replaceState (url) {
1856
- pushState(url, true);
1857
- }
1858
-
1859
- /* */
1860
-
1861
- function runQueue (queue, fn, cb) {
1862
- const step = index => {
1863
- if (index >= queue.length) {
1864
- cb();
1865
- } else {
1866
- if (queue[index]) {
1867
- fn(queue[index], () => {
1868
- step(index + 1);
1869
- });
1870
- } else {
1871
- step(index + 1);
1872
- }
1873
- }
1874
- };
1875
- step(0);
1876
- }
1877
-
1878
- const NavigationFailureType = {
1879
- redirected: 2,
1880
- aborted: 4,
1881
- cancelled: 8,
1882
- duplicated: 16
1883
- };
1884
-
1885
- function createNavigationRedirectedError (from, to) {
1886
- return createRouterError(
1887
- from,
1888
- to,
1889
- NavigationFailureType.redirected,
1890
- `Redirected when going from "${from.fullPath}" to "${stringifyRoute(
1891
- to
1892
- )}" via a navigation guard.`
1893
- )
1894
- }
1895
-
1896
- function createNavigationDuplicatedError (from, to) {
1897
- const error = createRouterError(
1898
- from,
1899
- to,
1900
- NavigationFailureType.duplicated,
1901
- `Avoided redundant navigation to current location: "${from.fullPath}".`
1902
- );
1903
- // backwards compatible with the first introduction of Errors
1904
- error.name = 'NavigationDuplicated';
1905
- return error
1906
- }
1907
-
1908
- function createNavigationCancelledError (from, to) {
1909
- return createRouterError(
1910
- from,
1911
- to,
1912
- NavigationFailureType.cancelled,
1913
- `Navigation cancelled from "${from.fullPath}" to "${
1914
- to.fullPath
1915
- }" with a new navigation.`
1916
- )
1917
- }
1918
-
1919
- function createNavigationAbortedError (from, to) {
1920
- return createRouterError(
1921
- from,
1922
- to,
1923
- NavigationFailureType.aborted,
1924
- `Navigation aborted from "${from.fullPath}" to "${
1925
- to.fullPath
1926
- }" via a navigation guard.`
1927
- )
1928
- }
1929
-
1930
- function createRouterError (from, to, type, message) {
1931
- const error = new Error(message);
1932
- error._isRouter = true;
1933
- error.from = from;
1934
- error.to = to;
1935
- error.type = type;
1936
-
1937
- return error
1938
- }
1939
-
1940
- const propertiesToLog = ['params', 'query', 'hash'];
1941
-
1942
- function stringifyRoute (to) {
1943
- if (typeof to === 'string') return to
1944
- if ('path' in to) return to.path
1945
- const location = {};
1946
- propertiesToLog.forEach(key => {
1947
- if (key in to) location[key] = to[key];
1948
- });
1949
- return JSON.stringify(location, null, 2)
1950
- }
1951
-
1952
- function isError (err) {
1953
- return Object.prototype.toString.call(err).indexOf('Error') > -1
1954
- }
1955
-
1956
- function isNavigationFailure (err, errorType) {
1957
- return (
1958
- isError(err) &&
1959
- err._isRouter &&
1960
- (errorType == null || err.type === errorType)
1961
- )
1962
- }
1963
-
1964
- /* */
1965
-
1966
- function resolveAsyncComponents (matched) {
1967
- return (to, from, next) => {
1968
- let hasAsync = false;
1969
- let pending = 0;
1970
- let error = null;
1971
-
1972
- flatMapComponents(matched, (def, _, match, key) => {
1973
- // if it's a function and doesn't have cid attached,
1974
- // assume it's an async component resolve function.
1975
- // we are not using Kdu's default async resolving mechanism because
1976
- // we want to halt the navigation until the incoming component has been
1977
- // resolved.
1978
- if (typeof def === 'function' && def.cid === undefined) {
1979
- hasAsync = true;
1980
- pending++;
1981
-
1982
- const resolve = once(resolvedDef => {
1983
- if (isESModule(resolvedDef)) {
1984
- resolvedDef = resolvedDef.default;
1985
- }
1986
- // save resolved on async factory in case it's used elsewhere
1987
- def.resolved = typeof resolvedDef === 'function'
1988
- ? resolvedDef
1989
- : _Kdu.extend(resolvedDef);
1990
- match.components[key] = resolvedDef;
1991
- pending--;
1992
- if (pending <= 0) {
1993
- next();
1994
- }
1995
- });
1996
-
1997
- const reject = once(reason => {
1998
- const msg = `Failed to resolve async component ${key}: ${reason}`;
1999
- warn(false, msg);
2000
- if (!error) {
2001
- error = isError(reason)
2002
- ? reason
2003
- : new Error(msg);
2004
- next(error);
2005
- }
2006
- });
2007
-
2008
- let res;
2009
- try {
2010
- res = def(resolve, reject);
2011
- } catch (e) {
2012
- reject(e);
2013
- }
2014
- if (res) {
2015
- if (typeof res.then === 'function') {
2016
- res.then(resolve, reject);
2017
- } else {
2018
- // new syntax in Kdu 2.3
2019
- const comp = res.component;
2020
- if (comp && typeof comp.then === 'function') {
2021
- comp.then(resolve, reject);
2022
- }
2023
- }
2024
- }
2025
- }
2026
- });
2027
-
2028
- if (!hasAsync) next();
2029
- }
2030
- }
2031
-
2032
- function flatMapComponents (
2033
- matched,
2034
- fn
2035
- ) {
2036
- return flatten(matched.map(m => {
2037
- return Object.keys(m.components).map(key => fn(
2038
- m.components[key],
2039
- m.instances[key],
2040
- m, key
2041
- ))
2042
- }))
2043
- }
2044
-
2045
- function flatten (arr) {
2046
- return Array.prototype.concat.apply([], arr)
2047
- }
2048
-
2049
- const hasSymbol =
2050
- typeof Symbol === 'function' &&
2051
- typeof Symbol.toStringTag === 'symbol';
2052
-
2053
- function isESModule (obj) {
2054
- return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
2055
- }
2056
-
2057
- // in Webpack 2, require.ensure now also returns a Promise
2058
- // so the resolve/reject functions may get called an extra time
2059
- // if the user uses an arrow function shorthand that happens to
2060
- // return that Promise.
2061
- function once (fn) {
2062
- let called = false;
2063
- return function (...args) {
2064
- if (called) return
2065
- called = true;
2066
- return fn.apply(this, args)
2067
- }
2068
- }
2069
-
2070
- /* */
2071
-
2072
- class History {
2073
-
2074
-
2075
-
2076
-
2077
-
2078
-
2079
-
2080
-
2081
-
2082
-
2083
-
2084
-
2085
- // implemented by sub-classes
2086
-
2087
-
2088
-
2089
-
2090
-
2091
-
2092
-
2093
- constructor (router, base) {
2094
- this.router = router;
2095
- this.base = normalizeBase(base);
2096
- // start with a route object that stands for "nowhere"
2097
- this.current = START;
2098
- this.pending = null;
2099
- this.ready = false;
2100
- this.readyCbs = [];
2101
- this.readyErrorCbs = [];
2102
- this.errorCbs = [];
2103
- this.listeners = [];
2104
- }
2105
-
2106
- listen (cb) {
2107
- this.cb = cb;
2108
- }
2109
-
2110
- onReady (cb, errorCb) {
2111
- if (this.ready) {
2112
- cb();
2113
- } else {
2114
- this.readyCbs.push(cb);
2115
- if (errorCb) {
2116
- this.readyErrorCbs.push(errorCb);
2117
- }
2118
- }
2119
- }
2120
-
2121
- onError (errorCb) {
2122
- this.errorCbs.push(errorCb);
2123
- }
2124
-
2125
- transitionTo (
2126
- location,
2127
- onComplete,
2128
- onAbort
2129
- ) {
2130
- let route;
2131
- try {
2132
- route = this.router.match(location, this.current);
2133
- } catch (e) {
2134
- this.errorCbs.forEach(cb => {
2135
- cb(e);
2136
- });
2137
- // Exception should still be thrown
2138
- throw e
2139
- }
2140
- this.confirmTransition(
2141
- route,
2142
- () => {
2143
- const prev = this.current;
2144
- this.updateRoute(route);
2145
- onComplete && onComplete(route);
2146
- this.ensureURL();
2147
- this.router.afterHooks.forEach(hook => {
2148
- hook && hook(route, prev);
2149
- });
2150
-
2151
- // fire ready cbs once
2152
- if (!this.ready) {
2153
- this.ready = true;
2154
- this.readyCbs.forEach(cb => {
2155
- cb(route);
2156
- });
2157
- }
2158
- },
2159
- err => {
2160
- if (onAbort) {
2161
- onAbort(err);
2162
- }
2163
- if (err && !this.ready) {
2164
- this.ready = true;
2165
- // Initial redirection should still trigger the onReady onSuccess
2166
- if (!isNavigationFailure(err, NavigationFailureType.redirected)) {
2167
- this.readyErrorCbs.forEach(cb => {
2168
- cb(err);
2169
- });
2170
- } else {
2171
- this.readyCbs.forEach(cb => {
2172
- cb(route);
2173
- });
2174
- }
2175
- }
2176
- }
2177
- );
2178
- }
2179
-
2180
- confirmTransition (route, onComplete, onAbort) {
2181
- const current = this.current;
2182
- const abort = err => {
2183
- // changed after adding errors before that change,
2184
- // redirect and aborted navigation would produce an err == null
2185
- if (!isNavigationFailure(err) && isError(err)) {
2186
- if (this.errorCbs.length) {
2187
- this.errorCbs.forEach(cb => {
2188
- cb(err);
2189
- });
2190
- } else {
2191
- warn(false, 'uncaught error during route navigation:');
2192
- console.error(err);
2193
- }
2194
- }
2195
- onAbort && onAbort(err);
2196
- };
2197
- const lastRouteIndex = route.matched.length - 1;
2198
- const lastCurrentIndex = current.matched.length - 1;
2199
- if (
2200
- isSameRoute(route, current) &&
2201
- // in the case the route map has been dynamically appended to
2202
- lastRouteIndex === lastCurrentIndex &&
2203
- route.matched[lastRouteIndex] === current.matched[lastCurrentIndex]
2204
- ) {
2205
- this.ensureURL();
2206
- return abort(createNavigationDuplicatedError(current, route))
2207
- }
2208
-
2209
- const { updated, deactivated, activated } = resolveQueue(
2210
- this.current.matched,
2211
- route.matched
2212
- );
2213
-
2214
- const queue = [].concat(
2215
- // in-component leave guards
2216
- extractLeaveGuards(deactivated),
2217
- // global before hooks
2218
- this.router.beforeHooks,
2219
- // in-component update hooks
2220
- extractUpdateHooks(updated),
2221
- // in-config enter guards
2222
- activated.map(m => m.beforeEnter),
2223
- // async components
2224
- resolveAsyncComponents(activated)
2225
- );
2226
-
2227
- this.pending = route;
2228
- const iterator = (hook, next) => {
2229
- if (this.pending !== route) {
2230
- return abort(createNavigationCancelledError(current, route))
2231
- }
2232
- try {
2233
- hook(route, current, (to) => {
2234
- if (to === false) {
2235
- // next(false) -> abort navigation, ensure current URL
2236
- this.ensureURL(true);
2237
- abort(createNavigationAbortedError(current, route));
2238
- } else if (isError(to)) {
2239
- this.ensureURL(true);
2240
- abort(to);
2241
- } else if (
2242
- typeof to === 'string' ||
2243
- (typeof to === 'object' &&
2244
- (typeof to.path === 'string' || typeof to.name === 'string'))
2245
- ) {
2246
- // next('/') or next({ path: '/' }) -> redirect
2247
- abort(createNavigationRedirectedError(current, route));
2248
- if (typeof to === 'object' && to.replace) {
2249
- this.replace(to);
2250
- } else {
2251
- this.push(to);
2252
- }
2253
- } else {
2254
- // confirm transition and pass on the value
2255
- next(to);
2256
- }
2257
- });
2258
- } catch (e) {
2259
- abort(e);
2260
- }
2261
- };
2262
-
2263
- runQueue(queue, iterator, () => {
2264
- const postEnterCbs = [];
2265
- const isValid = () => this.current === route;
2266
- // wait until async components are resolved before
2267
- // extracting in-component enter guards
2268
- const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid);
2269
- const queue = enterGuards.concat(this.router.resolveHooks);
2270
- runQueue(queue, iterator, () => {
2271
- if (this.pending !== route) {
2272
- return abort(createNavigationCancelledError(current, route))
2273
- }
2274
- this.pending = null;
2275
- onComplete(route);
2276
- if (this.router.app) {
2277
- this.router.app.$nextTick(() => {
2278
- postEnterCbs.forEach(cb => {
2279
- cb();
2280
- });
2281
- });
2282
- }
2283
- });
2284
- });
2285
- }
2286
-
2287
- updateRoute (route) {
2288
- this.current = route;
2289
- this.cb && this.cb(route);
2290
- }
2291
-
2292
- setupListeners () {
2293
- // Default implementation is empty
2294
- }
2295
-
2296
- teardownListeners () {
2297
- this.listeners.forEach(cleanupListener => {
2298
- cleanupListener();
2299
- });
2300
- this.listeners = [];
2301
- }
2302
- }
2303
-
2304
- function normalizeBase (base) {
2305
- if (!base) {
2306
- if (inBrowser) {
2307
- // respect <base> tag
2308
- const baseEl = document.querySelector('base');
2309
- base = (baseEl && baseEl.getAttribute('href')) || '/';
2310
- // strip full URL origin
2311
- base = base.replace(/^https?:\/\/[^\/]+/, '');
2312
- } else {
2313
- base = '/';
2314
- }
2315
- }
2316
- // make sure there's the starting slash
2317
- if (base.charAt(0) !== '/') {
2318
- base = '/' + base;
2319
- }
2320
- // remove trailing slash
2321
- return base.replace(/\/$/, '')
2322
- }
2323
-
2324
- function resolveQueue (
2325
- current,
2326
- next
2327
- ) {
2328
- let i;
2329
- const max = Math.max(current.length, next.length);
2330
- for (i = 0; i < max; i++) {
2331
- if (current[i] !== next[i]) {
2332
- break
2333
- }
2334
- }
2335
- return {
2336
- updated: next.slice(0, i),
2337
- activated: next.slice(i),
2338
- deactivated: current.slice(i)
2339
- }
2340
- }
2341
-
2342
- function extractGuards (
2343
- records,
2344
- name,
2345
- bind,
2346
- reverse
2347
- ) {
2348
- const guards = flatMapComponents(records, (def, instance, match, key) => {
2349
- const guard = extractGuard(def, name);
2350
- if (guard) {
2351
- return Array.isArray(guard)
2352
- ? guard.map(guard => bind(guard, instance, match, key))
2353
- : bind(guard, instance, match, key)
2354
- }
2355
- });
2356
- return flatten(reverse ? guards.reverse() : guards)
2357
- }
2358
-
2359
- function extractGuard (
2360
- def,
2361
- key
2362
- ) {
2363
- if (typeof def !== 'function') {
2364
- // extend now so that global mixins are applied.
2365
- def = _Kdu.extend(def);
2366
- }
2367
- return def.options[key]
2368
- }
2369
-
2370
- function extractLeaveGuards (deactivated) {
2371
- return extractGuards(deactivated, 'beforeRouteLeave', bindGuard, true)
2372
- }
2373
-
2374
- function extractUpdateHooks (updated) {
2375
- return extractGuards(updated, 'beforeRouteUpdate', bindGuard)
2376
- }
2377
-
2378
- function bindGuard (guard, instance) {
2379
- if (instance) {
2380
- return function boundRouteGuard () {
2381
- return guard.apply(instance, arguments)
2382
- }
2383
- }
2384
- }
2385
-
2386
- function extractEnterGuards (
2387
- activated,
2388
- cbs,
2389
- isValid
2390
- ) {
2391
- return extractGuards(
2392
- activated,
2393
- 'beforeRouteEnter',
2394
- (guard, _, match, key) => {
2395
- return bindEnterGuard(guard, match, key, cbs, isValid)
2396
- }
2397
- )
2398
- }
2399
-
2400
- function bindEnterGuard (
2401
- guard,
2402
- match,
2403
- key,
2404
- cbs,
2405
- isValid
2406
- ) {
2407
- return function routeEnterGuard (to, from, next) {
2408
- return guard(to, from, cb => {
2409
- if (typeof cb === 'function') {
2410
- cbs.push(() => {
2411
- // #750
2412
- // if a router-view is wrapped with an out-in transition,
2413
- // the instance may not have been registered at this time.
2414
- // we will need to poll for registration until current route
2415
- // is no longer valid.
2416
- poll(cb, match.instances, key, isValid);
2417
- });
2418
- }
2419
- next(cb);
2420
- })
2421
- }
2422
- }
2423
-
2424
- function poll (
2425
- cb, // somehow flow cannot infer this is a function
2426
- instances,
2427
- key,
2428
- isValid
2429
- ) {
2430
- if (
2431
- instances[key] &&
2432
- !instances[key]._isBeingDestroyed // do not reuse being destroyed instance
2433
- ) {
2434
- cb(instances[key]);
2435
- } else if (isValid()) {
2436
- setTimeout(() => {
2437
- poll(cb, instances, key, isValid);
2438
- }, 16);
2439
- }
2440
- }
2441
-
2442
- /* */
2443
-
2444
- class HTML5History extends History {
2445
-
2446
-
2447
- constructor (router, base) {
2448
- super(router, base);
2449
-
2450
- this._startLocation = getLocation(this.base);
2451
- }
2452
-
2453
- setupListeners () {
2454
- if (this.listeners.length > 0) {
2455
- return
2456
- }
2457
-
2458
- const router = this.router;
2459
- const expectScroll = router.options.scrollBehavior;
2460
- const supportsScroll = supportsPushState && expectScroll;
2461
-
2462
- if (supportsScroll) {
2463
- this.listeners.push(setupScroll());
2464
- }
2465
-
2466
- const handleRoutingEvent = () => {
2467
- const current = this.current;
2468
-
2469
- // Avoiding first `popstate` event dispatched in some browsers but first
2470
- // history route not updated since async guard at the same time.
2471
- const location = getLocation(this.base);
2472
- if (this.current === START && location === this._startLocation) {
2473
- return
2474
- }
2475
-
2476
- this.transitionTo(location, route => {
2477
- if (supportsScroll) {
2478
- handleScroll(router, route, current, true);
2479
- }
2480
- });
2481
- };
2482
- window.addEventListener('popstate', handleRoutingEvent);
2483
- this.listeners.push(() => {
2484
- window.removeEventListener('popstate', handleRoutingEvent);
2485
- });
2486
- }
2487
-
2488
- go (n) {
2489
- window.history.go(n);
2490
- }
2491
-
2492
- push (location, onComplete, onAbort) {
2493
- const { current: fromRoute } = this;
2494
- this.transitionTo(location, route => {
2495
- pushState(cleanPath(this.base + route.fullPath));
2496
- handleScroll(this.router, route, fromRoute, false);
2497
- onComplete && onComplete(route);
2498
- }, onAbort);
2499
- }
2500
-
2501
- replace (location, onComplete, onAbort) {
2502
- const { current: fromRoute } = this;
2503
- this.transitionTo(location, route => {
2504
- replaceState(cleanPath(this.base + route.fullPath));
2505
- handleScroll(this.router, route, fromRoute, false);
2506
- onComplete && onComplete(route);
2507
- }, onAbort);
2508
- }
2509
-
2510
- ensureURL (push) {
2511
- if (getLocation(this.base) !== this.current.fullPath) {
2512
- const current = cleanPath(this.base + this.current.fullPath);
2513
- push ? pushState(current) : replaceState(current);
2514
- }
2515
- }
2516
-
2517
- getCurrentLocation () {
2518
- return getLocation(this.base)
2519
- }
2520
- }
2521
-
2522
- function getLocation (base) {
2523
- let path = decodeURI(window.location.pathname);
2524
- if (base && path.toLowerCase().indexOf(base.toLowerCase()) === 0) {
2525
- path = path.slice(base.length);
2526
- }
2527
- return (path || '/') + window.location.search + window.location.hash
2528
- }
2529
-
2530
- /* */
2531
-
2532
- class HashHistory extends History {
2533
- constructor (router, base, fallback) {
2534
- super(router, base);
2535
- // check history fallback deeplinking
2536
- if (fallback && checkFallback(this.base)) {
2537
- return
2538
- }
2539
- ensureSlash();
2540
- }
2541
-
2542
- // this is delayed until the app mounts
2543
- // to avoid the hashchange listener being fired too early
2544
- setupListeners () {
2545
- if (this.listeners.length > 0) {
2546
- return
2547
- }
2548
-
2549
- const router = this.router;
2550
- const expectScroll = router.options.scrollBehavior;
2551
- const supportsScroll = supportsPushState && expectScroll;
2552
-
2553
- if (supportsScroll) {
2554
- this.listeners.push(setupScroll());
2555
- }
2556
-
2557
- const handleRoutingEvent = () => {
2558
- const current = this.current;
2559
- if (!ensureSlash()) {
2560
- return
2561
- }
2562
- this.transitionTo(getHash(), route => {
2563
- if (supportsScroll) {
2564
- handleScroll(this.router, route, current, true);
2565
- }
2566
- if (!supportsPushState) {
2567
- replaceHash(route.fullPath);
2568
- }
2569
- });
2570
- };
2571
- const eventType = supportsPushState ? 'popstate' : 'hashchange';
2572
- window.addEventListener(
2573
- eventType,
2574
- handleRoutingEvent
2575
- );
2576
- this.listeners.push(() => {
2577
- window.removeEventListener(eventType, handleRoutingEvent);
2578
- });
2579
- }
2580
-
2581
- push (location, onComplete, onAbort) {
2582
- const { current: fromRoute } = this;
2583
- this.transitionTo(
2584
- location,
2585
- route => {
2586
- pushHash(route.fullPath);
2587
- handleScroll(this.router, route, fromRoute, false);
2588
- onComplete && onComplete(route);
2589
- },
2590
- onAbort
2591
- );
2592
- }
2593
-
2594
- replace (location, onComplete, onAbort) {
2595
- const { current: fromRoute } = this;
2596
- this.transitionTo(
2597
- location,
2598
- route => {
2599
- replaceHash(route.fullPath);
2600
- handleScroll(this.router, route, fromRoute, false);
2601
- onComplete && onComplete(route);
2602
- },
2603
- onAbort
2604
- );
2605
- }
2606
-
2607
- go (n) {
2608
- window.history.go(n);
2609
- }
2610
-
2611
- ensureURL (push) {
2612
- const current = this.current.fullPath;
2613
- if (getHash() !== current) {
2614
- push ? pushHash(current) : replaceHash(current);
2615
- }
2616
- }
2617
-
2618
- getCurrentLocation () {
2619
- return getHash()
2620
- }
2621
- }
2622
-
2623
- function checkFallback (base) {
2624
- const location = getLocation(base);
2625
- if (!/^\/#/.test(location)) {
2626
- window.location.replace(cleanPath(base + '/#' + location));
2627
- return true
2628
- }
2629
- }
2630
-
2631
- function ensureSlash () {
2632
- const path = getHash();
2633
- if (path.charAt(0) === '/') {
2634
- return true
2635
- }
2636
- replaceHash('/' + path);
2637
- return false
2638
- }
2639
-
2640
- function getHash () {
2641
- // We can't use window.location.hash here because it's not
2642
- // consistent across browsers - Firefox will pre-decode it!
2643
- let href = window.location.href;
2644
- const index = href.indexOf('#');
2645
- // empty path
2646
- if (index < 0) return ''
2647
-
2648
- href = href.slice(index + 1);
2649
- // decode the hash but not the search or hash
2650
- // as search(query) is already decoded
2651
- const searchIndex = href.indexOf('?');
2652
- if (searchIndex < 0) {
2653
- const hashIndex = href.indexOf('#');
2654
- if (hashIndex > -1) {
2655
- href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex);
2656
- } else href = decodeURI(href);
2657
- } else {
2658
- href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex);
2659
- }
2660
-
2661
- return href
2662
- }
2663
-
2664
- function getUrl (path) {
2665
- const href = window.location.href;
2666
- const i = href.indexOf('#');
2667
- const base = i >= 0 ? href.slice(0, i) : href;
2668
- return `${base}#${path}`
2669
- }
2670
-
2671
- function pushHash (path) {
2672
- if (supportsPushState) {
2673
- pushState(getUrl(path));
2674
- } else {
2675
- window.location.hash = path;
2676
- }
2677
- }
2678
-
2679
- function replaceHash (path) {
2680
- if (supportsPushState) {
2681
- replaceState(getUrl(path));
2682
- } else {
2683
- window.location.replace(getUrl(path));
2684
- }
2685
- }
2686
-
2687
- /* */
2688
-
2689
- class AbstractHistory extends History {
2690
-
2691
-
2692
-
2693
- constructor (router, base) {
2694
- super(router, base);
2695
- this.stack = [];
2696
- this.index = -1;
2697
- }
2698
-
2699
- push (location, onComplete, onAbort) {
2700
- this.transitionTo(
2701
- location,
2702
- route => {
2703
- this.stack = this.stack.slice(0, this.index + 1).concat(route);
2704
- this.index++;
2705
- onComplete && onComplete(route);
2706
- },
2707
- onAbort
2708
- );
2709
- }
2710
-
2711
- replace (location, onComplete, onAbort) {
2712
- this.transitionTo(
2713
- location,
2714
- route => {
2715
- this.stack = this.stack.slice(0, this.index).concat(route);
2716
- onComplete && onComplete(route);
2717
- },
2718
- onAbort
2719
- );
2720
- }
2721
-
2722
- go (n) {
2723
- const targetIndex = this.index + n;
2724
- if (targetIndex < 0 || targetIndex >= this.stack.length) {
2725
- return
2726
- }
2727
- const route = this.stack[targetIndex];
2728
- this.confirmTransition(
2729
- route,
2730
- () => {
2731
- this.index = targetIndex;
2732
- this.updateRoute(route);
2733
- },
2734
- err => {
2735
- if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
2736
- this.index = targetIndex;
2737
- }
2738
- }
2739
- );
2740
- }
2741
-
2742
- getCurrentLocation () {
2743
- const current = this.stack[this.stack.length - 1];
2744
- return current ? current.fullPath : '/'
2745
- }
2746
-
2747
- ensureURL () {
2748
- // noop
2749
- }
2750
- }
2751
-
2752
- /* */
2753
-
2754
- class KduRouter {
2755
-
2756
-
2757
-
2758
-
2759
-
2760
-
2761
-
2762
-
2763
-
2764
-
2765
-
2766
-
2767
-
2768
-
2769
-
2770
-
2771
-
2772
-
2773
- constructor (options = {}) {
2774
- this.app = null;
2775
- this.apps = [];
2776
- this.options = options;
2777
- this.beforeHooks = [];
2778
- this.resolveHooks = [];
2779
- this.afterHooks = [];
2780
- this.matcher = createMatcher(options.routes || [], this);
2781
-
2782
- let mode = options.mode || 'hash';
2783
- this.fallback =
2784
- mode === 'history' && !supportsPushState && options.fallback !== false;
2785
- if (this.fallback) {
2786
- mode = 'hash';
2787
- }
2788
- if (!inBrowser) {
2789
- mode = 'abstract';
2790
- }
2791
- this.mode = mode;
2792
-
2793
- switch (mode) {
2794
- case 'history':
2795
- this.history = new HTML5History(this, options.base);
2796
- break
2797
- case 'hash':
2798
- this.history = new HashHistory(this, options.base, this.fallback);
2799
- break
2800
- case 'abstract':
2801
- this.history = new AbstractHistory(this, options.base);
2802
- break
2803
- default:
2804
- {
2805
- assert(false, `invalid mode: ${mode}`);
2806
- }
2807
- }
2808
- }
2809
-
2810
- match (raw, current, redirectedFrom) {
2811
- return this.matcher.match(raw, current, redirectedFrom)
2812
- }
2813
-
2814
- get currentRoute () {
2815
- return this.history && this.history.current
2816
- }
2817
-
2818
- init (app /* Kdu component instance */) {
2819
-
2820
- assert(
2821
- install.installed,
2822
- `not installed. Make sure to call \`Kdu.use(KduRouter)\` ` +
2823
- `before creating root instance.`
2824
- );
2825
-
2826
- this.apps.push(app);
2827
-
2828
- // set up app destroyed handler
2829
- app.$once('hook:destroyed', () => {
2830
- // clean out app from this.apps array once destroyed
2831
- const index = this.apps.indexOf(app);
2832
- if (index > -1) this.apps.splice(index, 1);
2833
- // ensure we still have a main app or null if no apps
2834
- // we do not release the router so it can be reused
2835
- if (this.app === app) this.app = this.apps[0] || null;
2836
-
2837
- if (!this.app) {
2838
- // clean up event listeners
2839
- this.history.teardownListeners();
2840
- }
2841
- });
2842
-
2843
- // main app previously initialized
2844
- // return as we don't need to set up new history listener
2845
- if (this.app) {
2846
- return
2847
- }
2848
-
2849
- this.app = app;
2850
-
2851
- const history = this.history;
2852
-
2853
- if (history instanceof HTML5History || history instanceof HashHistory) {
2854
- const handleInitialScroll = routeOrError => {
2855
- const from = history.current;
2856
- const expectScroll = this.options.scrollBehavior;
2857
- const supportsScroll = supportsPushState && expectScroll;
2858
-
2859
- if (supportsScroll && 'fullPath' in routeOrError) {
2860
- handleScroll(this, routeOrError, from, false);
2861
- }
2862
- };
2863
- const setupListeners = routeOrError => {
2864
- history.setupListeners();
2865
- handleInitialScroll(routeOrError);
2866
- };
2867
- history.transitionTo(
2868
- history.getCurrentLocation(),
2869
- setupListeners,
2870
- setupListeners
2871
- );
2872
- }
2873
-
2874
- history.listen(route => {
2875
- this.apps.forEach(app => {
2876
- app._route = route;
2877
- });
2878
- });
2879
- }
2880
-
2881
- beforeEach (fn) {
2882
- return registerHook(this.beforeHooks, fn)
2883
- }
2884
-
2885
- beforeResolve (fn) {
2886
- return registerHook(this.resolveHooks, fn)
2887
- }
2888
-
2889
- afterEach (fn) {
2890
- return registerHook(this.afterHooks, fn)
2891
- }
2892
-
2893
- onReady (cb, errorCb) {
2894
- this.history.onReady(cb, errorCb);
2895
- }
2896
-
2897
- onError (errorCb) {
2898
- this.history.onError(errorCb);
2899
- }
2900
-
2901
- push (location, onComplete, onAbort) {
2902
- // $flow-disable-line
2903
- if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
2904
- return new Promise((resolve, reject) => {
2905
- this.history.push(location, resolve, reject);
2906
- })
2907
- } else {
2908
- this.history.push(location, onComplete, onAbort);
2909
- }
2910
- }
2911
-
2912
- replace (location, onComplete, onAbort) {
2913
- // $flow-disable-line
2914
- if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
2915
- return new Promise((resolve, reject) => {
2916
- this.history.replace(location, resolve, reject);
2917
- })
2918
- } else {
2919
- this.history.replace(location, onComplete, onAbort);
2920
- }
2921
- }
2922
-
2923
- go (n) {
2924
- this.history.go(n);
2925
- }
2926
-
2927
- back () {
2928
- this.go(-1);
2929
- }
2930
-
2931
- forward () {
2932
- this.go(1);
2933
- }
2934
-
2935
- getMatchedComponents (to) {
2936
- const route = to
2937
- ? to.matched
2938
- ? to
2939
- : this.resolve(to).route
2940
- : this.currentRoute;
2941
- if (!route) {
2942
- return []
2943
- }
2944
- return [].concat.apply(
2945
- [],
2946
- route.matched.map(m => {
2947
- return Object.keys(m.components).map(key => {
2948
- return m.components[key]
2949
- })
2950
- })
2951
- )
2952
- }
2953
-
2954
- resolve (
2955
- to,
2956
- current,
2957
- append
2958
- ) {
2959
- current = current || this.history.current;
2960
- const location = normalizeLocation(to, current, append, this);
2961
- const route = this.match(location, current);
2962
- const fullPath = route.redirectedFrom || route.fullPath;
2963
- const base = this.history.base;
2964
- const href = createHref(base, fullPath, this.mode);
2965
- return {
2966
- location,
2967
- route,
2968
- href,
2969
- // for backwards compat
2970
- normalizedTo: location,
2971
- resolved: route
2972
- }
2973
- }
2974
-
2975
- addRoutes (routes) {
2976
- this.matcher.addRoutes(routes);
2977
- if (this.history.current !== START) {
2978
- this.history.transitionTo(this.history.getCurrentLocation());
2979
- }
2980
- }
2981
- }
2982
-
2983
- function registerHook (list, fn) {
2984
- list.push(fn);
2985
- return () => {
2986
- const i = list.indexOf(fn);
2987
- if (i > -1) list.splice(i, 1);
2988
- }
2989
- }
2990
-
2991
- function createHref (base, fullPath, mode) {
2992
- var path = mode === 'hash' ? '#' + fullPath : fullPath;
2993
- return base ? cleanPath(base + '/' + path) : path
2994
- }
2995
-
2996
- KduRouter.install = install;
2997
- KduRouter.version = '3.4.0-beta.0';
2998
- KduRouter.isNavigationFailure = isNavigationFailure;
2999
- KduRouter.NavigationFailureType = NavigationFailureType;
3000
-
3001
- if (inBrowser && window.Kdu) {
3002
- window.Kdu.use(KduRouter);
3003
- }
3004
-
3005
- export default KduRouter;