kdu-router 3.0.1 → 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 +117 -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 +117 -68
- package/dist/kdu-router.js +117 -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,6 +106,17 @@ var View = {
|
|
|
95
106
|
matched.instances[name] = knode.componentInstance;
|
|
96
107
|
};
|
|
97
108
|
|
|
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
|
+
};
|
|
119
|
+
|
|
98
120
|
// resolve props
|
|
99
121
|
var propsToPass = data.props = resolveProps(route, matched.props && matched.props[name]);
|
|
100
122
|
if (propsToPass) {
|
|
@@ -112,7 +134,7 @@ var View = {
|
|
|
112
134
|
|
|
113
135
|
return h(component, data, children)
|
|
114
136
|
}
|
|
115
|
-
}
|
|
137
|
+
}
|
|
116
138
|
|
|
117
139
|
function resolveProps (route, config) {
|
|
118
140
|
switch (typeof config) {
|
|
@@ -135,13 +157,6 @@ function resolveProps (route, config) {
|
|
|
135
157
|
}
|
|
136
158
|
}
|
|
137
159
|
|
|
138
|
-
function extend (to, from) {
|
|
139
|
-
for (var key in from) {
|
|
140
|
-
to[key] = from[key];
|
|
141
|
-
}
|
|
142
|
-
return to
|
|
143
|
-
}
|
|
144
|
-
|
|
145
160
|
/* */
|
|
146
161
|
|
|
147
162
|
var encodeReserveRE = /[!'()*]/g;
|
|
@@ -240,7 +255,6 @@ function stringifyQuery (obj) {
|
|
|
240
255
|
|
|
241
256
|
/* */
|
|
242
257
|
|
|
243
|
-
|
|
244
258
|
var trailingSlashRE = /\/?$/;
|
|
245
259
|
|
|
246
260
|
function createRoute (
|
|
@@ -383,7 +397,7 @@ var toTypes = [String, Object];
|
|
|
383
397
|
var eventTypes = [String, Array];
|
|
384
398
|
|
|
385
399
|
var Link = {
|
|
386
|
-
name: '
|
|
400
|
+
name: 'RouterLink',
|
|
387
401
|
props: {
|
|
388
402
|
to: {
|
|
389
403
|
type: toTypes,
|
|
@@ -418,17 +432,17 @@ var Link = {
|
|
|
418
432
|
var globalExactActiveClass = router.options.linkExactActiveClass;
|
|
419
433
|
// Support global empty active class
|
|
420
434
|
var activeClassFallback = globalActiveClass == null
|
|
421
|
-
|
|
422
|
-
|
|
435
|
+
? 'router-link-active'
|
|
436
|
+
: globalActiveClass;
|
|
423
437
|
var exactActiveClassFallback = globalExactActiveClass == null
|
|
424
|
-
|
|
425
|
-
|
|
438
|
+
? 'router-link-exact-active'
|
|
439
|
+
: globalExactActiveClass;
|
|
426
440
|
var activeClass = this.activeClass == null
|
|
427
|
-
|
|
428
|
-
|
|
441
|
+
? activeClassFallback
|
|
442
|
+
: this.activeClass;
|
|
429
443
|
var exactActiveClass = this.exactActiveClass == null
|
|
430
|
-
|
|
431
|
-
|
|
444
|
+
? exactActiveClassFallback
|
|
445
|
+
: this.exactActiveClass;
|
|
432
446
|
var compareTarget = location.path
|
|
433
447
|
? createRoute(null, location, null, router)
|
|
434
448
|
: route;
|
|
@@ -468,7 +482,6 @@ var Link = {
|
|
|
468
482
|
if (a) {
|
|
469
483
|
// in case the <a> is a static node
|
|
470
484
|
a.isStatic = false;
|
|
471
|
-
var extend = _Kdu.util.extend;
|
|
472
485
|
var aData = a.data = extend({}, a.data);
|
|
473
486
|
aData.on = on;
|
|
474
487
|
var aAttrs = a.data.attrs = extend({}, a.data.attrs);
|
|
@@ -481,7 +494,7 @@ var Link = {
|
|
|
481
494
|
|
|
482
495
|
return h(this.tag, data, this.$slots.default)
|
|
483
496
|
}
|
|
484
|
-
}
|
|
497
|
+
}
|
|
485
498
|
|
|
486
499
|
function guardEvent (e) {
|
|
487
500
|
// don't redirect with control keys
|
|
@@ -559,8 +572,8 @@ function install (Kdu) {
|
|
|
559
572
|
get: function get () { return this._routerRoot._route }
|
|
560
573
|
});
|
|
561
574
|
|
|
562
|
-
Kdu.component('
|
|
563
|
-
Kdu.component('
|
|
575
|
+
Kdu.component('RouterView', View);
|
|
576
|
+
Kdu.component('RouterLink', Link);
|
|
564
577
|
|
|
565
578
|
var strats = Kdu.config.optionMergeStrategies;
|
|
566
579
|
// use the same hook merging strategy for route hooks
|
|
@@ -1070,7 +1083,6 @@ function pathToRegexp (path, keys, options) {
|
|
|
1070
1083
|
|
|
1071
1084
|
return stringToRegexp(/** @type {string} */ (path), /** @type {!Array} */ (keys), options)
|
|
1072
1085
|
}
|
|
1073
|
-
|
|
1074
1086
|
pathToRegexp_1.parse = parse_1;
|
|
1075
1087
|
pathToRegexp_1.compile = compile_1;
|
|
1076
1088
|
pathToRegexp_1.tokensToFunction = tokensToFunction_1;
|
|
@@ -1086,16 +1098,24 @@ function fillParams (
|
|
|
1086
1098
|
params,
|
|
1087
1099
|
routeMsg
|
|
1088
1100
|
) {
|
|
1101
|
+
params = params || {};
|
|
1089
1102
|
try {
|
|
1090
1103
|
var filler =
|
|
1091
1104
|
regexpCompileCache[path] ||
|
|
1092
1105
|
(regexpCompileCache[path] = pathToRegexp_1.compile(path));
|
|
1093
|
-
|
|
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 })
|
|
1094
1111
|
} catch (e) {
|
|
1095
1112
|
if (process.env.NODE_ENV !== 'production') {
|
|
1096
1113
|
warn(false, ("missing param for " + routeMsg + ": " + (e.message)));
|
|
1097
1114
|
}
|
|
1098
1115
|
return ''
|
|
1116
|
+
} finally {
|
|
1117
|
+
// delete the 0 if it was added
|
|
1118
|
+
delete params[0];
|
|
1099
1119
|
}
|
|
1100
1120
|
}
|
|
1101
1121
|
|
|
@@ -1266,7 +1286,6 @@ function normalizePath (path, parent, strict) {
|
|
|
1266
1286
|
|
|
1267
1287
|
/* */
|
|
1268
1288
|
|
|
1269
|
-
|
|
1270
1289
|
function normalizeLocation (
|
|
1271
1290
|
raw,
|
|
1272
1291
|
current,
|
|
@@ -1275,15 +1294,17 @@ function normalizeLocation (
|
|
|
1275
1294
|
) {
|
|
1276
1295
|
var next = typeof raw === 'string' ? { path: raw } : raw;
|
|
1277
1296
|
// named target
|
|
1278
|
-
if (next.
|
|
1297
|
+
if (next._normalized) {
|
|
1279
1298
|
return next
|
|
1299
|
+
} else if (next.name) {
|
|
1300
|
+
return extend({}, raw)
|
|
1280
1301
|
}
|
|
1281
1302
|
|
|
1282
1303
|
// relative params
|
|
1283
1304
|
if (!next.path && next.params && current) {
|
|
1284
|
-
next =
|
|
1305
|
+
next = extend({}, next);
|
|
1285
1306
|
next._normalized = true;
|
|
1286
|
-
var params =
|
|
1307
|
+
var params = extend(extend({}, current.params), next.params);
|
|
1287
1308
|
if (current.name) {
|
|
1288
1309
|
next.name = current.name;
|
|
1289
1310
|
next.params = params;
|
|
@@ -1321,16 +1342,10 @@ function normalizeLocation (
|
|
|
1321
1342
|
}
|
|
1322
1343
|
}
|
|
1323
1344
|
|
|
1324
|
-
function assign (a, b) {
|
|
1325
|
-
for (var key in b) {
|
|
1326
|
-
a[key] = b[key];
|
|
1327
|
-
}
|
|
1328
|
-
return a
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
1345
|
/* */
|
|
1332
1346
|
|
|
1333
1347
|
|
|
1348
|
+
|
|
1334
1349
|
function createMatcher (
|
|
1335
1350
|
routes,
|
|
1336
1351
|
router
|
|
@@ -1374,10 +1389,8 @@ function createMatcher (
|
|
|
1374
1389
|
}
|
|
1375
1390
|
}
|
|
1376
1391
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
return _createRoute(record, location, redirectedFrom)
|
|
1380
|
-
}
|
|
1392
|
+
location.path = fillParams(record.path, location.params, ("named route \"" + name + "\""));
|
|
1393
|
+
return _createRoute(record, location, redirectedFrom)
|
|
1381
1394
|
} else if (location.path) {
|
|
1382
1395
|
location.params = {};
|
|
1383
1396
|
for (var i = 0; i < pathList.length; i++) {
|
|
@@ -1398,8 +1411,8 @@ function createMatcher (
|
|
|
1398
1411
|
) {
|
|
1399
1412
|
var originalRedirect = record.redirect;
|
|
1400
1413
|
var redirect = typeof originalRedirect === 'function'
|
|
1401
|
-
|
|
1402
|
-
|
|
1414
|
+
? originalRedirect(createRoute(record, location, null, router))
|
|
1415
|
+
: originalRedirect;
|
|
1403
1416
|
|
|
1404
1417
|
if (typeof redirect === 'string') {
|
|
1405
1418
|
redirect = { path: redirect };
|
|
@@ -1513,7 +1526,8 @@ function matchRoute (
|
|
|
1513
1526
|
var key = regex.keys[i - 1];
|
|
1514
1527
|
var val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i];
|
|
1515
1528
|
if (key) {
|
|
1516
|
-
|
|
1529
|
+
// Fix #1994: using * with props: true generates a param named 0
|
|
1530
|
+
params[key.name || 'pathMatch'] = val;
|
|
1517
1531
|
}
|
|
1518
1532
|
}
|
|
1519
1533
|
|
|
@@ -1526,12 +1540,17 @@ function resolveRecordPath (path, record) {
|
|
|
1526
1540
|
|
|
1527
1541
|
/* */
|
|
1528
1542
|
|
|
1529
|
-
|
|
1530
1543
|
var positionStore = Object.create(null);
|
|
1531
1544
|
|
|
1532
1545
|
function setupScroll () {
|
|
1533
1546
|
// Fix for #1585 for Firefox
|
|
1534
|
-
|
|
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);
|
|
1535
1554
|
window.addEventListener('popstate', function (e) {
|
|
1536
1555
|
saveScrollPosition();
|
|
1537
1556
|
if (e.state && e.state.key) {
|
|
@@ -1562,7 +1581,7 @@ function handleScroll (
|
|
|
1562
1581
|
// wait until re-render finishes before scrolling
|
|
1563
1582
|
router.app.$nextTick(function () {
|
|
1564
1583
|
var position = getScrollPosition();
|
|
1565
|
-
var shouldScroll = behavior(to, from, isPop ? position : null);
|
|
1584
|
+
var shouldScroll = behavior.call(router, to, from, isPop ? position : null);
|
|
1566
1585
|
|
|
1567
1586
|
if (!shouldScroll) {
|
|
1568
1587
|
return
|
|
@@ -2103,7 +2122,6 @@ function bindEnterGuard (
|
|
|
2103
2122
|
) {
|
|
2104
2123
|
return function routeEnterGuard (to, from, next) {
|
|
2105
2124
|
return guard(to, from, function (cb) {
|
|
2106
|
-
next(cb);
|
|
2107
2125
|
if (typeof cb === 'function') {
|
|
2108
2126
|
cbs.push(function () {
|
|
2109
2127
|
// #750
|
|
@@ -2114,6 +2132,7 @@ function bindEnterGuard (
|
|
|
2114
2132
|
poll(cb, match.instances, key, isValid);
|
|
2115
2133
|
});
|
|
2116
2134
|
}
|
|
2135
|
+
next(cb);
|
|
2117
2136
|
})
|
|
2118
2137
|
}
|
|
2119
2138
|
}
|
|
@@ -2124,7 +2143,10 @@ function poll (
|
|
|
2124
2143
|
key,
|
|
2125
2144
|
isValid
|
|
2126
2145
|
) {
|
|
2127
|
-
if (
|
|
2146
|
+
if (
|
|
2147
|
+
instances[key] &&
|
|
2148
|
+
!instances[key]._isBeingDestroyed // do not reuse being destroyed instance
|
|
2149
|
+
) {
|
|
2128
2150
|
cb(instances[key]);
|
|
2129
2151
|
} else if (isValid()) {
|
|
2130
2152
|
setTimeout(function () {
|
|
@@ -2135,16 +2157,16 @@ function poll (
|
|
|
2135
2157
|
|
|
2136
2158
|
/* */
|
|
2137
2159
|
|
|
2138
|
-
|
|
2139
|
-
var HTML5History = (function (History$$1) {
|
|
2160
|
+
var HTML5History = /*@__PURE__*/(function (History$$1) {
|
|
2140
2161
|
function HTML5History (router, base) {
|
|
2141
2162
|
var this$1 = this;
|
|
2142
2163
|
|
|
2143
2164
|
History$$1.call(this, router, base);
|
|
2144
2165
|
|
|
2145
2166
|
var expectScroll = router.options.scrollBehavior;
|
|
2167
|
+
var supportsScroll = supportsPushState && expectScroll;
|
|
2146
2168
|
|
|
2147
|
-
if (
|
|
2169
|
+
if (supportsScroll) {
|
|
2148
2170
|
setupScroll();
|
|
2149
2171
|
}
|
|
2150
2172
|
|
|
@@ -2160,7 +2182,7 @@ var HTML5History = (function (History$$1) {
|
|
|
2160
2182
|
}
|
|
2161
2183
|
|
|
2162
2184
|
this$1.transitionTo(location, function (route) {
|
|
2163
|
-
if (
|
|
2185
|
+
if (supportsScroll) {
|
|
2164
2186
|
handleScroll(router, route, current, true);
|
|
2165
2187
|
}
|
|
2166
2188
|
});
|
|
@@ -2214,7 +2236,7 @@ var HTML5History = (function (History$$1) {
|
|
|
2214
2236
|
}(History));
|
|
2215
2237
|
|
|
2216
2238
|
function getLocation (base) {
|
|
2217
|
-
var path = window.location.pathname;
|
|
2239
|
+
var path = decodeURI(window.location.pathname);
|
|
2218
2240
|
if (base && path.indexOf(base) === 0) {
|
|
2219
2241
|
path = path.slice(base.length);
|
|
2220
2242
|
}
|
|
@@ -2223,8 +2245,7 @@ function getLocation (base) {
|
|
|
2223
2245
|
|
|
2224
2246
|
/* */
|
|
2225
2247
|
|
|
2226
|
-
|
|
2227
|
-
var HashHistory = (function (History$$1) {
|
|
2248
|
+
var HashHistory = /*@__PURE__*/(function (History$$1) {
|
|
2228
2249
|
function HashHistory (router, base, fallback) {
|
|
2229
2250
|
History$$1.call(this, router, base);
|
|
2230
2251
|
// check history fallback deeplinking
|
|
@@ -2333,7 +2354,22 @@ function getHash () {
|
|
|
2333
2354
|
// consistent across browsers - Firefox will pre-decode it!
|
|
2334
2355
|
var href = window.location.href;
|
|
2335
2356
|
var index = href.indexOf('#');
|
|
2336
|
-
|
|
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
|
|
2337
2373
|
}
|
|
2338
2374
|
|
|
2339
2375
|
function getUrl (path) {
|
|
@@ -2361,8 +2397,7 @@ function replaceHash (path) {
|
|
|
2361
2397
|
|
|
2362
2398
|
/* */
|
|
2363
2399
|
|
|
2364
|
-
|
|
2365
|
-
var AbstractHistory = (function (History$$1) {
|
|
2400
|
+
var AbstractHistory = /*@__PURE__*/(function (History$$1) {
|
|
2366
2401
|
function AbstractHistory (router, base) {
|
|
2367
2402
|
History$$1.call(this, router, base);
|
|
2368
2403
|
this.stack = [];
|
|
@@ -2420,6 +2455,8 @@ var AbstractHistory = (function (History$$1) {
|
|
|
2420
2455
|
|
|
2421
2456
|
/* */
|
|
2422
2457
|
|
|
2458
|
+
|
|
2459
|
+
|
|
2423
2460
|
var KduRouter = function KduRouter (options) {
|
|
2424
2461
|
if ( options === void 0 ) options = {};
|
|
2425
2462
|
|
|
@@ -2483,7 +2520,18 @@ KduRouter.prototype.init = function init (app /* Kdu component instance */) {
|
|
|
2483
2520
|
|
|
2484
2521
|
this.apps.push(app);
|
|
2485
2522
|
|
|
2486
|
-
//
|
|
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
|
|
2487
2535
|
if (this.app) {
|
|
2488
2536
|
return
|
|
2489
2537
|
}
|
|
@@ -2573,9 +2621,10 @@ KduRouter.prototype.resolve = function resolve (
|
|
|
2573
2621
|
current,
|
|
2574
2622
|
append
|
|
2575
2623
|
) {
|
|
2624
|
+
current = current || this.history.current;
|
|
2576
2625
|
var location = normalizeLocation(
|
|
2577
2626
|
to,
|
|
2578
|
-
current
|
|
2627
|
+
current,
|
|
2579
2628
|
append,
|
|
2580
2629
|
this
|
|
2581
2630
|
);
|
|
@@ -2616,7 +2665,7 @@ function createHref (base, fullPath, mode) {
|
|
|
2616
2665
|
}
|
|
2617
2666
|
|
|
2618
2667
|
KduRouter.install = install;
|
|
2619
|
-
KduRouter.version = '3.0.
|
|
2668
|
+
KduRouter.version = '3.0.7';
|
|
2620
2669
|
|
|
2621
2670
|
if (inBrowser && window.Kdu) {
|
|
2622
2671
|
window.Kdu.use(KduRouter);
|