kdu-router 2.7.0 → 3.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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * kdu-router v2.7.0
2
+ * kdu-router v3.0.0
3
3
  * (c) 2022 NKDuy
4
4
  * @license MIT
5
5
  */
@@ -50,7 +50,7 @@ var View = {
50
50
  var depth = 0;
51
51
  var inactive = false;
52
52
  while (parent && parent._routerRoot !== parent) {
53
- if (parent.$vnode && parent.$vnode.data.routerView) {
53
+ if (parent.$knode && parent.$knode.data.routerView) {
54
54
  depth++;
55
55
  }
56
56
  if (parent._inactive) {
@@ -87,14 +87,22 @@ var View = {
87
87
  }
88
88
  }
89
89
 
90
- // also regiseter instance in prepatch hook
90
+ // also register instance in prepatch hook
91
91
  // in case the same component instance is reused across different routes
92
- ;(data.hook || (data.hook = {})).prepatch = function (_, vnode) {
93
- matched.instances[name] = vnode.componentInstance;
92
+ ;(data.hook || (data.hook = {})).prepatch = function (_, knode) {
93
+ matched.instances[name] = knode.componentInstance;
94
94
  };
95
95
 
96
96
  // resolve props
97
97
  data.props = resolveProps(route, matched.props && matched.props[name]);
98
+ data.attrs = {};
99
+
100
+ for (var key in data.props) {
101
+ if (!('props' in component) || !(key in component.props)) {
102
+ data.attrs[key] = data.props[key];
103
+ delete data.props[key];
104
+ }
105
+ }
98
106
 
99
107
  return h(component, data, children)
100
108
  }
@@ -152,8 +160,7 @@ function resolveQuery (
152
160
  parsedQuery = {};
153
161
  }
154
162
  for (var key in extraQuery) {
155
- var val = extraQuery[key];
156
- parsedQuery[key] = Array.isArray(val) ? val.slice() : val;
163
+ parsedQuery[key] = extraQuery[key];
157
164
  }
158
165
  return parsedQuery
159
166
  }
@@ -230,12 +237,18 @@ function createRoute (
230
237
  router
231
238
  ) {
232
239
  var stringifyQuery$$1 = router && router.options.stringifyQuery;
240
+
241
+ var query = location.query || {};
242
+ try {
243
+ query = clone(query);
244
+ } catch (e) {}
245
+
233
246
  var route = {
234
247
  name: location.name || (record && record.name),
235
248
  meta: (record && record.meta) || {},
236
249
  path: location.path || '/',
237
250
  hash: location.hash || '',
238
- query: location.query || {},
251
+ query: query,
239
252
  params: location.params || {},
240
253
  fullPath: getFullPath(location, stringifyQuery$$1),
241
254
  matched: record ? formatMatch(record) : []
@@ -246,6 +259,20 @@ function createRoute (
246
259
  return Object.freeze(route)
247
260
  }
248
261
 
262
+ function clone (value) {
263
+ if (Array.isArray(value)) {
264
+ return value.map(clone)
265
+ } else if (value && typeof value === 'object') {
266
+ var res = {};
267
+ for (var key in value) {
268
+ res[key] = clone(value[key]);
269
+ }
270
+ return res
271
+ } else {
272
+ return value
273
+ }
274
+ }
275
+
249
276
  // the starting route that represents the initial state
250
277
  var START = createRoute(null, {
251
278
  path: '/'
@@ -299,6 +326,8 @@ function isObjectEqual (a, b) {
299
326
  if ( a === void 0 ) a = {};
300
327
  if ( b === void 0 ) b = {};
301
328
 
329
+ // handle null value #1566
330
+ if (!a || !b) { return a === b }
302
331
  var aKeys = Object.keys(a);
303
332
  var bKeys = Object.keys(b);
304
333
  if (aKeys.length !== bKeys.length) {
@@ -478,7 +507,7 @@ function findAnchor (children) {
478
507
  var _Kdu;
479
508
 
480
509
  function install (Kdu) {
481
- if (install.installed) { return }
510
+ if (install.installed && _Kdu === Kdu) { return }
482
511
  install.installed = true;
483
512
 
484
513
  _Kdu = Kdu;
@@ -486,7 +515,7 @@ function install (Kdu) {
486
515
  var isDef = function (v) { return v !== undefined; };
487
516
 
488
517
  var registerInstance = function (vm, callVal) {
489
- var i = vm.$options._parentVnode;
518
+ var i = vm.$options._parentKnode;
490
519
  if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
491
520
  i(vm, callVal);
492
521
  }
@@ -600,14 +629,14 @@ function cleanPath (path) {
600
629
  return path.replace(/\/\//g, '/')
601
630
  }
602
631
 
603
- var index$1 = Array.isArray || function (arr) {
632
+ var isarray = Array.isArray || function (arr) {
604
633
  return Object.prototype.toString.call(arr) == '[object Array]';
605
634
  };
606
635
 
607
636
  /**
608
637
  * Expose `pathToRegexp`.
609
638
  */
610
- var index = pathToRegexp;
639
+ var pathToRegexp_1 = pathToRegexp;
611
640
  var parse_1 = parse;
612
641
  var compile_1 = compile;
613
642
  var tokensToFunction_1 = tokensToFunction;
@@ -712,7 +741,7 @@ function parse (str, options) {
712
741
  * @return {!function(Object=, Object=)}
713
742
  */
714
743
  function compile (str, options) {
715
- return tokensToFunction(parse(str, options))
744
+ return tokensToFunction(parse(str, options), options)
716
745
  }
717
746
 
718
747
  /**
@@ -742,14 +771,14 @@ function encodeAsterisk (str) {
742
771
  /**
743
772
  * Expose a method for transforming tokens into the path function.
744
773
  */
745
- function tokensToFunction (tokens) {
774
+ function tokensToFunction (tokens, options) {
746
775
  // Compile all the tokens into regexps.
747
776
  var matches = new Array(tokens.length);
748
777
 
749
778
  // Compile all the patterns before compilation.
750
779
  for (var i = 0; i < tokens.length; i++) {
751
780
  if (typeof tokens[i] === 'object') {
752
- matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$');
781
+ matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options));
753
782
  }
754
783
  }
755
784
 
@@ -784,7 +813,7 @@ function tokensToFunction (tokens) {
784
813
  }
785
814
  }
786
815
 
787
- if (index$1(value)) {
816
+ if (isarray(value)) {
788
817
  if (!token.repeat) {
789
818
  throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
790
819
  }
@@ -862,7 +891,7 @@ function attachKeys (re, keys) {
862
891
  * @return {string}
863
892
  */
864
893
  function flags (options) {
865
- return options.sensitive ? '' : 'i'
894
+ return options && options.sensitive ? '' : 'i'
866
895
  }
867
896
 
868
897
  /**
@@ -935,7 +964,7 @@ function stringToRegexp (path, keys, options) {
935
964
  * @return {!RegExp}
936
965
  */
937
966
  function tokensToRegExp (tokens, keys, options) {
938
- if (!index$1(keys)) {
967
+ if (!isarray(keys)) {
939
968
  options = /** @type {!Object} */ (keys || options);
940
969
  keys = [];
941
970
  }
@@ -1011,7 +1040,7 @@ function tokensToRegExp (tokens, keys, options) {
1011
1040
  * @return {!RegExp}
1012
1041
  */
1013
1042
  function pathToRegexp (path, keys, options) {
1014
- if (!index$1(keys)) {
1043
+ if (!isarray(keys)) {
1015
1044
  options = /** @type {!Object} */ (keys || options);
1016
1045
  keys = [];
1017
1046
  }
@@ -1022,20 +1051,21 @@ function pathToRegexp (path, keys, options) {
1022
1051
  return regexpToRegexp(path, /** @type {!Array} */ (keys))
1023
1052
  }
1024
1053
 
1025
- if (index$1(path)) {
1054
+ if (isarray(path)) {
1026
1055
  return arrayToRegexp(/** @type {!Array} */ (path), /** @type {!Array} */ (keys), options)
1027
1056
  }
1028
1057
 
1029
1058
  return stringToRegexp(/** @type {string} */ (path), /** @type {!Array} */ (keys), options)
1030
1059
  }
1031
1060
 
1032
- index.parse = parse_1;
1033
- index.compile = compile_1;
1034
- index.tokensToFunction = tokensToFunction_1;
1035
- index.tokensToRegExp = tokensToRegExp_1;
1061
+ pathToRegexp_1.parse = parse_1;
1062
+ pathToRegexp_1.compile = compile_1;
1063
+ pathToRegexp_1.tokensToFunction = tokensToFunction_1;
1064
+ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
1036
1065
 
1037
1066
  /* */
1038
1067
 
1068
+ // $flow-disable-line
1039
1069
  var regexpCompileCache = Object.create(null);
1040
1070
 
1041
1071
  function fillParams (
@@ -1046,7 +1076,7 @@ function fillParams (
1046
1076
  try {
1047
1077
  var filler =
1048
1078
  regexpCompileCache[path] ||
1049
- (regexpCompileCache[path] = index.compile(path));
1079
+ (regexpCompileCache[path] = pathToRegexp_1.compile(path));
1050
1080
  return filler(params || {}, { pretty: true })
1051
1081
  } catch (e) {
1052
1082
  if (process.env.NODE_ENV !== 'production') {
@@ -1066,7 +1096,9 @@ function createRouteMap (
1066
1096
  ) {
1067
1097
  // the path list is used to control path matching priority
1068
1098
  var pathList = oldPathList || [];
1099
+ // $flow-disable-line
1069
1100
  var pathMap = oldPathMap || Object.create(null);
1101
+ // $flow-disable-line
1070
1102
  var nameMap = oldNameMap || Object.create(null);
1071
1103
 
1072
1104
  routes.forEach(function (route) {
@@ -1108,8 +1140,12 @@ function addRouteRecord (
1108
1140
  );
1109
1141
  }
1110
1142
 
1111
- var normalizedPath = normalizePath(path, parent);
1112
1143
  var pathToRegexpOptions = route.pathToRegexpOptions || {};
1144
+ var normalizedPath = normalizePath(
1145
+ path,
1146
+ parent,
1147
+ pathToRegexpOptions.strict
1148
+ );
1113
1149
 
1114
1150
  if (typeof route.caseSensitive === 'boolean') {
1115
1151
  pathToRegexpOptions.sensitive = route.caseSensitive;
@@ -1197,9 +1233,9 @@ function addRouteRecord (
1197
1233
  }
1198
1234
 
1199
1235
  function compileRouteRegex (path, pathToRegexpOptions) {
1200
- var regex = index(path, [], pathToRegexpOptions);
1236
+ var regex = pathToRegexp_1(path, [], pathToRegexpOptions);
1201
1237
  if (process.env.NODE_ENV !== 'production') {
1202
- var keys = {};
1238
+ var keys = Object.create(null);
1203
1239
  regex.keys.forEach(function (key) {
1204
1240
  warn(!keys[key.name], ("Duplicate param keys in route with path: \"" + path + "\""));
1205
1241
  keys[key.name] = true;
@@ -1208,8 +1244,8 @@ function compileRouteRegex (path, pathToRegexpOptions) {
1208
1244
  return regex
1209
1245
  }
1210
1246
 
1211
- function normalizePath (path, parent) {
1212
- path = path.replace(/\/$/, '');
1247
+ function normalizePath (path, parent, strict) {
1248
+ if (!strict) { path = path.replace(/\/$/, ''); }
1213
1249
  if (path[0] === '/') { return path }
1214
1250
  if (parent == null) { return path }
1215
1251
  return cleanPath(((parent.path) + "/" + path))
@@ -1481,6 +1517,8 @@ function resolveRecordPath (path, record) {
1481
1517
  var positionStore = Object.create(null);
1482
1518
 
1483
1519
  function setupScroll () {
1520
+ // Fix for #1585 for Firefox
1521
+ window.history.replaceState({ key: getStateKey() }, '');
1484
1522
  window.addEventListener('popstate', function (e) {
1485
1523
  saveScrollPosition();
1486
1524
  if (e.state && e.state.key) {
@@ -1512,25 +1550,21 @@ function handleScroll (
1512
1550
  router.app.$nextTick(function () {
1513
1551
  var position = getScrollPosition();
1514
1552
  var shouldScroll = behavior(to, from, isPop ? position : null);
1553
+
1515
1554
  if (!shouldScroll) {
1516
1555
  return
1517
1556
  }
1518
- var isObject = typeof shouldScroll === 'object';
1519
- if (isObject && typeof shouldScroll.selector === 'string') {
1520
- var el = document.querySelector(shouldScroll.selector);
1521
- if (el) {
1522
- var offset = shouldScroll.offset && typeof shouldScroll.offset === 'object' ? shouldScroll.offset : {};
1523
- offset = normalizeOffset(offset);
1524
- position = getElementPosition(el, offset);
1525
- } else if (isValidPosition(shouldScroll)) {
1526
- position = normalizePosition(shouldScroll);
1527
- }
1528
- } else if (isObject && isValidPosition(shouldScroll)) {
1529
- position = normalizePosition(shouldScroll);
1530
- }
1531
1557
 
1532
- if (position) {
1533
- window.scrollTo(position.x, position.y);
1558
+ if (typeof shouldScroll.then === 'function') {
1559
+ shouldScroll.then(function (shouldScroll) {
1560
+ scrollToPosition((shouldScroll), position);
1561
+ }).catch(function (err) {
1562
+ if (process.env.NODE_ENV !== 'production') {
1563
+ assert(false, err.toString());
1564
+ }
1565
+ });
1566
+ } else {
1567
+ scrollToPosition(shouldScroll, position);
1534
1568
  }
1535
1569
  });
1536
1570
  }
@@ -1584,6 +1618,26 @@ function isNumber (v) {
1584
1618
  return typeof v === 'number'
1585
1619
  }
1586
1620
 
1621
+ function scrollToPosition (shouldScroll, position) {
1622
+ var isObject = typeof shouldScroll === 'object';
1623
+ if (isObject && typeof shouldScroll.selector === 'string') {
1624
+ var el = document.querySelector(shouldScroll.selector);
1625
+ if (el) {
1626
+ var offset = shouldScroll.offset && typeof shouldScroll.offset === 'object' ? shouldScroll.offset : {};
1627
+ offset = normalizeOffset(offset);
1628
+ position = getElementPosition(el, offset);
1629
+ } else if (isValidPosition(shouldScroll)) {
1630
+ position = normalizePosition(shouldScroll);
1631
+ }
1632
+ } else if (isObject && isValidPosition(shouldScroll)) {
1633
+ position = normalizePosition(shouldScroll);
1634
+ }
1635
+
1636
+ if (position) {
1637
+ window.scrollTo(position.x, position.y);
1638
+ }
1639
+ }
1640
+
1587
1641
  /* */
1588
1642
 
1589
1643
  var supportsPushState = inBrowser && (function () {
@@ -1679,7 +1733,7 @@ function resolveAsyncComponents (matched) {
1679
1733
  pending++;
1680
1734
 
1681
1735
  var resolve = once(function (resolvedDef) {
1682
- if (resolvedDef.__esModule && resolvedDef.default) {
1736
+ if (isESModule(resolvedDef)) {
1683
1737
  resolvedDef = resolvedDef.default;
1684
1738
  }
1685
1739
  // save resolved on async factory in case it's used elsewhere
@@ -1714,7 +1768,7 @@ function resolveAsyncComponents (matched) {
1714
1768
  if (typeof res.then === 'function') {
1715
1769
  res.then(resolve, reject);
1716
1770
  } else {
1717
- // new syntax in Kdu
1771
+ // new syntax in Kdu 2.3
1718
1772
  var comp = res.component;
1719
1773
  if (comp && typeof comp.then === 'function') {
1720
1774
  comp.then(resolve, reject);
@@ -1745,6 +1799,14 @@ function flatten (arr) {
1745
1799
  return Array.prototype.concat.apply([], arr)
1746
1800
  }
1747
1801
 
1802
+ var hasSymbol =
1803
+ typeof Symbol === 'function' &&
1804
+ typeof Symbol.toStringTag === 'symbol';
1805
+
1806
+ function isESModule (obj) {
1807
+ return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
1808
+ }
1809
+
1748
1810
  // in Webpack 2, require.ensure now also returns a Promise
1749
1811
  // so the resolve/reject functions may get called an extra time
1750
1812
  // if the user uses an arrow function shorthand that happens to
@@ -2073,9 +2135,18 @@ var HTML5History = (function (History$$1) {
2073
2135
  setupScroll();
2074
2136
  }
2075
2137
 
2138
+ var initLocation = getLocation(this.base);
2076
2139
  window.addEventListener('popstate', function (e) {
2077
2140
  var current = this$1.current;
2078
- this$1.transitionTo(getLocation(this$1.base), function (route) {
2141
+
2142
+ // Avoiding first `popstate` event dispatched in some browsers but first
2143
+ // history route not updated since async guard at the same time.
2144
+ var location = getLocation(this$1.base);
2145
+ if (this$1.current === START && location === initLocation) {
2146
+ return
2147
+ }
2148
+
2149
+ this$1.transitionTo(location, function (route) {
2079
2150
  if (expectScroll) {
2080
2151
  handleScroll(router, route, current, true);
2081
2152
  }
@@ -2159,26 +2230,50 @@ var HashHistory = (function (History$$1) {
2159
2230
  HashHistory.prototype.setupListeners = function setupListeners () {
2160
2231
  var this$1 = this;
2161
2232
 
2162
- window.addEventListener('hashchange', function () {
2233
+ var router = this.router;
2234
+ var expectScroll = router.options.scrollBehavior;
2235
+ var supportsScroll = supportsPushState && expectScroll;
2236
+
2237
+ if (supportsScroll) {
2238
+ setupScroll();
2239
+ }
2240
+
2241
+ window.addEventListener(supportsPushState ? 'popstate' : 'hashchange', function () {
2242
+ var current = this$1.current;
2163
2243
  if (!ensureSlash()) {
2164
2244
  return
2165
2245
  }
2166
2246
  this$1.transitionTo(getHash(), function (route) {
2167
- replaceHash(route.fullPath);
2247
+ if (supportsScroll) {
2248
+ handleScroll(this$1.router, route, current, true);
2249
+ }
2250
+ if (!supportsPushState) {
2251
+ replaceHash(route.fullPath);
2252
+ }
2168
2253
  });
2169
2254
  });
2170
2255
  };
2171
2256
 
2172
2257
  HashHistory.prototype.push = function push (location, onComplete, onAbort) {
2258
+ var this$1 = this;
2259
+
2260
+ var ref = this;
2261
+ var fromRoute = ref.current;
2173
2262
  this.transitionTo(location, function (route) {
2174
2263
  pushHash(route.fullPath);
2264
+ handleScroll(this$1.router, route, fromRoute, false);
2175
2265
  onComplete && onComplete(route);
2176
2266
  }, onAbort);
2177
2267
  };
2178
2268
 
2179
2269
  HashHistory.prototype.replace = function replace (location, onComplete, onAbort) {
2270
+ var this$1 = this;
2271
+
2272
+ var ref = this;
2273
+ var fromRoute = ref.current;
2180
2274
  this.transitionTo(location, function (route) {
2181
2275
  replaceHash(route.fullPath);
2276
+ handleScroll(this$1.router, route, fromRoute, false);
2182
2277
  onComplete && onComplete(route);
2183
2278
  }, onAbort);
2184
2279
  };
@@ -2228,15 +2323,27 @@ function getHash () {
2228
2323
  return index === -1 ? '' : href.slice(index + 1)
2229
2324
  }
2230
2325
 
2326
+ function getUrl (path) {
2327
+ var href = window.location.href;
2328
+ var i = href.indexOf('#');
2329
+ var base = i >= 0 ? href.slice(0, i) : href;
2330
+ return (base + "#" + path)
2331
+ }
2332
+
2231
2333
  function pushHash (path) {
2232
- window.location.hash = path;
2334
+ if (supportsPushState) {
2335
+ pushState(getUrl(path));
2336
+ } else {
2337
+ window.location.hash = path;
2338
+ }
2233
2339
  }
2234
2340
 
2235
2341
  function replaceHash (path) {
2236
- var href = window.location.href;
2237
- var i = href.indexOf('#');
2238
- var base = i >= 0 ? href.slice(0, i) : href;
2239
- window.location.replace((base + "#" + path));
2342
+ if (supportsPushState) {
2343
+ replaceState(getUrl(path));
2344
+ } else {
2345
+ window.location.replace(getUrl(path));
2346
+ }
2240
2347
  }
2241
2348
 
2242
2349
  /* */
@@ -2338,7 +2445,7 @@ var KduRouter = function KduRouter (options) {
2338
2445
  }
2339
2446
  };
2340
2447
 
2341
- var prototypeAccessors = { currentRoute: {} };
2448
+ var prototypeAccessors = { currentRoute: { configurable: true } };
2342
2449
 
2343
2450
  KduRouter.prototype.match = function match (
2344
2451
  raw,
@@ -2496,7 +2603,7 @@ function createHref (base, fullPath, mode) {
2496
2603
  }
2497
2604
 
2498
2605
  KduRouter.install = install;
2499
- KduRouter.version = '2.7.0';
2606
+ KduRouter.version = '3.0.0';
2500
2607
 
2501
2608
  if (inBrowser && window.Kdu) {
2502
2609
  window.Kdu.use(KduRouter);