kdu-router 3.0.0 → 3.0.7
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.
- package/README.md +0 -2
- package/dist/kdu-router.common.js +128 -68
- package/dist/kdu-router.esm.browser.js +2632 -0
- package/dist/kdu-router.esm.browser.min.js +11 -0
- package/dist/kdu-router.esm.js +128 -68
- package/dist/kdu-router.js +128 -68
- package/dist/kdu-router.min.js +8 -3
- package/package.json +43 -26
- package/src/create-matcher.js +200 -0
- package/src/create-route-map.js +171 -0
- package/src/index.js +247 -0
- package/src/install.js +52 -0
- package/types/kdu.d.ts +3 -3
- package/types/router.d.ts +9 -8
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
* kdu-router v3.0.
|
|
1
|
+
/*!
|
|
2
|
+
* kdu-router v3.0.7
|
|
3
3
|
* (c) 2022 NKDuy
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -23,8 +23,15 @@ function isError (err) {
|
|
|
23
23
|
return Object.prototype.toString.call(err).indexOf('Error') > -1
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
function extend (a, b) {
|
|
27
|
+
for (var key in b) {
|
|
28
|
+
a[key] = b[key];
|
|
29
|
+
}
|
|
30
|
+
return a
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
var View = {
|
|
27
|
-
name: '
|
|
34
|
+
name: 'RouterView',
|
|
28
35
|
functional: true,
|
|
29
36
|
props: {
|
|
30
37
|
name: {
|
|
@@ -38,6 +45,7 @@ var View = {
|
|
|
38
45
|
var parent = ref.parent;
|
|
39
46
|
var data = ref.data;
|
|
40
47
|
|
|
48
|
+
// used by devtools to display a router-view badge
|
|
41
49
|
data.routerView = true;
|
|
42
50
|
|
|
43
51
|
// directly use parent context's createElement() function
|
|
@@ -52,11 +60,14 @@ var View = {
|
|
|
52
60
|
var depth = 0;
|
|
53
61
|
var inactive = false;
|
|
54
62
|
while (parent && parent._routerRoot !== parent) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
var knodeData = parent.$knode && parent.$knode.data;
|
|
64
|
+
if (knodeData) {
|
|
65
|
+
if (knodeData.routerView) {
|
|
66
|
+
depth++;
|
|
67
|
+
}
|
|
68
|
+
if (knodeData.keepAlive && parent._inactive) {
|
|
69
|
+
inactive = true;
|
|
70
|
+
}
|
|
60
71
|
}
|
|
61
72
|
parent = parent.$parent;
|
|
62
73
|
}
|
|
@@ -95,20 +106,35 @@ var View = {
|
|
|
95
106
|
matched.instances[name] = knode.componentInstance;
|
|
96
107
|
};
|
|
97
108
|
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
data.
|
|
109
|
+
// register instance in init hook
|
|
110
|
+
// in case kept-alive component be actived when routes changed
|
|
111
|
+
data.hook.init = function (knode) {
|
|
112
|
+
if (knode.data.keepAlive &&
|
|
113
|
+
knode.componentInstance &&
|
|
114
|
+
knode.componentInstance !== matched.instances[name]
|
|
115
|
+
) {
|
|
116
|
+
matched.instances[name] = knode.componentInstance;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
101
119
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
120
|
+
// resolve props
|
|
121
|
+
var propsToPass = data.props = resolveProps(route, matched.props && matched.props[name]);
|
|
122
|
+
if (propsToPass) {
|
|
123
|
+
// clone to prevent mutation
|
|
124
|
+
propsToPass = data.props = extend({}, propsToPass);
|
|
125
|
+
// pass non-declared props as attrs
|
|
126
|
+
var attrs = data.attrs = data.attrs || {};
|
|
127
|
+
for (var key in propsToPass) {
|
|
128
|
+
if (!component.props || !(key in component.props)) {
|
|
129
|
+
attrs[key] = propsToPass[key];
|
|
130
|
+
delete propsToPass[key];
|
|
131
|
+
}
|
|
106
132
|
}
|
|
107
133
|
}
|
|
108
134
|
|
|
109
135
|
return h(component, data, children)
|
|
110
136
|
}
|
|
111
|
-
}
|
|
137
|
+
}
|
|
112
138
|
|
|
113
139
|
function resolveProps (route, config) {
|
|
114
140
|
switch (typeof config) {
|
|
@@ -229,7 +255,6 @@ function stringifyQuery (obj) {
|
|
|
229
255
|
|
|
230
256
|
/* */
|
|
231
257
|
|
|
232
|
-
|
|
233
258
|
var trailingSlashRE = /\/?$/;
|
|
234
259
|
|
|
235
260
|
function createRoute (
|
|
@@ -372,7 +397,7 @@ var toTypes = [String, Object];
|
|
|
372
397
|
var eventTypes = [String, Array];
|
|
373
398
|
|
|
374
399
|
var Link = {
|
|
375
|
-
name: '
|
|
400
|
+
name: 'RouterLink',
|
|
376
401
|
props: {
|
|
377
402
|
to: {
|
|
378
403
|
type: toTypes,
|
|
@@ -407,17 +432,17 @@ var Link = {
|
|
|
407
432
|
var globalExactActiveClass = router.options.linkExactActiveClass;
|
|
408
433
|
// Support global empty active class
|
|
409
434
|
var activeClassFallback = globalActiveClass == null
|
|
410
|
-
|
|
411
|
-
|
|
435
|
+
? 'router-link-active'
|
|
436
|
+
: globalActiveClass;
|
|
412
437
|
var exactActiveClassFallback = globalExactActiveClass == null
|
|
413
|
-
|
|
414
|
-
|
|
438
|
+
? 'router-link-exact-active'
|
|
439
|
+
: globalExactActiveClass;
|
|
415
440
|
var activeClass = this.activeClass == null
|
|
416
|
-
|
|
417
|
-
|
|
441
|
+
? activeClassFallback
|
|
442
|
+
: this.activeClass;
|
|
418
443
|
var exactActiveClass = this.exactActiveClass == null
|
|
419
|
-
|
|
420
|
-
|
|
444
|
+
? exactActiveClassFallback
|
|
445
|
+
: this.exactActiveClass;
|
|
421
446
|
var compareTarget = location.path
|
|
422
447
|
? createRoute(null, location, null, router)
|
|
423
448
|
: route;
|
|
@@ -457,7 +482,6 @@ var Link = {
|
|
|
457
482
|
if (a) {
|
|
458
483
|
// in case the <a> is a static node
|
|
459
484
|
a.isStatic = false;
|
|
460
|
-
var extend = _Kdu.util.extend;
|
|
461
485
|
var aData = a.data = extend({}, a.data);
|
|
462
486
|
aData.on = on;
|
|
463
487
|
var aAttrs = a.data.attrs = extend({}, a.data.attrs);
|
|
@@ -470,7 +494,7 @@ var Link = {
|
|
|
470
494
|
|
|
471
495
|
return h(this.tag, data, this.$slots.default)
|
|
472
496
|
}
|
|
473
|
-
}
|
|
497
|
+
}
|
|
474
498
|
|
|
475
499
|
function guardEvent (e) {
|
|
476
500
|
// don't redirect with control keys
|
|
@@ -548,8 +572,8 @@ function install (Kdu) {
|
|
|
548
572
|
get: function get () { return this._routerRoot._route }
|
|
549
573
|
});
|
|
550
574
|
|
|
551
|
-
Kdu.component('
|
|
552
|
-
Kdu.component('
|
|
575
|
+
Kdu.component('RouterView', View);
|
|
576
|
+
Kdu.component('RouterLink', Link);
|
|
553
577
|
|
|
554
578
|
var strats = Kdu.config.optionMergeStrategies;
|
|
555
579
|
// use the same hook merging strategy for route hooks
|
|
@@ -1059,7 +1083,6 @@ function pathToRegexp (path, keys, options) {
|
|
|
1059
1083
|
|
|
1060
1084
|
return stringToRegexp(/** @type {string} */ (path), /** @type {!Array} */ (keys), options)
|
|
1061
1085
|
}
|
|
1062
|
-
|
|
1063
1086
|
pathToRegexp_1.parse = parse_1;
|
|
1064
1087
|
pathToRegexp_1.compile = compile_1;
|
|
1065
1088
|
pathToRegexp_1.tokensToFunction = tokensToFunction_1;
|
|
@@ -1075,16 +1098,24 @@ function fillParams (
|
|
|
1075
1098
|
params,
|
|
1076
1099
|
routeMsg
|
|
1077
1100
|
) {
|
|
1101
|
+
params = params || {};
|
|
1078
1102
|
try {
|
|
1079
1103
|
var filler =
|
|
1080
1104
|
regexpCompileCache[path] ||
|
|
1081
1105
|
(regexpCompileCache[path] = pathToRegexp_1.compile(path));
|
|
1082
|
-
|
|
1106
|
+
|
|
1107
|
+
// Fix #2505 resolving asterisk routes { name: 'not-found', params: { pathMatch: '/not-found' }}
|
|
1108
|
+
if (params.pathMatch) { params[0] = params.pathMatch; }
|
|
1109
|
+
|
|
1110
|
+
return filler(params, { pretty: true })
|
|
1083
1111
|
} catch (e) {
|
|
1084
1112
|
if (process.env.NODE_ENV !== 'production') {
|
|
1085
1113
|
warn(false, ("missing param for " + routeMsg + ": " + (e.message)));
|
|
1086
1114
|
}
|
|
1087
1115
|
return ''
|
|
1116
|
+
} finally {
|
|
1117
|
+
// delete the 0 if it was added
|
|
1118
|
+
delete params[0];
|
|
1088
1119
|
}
|
|
1089
1120
|
}
|
|
1090
1121
|
|
|
@@ -1255,7 +1286,6 @@ function normalizePath (path, parent, strict) {
|
|
|
1255
1286
|
|
|
1256
1287
|
/* */
|
|
1257
1288
|
|
|
1258
|
-
|
|
1259
1289
|
function normalizeLocation (
|
|
1260
1290
|
raw,
|
|
1261
1291
|
current,
|
|
@@ -1264,15 +1294,17 @@ function normalizeLocation (
|
|
|
1264
1294
|
) {
|
|
1265
1295
|
var next = typeof raw === 'string' ? { path: raw } : raw;
|
|
1266
1296
|
// named target
|
|
1267
|
-
if (next.
|
|
1297
|
+
if (next._normalized) {
|
|
1268
1298
|
return next
|
|
1299
|
+
} else if (next.name) {
|
|
1300
|
+
return extend({}, raw)
|
|
1269
1301
|
}
|
|
1270
1302
|
|
|
1271
1303
|
// relative params
|
|
1272
1304
|
if (!next.path && next.params && current) {
|
|
1273
|
-
next =
|
|
1305
|
+
next = extend({}, next);
|
|
1274
1306
|
next._normalized = true;
|
|
1275
|
-
var params =
|
|
1307
|
+
var params = extend(extend({}, current.params), next.params);
|
|
1276
1308
|
if (current.name) {
|
|
1277
1309
|
next.name = current.name;
|
|
1278
1310
|
next.params = params;
|
|
@@ -1310,16 +1342,10 @@ function normalizeLocation (
|
|
|
1310
1342
|
}
|
|
1311
1343
|
}
|
|
1312
1344
|
|
|
1313
|
-
function assign (a, b) {
|
|
1314
|
-
for (var key in b) {
|
|
1315
|
-
a[key] = b[key];
|
|
1316
|
-
}
|
|
1317
|
-
return a
|
|
1318
|
-
}
|
|
1319
|
-
|
|
1320
1345
|
/* */
|
|
1321
1346
|
|
|
1322
1347
|
|
|
1348
|
+
|
|
1323
1349
|
function createMatcher (
|
|
1324
1350
|
routes,
|
|
1325
1351
|
router
|
|
@@ -1363,10 +1389,8 @@ function createMatcher (
|
|
|
1363
1389
|
}
|
|
1364
1390
|
}
|
|
1365
1391
|
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
return _createRoute(record, location, redirectedFrom)
|
|
1369
|
-
}
|
|
1392
|
+
location.path = fillParams(record.path, location.params, ("named route \"" + name + "\""));
|
|
1393
|
+
return _createRoute(record, location, redirectedFrom)
|
|
1370
1394
|
} else if (location.path) {
|
|
1371
1395
|
location.params = {};
|
|
1372
1396
|
for (var i = 0; i < pathList.length; i++) {
|
|
@@ -1387,8 +1411,8 @@ function createMatcher (
|
|
|
1387
1411
|
) {
|
|
1388
1412
|
var originalRedirect = record.redirect;
|
|
1389
1413
|
var redirect = typeof originalRedirect === 'function'
|
|
1390
|
-
|
|
1391
|
-
|
|
1414
|
+
? originalRedirect(createRoute(record, location, null, router))
|
|
1415
|
+
: originalRedirect;
|
|
1392
1416
|
|
|
1393
1417
|
if (typeof redirect === 'string') {
|
|
1394
1418
|
redirect = { path: redirect };
|
|
@@ -1502,7 +1526,8 @@ function matchRoute (
|
|
|
1502
1526
|
var key = regex.keys[i - 1];
|
|
1503
1527
|
var val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i];
|
|
1504
1528
|
if (key) {
|
|
1505
|
-
|
|
1529
|
+
// Fix #1994: using * with props: true generates a param named 0
|
|
1530
|
+
params[key.name || 'pathMatch'] = val;
|
|
1506
1531
|
}
|
|
1507
1532
|
}
|
|
1508
1533
|
|
|
@@ -1515,12 +1540,17 @@ function resolveRecordPath (path, record) {
|
|
|
1515
1540
|
|
|
1516
1541
|
/* */
|
|
1517
1542
|
|
|
1518
|
-
|
|
1519
1543
|
var positionStore = Object.create(null);
|
|
1520
1544
|
|
|
1521
1545
|
function setupScroll () {
|
|
1522
1546
|
// Fix for #1585 for Firefox
|
|
1523
|
-
|
|
1547
|
+
// Fix for #2195 Add optional third attribute to workaround a bug in safari https://bugs.webkit.org/show_bug.cgi?id=182678
|
|
1548
|
+
// Fix for #2774 Support for apps loaded from Windows file shares not mapped to network drives: replaced location.origin with
|
|
1549
|
+
// window.location.protocol + '//' + window.location.host
|
|
1550
|
+
// location.host contains the port and location.hostname doesn't
|
|
1551
|
+
var protocolAndPath = window.location.protocol + '//' + window.location.host;
|
|
1552
|
+
var absolutePath = window.location.href.replace(protocolAndPath, '');
|
|
1553
|
+
window.history.replaceState({ key: getStateKey() }, '', absolutePath);
|
|
1524
1554
|
window.addEventListener('popstate', function (e) {
|
|
1525
1555
|
saveScrollPosition();
|
|
1526
1556
|
if (e.state && e.state.key) {
|
|
@@ -1551,7 +1581,7 @@ function handleScroll (
|
|
|
1551
1581
|
// wait until re-render finishes before scrolling
|
|
1552
1582
|
router.app.$nextTick(function () {
|
|
1553
1583
|
var position = getScrollPosition();
|
|
1554
|
-
var shouldScroll = behavior(to, from, isPop ? position : null);
|
|
1584
|
+
var shouldScroll = behavior.call(router, to, from, isPop ? position : null);
|
|
1555
1585
|
|
|
1556
1586
|
if (!shouldScroll) {
|
|
1557
1587
|
return
|
|
@@ -2092,7 +2122,6 @@ function bindEnterGuard (
|
|
|
2092
2122
|
) {
|
|
2093
2123
|
return function routeEnterGuard (to, from, next) {
|
|
2094
2124
|
return guard(to, from, function (cb) {
|
|
2095
|
-
next(cb);
|
|
2096
2125
|
if (typeof cb === 'function') {
|
|
2097
2126
|
cbs.push(function () {
|
|
2098
2127
|
// #750
|
|
@@ -2103,6 +2132,7 @@ function bindEnterGuard (
|
|
|
2103
2132
|
poll(cb, match.instances, key, isValid);
|
|
2104
2133
|
});
|
|
2105
2134
|
}
|
|
2135
|
+
next(cb);
|
|
2106
2136
|
})
|
|
2107
2137
|
}
|
|
2108
2138
|
}
|
|
@@ -2113,7 +2143,10 @@ function poll (
|
|
|
2113
2143
|
key,
|
|
2114
2144
|
isValid
|
|
2115
2145
|
) {
|
|
2116
|
-
if (
|
|
2146
|
+
if (
|
|
2147
|
+
instances[key] &&
|
|
2148
|
+
!instances[key]._isBeingDestroyed // do not reuse being destroyed instance
|
|
2149
|
+
) {
|
|
2117
2150
|
cb(instances[key]);
|
|
2118
2151
|
} else if (isValid()) {
|
|
2119
2152
|
setTimeout(function () {
|
|
@@ -2124,16 +2157,16 @@ function poll (
|
|
|
2124
2157
|
|
|
2125
2158
|
/* */
|
|
2126
2159
|
|
|
2127
|
-
|
|
2128
|
-
var HTML5History = (function (History$$1) {
|
|
2160
|
+
var HTML5History = /*@__PURE__*/(function (History$$1) {
|
|
2129
2161
|
function HTML5History (router, base) {
|
|
2130
2162
|
var this$1 = this;
|
|
2131
2163
|
|
|
2132
2164
|
History$$1.call(this, router, base);
|
|
2133
2165
|
|
|
2134
2166
|
var expectScroll = router.options.scrollBehavior;
|
|
2167
|
+
var supportsScroll = supportsPushState && expectScroll;
|
|
2135
2168
|
|
|
2136
|
-
if (
|
|
2169
|
+
if (supportsScroll) {
|
|
2137
2170
|
setupScroll();
|
|
2138
2171
|
}
|
|
2139
2172
|
|
|
@@ -2149,7 +2182,7 @@ var HTML5History = (function (History$$1) {
|
|
|
2149
2182
|
}
|
|
2150
2183
|
|
|
2151
2184
|
this$1.transitionTo(location, function (route) {
|
|
2152
|
-
if (
|
|
2185
|
+
if (supportsScroll) {
|
|
2153
2186
|
handleScroll(router, route, current, true);
|
|
2154
2187
|
}
|
|
2155
2188
|
});
|
|
@@ -2203,7 +2236,7 @@ var HTML5History = (function (History$$1) {
|
|
|
2203
2236
|
}(History));
|
|
2204
2237
|
|
|
2205
2238
|
function getLocation (base) {
|
|
2206
|
-
var path = window.location.pathname;
|
|
2239
|
+
var path = decodeURI(window.location.pathname);
|
|
2207
2240
|
if (base && path.indexOf(base) === 0) {
|
|
2208
2241
|
path = path.slice(base.length);
|
|
2209
2242
|
}
|
|
@@ -2212,8 +2245,7 @@ function getLocation (base) {
|
|
|
2212
2245
|
|
|
2213
2246
|
/* */
|
|
2214
2247
|
|
|
2215
|
-
|
|
2216
|
-
var HashHistory = (function (History$$1) {
|
|
2248
|
+
var HashHistory = /*@__PURE__*/(function (History$$1) {
|
|
2217
2249
|
function HashHistory (router, base, fallback) {
|
|
2218
2250
|
History$$1.call(this, router, base);
|
|
2219
2251
|
// check history fallback deeplinking
|
|
@@ -2322,7 +2354,22 @@ function getHash () {
|
|
|
2322
2354
|
// consistent across browsers - Firefox will pre-decode it!
|
|
2323
2355
|
var href = window.location.href;
|
|
2324
2356
|
var index = href.indexOf('#');
|
|
2325
|
-
|
|
2357
|
+
// empty path
|
|
2358
|
+
if (index < 0) { return '' }
|
|
2359
|
+
|
|
2360
|
+
href = href.slice(index + 1);
|
|
2361
|
+
// decode the hash but not the search or hash
|
|
2362
|
+
// as search(query) is already decoded
|
|
2363
|
+
var searchIndex = href.indexOf('?');
|
|
2364
|
+
if (searchIndex < 0) {
|
|
2365
|
+
var hashIndex = href.indexOf('#');
|
|
2366
|
+
if (hashIndex > -1) { href = decodeURI(href.slice(0, hashIndex)) + href.slice(hashIndex); }
|
|
2367
|
+
else { href = decodeURI(href); }
|
|
2368
|
+
} else {
|
|
2369
|
+
if (searchIndex > -1) { href = decodeURI(href.slice(0, searchIndex)) + href.slice(searchIndex); }
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
return href
|
|
2326
2373
|
}
|
|
2327
2374
|
|
|
2328
2375
|
function getUrl (path) {
|
|
@@ -2350,8 +2397,7 @@ function replaceHash (path) {
|
|
|
2350
2397
|
|
|
2351
2398
|
/* */
|
|
2352
2399
|
|
|
2353
|
-
|
|
2354
|
-
var AbstractHistory = (function (History$$1) {
|
|
2400
|
+
var AbstractHistory = /*@__PURE__*/(function (History$$1) {
|
|
2355
2401
|
function AbstractHistory (router, base) {
|
|
2356
2402
|
History$$1.call(this, router, base);
|
|
2357
2403
|
this.stack = [];
|
|
@@ -2409,6 +2455,8 @@ var AbstractHistory = (function (History$$1) {
|
|
|
2409
2455
|
|
|
2410
2456
|
/* */
|
|
2411
2457
|
|
|
2458
|
+
|
|
2459
|
+
|
|
2412
2460
|
var KduRouter = function KduRouter (options) {
|
|
2413
2461
|
if ( options === void 0 ) options = {};
|
|
2414
2462
|
|
|
@@ -2472,7 +2520,18 @@ KduRouter.prototype.init = function init (app /* Kdu component instance */) {
|
|
|
2472
2520
|
|
|
2473
2521
|
this.apps.push(app);
|
|
2474
2522
|
|
|
2475
|
-
//
|
|
2523
|
+
// set up app destroyed handler
|
|
2524
|
+
app.$once('hook:destroyed', function () {
|
|
2525
|
+
// clean out app from this.apps array once destroyed
|
|
2526
|
+
var index = this$1.apps.indexOf(app);
|
|
2527
|
+
if (index > -1) { this$1.apps.splice(index, 1); }
|
|
2528
|
+
// ensure we still have a main app or null if no apps
|
|
2529
|
+
// we do not release the router so it can be reused
|
|
2530
|
+
if (this$1.app === app) { this$1.app = this$1.apps[0] || null; }
|
|
2531
|
+
});
|
|
2532
|
+
|
|
2533
|
+
// main app previously initialized
|
|
2534
|
+
// return as we don't need to set up new history listener
|
|
2476
2535
|
if (this.app) {
|
|
2477
2536
|
return
|
|
2478
2537
|
}
|
|
@@ -2562,9 +2621,10 @@ KduRouter.prototype.resolve = function resolve (
|
|
|
2562
2621
|
current,
|
|
2563
2622
|
append
|
|
2564
2623
|
) {
|
|
2624
|
+
current = current || this.history.current;
|
|
2565
2625
|
var location = normalizeLocation(
|
|
2566
2626
|
to,
|
|
2567
|
-
current
|
|
2627
|
+
current,
|
|
2568
2628
|
append,
|
|
2569
2629
|
this
|
|
2570
2630
|
);
|
|
@@ -2605,7 +2665,7 @@ function createHref (base, fullPath, mode) {
|
|
|
2605
2665
|
}
|
|
2606
2666
|
|
|
2607
2667
|
KduRouter.install = install;
|
|
2608
|
-
KduRouter.version = '3.0.
|
|
2668
|
+
KduRouter.version = '3.0.7';
|
|
2609
2669
|
|
|
2610
2670
|
if (inBrowser && window.Kdu) {
|
|
2611
2671
|
window.Kdu.use(KduRouter);
|