@tarojs/router 3.4.0-beta.0 → 3.4.0-beta.1

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/dist/index.js CHANGED
@@ -3,31 +3,32 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _extends = require('@babel/runtime/helpers/esm/extends');
6
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
6
7
  var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
7
8
  var _regeneratorRuntime = require('@babel/runtime/regenerator');
9
+ var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
8
10
  var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
9
11
  var _createClass = require('@babel/runtime/helpers/createClass');
10
12
  var _defineProperty = require('@babel/runtime/helpers/defineProperty');
11
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
12
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
13
13
  var runtime = require('@tarojs/runtime');
14
14
  var _typeof = require('@babel/runtime/helpers/typeof');
15
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
15
16
  var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
16
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
17
+ var taro = require('@tarojs/taro');
17
18
 
18
19
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
20
 
20
21
  var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
22
+ var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
21
23
  var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
22
24
  var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
25
+ var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
23
26
  var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
24
27
  var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
25
28
  var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
26
- var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
27
- var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
28
29
  var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
30
+ var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
29
31
  var _createForOfIteratorHelper__default = /*#__PURE__*/_interopDefaultLegacy(_createForOfIteratorHelper);
30
- var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
31
32
 
32
33
  var r,
33
34
  B = r || (r = {});
@@ -457,10 +458,14 @@ function prependBasename() {
457
458
  var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
458
459
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
459
460
  }
460
- var hasBasename = function hasBasename(path, prefix) {
461
+ var hasBasename = function hasBasename() {
462
+ var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
463
+ var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
461
464
  return new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path);
462
465
  };
463
- var stripBasename = function stripBasename(path, prefix) {
466
+ var stripBasename = function stripBasename() {
467
+ var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
468
+ var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
464
469
  return hasBasename(path, prefix) ? path.substr(prefix.length) : path;
465
470
  };
466
471
 
@@ -502,6 +507,19 @@ var Stacks = /*#__PURE__*/function () {
502
507
  value: function getItem(index) {
503
508
  return this.stacks[index];
504
509
  }
510
+ }, {
511
+ key: "getLastIndex",
512
+ value: function getLastIndex(pathname) {
513
+ var stateWith = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
514
+
515
+ var list = _toConsumableArray__default['default'](this.stacks).reverse();
516
+
517
+ return list.findIndex(function (page, i) {
518
+ var _page$path;
519
+
520
+ return i >= stateWith && ((_page$path = page.path) === null || _page$path === void 0 ? void 0 : _page$path.replace(/\?.*/g, '')) === pathname;
521
+ });
522
+ }
505
523
  }, {
506
524
  key: "getDelta",
507
525
  value: function getDelta(pathname) {
@@ -510,17 +528,19 @@ var Stacks = /*#__PURE__*/function () {
510
528
  } // NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
511
529
 
512
530
 
513
- var prevIndex = this.stacks.findIndex(function (r) {
514
- var _r$path;
515
-
516
- return ((_r$path = r.path) === null || _r$path === void 0 ? void 0 : _r$path.replace(/\?.*/g, '')) === pathname;
517
- });
518
- return this.length - 1 - prevIndex;
531
+ return this.getLastIndex(pathname) || 1;
519
532
  }
520
533
  }, {
521
534
  key: "getPrevIndex",
522
535
  value: function getPrevIndex(pathname) {
523
- return this.length - 1 - this.getDelta(pathname);
536
+ var stateWith = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
537
+ var lastIndex = this.getLastIndex(pathname, stateWith);
538
+
539
+ if (lastIndex < 0) {
540
+ return -1;
541
+ }
542
+
543
+ return this.length - 1 - lastIndex;
524
544
  }
525
545
  }, {
526
546
  key: "pop",
@@ -539,48 +559,98 @@ var Stacks = /*#__PURE__*/function () {
539
559
 
540
560
  var stacks = new Stacks();
541
561
 
542
- var routesAlias = {};
543
- function setRoutesAlias(alias) {
544
- routesAlias = alias;
545
- }
546
562
  function addLeadingSlash(path) {
547
563
  if (path == null) {
548
564
  return '';
549
565
  }
550
566
 
551
567
  return path.charAt(0) === '/' ? path : '/' + path;
552
- } // eslint-disable-next-line @typescript-eslint/ban-types
553
- var isTabBar = function isTabBar(config) {
554
- var _Object$entries$find, _config$tabBar;
555
-
556
- var _config$router = config.router,
557
- _config$router$custom = _config$router.customRoutes,
558
- customRoutes = _config$router$custom === void 0 ? {} : _config$router$custom,
559
- _config$router$basena = _config$router.basename,
560
- basename = _config$router$basena === void 0 ? '' : _config$router$basena,
561
- pathname = _config$router.pathname;
562
- var routePath = stripBasename(pathname, basename);
563
- var pagePath = ((_Object$entries$find = Object.entries(customRoutes).find(function (_ref) {
564
- var _ref2 = _slicedToArray__default['default'](_ref, 2),
565
- target = _ref2[1];
566
-
567
- return target === routePath;
568
- })) === null || _Object$entries$find === void 0 ? void 0 : _Object$entries$find[0]) || routePath;
569
- return !!pagePath && (((_config$tabBar = config.tabBar) === null || _config$tabBar === void 0 ? void 0 : _config$tabBar.list) || []).some(function (t) {
570
- return t.pagePath === pagePath;
571
- });
572
- };
568
+ }
569
+
570
+ var RoutesAlias = /*#__PURE__*/function () {
571
+ function RoutesAlias() {
572
+ var _this = this;
573
+
574
+ _classCallCheck__default['default'](this, RoutesAlias);
575
+
576
+ _defineProperty__default['default'](this, "conf", []);
577
+
578
+ _defineProperty__default['default'](this, "getConfig", function () {
579
+ var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
580
+
581
+ var customRoute = _this.conf.filter(function (arr) {
582
+ return arr.includes(url);
583
+ });
584
+
585
+ return customRoute[0];
586
+ });
587
+
588
+ _defineProperty__default['default'](this, "getOrigin", function () {
589
+ var _this$getConfig;
590
+
591
+ var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
592
+ return ((_this$getConfig = _this.getConfig(url)) === null || _this$getConfig === void 0 ? void 0 : _this$getConfig[0]) || url;
593
+ });
594
+
595
+ _defineProperty__default['default'](this, "getAlias", function () {
596
+ var _this$getConfig2;
597
+
598
+ var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
599
+ return ((_this$getConfig2 = _this.getConfig(url)) === null || _this$getConfig2 === void 0 ? void 0 : _this$getConfig2[1]) || url;
600
+ });
601
+
602
+ _defineProperty__default['default'](this, "getAll", function () {
603
+ var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
604
+ return _this.conf.filter(function (arr) {
605
+ return arr.includes(url);
606
+ }).reduce(function (p, a) {
607
+ p.push(a[1]);
608
+ return p;
609
+ }, [url]);
610
+ });
611
+ }
612
+
613
+ _createClass__default['default'](RoutesAlias, [{
614
+ key: "set",
615
+ value: function set() {
616
+ var _this2 = this;
617
+
618
+ var customRoutes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
619
+
620
+ var _loop = function _loop(_key) {
621
+ var path = customRoutes[_key];
622
+ _key = addLeadingSlash(_key);
623
+
624
+ if (typeof path === 'string') {
625
+ _this2.conf.push([_key, addLeadingSlash(path)]);
626
+ } else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
627
+ var _this2$conf;
628
+
629
+ (_this2$conf = _this2.conf).push.apply(_this2$conf, _toConsumableArray__default['default'](path.map(function (p) {
630
+ return [_key, addLeadingSlash(p)];
631
+ })));
632
+ }
633
+
634
+ key = _key;
635
+ };
636
+
637
+ for (var key in customRoutes) {
638
+ _loop(key);
639
+ }
640
+ }
641
+ }]);
642
+
643
+ return RoutesAlias;
644
+ }();
645
+
646
+ var routesAlias = new RoutesAlias();
573
647
 
574
648
  function processNavigateUrl(option) {
575
649
  var _pathPieces$pathname;
576
650
 
577
651
  var pathPieces = J(option.url); // 处理自定义路由
578
652
 
579
- Object.keys(routesAlias).forEach(function (key) {
580
- if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
581
- pathPieces.pathname = routesAlias[key];
582
- }
583
- }); // 处理相对路径
653
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname)); // 处理相对路径
584
654
 
585
655
  if (pathPieces !== null && pathPieces !== void 0 && (_pathPieces$pathname = pathPieces.pathname) !== null && _pathPieces$pathname !== void 0 && _pathPieces$pathname.includes('./')) {
586
656
  var parts = exports.history.location.pathname.split('/');
@@ -617,9 +687,12 @@ function _navigate() {
617
687
  complete = option.complete,
618
688
  fail = option.fail;
619
689
  var unListen = exports.history.listen(function () {
620
- success === null || success === void 0 ? void 0 : success();
621
- complete === null || complete === void 0 ? void 0 : complete();
622
- resolve();
690
+ var res = {
691
+ errMsg: "".concat(method, ": ok")
692
+ };
693
+ success === null || success === void 0 ? void 0 : success(res);
694
+ complete === null || complete === void 0 ? void 0 : complete(res);
695
+ resolve(res);
623
696
  unListen();
624
697
  });
625
698
 
@@ -632,7 +705,10 @@ function _navigate() {
632
705
 
633
706
  if (method === 'navigateTo') {
634
707
  exports.history.push(pathPieces, state);
635
- } else if (method === 'redirectTo') {
708
+ } else if (method === 'redirectTo' || method === 'switchTab') {
709
+ exports.history.replace(pathPieces, state);
710
+ } else if (method === 'reLaunch') {
711
+ stacks.delta = stacks.length;
636
712
  exports.history.replace(pathPieces, state);
637
713
  }
638
714
  } else if (method === 'navigateBack') {
@@ -640,9 +716,12 @@ function _navigate() {
640
716
  exports.history.go(-option.delta);
641
717
  }
642
718
  } catch (error) {
643
- fail === null || fail === void 0 ? void 0 : fail(error);
644
- complete === null || complete === void 0 ? void 0 : complete();
645
- reject(error);
719
+ var res = {
720
+ errMsg: "".concat(method, ": fail ").concat(error.message || error)
721
+ };
722
+ fail === null || fail === void 0 ? void 0 : fail(res);
723
+ complete === null || complete === void 0 ? void 0 : complete(res);
724
+ reject(res);
646
725
  }
647
726
  }));
648
727
 
@@ -674,15 +753,18 @@ function navigateBack() {
674
753
  return navigate(options, 'navigateBack');
675
754
  }
676
755
  function switchTab(option) {
677
- // TODO: 清除掉所有的栈去目标页面
678
- return redirectTo(option);
756
+ return navigate(option, 'switchTab');
679
757
  }
680
758
  function reLaunch(option) {
681
- // TODO: 清除掉所有的栈去目标页面
682
- return redirectTo(option);
759
+ return navigate(option, 'reLaunch');
683
760
  }
684
761
  function getCurrentPages() {
685
- return stacks.get();
762
+ var pages = stacks.get();
763
+ return pages.map(function (e) {
764
+ return _objectSpread__default['default'](_objectSpread__default['default']({}, e), {}, {
765
+ route: e.path || ''
766
+ });
767
+ });
686
768
  }
687
769
 
688
770
  var pathToRegexp$2 = {exports: {}};
@@ -1315,117 +1397,6 @@ var UniversalRouter = function () {
1315
1397
 
1316
1398
  UniversalRouter.pathToRegexp = pathToRegexp$1;
1317
1399
 
1318
- var Taro = require('@tarojs/taro-h5');
1319
-
1320
- function initTabbar(config) {
1321
- if (config.tabBar == null) {
1322
- return;
1323
- } // TODO: 找到 tabbar 的类型
1324
-
1325
-
1326
- var tabbar = document.createElement('taro-tabbar');
1327
- var homePage = config.pages ? config.pages[0] : '';
1328
- tabbar.conf = config.tabBar;
1329
- tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
1330
- var routerConfig = config.router;
1331
- tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
1332
-
1333
- if (routerConfig.customRoutes) {
1334
- tabbar.conf.custom = true;
1335
- tabbar.conf.customRoutes = routerConfig.customRoutes;
1336
- } else {
1337
- tabbar.conf.custom = false;
1338
- tabbar.conf.customRoutes = {};
1339
- }
1340
-
1341
- if (typeof routerConfig.basename !== 'undefined') {
1342
- tabbar.conf.basename = routerConfig.basename;
1343
- }
1344
-
1345
- var container = document.getElementById('container'); // eslint-disable-next-line no-unused-expressions
1346
-
1347
- container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
1348
- Taro.initTabBarApis(config);
1349
- }
1350
-
1351
- var routerConfig = Object.create(null);
1352
- function init(config) {
1353
- var _document$getElementB;
1354
-
1355
- config.router.mode = config.router.mode || 'hash';
1356
- setHistoryMode(config.router.mode, config.router.basename);
1357
- Object.assign(routerConfig, config);
1358
- (_document$getElementB = document.getElementById('app')) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.remove();
1359
- var container = document.createElement('div');
1360
- container.classList.add('taro-tabbar__container');
1361
- container.id = 'container';
1362
- var panel = document.createElement('div');
1363
- panel.classList.add('taro-tabbar__panel');
1364
- var app = document.createElement('div');
1365
- app.id = 'app';
1366
- app.classList.add('taro_router');
1367
- panel.appendChild(app);
1368
- container.appendChild(panel);
1369
- document.body.appendChild(container);
1370
- initTabbar(config);
1371
- }
1372
-
1373
- var pageScrollFn;
1374
- var pageDOM = window;
1375
- function bindPageScroll(page, config) {
1376
- pageDOM.removeEventListener('scroll', pageScrollFn);
1377
- pageDOM = getScrollContainer();
1378
- var distance = config.onReachBottomDistance || 50;
1379
- var isReachBottom = false;
1380
-
1381
- pageScrollFn = function pageScrollFn() {
1382
- page.onPageScroll && page.onPageScroll({
1383
- scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
1384
- });
1385
-
1386
- if (isReachBottom && getOffset() > distance) {
1387
- isReachBottom = false;
1388
- }
1389
-
1390
- if (page.onReachBottom && !isReachBottom && getOffset() < distance) {
1391
- isReachBottom = true;
1392
- page.onReachBottom();
1393
- }
1394
- };
1395
-
1396
- pageDOM.addEventListener('scroll', pageScrollFn, false);
1397
- }
1398
- window.addEventListener('DOMSubtreeModified', function (e) {
1399
- var _e$target;
1400
-
1401
- // @ts-ignore
1402
- var className = (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.className;
1403
-
1404
- if (className && /taro-tabbar__/.test(className)) {
1405
- pageDOM.removeEventListener('scroll', pageScrollFn);
1406
- pageDOM = getScrollContainer();
1407
- pageDOM.addEventListener('scroll', pageScrollFn, false);
1408
- }
1409
- }, false);
1410
-
1411
- function getScrollContainer() {
1412
- if (document.querySelector('.taro-tabbar__tabbar') === null) {
1413
- // 没设置tabbar
1414
- return window;
1415
- } else {
1416
- // 有设置tabbar
1417
- return document.querySelector('.taro-tabbar__panel') || window;
1418
- }
1419
- }
1420
-
1421
- function getOffset() {
1422
- if (pageDOM instanceof Window) {
1423
- return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
1424
- } else {
1425
- return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
1426
- }
1427
- }
1428
-
1429
1400
  var queryString = {};
1430
1401
 
1431
1402
  var strictUriEncode = function strictUriEncode(str) {
@@ -1990,129 +1961,474 @@ var filterObj = function filterObj(obj, predicate) {
1990
1961
  };
1991
1962
  })(queryString);
1992
1963
 
1993
- function getSearch() {
1994
- var search = '?';
1964
+ var pageScrollFn;
1965
+ var pageDOM = window;
1966
+ function bindPageScroll(page, config) {
1967
+ pageDOM.removeEventListener('scroll', pageScrollFn);
1968
+ pageDOM = getScrollContainer();
1969
+ var distance = config.onReachBottomDistance || 50;
1970
+ var isReachBottom = false;
1995
1971
 
1996
- if (routerConfig.router.mode === 'hash') {
1997
- var idx = location.hash.indexOf('?');
1972
+ pageScrollFn = function pageScrollFn() {
1973
+ page.onPageScroll && page.onPageScroll({
1974
+ scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
1975
+ });
1998
1976
 
1999
- if (idx > -1) {
2000
- search = location.hash.slice(idx);
1977
+ if (isReachBottom && getOffset() > distance) {
1978
+ isReachBottom = false;
2001
1979
  }
2002
- } else {
2003
- search = location.search;
2004
- }
2005
1980
 
2006
- return search.substr(1);
2007
- }
1981
+ if (page.onReachBottom && !isReachBottom && getOffset() < distance) {
1982
+ isReachBottom = true;
1983
+ page.onReachBottom();
1984
+ }
1985
+ };
2008
1986
 
2009
- var qs = function qs() {
2010
- var stamp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
2011
- var search = getSearch();
2012
- var query = search ? queryString.parse(search) : {};
1987
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
1988
+ }
1989
+ window.addEventListener('DOMSubtreeModified', function (e) {
1990
+ var target = e.target;
1991
+ var className = target === null || target === void 0 ? void 0 : target.className;
2013
1992
 
2014
- if (stamp) {
2015
- query.stamp = stamp.toString();
1993
+ if (className && /taro_page/.test(className)) {
1994
+ pageDOM.removeEventListener('scroll', pageScrollFn);
1995
+ pageDOM = getScrollContainer();
1996
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
2016
1997
  }
1998
+ }, false);
2017
1999
 
2018
- return query;
2019
- };
2020
-
2021
- /* eslint-disable dot-notation */
2022
- function pageOnReady(page) {
2023
- var onLoad = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
2024
- var pageEl = document.getElementById(page.path);
2025
-
2026
- if (pageEl && !(pageEl !== null && pageEl !== void 0 && pageEl['__isReady'])) {
2027
- var _el$componentOnReady;
2000
+ function getScrollContainer() {
2001
+ var _Current$page;
2028
2002
 
2029
- var el = pageEl.firstElementChild;
2030
- el === null || el === void 0 ? void 0 : (_el$componentOnReady = el['componentOnReady']) === null || _el$componentOnReady === void 0 ? void 0 : _el$componentOnReady.call(el).then(function () {
2031
- runtime.requestAnimationFrame(function () {
2032
- page.onReady();
2033
- pageEl['__isReady'] = true;
2034
- });
2035
- });
2036
- onLoad && (pageEl['__page'] = page);
2037
- }
2003
+ var id = (_Current$page = runtime.Current.page) === null || _Current$page === void 0 ? void 0 : _Current$page.path;
2004
+ var el = id ? document.getElementById(id) : document.querySelector('.taro_page') || document.querySelector('.taro_router');
2005
+ return el || window;
2038
2006
  }
2039
- function loadPage(page) {
2040
- var pageConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2041
- var stacksIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2042
- if (!page) return;
2043
- var pageEl = document.getElementById(page.path);
2044
-
2045
- if (pageEl) {
2046
- pageEl.style.display = 'block';
2007
+
2008
+ function getOffset() {
2009
+ if (pageDOM instanceof Window) {
2010
+ return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
2047
2011
  } else {
2048
- page.onLoad(qs(stacksIndex), function () {
2049
- return pageOnReady(page, true);
2050
- });
2012
+ return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
2051
2013
  }
2052
-
2053
- stacks.push(page);
2054
- page.onShow();
2055
- bindPageScroll(page, pageConfig);
2056
2014
  }
2057
- function unloadPage(page) {
2058
- var delta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
2059
- if (!page) return;
2060
- stacks.delta = --delta;
2061
- stacks.pop();
2062
- page.onUnload();
2063
- if (delta >= 1) unloadPage(stacks.last, delta);
2015
+
2016
+ /**
2017
+ * 插入页面动画需要的样式
2018
+ */
2019
+ function loadAnimateStyle() {
2020
+ var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 300;
2021
+ var css = "\n.taro_router .taro_page {\n position: absolute;\n left: 0;\n top: 0;\n width: 100%;\n height: 100%;\n transform: translate3d(100%, 0, 0);\n transition: transform ".concat(ms, "ms;\n}\n\n.taro_router .taro_page.taro_tabbar_page {\n transform: none;\n}\n\n.taro_router .taro_page.taro_page_show {\n transform: translate3d(0, 0, 0);\n}");
2022
+ var style = document.createElement('style');
2023
+ style.innerHTML = css;
2024
+ document.getElementsByTagName('head')[0].appendChild(style);
2064
2025
  }
2065
- function showPage(page) {
2066
- var pageConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2067
- var stacksIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2068
- if (!page) return;
2069
- page.onShow();
2070
- var pageEl = document.getElementById(page.path);
2071
-
2072
- if (pageEl) {
2073
- pageEl.style.display = 'block';
2026
+
2027
+ // @ts-nocheck
2028
+ function initTabbar(config) {
2029
+ if (config.tabBar == null) {
2030
+ return;
2031
+ } // TODO: custom-tab-bar
2032
+
2033
+
2034
+ var tabbar = document.createElement('taro-tabbar');
2035
+ var homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
2036
+ tabbar.conf = config.tabBar;
2037
+ tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
2038
+ var routerConfig = config.router;
2039
+ tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
2040
+
2041
+ if (routerConfig.customRoutes) {
2042
+ tabbar.conf.custom = true;
2043
+ tabbar.conf.customRoutes = routerConfig.customRoutes;
2074
2044
  } else {
2075
- page.onLoad(qs(stacksIndex), function () {
2076
- return pageOnReady(page, false);
2077
- });
2045
+ tabbar.conf.custom = false;
2046
+ tabbar.conf.customRoutes = {};
2047
+ }
2048
+
2049
+ if (typeof routerConfig.basename !== 'undefined') {
2050
+ tabbar.conf.basename = routerConfig.basename;
2078
2051
  }
2079
2052
 
2080
- bindPageScroll(page, pageConfig);
2053
+ var container = document.getElementById('container');
2054
+ container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
2055
+ taro.initTabBarApis(config);
2081
2056
  }
2082
- function hidePage(page) {
2083
- if (!page) return; // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
2084
2057
 
2085
- var pageEl = document.getElementById(page.path);
2058
+ function setDisplay(el) {
2059
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
2086
2060
 
2087
- if (pageEl) {
2088
- pageEl.style.display = 'none';
2089
- page.onHide();
2090
- } else {
2091
- setTimeout(function () {
2092
- return hidePage(page);
2093
- }, 0);
2061
+ if (el) {
2062
+ el.style.display = type;
2094
2063
  }
2095
2064
  }
2096
2065
 
2097
- function createRouter(app, config, framework) {
2098
- var _config$router$custom;
2066
+ var PageHandler = /*#__PURE__*/function () {
2067
+ function PageHandler(config) {
2068
+ _classCallCheck__default['default'](this, PageHandler);
2099
2069
 
2100
- init(config);
2101
- var routes = [];
2102
- var alias = (_config$router$custom = config.router.customRoutes) !== null && _config$router$custom !== void 0 ? _config$router$custom : {};
2103
- var runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
2104
- setRoutesAlias(alias);
2070
+ _defineProperty__default['default'](this, "config", void 0);
2105
2071
 
2106
- for (var i = 0; i < config.routes.length; i++) {
2107
- var route = config.routes[i];
2108
- var path = addLeadingSlash(route.path);
2109
- routes.push({
2110
- path: alias[path] || path,
2111
- action: route.load
2072
+ _defineProperty__default['default'](this, "defaultAnimation", {
2073
+ duration: 300,
2074
+ delay: 50
2112
2075
  });
2076
+
2077
+ _defineProperty__default['default'](this, "hideTimer", void 0);
2078
+
2079
+ _defineProperty__default['default'](this, "unloadTimer", void 0);
2080
+
2081
+ _defineProperty__default['default'](this, "lastHidePage", void 0);
2082
+
2083
+ _defineProperty__default['default'](this, "lastUnloadPage", void 0);
2084
+
2085
+ this.config = config;
2086
+ this.mount();
2113
2087
  }
2114
2088
 
2115
- var basename = config.router.basename;
2089
+ _createClass__default['default'](PageHandler, [{
2090
+ key: "appId",
2091
+ get: function get() {
2092
+ return 'app';
2093
+ }
2094
+ }, {
2095
+ key: "router",
2096
+ get: function get() {
2097
+ return this.config.router;
2098
+ }
2099
+ }, {
2100
+ key: "routerMode",
2101
+ get: function get() {
2102
+ return this.router.mode || 'hash';
2103
+ }
2104
+ }, {
2105
+ key: "customRoutes",
2106
+ get: function get() {
2107
+ return this.router.customRoutes || {};
2108
+ }
2109
+ }, {
2110
+ key: "routes",
2111
+ get: function get() {
2112
+ return this.config.routes;
2113
+ }
2114
+ }, {
2115
+ key: "tabBarList",
2116
+ get: function get() {
2117
+ var _this$config$tabBar;
2118
+
2119
+ return ((_this$config$tabBar = this.config.tabBar) === null || _this$config$tabBar === void 0 ? void 0 : _this$config$tabBar.list) || [];
2120
+ }
2121
+ }, {
2122
+ key: "PullDownRefresh",
2123
+ get: function get() {
2124
+ return this.config.PullDownRefresh;
2125
+ }
2126
+ }, {
2127
+ key: "animation",
2128
+ get: function get() {
2129
+ var _this$config$animatio, _this$config;
2130
+
2131
+ return (_this$config$animatio = (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.animation) !== null && _this$config$animatio !== void 0 ? _this$config$animatio : this.defaultAnimation;
2132
+ }
2133
+ }, {
2134
+ key: "animationDelay",
2135
+ get: function get() {
2136
+ var _this$defaultAnimatio;
2137
+
2138
+ return _typeof__default['default'](this.animation) === 'object' ? this.animation.delay : this.animation ? (_this$defaultAnimatio = this.defaultAnimation) === null || _this$defaultAnimatio === void 0 ? void 0 : _this$defaultAnimatio.delay : 0;
2139
+ }
2140
+ }, {
2141
+ key: "animationDuration",
2142
+ get: function get() {
2143
+ var _this$defaultAnimatio2;
2144
+
2145
+ return _typeof__default['default'](this.animation) === 'object' ? this.animation.duration : this.animation ? (_this$defaultAnimatio2 = this.defaultAnimation) === null || _this$defaultAnimatio2 === void 0 ? void 0 : _this$defaultAnimatio2.duration : 0;
2146
+ }
2147
+ }, {
2148
+ key: "pathname",
2149
+ get: function get() {
2150
+ return this.router.pathname;
2151
+ },
2152
+ set: function set(p) {
2153
+ this.router.pathname = p;
2154
+ }
2155
+ }, {
2156
+ key: "basename",
2157
+ get: function get() {
2158
+ return this.router.basename || '';
2159
+ }
2160
+ }, {
2161
+ key: "pageConfig",
2162
+ get: function get() {
2163
+ var _this = this;
2164
+
2165
+ return this.routes.find(function (r) {
2166
+ var _routesAlias$getConfi;
2167
+
2168
+ var routePath = stripBasename(_this.pathname, _this.basename);
2169
+ var pagePath = addLeadingSlash(r.path);
2170
+ return pagePath === routePath || ((_routesAlias$getConfi = routesAlias.getConfig(pagePath)) === null || _routesAlias$getConfi === void 0 ? void 0 : _routesAlias$getConfi.includes(routePath));
2171
+ });
2172
+ }
2173
+ }, {
2174
+ key: "isTabBar",
2175
+ get: function get() {
2176
+ var _Object$entries$find;
2177
+
2178
+ var routePath = stripBasename(this.pathname, this.basename);
2179
+ var pagePath = ((_Object$entries$find = Object.entries(this.customRoutes).find(function (_ref) {
2180
+ var _ref2 = _slicedToArray__default['default'](_ref, 2),
2181
+ target = _ref2[1];
2182
+
2183
+ if (typeof target === 'string') {
2184
+ return target === routePath;
2185
+ } else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
2186
+ return target.includes(routePath);
2187
+ }
2188
+
2189
+ return false;
2190
+ })) === null || _Object$entries$find === void 0 ? void 0 : _Object$entries$find[0]) || routePath;
2191
+ return !!pagePath && this.tabBarList.some(function (t) {
2192
+ return t.pagePath === pagePath;
2193
+ });
2194
+ }
2195
+ }, {
2196
+ key: "isSamePage",
2197
+ value: function isSamePage(page) {
2198
+ var routePath = stripBasename(this.pathname, this.basename);
2199
+ var pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
2200
+ return pagePath.startsWith(routePath + '?');
2201
+ }
2202
+ }, {
2203
+ key: "search",
2204
+ get: function get() {
2205
+ var search = '?';
2206
+
2207
+ if (this.routerMode) {
2208
+ var idx = location.hash.indexOf('?');
2209
+
2210
+ if (idx > -1) {
2211
+ search = location.hash.slice(idx);
2212
+ }
2213
+ } else {
2214
+ search = location.search;
2215
+ }
2216
+
2217
+ return search.substr(1);
2218
+ }
2219
+ }, {
2220
+ key: "getQuery",
2221
+ value: function getQuery() {
2222
+ var stamp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
2223
+ var search = this.search;
2224
+ var query = search ? queryString.parse(search) : {};
2225
+
2226
+ if (stamp) {
2227
+ query.stamp = stamp.toString();
2228
+ }
2229
+
2230
+ return query;
2231
+ }
2232
+ }, {
2233
+ key: "mount",
2234
+ value: function mount() {
2235
+ var _document$getElementB;
2236
+
2237
+ setHistoryMode(this.routerMode, this.router.basename);
2238
+ (_document$getElementB = document.getElementById('app')) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.remove();
2239
+ this.animation && loadAnimateStyle(this.animationDuration);
2240
+ var app = document.createElement('div');
2241
+ app.id = this.appId;
2242
+ app.classList.add('taro_router');
2243
+
2244
+ if (this.tabBarList.length > 1) {
2245
+ var container = document.createElement('div');
2246
+ container.classList.add('taro-tabbar__container');
2247
+ container.id = 'container';
2248
+ var panel = document.createElement('div');
2249
+ panel.classList.add('taro-tabbar__panel');
2250
+ panel.appendChild(app);
2251
+ container.appendChild(panel);
2252
+ document.body.appendChild(container);
2253
+ initTabbar(this.config);
2254
+ } else {
2255
+ document.body.appendChild(app);
2256
+ }
2257
+ }
2258
+ }, {
2259
+ key: "onReady",
2260
+ value: function onReady(page) {
2261
+ var onLoad = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
2262
+ var pageEl = document.getElementById(page.path);
2263
+
2264
+ if (pageEl && !(pageEl !== null && pageEl !== void 0 && pageEl['__isReady'])) {
2265
+ var _el$componentOnReady, _el$componentOnReady$;
2266
+
2267
+ var el = pageEl.firstElementChild;
2268
+ el === null || el === void 0 ? void 0 : (_el$componentOnReady = el['componentOnReady']) === null || _el$componentOnReady === void 0 ? void 0 : (_el$componentOnReady$ = _el$componentOnReady.call(el)) === null || _el$componentOnReady$ === void 0 ? void 0 : _el$componentOnReady$.then(function () {
2269
+ runtime.requestAnimationFrame(function () {
2270
+ page.onReady();
2271
+ pageEl['__isReady'] = true;
2272
+ });
2273
+ });
2274
+ onLoad && (pageEl['__page'] = page);
2275
+ }
2276
+ }
2277
+ }, {
2278
+ key: "load",
2279
+ value: function load(page) {
2280
+ var _this2 = this;
2281
+
2282
+ var pageConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2283
+ var stacksIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2284
+ if (!page) return;
2285
+ var pageEl = document.getElementById(page.path);
2286
+
2287
+ if (pageEl) {
2288
+ setDisplay(pageEl);
2289
+ this.isTabBar && pageEl.classList.add('taro_tabbar_page');
2290
+ this.addAnimation(pageEl, stacksIndex === 0);
2291
+ } else {
2292
+ var _page$onLoad;
2293
+
2294
+ (_page$onLoad = page.onLoad) === null || _page$onLoad === void 0 ? void 0 : _page$onLoad.call(page, this.getQuery(stacksIndex), function () {
2295
+ var _pageEl;
2296
+
2297
+ pageEl = document.getElementById(page.path);
2298
+ _this2.isTabBar && ((_pageEl = pageEl) === null || _pageEl === void 0 ? void 0 : _pageEl.classList.add('taro_tabbar_page'));
2299
+
2300
+ _this2.addAnimation(pageEl, stacksIndex === 0);
2301
+
2302
+ _this2.onReady(page, true);
2303
+ });
2304
+ }
2305
+
2306
+ stacks.push(page);
2307
+ page.onShow();
2308
+ bindPageScroll(page, pageConfig);
2309
+ }
2310
+ }, {
2311
+ key: "unload",
2312
+ value: function unload(page) {
2313
+ var _this3 = this;
2314
+
2315
+ var delta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
2316
+ var top = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
2317
+ if (!page) return;
2318
+ stacks.delta = --delta;
2319
+ stacks.pop();
2320
+
2321
+ if (this.animation && top) {
2322
+ if (this.unloadTimer) {
2323
+ var _this$lastUnloadPage, _this$lastUnloadPage$;
2324
+
2325
+ clearTimeout(this.unloadTimer);
2326
+ (_this$lastUnloadPage = this.lastUnloadPage) === null || _this$lastUnloadPage === void 0 ? void 0 : (_this$lastUnloadPage$ = _this$lastUnloadPage.onUnload) === null || _this$lastUnloadPage$ === void 0 ? void 0 : _this$lastUnloadPage$.call(_this$lastUnloadPage);
2327
+ this.unloadTimer = null;
2328
+ }
2329
+
2330
+ this.lastUnloadPage = page;
2331
+ var pageEl = document.getElementById(page.path);
2332
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
2333
+ this.unloadTimer = setTimeout(function () {
2334
+ var _this3$lastUnloadPage, _this3$lastUnloadPage2;
2335
+
2336
+ _this3.unloadTimer = null;
2337
+ (_this3$lastUnloadPage = _this3.lastUnloadPage) === null || _this3$lastUnloadPage === void 0 ? void 0 : (_this3$lastUnloadPage2 = _this3$lastUnloadPage.onUnload) === null || _this3$lastUnloadPage2 === void 0 ? void 0 : _this3$lastUnloadPage2.call(_this3$lastUnloadPage);
2338
+ }, this.animationDuration);
2339
+ }
2340
+
2341
+ if (delta >= 1) this.unload(stacks.last, delta);
2342
+ }
2343
+ }, {
2344
+ key: "show",
2345
+ value: function show(page) {
2346
+ var _this4 = this;
2347
+
2348
+ var pageConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2349
+ var stacksIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2350
+ if (!page) return;
2351
+ page.onShow();
2352
+ var pageEl = document.getElementById(page.path);
2353
+
2354
+ if (pageEl) {
2355
+ setDisplay(pageEl);
2356
+ this.addAnimation(pageEl, stacksIndex === 0);
2357
+ } else {
2358
+ var _page$onLoad2;
2359
+
2360
+ (_page$onLoad2 = page.onLoad) === null || _page$onLoad2 === void 0 ? void 0 : _page$onLoad2.call(page, this.getQuery(stacksIndex), function () {
2361
+ pageEl = document.getElementById(page.path);
2362
+
2363
+ _this4.addAnimation(pageEl, stacksIndex === 0);
2364
+
2365
+ _this4.onReady(page, false);
2366
+ });
2367
+ }
2368
+
2369
+ bindPageScroll(page, pageConfig);
2370
+ }
2371
+ }, {
2372
+ key: "hide",
2373
+ value: function hide(page) {
2374
+ var _this5 = this;
2375
+
2376
+ if (!page) return; // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
2377
+
2378
+ var pageEl = document.getElementById(page.path);
2379
+
2380
+ if (pageEl) {
2381
+ if (this.hideTimer) {
2382
+ clearTimeout(this.hideTimer);
2383
+ this.hideTimer = null;
2384
+ setDisplay(this.lastHidePage, 'none');
2385
+ }
2386
+
2387
+ this.lastHidePage = pageEl;
2388
+ this.hideTimer = setTimeout(function () {
2389
+ _this5.hideTimer = null;
2390
+ setDisplay(_this5.lastHidePage, 'none');
2391
+ }, this.animationDelay);
2392
+ page.onHide();
2393
+ } else {
2394
+ setTimeout(function () {
2395
+ return _this5.hide(page);
2396
+ }, 0);
2397
+ }
2398
+ }
2399
+ }, {
2400
+ key: "addAnimation",
2401
+ value: function addAnimation(pageEl) {
2402
+ var first = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2403
+ if (!pageEl) return;
2404
+
2405
+ if (this.animation && !first) {
2406
+ setTimeout(function () {
2407
+ pageEl.classList.add('taro_page_show');
2408
+ }, this.animationDelay);
2409
+ } else {
2410
+ pageEl.classList.add('taro_page_show');
2411
+ }
2412
+ }
2413
+ }]);
2414
+
2415
+ return PageHandler;
2416
+ }();
2417
+
2418
+ function createRouter(app, config, framework) {
2419
+ var _routes$0$path;
2420
+
2421
+ var handler = new PageHandler(config);
2422
+ var runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
2423
+ routesAlias.set(handler.router.customRoutes);
2424
+ var basename = handler.router.basename;
2425
+ var routes = handler.routes.map(function (route) {
2426
+ return {
2427
+ path: routesAlias.getAll(addLeadingSlash(route.path)),
2428
+ action: route.load
2429
+ };
2430
+ });
2431
+ var entryPagePath = config.entryPagePath || ((_routes$0$path = routes[0].path) === null || _routes$0$path === void 0 ? void 0 : _routes$0$path[0]);
2116
2432
  var router = new UniversalRouter(routes, {
2117
2433
  baseUrl: basename || ''
2118
2434
  });
@@ -2120,17 +2436,17 @@ function createRouter(app, config, framework) {
2120
2436
 
2121
2437
  var render = /*#__PURE__*/function () {
2122
2438
  var _ref2 = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee(_ref) {
2123
- var location, action, element, _app$onPageNotFound, pageConfig, enablePullDownRefresh, _pageConfig$navigatio, currentPage, pathname, shouldLoad, prevIndex, delta, _prevIndex, _element$default, _runtimeHooks$createP, el, loadConfig, stacksIndex, page;
2439
+ var location, action, element, _app$onPageNotFound, pageConfig, enablePullDownRefresh, _pageConfig$navigatio, currentPage, pathname, shouldLoad, prevIndex, delta, _prevIndex, _delta, _element$default, _runtimeHooks$createP, el, loadConfig, stacksIndex, page;
2124
2440
 
2125
2441
  return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
2126
2442
  while (1) {
2127
2443
  switch (_context.prev = _context.next) {
2128
2444
  case 0:
2129
2445
  location = _ref.location, action = _ref.action;
2130
- routerConfig.router.pathname = location.pathname;
2446
+ handler.pathname = location.pathname;
2131
2447
  _context.prev = 2;
2132
2448
  _context.next = 5;
2133
- return router.resolve(config.router.forcePath || location.pathname);
2449
+ return router.resolve(handler.router.forcePath || handler.pathname);
2134
2450
 
2135
2451
  case 5:
2136
2452
  element = _context.sent;
@@ -2147,7 +2463,7 @@ function createRouter(app, config, framework) {
2147
2463
  }
2148
2464
 
2149
2465
  (_app$onPageNotFound = app.onPageNotFound) === null || _app$onPageNotFound === void 0 ? void 0 : _app$onPageNotFound.call(app, {
2150
- path: location.pathname
2466
+ path: handler.pathname
2151
2467
  });
2152
2468
  _context.next = 15;
2153
2469
  break;
@@ -2164,14 +2480,11 @@ function createRouter(app, config, framework) {
2164
2480
  return _context.abrupt("return");
2165
2481
 
2166
2482
  case 17:
2167
- pageConfig = config.routes.find(function (r) {
2168
- var path = addLeadingSlash(r.path);
2169
- return path === location.pathname || alias[path] === location.pathname;
2170
- });
2483
+ pageConfig = handler.pageConfig;
2171
2484
  enablePullDownRefresh = false;
2172
2485
  runtime.eventCenter.trigger('__taroRouterChange', {
2173
2486
  toLocation: {
2174
- path: location.pathname
2487
+ path: handler.pathname
2175
2488
  }
2176
2489
  });
2177
2490
 
@@ -2181,7 +2494,7 @@ function createRouter(app, config, framework) {
2181
2494
  }
2182
2495
 
2183
2496
  currentPage = runtime.Current.page;
2184
- pathname = location.pathname;
2497
+ pathname = handler.pathname;
2185
2498
  shouldLoad = false;
2186
2499
 
2187
2500
  if (!(action === 'POP')) {
@@ -2192,62 +2505,60 @@ function createRouter(app, config, framework) {
2192
2505
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
2193
2506
  prevIndex = stacks.getPrevIndex(pathname);
2194
2507
  delta = stacks.getDelta(pathname);
2195
- unloadPage(currentPage, delta);
2508
+ handler.unload(currentPage, delta, prevIndex > -1);
2196
2509
 
2197
2510
  if (prevIndex > -1) {
2198
- showPage(stacks.getItem(prevIndex), pageConfig, prevIndex);
2511
+ handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
2199
2512
  } else {
2200
2513
  shouldLoad = true;
2201
2514
  }
2202
2515
 
2203
- _context.next = 46;
2516
+ _context.next = 42;
2204
2517
  break;
2205
2518
 
2206
2519
  case 31:
2207
- if (!(action === 'PUSH')) {
2208
- _context.next = 36;
2520
+ if (!handler.isTabBar) {
2521
+ _context.next = 40;
2209
2522
  break;
2210
2523
  }
2211
2524
 
2212
- hidePage(currentPage);
2213
- shouldLoad = true;
2214
- _context.next = 46;
2215
- break;
2216
-
2217
- case 36:
2218
- if (!(action === 'REPLACE')) {
2219
- _context.next = 46;
2525
+ if (!handler.isSamePage(currentPage)) {
2526
+ _context.next = 34;
2220
2527
  break;
2221
2528
  }
2222
2529
 
2223
- if (!isTabBar(config)) {
2224
- _context.next = 44;
2225
- break;
2226
- }
2530
+ return _context.abrupt("return");
2227
2531
 
2228
- hidePage(currentPage);
2229
- _prevIndex = stacks.getPrevIndex(pathname);
2532
+ case 34:
2533
+ _prevIndex = stacks.getPrevIndex(pathname, 0);
2534
+ handler.hide(currentPage);
2230
2535
 
2231
2536
  if (!(_prevIndex > -1)) {
2232
- _context.next = 42;
2537
+ _context.next = 38;
2233
2538
  break;
2234
2539
  }
2235
2540
 
2236
- return _context.abrupt("return", showPage(stacks.getItem(_prevIndex), pageConfig, _prevIndex));
2541
+ return _context.abrupt("return", handler.show(stacks.getItem(_prevIndex), pageConfig, _prevIndex));
2237
2542
 
2238
- case 42:
2239
- _context.next = 45;
2543
+ case 38:
2544
+ _context.next = 41;
2240
2545
  break;
2241
2546
 
2242
- case 44:
2243
- unloadPage(currentPage);
2547
+ case 40:
2548
+ if (action === 'REPLACE') {
2549
+ _delta = stacks.getDelta(pathname); // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
2244
2550
 
2245
- case 45:
2551
+ handler.unload(currentPage, _delta);
2552
+ } else if (action === 'PUSH') {
2553
+ handler.hide(currentPage);
2554
+ }
2555
+
2556
+ case 41:
2246
2557
  shouldLoad = true;
2247
2558
 
2248
- case 46:
2249
- if (!shouldLoad) {
2250
- _context.next = 54;
2559
+ case 42:
2560
+ if (!(shouldLoad || stacks.length < 1)) {
2561
+ _context.next = 50;
2251
2562
  break;
2252
2563
  }
2253
2564
 
@@ -2256,10 +2567,10 @@ function createRouter(app, config, framework) {
2256
2567
  stacksIndex = stacks.length;
2257
2568
  delete loadConfig['path'];
2258
2569
  delete loadConfig['load'];
2259
- page = runtime.createPageConfig(enablePullDownRefresh ? (_runtimeHooks$createP = runtimeHooks.createPullDownComponent) === null || _runtimeHooks$createP === void 0 ? void 0 : _runtimeHooks$createP.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname + runtime.stringify(qs(stacksIndex)), {}, loadConfig);
2260
- return _context.abrupt("return", loadPage(page, pageConfig, stacksIndex));
2570
+ page = runtime.createPageConfig(enablePullDownRefresh ? (_runtimeHooks$createP = runtimeHooks.createPullDownComponent) === null || _runtimeHooks$createP === void 0 ? void 0 : _runtimeHooks$createP.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
2571
+ return _context.abrupt("return", handler.load(page, pageConfig, stacksIndex));
2261
2572
 
2262
- case 54:
2573
+ case 50:
2263
2574
  case "end":
2264
2575
  return _context.stop();
2265
2576
  }
@@ -2273,14 +2584,14 @@ function createRouter(app, config, framework) {
2273
2584
  }();
2274
2585
 
2275
2586
  if (exports.history.location.pathname === '/') {
2276
- exports.history.replace(prependBasename(routes[0].path + exports.history.location.search));
2587
+ exports.history.replace(prependBasename(entryPagePath + exports.history.location.search));
2277
2588
  }
2278
2589
 
2279
2590
  render({
2280
2591
  location: exports.history.location,
2281
2592
  action: r.Push
2282
2593
  });
2283
- app.onShow(qs(stacks.length));
2594
+ app.onShow(handler.getQuery(stacks.length));
2284
2595
  return exports.history.listen(render);
2285
2596
  }
2286
2597