@tarojs/router 3.3.15 → 3.3.19

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,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var runtime = require('@tarojs/runtime');
6
+ var taro = require('@tarojs/taro');
6
7
 
7
8
  function _extends() {
8
9
  _extends = Object.assign || function (target) {
@@ -57,8 +58,18 @@ function prependBasename(url) {
57
58
 
58
59
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
59
60
  }
60
- var hasBasename = function (path, prefix) { return new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path); };
61
- var stripBasename = function (path, prefix) { return hasBasename(path, prefix) ? path.substr(prefix.length) : path; };
61
+ var hasBasename = function (path, prefix) {
62
+ if ( path === void 0 ) path = '';
63
+ if ( prefix === void 0 ) prefix = '';
64
+
65
+ return new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path);
66
+ };
67
+ var stripBasename = function (path, prefix) {
68
+ if ( path === void 0 ) path = '';
69
+ if ( prefix === void 0 ) prefix = '';
70
+
71
+ return hasBasename(path, prefix) ? path.substr(prefix.length) : path;
72
+ };
62
73
 
63
74
  var Stacks = function Stacks() {
64
75
  this.stacks = [];
@@ -70,9 +81,12 @@ prototypeAccessors.delta.set = function (delta) {
70
81
  if (delta > 0) {
71
82
  this.backDelta = delta;
72
83
  }
73
- else {
84
+ else if (this.backDelta > 0) {
74
85
  --this.backDelta;
75
86
  }
87
+ else {
88
+ this.backDelta = 0;
89
+ }
76
90
  };
77
91
  prototypeAccessors.length.get = function () {
78
92
  return this.stacks.length;
@@ -86,16 +100,28 @@ Stacks.prototype.get = function get () {
86
100
  Stacks.prototype.getItem = function getItem (index) {
87
101
  return this.stacks[index];
88
102
  };
103
+ Stacks.prototype.getLastIndex = function getLastIndex (pathname, stateWith) {
104
+ if ( stateWith === void 0 ) stateWith = 1;
105
+
106
+ var list = [].concat( this.stacks ).reverse();
107
+ return list.findIndex(function (page, i) { var _a; return i >= stateWith && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
108
+ };
89
109
  Stacks.prototype.getDelta = function getDelta (pathname) {
90
110
  if (this.backDelta >= 1) {
91
111
  return this.backDelta;
92
112
  }
113
+ var index = this.getLastIndex(pathname);
93
114
  // NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
94
- var prevIndex = this.stacks.findIndex(function (r) { var _a; return ((_a = r.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
95
- return this.length - 1 - prevIndex;
115
+ return index > 0 ? index : 1;
96
116
  };
97
- Stacks.prototype.getPrevIndex = function getPrevIndex (pathname) {
98
- return this.length - 1 - this.getDelta(pathname);
117
+ Stacks.prototype.getPrevIndex = function getPrevIndex (pathname, stateWith) {
118
+ if ( stateWith === void 0 ) stateWith = 1;
119
+
120
+ var lastIndex = this.getLastIndex(pathname, stateWith);
121
+ if (lastIndex < 0) {
122
+ return -1;
123
+ }
124
+ return this.length - 1 - lastIndex;
99
125
  };
100
126
  Stacks.prototype.pop = function pop () {
101
127
  return this.stacks.pop();
@@ -107,40 +133,72 @@ Stacks.prototype.push = function push (page) {
107
133
  Object.defineProperties( Stacks.prototype, prototypeAccessors );
108
134
  var stacks = new Stacks();
109
135
 
110
- var routesAlias = {};
111
- function setRoutesAlias(alias) {
112
- routesAlias = alias;
113
- }
114
136
  function addLeadingSlash(path) {
115
137
  if (path == null) {
116
138
  return '';
117
139
  }
118
140
  return path.charAt(0) === '/' ? path : '/' + path;
119
141
  }
120
- var isTabBar = function (config) {
121
- var _a, _b;
122
- var ref = config.router;
123
- var customRoutes = ref.customRoutes; if ( customRoutes === void 0 ) customRoutes = {};
124
- var basename = ref.basename; if ( basename === void 0 ) basename = '';
125
- var pathname = ref.pathname;
126
- var routePath = stripBasename(pathname, basename);
127
- var pagePath = ((_a = Object.entries(customRoutes).find(function (ref) {
128
- var target = ref[1];
129
-
130
- return target === routePath;
131
- })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
132
- return !!pagePath && (((_b = config.tabBar) === null || _b === void 0 ? void 0 : _b.list) || []).some(function (t) { return t.pagePath === pagePath; });
133
- };
142
+ var RoutesAlias = function RoutesAlias() {
143
+ var this$1 = this;
144
+
145
+ this.conf = [];
146
+ this.getConfig = function (url) {
147
+ if ( url === void 0 ) url = '';
148
+
149
+ var customRoute = this$1.conf.filter(function (arr) {
150
+ return arr.includes(url);
151
+ });
152
+ return customRoute[0];
153
+ };
154
+ this.getOrigin = function (url) {
155
+ if ( url === void 0 ) url = '';
156
+
157
+ var _a;
158
+ return ((_a = this$1.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
159
+ };
160
+ this.getAlias = function (url) {
161
+ if ( url === void 0 ) url = '';
162
+
163
+ var _a;
164
+ return ((_a = this$1.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
165
+ };
166
+ this.getAll = function (url) {
167
+ if ( url === void 0 ) url = '';
168
+
169
+ return this$1.conf.filter(function (arr) {
170
+ return arr.includes(url);
171
+ }).reduce(function (p, a) {
172
+ p.push(a[1]);
173
+ return p;
174
+ }, [url]);
175
+ };
176
+ };
177
+ RoutesAlias.prototype.set = function set (customRoutes) {
178
+ var this$1 = this;
179
+ var ref;
180
+
181
+ if ( customRoutes === void 0 ) customRoutes = {};
182
+ var loop = function ( key ) {
183
+ var path = customRoutes[key];
184
+ key = addLeadingSlash(key);
185
+ if (typeof path === 'string') {
186
+ this$1.conf.push([key, addLeadingSlash(path)]);
187
+ }
188
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
189
+ (ref = this$1.conf).push.apply(ref, path.map(function (p) { return [key, addLeadingSlash(p)]; }));
190
+ }
191
+ };
192
+
193
+ for (var key in customRoutes) loop( key );
194
+ };
195
+ var routesAlias = new RoutesAlias();
134
196
 
135
197
  function processNavigateUrl(option) {
136
198
  var _a;
137
199
  var pathPieces = J(option.url);
138
200
  // 处理自定义路由
139
- Object.keys(routesAlias).forEach(function (key) {
140
- if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
141
- pathPieces.pathname = routesAlias[key];
142
- }
143
- });
201
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
144
202
  // 处理相对路径
145
203
  if ((_a = pathPieces === null || pathPieces === void 0 ? void 0 : pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
146
204
  var parts = exports.history.location.pathname.split('/');
@@ -166,9 +224,10 @@ async function navigate(option, method) {
166
224
  var complete = option.complete;
167
225
  var fail = option.fail;
168
226
  var unListen = exports.history.listen(function () {
169
- success === null || success === void 0 ? void 0 : success();
170
- complete === null || complete === void 0 ? void 0 : complete();
171
- resolve();
227
+ var res = { errMsg: (method + ": ok") };
228
+ success === null || success === void 0 ? void 0 : success(res);
229
+ complete === null || complete === void 0 ? void 0 : complete(res);
230
+ resolve(res);
172
231
  unListen();
173
232
  });
174
233
  try {
@@ -178,7 +237,11 @@ async function navigate(option, method) {
178
237
  if (method === 'navigateTo') {
179
238
  exports.history.push(pathPieces, state);
180
239
  }
181
- else if (method === 'redirectTo') {
240
+ else if (method === 'redirectTo' || method === 'switchTab') {
241
+ exports.history.replace(pathPieces, state);
242
+ }
243
+ else if (method === 'reLaunch') {
244
+ stacks.delta = stacks.length;
182
245
  exports.history.replace(pathPieces, state);
183
246
  }
184
247
  }
@@ -188,9 +251,10 @@ async function navigate(option, method) {
188
251
  }
189
252
  }
190
253
  catch (error) {
191
- fail === null || fail === void 0 ? void 0 : fail(error);
192
- complete === null || complete === void 0 ? void 0 : complete();
193
- reject(error);
254
+ var res = { errMsg: (method + ": fail " + (error.message || error)) };
255
+ fail === null || fail === void 0 ? void 0 : fail(res);
256
+ complete === null || complete === void 0 ? void 0 : complete(res);
257
+ reject(res);
194
258
  }
195
259
  });
196
260
  }
@@ -209,15 +273,14 @@ function navigateBack(options) {
209
273
  return navigate(options, 'navigateBack');
210
274
  }
211
275
  function switchTab(option) {
212
- // TODO: 清除掉所有的栈去目标页面
213
- return redirectTo(option);
276
+ return navigate(option, 'switchTab');
214
277
  }
215
278
  function reLaunch(option) {
216
- // TODO: 清除掉所有的栈去目标页面
217
- return redirectTo(option);
279
+ return navigate(option, 'reLaunch');
218
280
  }
219
281
  function getCurrentPages() {
220
- return stacks.get();
282
+ var pages = stacks.get();
283
+ return pages.map(function (e) { return (Object.assign(Object.assign({}, e), { route: e.path || '' })); });
221
284
  }
222
285
 
223
286
  /**
@@ -863,108 +926,6 @@ var UniversalRouter = function () {
863
926
 
864
927
  UniversalRouter.pathToRegexp = pathToRegexp_1;
865
928
 
866
- var Taro = require('@tarojs/taro-h5');
867
- function initTabbar(config) {
868
- if (config.tabBar == null) {
869
- return;
870
- }
871
- // TODO: 找到 tabbar 的类型
872
- var tabbar = document.createElement('taro-tabbar');
873
- var homePage = config.pages ? config.pages[0] : '';
874
- tabbar.conf = config.tabBar;
875
- tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
876
- var routerConfig = config.router;
877
- tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
878
- if (routerConfig.customRoutes) {
879
- tabbar.conf.custom = true;
880
- tabbar.conf.customRoutes = routerConfig.customRoutes;
881
- }
882
- else {
883
- tabbar.conf.custom = false;
884
- tabbar.conf.customRoutes = {};
885
- }
886
- if (typeof routerConfig.basename !== 'undefined') {
887
- tabbar.conf.basename = routerConfig.basename;
888
- }
889
- var container = document.getElementById('container');
890
- // eslint-disable-next-line no-unused-expressions
891
- container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
892
- Taro.initTabBarApis(config);
893
- }
894
-
895
- var routerConfig = Object.create(null);
896
- function init(config) {
897
- var _a;
898
- config.router.mode = config.router.mode || 'hash';
899
- setHistoryMode(config.router.mode, config.router.basename);
900
- Object.assign(routerConfig, config);
901
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
902
- var container = document.createElement('div');
903
- container.classList.add('taro-tabbar__container');
904
- container.id = 'container';
905
- var panel = document.createElement('div');
906
- panel.classList.add('taro-tabbar__panel');
907
- var app = document.createElement('div');
908
- app.id = 'app';
909
- app.classList.add('taro_router');
910
- panel.appendChild(app);
911
- container.appendChild(panel);
912
- document.body.appendChild(container);
913
- initTabbar(config);
914
- }
915
-
916
- var pageScrollFn;
917
- var pageDOM = window;
918
- function bindPageScroll(page, config) {
919
- pageDOM.removeEventListener('scroll', pageScrollFn);
920
- pageDOM = getScrollContainer();
921
- var distance = config.onReachBottomDistance || 50;
922
- var isReachBottom = false;
923
- pageScrollFn = function () {
924
- page.onPageScroll && page.onPageScroll({
925
- scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
926
- });
927
- if (isReachBottom && getOffset() > distance) {
928
- isReachBottom = false;
929
- }
930
- if (page.onReachBottom &&
931
- !isReachBottom &&
932
- getOffset() < distance) {
933
- isReachBottom = true;
934
- page.onReachBottom();
935
- }
936
- };
937
- pageDOM.addEventListener('scroll', pageScrollFn, false);
938
- }
939
- window.addEventListener('DOMSubtreeModified', function (e) {
940
- var _a;
941
- // @ts-ignore
942
- var className = (_a = e.target) === null || _a === void 0 ? void 0 : _a.className;
943
- if (className && /taro-tabbar__/.test(className)) {
944
- pageDOM.removeEventListener('scroll', pageScrollFn);
945
- pageDOM = getScrollContainer();
946
- pageDOM.addEventListener('scroll', pageScrollFn, false);
947
- }
948
- }, false);
949
- function getScrollContainer() {
950
- if (document.querySelector('.taro-tabbar__tabbar') === null) {
951
- // 没设置tabbar
952
- return window;
953
- }
954
- else {
955
- // 有设置tabbar
956
- return document.querySelector('.taro-tabbar__panel') || window;
957
- }
958
- }
959
- function getOffset() {
960
- if (pageDOM instanceof Window) {
961
- return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
962
- }
963
- else {
964
- return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
965
- }
966
- }
967
-
968
929
  function createCommonjsModule(fn, module) {
969
930
  return module = { exports: {} }, fn(module, module.exports), module.exports;
970
931
  }
@@ -1521,9 +1482,171 @@ var queryString_5 = queryString.stringifyUrl;
1521
1482
  var queryString_6 = queryString.pick;
1522
1483
  var queryString_7 = queryString.exclude;
1523
1484
 
1524
- function getSearch() {
1485
+ var pageScrollFn;
1486
+ var pageDOM = window;
1487
+ function bindPageScroll(page, config) {
1488
+ pageDOM.removeEventListener('scroll', pageScrollFn);
1489
+ pageDOM = getScrollContainer();
1490
+ var distance = config.onReachBottomDistance || 50;
1491
+ var isReachBottom = false;
1492
+ pageScrollFn = function () {
1493
+ page.onPageScroll && page.onPageScroll({
1494
+ scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
1495
+ });
1496
+ if (isReachBottom && getOffset() > distance) {
1497
+ isReachBottom = false;
1498
+ }
1499
+ if (page.onReachBottom &&
1500
+ !isReachBottom &&
1501
+ getOffset() < distance) {
1502
+ isReachBottom = true;
1503
+ page.onReachBottom();
1504
+ }
1505
+ };
1506
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
1507
+ }
1508
+ window.addEventListener('DOMSubtreeModified', function (e) {
1509
+ var target = e.target;
1510
+ var className = target === null || target === void 0 ? void 0 : target.className;
1511
+ if (className && /taro_page/.test(className)) {
1512
+ pageDOM.removeEventListener('scroll', pageScrollFn);
1513
+ pageDOM = getScrollContainer();
1514
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
1515
+ }
1516
+ }, false);
1517
+ function getScrollContainer() {
1518
+ var _a;
1519
+ var id = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
1520
+ var el = (id
1521
+ ? document.getElementById(id)
1522
+ : document.querySelector('.taro_page') ||
1523
+ document.querySelector('.taro_router'));
1524
+ return el || window;
1525
+ }
1526
+ function getOffset() {
1527
+ if (pageDOM instanceof Window) {
1528
+ return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
1529
+ }
1530
+ else {
1531
+ return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
1532
+ }
1533
+ }
1534
+
1535
+ /**
1536
+ * 插入页面动画需要的样式
1537
+ */
1538
+ function loadAnimateStyle(ms) {
1539
+ if ( ms === void 0 ) ms = 300;
1540
+
1541
+ 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 " + 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}";
1542
+ var style = document.createElement('style');
1543
+ style.innerHTML = css;
1544
+ document.getElementsByTagName('head')[0].appendChild(style);
1545
+ }
1546
+
1547
+ // @ts-nocheck
1548
+ function initTabbar(config) {
1549
+ if (config.tabBar == null) {
1550
+ return;
1551
+ }
1552
+ // TODO: custom-tab-bar
1553
+ var tabbar = document.createElement('taro-tabbar');
1554
+ var homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
1555
+ tabbar.conf = config.tabBar;
1556
+ tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
1557
+ var routerConfig = config.router;
1558
+ tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
1559
+ if (routerConfig.customRoutes) {
1560
+ tabbar.conf.custom = true;
1561
+ tabbar.conf.customRoutes = routerConfig.customRoutes;
1562
+ }
1563
+ else {
1564
+ tabbar.conf.custom = false;
1565
+ tabbar.conf.customRoutes = {};
1566
+ }
1567
+ if (typeof routerConfig.basename !== 'undefined') {
1568
+ tabbar.conf.basename = routerConfig.basename;
1569
+ }
1570
+ var container = document.getElementById('container');
1571
+ container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
1572
+ taro.initTabBarApis(config);
1573
+ }
1574
+
1575
+ function setDisplay(el, type) {
1576
+ if ( type === void 0 ) type = '';
1577
+
1578
+ if (el) {
1579
+ el.style.display = type;
1580
+ }
1581
+ }
1582
+ var PageHandler = function PageHandler(config) {
1583
+ this.defaultAnimation = { duration: 300, delay: 50 };
1584
+ this.config = config;
1585
+ this.mount();
1586
+ };
1587
+
1588
+ var prototypeAccessors$1 = { appId: { configurable: true },router: { configurable: true },routerMode: { configurable: true },customRoutes: { configurable: true },routes: { configurable: true },tabBarList: { configurable: true },PullDownRefresh: { configurable: true },animation: { configurable: true },animationDelay: { configurable: true },animationDuration: { configurable: true },pathname: { configurable: true },basename: { configurable: true },pageConfig: { configurable: true },isTabBar: { configurable: true },search: { configurable: true } };
1589
+ prototypeAccessors$1.appId.get = function () { return 'app'; };
1590
+ prototypeAccessors$1.router.get = function () { return this.config.router; };
1591
+ prototypeAccessors$1.routerMode.get = function () { return this.router.mode || 'hash'; };
1592
+ prototypeAccessors$1.customRoutes.get = function () { return this.router.customRoutes || {}; };
1593
+ prototypeAccessors$1.routes.get = function () { return this.config.routes; };
1594
+ prototypeAccessors$1.tabBarList.get = function () { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; };
1595
+ prototypeAccessors$1.PullDownRefresh.get = function () { return this.config.PullDownRefresh; };
1596
+ prototypeAccessors$1.animation.get = function () { var _a, _b; return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.animation) !== null && _b !== void 0 ? _b : this.defaultAnimation; };
1597
+ prototypeAccessors$1.animationDelay.get = function () {
1598
+ var _a;
1599
+ return typeof this.animation === 'object'
1600
+ ? this.animation.delay
1601
+ : this.animation
1602
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.delay
1603
+ : 0;
1604
+ };
1605
+ prototypeAccessors$1.animationDuration.get = function () {
1606
+ var _a;
1607
+ return typeof this.animation === 'object'
1608
+ ? this.animation.duration
1609
+ : this.animation
1610
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.duration
1611
+ : 0;
1612
+ };
1613
+ prototypeAccessors$1.pathname.set = function (p) { this.router.pathname = p; };
1614
+ prototypeAccessors$1.pathname.get = function () { return this.router.pathname; };
1615
+ prototypeAccessors$1.basename.get = function () { return this.router.basename || ''; };
1616
+ prototypeAccessors$1.pageConfig.get = function () {
1617
+ var this$1 = this;
1618
+
1619
+ return this.routes.find(function (r) {
1620
+ var _a;
1621
+ var routePath = stripBasename(this$1.pathname, this$1.basename);
1622
+ var pagePath = addLeadingSlash(r.path);
1623
+ return pagePath === routePath || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
1624
+ });
1625
+ };
1626
+ prototypeAccessors$1.isTabBar.get = function () {
1627
+ var _a;
1628
+ var routePath = stripBasename(this.pathname, this.basename);
1629
+ var pagePath = ((_a = Object.entries(this.customRoutes).find(function (ref) {
1630
+ var target = ref[1];
1631
+
1632
+ if (typeof target === 'string') {
1633
+ return target === routePath;
1634
+ }
1635
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
1636
+ return target.includes(routePath);
1637
+ }
1638
+ return false;
1639
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
1640
+ return !!pagePath && this.tabBarList.some(function (t) { return t.pagePath === pagePath; });
1641
+ };
1642
+ PageHandler.prototype.isSamePage = function isSamePage (page) {
1643
+ var routePath = stripBasename(this.pathname, this.basename);
1644
+ var pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
1645
+ return pagePath.startsWith(routePath + '?');
1646
+ };
1647
+ prototypeAccessors$1.search.get = function () {
1525
1648
  var search = '?';
1526
- if (routerConfig.router.mode === 'hash') {
1649
+ if (this.routerMode) {
1527
1650
  var idx = location.hash.indexOf('?');
1528
1651
  if (idx > -1) {
1529
1652
  search = location.hash.slice(idx);
@@ -1533,11 +1656,11 @@ function getSearch() {
1533
1656
  search = location.search;
1534
1657
  }
1535
1658
  return search.substr(1);
1536
- }
1537
- var qs = function (stamp) {
1538
- if ( stamp === void 0 ) stamp = 0;
1659
+ };
1660
+ PageHandler.prototype.getQuery = function getQuery (stamp) {
1661
+ if ( stamp === void 0 ) stamp = 0;
1539
1662
 
1540
- var search = getSearch();
1663
+ var search = this.search;
1541
1664
  var query = search
1542
1665
  ? queryString.parse(search)
1543
1666
  : {};
@@ -1545,116 +1668,199 @@ var qs = function (stamp) {
1545
1668
  query.stamp = stamp.toString();
1546
1669
  }
1547
1670
  return query;
1548
- };
1549
-
1550
- /* eslint-disable dot-notation */
1551
- function pageOnReady(page, onLoad) {
1552
- if ( onLoad === void 0 ) onLoad = true;
1553
-
1671
+ };
1672
+ PageHandler.prototype.mount = function mount () {
1554
1673
  var _a;
1674
+ setHistoryMode(this.routerMode, this.router.basename);
1675
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
1676
+ this.animation && loadAnimateStyle(this.animationDuration);
1677
+ var app = document.createElement('div');
1678
+ app.id = this.appId;
1679
+ app.classList.add('taro_router');
1680
+ if (this.tabBarList.length > 1) {
1681
+ var container = document.createElement('div');
1682
+ container.classList.add('taro-tabbar__container');
1683
+ container.id = 'container';
1684
+ var panel = document.createElement('div');
1685
+ panel.classList.add('taro-tabbar__panel');
1686
+ panel.appendChild(app);
1687
+ container.appendChild(panel);
1688
+ document.body.appendChild(container);
1689
+ initTabbar(this.config);
1690
+ }
1691
+ else {
1692
+ document.body.appendChild(app);
1693
+ }
1694
+ };
1695
+ PageHandler.prototype.onReady = function onReady (page, onLoad) {
1696
+ if ( onLoad === void 0 ) onLoad = true;
1697
+
1698
+ var _a, _b;
1555
1699
  var pageEl = document.getElementById(page.path);
1556
1700
  if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
1557
1701
  var el = pageEl.firstElementChild;
1558
- (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el).then(function () {
1702
+ (_b = (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el)) === null || _b === void 0 ? void 0 : _b.then(function () {
1559
1703
  runtime.requestAnimationFrame(function () {
1560
- page.onReady();
1704
+ var _a;
1705
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
1561
1706
  pageEl['__isReady'] = true;
1562
1707
  });
1563
1708
  });
1564
1709
  onLoad && (pageEl['__page'] = page);
1565
1710
  }
1566
- }
1567
- function loadPage(page, pageConfig, stacksIndex) {
1568
- if ( pageConfig === void 0 ) pageConfig = {};
1569
- if ( stacksIndex === void 0 ) stacksIndex = 0;
1711
+ };
1712
+ PageHandler.prototype.load = function load (page, pageConfig, stacksIndex) {
1713
+ var this$1 = this;
1714
+ if ( pageConfig === void 0 ) pageConfig = {};
1715
+ if ( stacksIndex === void 0 ) stacksIndex = 0;
1570
1716
 
1717
+ var _a;
1571
1718
  if (!page)
1572
1719
  { return; }
1573
1720
  var pageEl = document.getElementById(page.path);
1574
1721
  if (pageEl) {
1575
- pageEl.style.display = 'block';
1722
+ setDisplay(pageEl);
1723
+ this.isTabBar && pageEl.classList.add('taro_tabbar_page');
1724
+ this.addAnimation(pageEl, stacksIndex === 0);
1576
1725
  }
1577
1726
  else {
1578
- page.onLoad(qs(stacksIndex), function () { return pageOnReady(page, true); });
1727
+ page.onLoad(this.getQuery(stacksIndex), function () {
1728
+ pageEl = document.getElementById(page.path);
1729
+ this$1.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
1730
+ this$1.addAnimation(pageEl, stacksIndex === 0);
1731
+ this$1.onReady(page, true);
1732
+ });
1579
1733
  }
1580
1734
  stacks.push(page);
1581
- page.onShow();
1735
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1582
1736
  bindPageScroll(page, pageConfig);
1583
- }
1584
- function unloadPage(page, delta) {
1585
- if ( delta === void 0 ) delta = 1;
1737
+ };
1738
+ PageHandler.prototype.unload = function unload (page, delta, top) {
1739
+ var this$1 = this;
1740
+ if ( delta === void 0 ) delta = 1;
1741
+ if ( top === void 0 ) top = false;
1586
1742
 
1743
+ var _a;
1587
1744
  if (!page)
1588
1745
  { return; }
1589
1746
  stacks.delta = --delta;
1590
1747
  stacks.pop();
1591
- page.onUnload();
1748
+ if (this.animation && top) {
1749
+ if (this.unloadTimer) {
1750
+ clearTimeout(this.unloadTimer);
1751
+ (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload();
1752
+ this.unloadTimer = null;
1753
+ }
1754
+ this.lastUnloadPage = page;
1755
+ var pageEl = document.getElementById(page.path);
1756
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
1757
+ this.unloadTimer = setTimeout(function () {
1758
+ var _a;
1759
+ this$1.unloadTimer = null;
1760
+ (_a = this$1.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload();
1761
+ }, this.animationDuration);
1762
+ }
1763
+ else {
1764
+ var pageEl$1 = document.getElementById(page.path);
1765
+ pageEl$1 === null || pageEl$1 === void 0 ? void 0 : pageEl$1.classList.remove('taro_page_show');
1766
+ page === null || page === void 0 ? void 0 : page.onUnload();
1767
+ }
1592
1768
  if (delta >= 1)
1593
- { unloadPage(stacks.last, delta); }
1594
- }
1595
- function showPage(page, pageConfig, stacksIndex) {
1596
- if ( pageConfig === void 0 ) pageConfig = {};
1597
- if ( stacksIndex === void 0 ) stacksIndex = 0;
1769
+ { this.unload(stacks.last, delta); }
1770
+ };
1771
+ PageHandler.prototype.show = function show (page, pageConfig, stacksIndex) {
1772
+ var this$1 = this;
1773
+ if ( pageConfig === void 0 ) pageConfig = {};
1774
+ if ( stacksIndex === void 0 ) stacksIndex = 0;
1598
1775
 
1776
+ var _a;
1599
1777
  if (!page)
1600
1778
  { return; }
1601
- page.onShow();
1779
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1602
1780
  var pageEl = document.getElementById(page.path);
1603
1781
  if (pageEl) {
1604
- pageEl.style.display = 'block';
1782
+ setDisplay(pageEl);
1783
+ this.addAnimation(pageEl, stacksIndex === 0);
1605
1784
  }
1606
1785
  else {
1607
- page.onLoad(qs(stacksIndex), function () { return pageOnReady(page, false); });
1786
+ page.onLoad(this.getQuery(stacksIndex), function () {
1787
+ pageEl = document.getElementById(page.path);
1788
+ this$1.addAnimation(pageEl, stacksIndex === 0);
1789
+ this$1.onReady(page, false);
1790
+ });
1608
1791
  }
1609
1792
  bindPageScroll(page, pageConfig);
1610
- }
1611
- function hidePage(page) {
1793
+ };
1794
+ PageHandler.prototype.hide = function hide (page) {
1795
+ var this$1 = this;
1796
+
1797
+ var _a;
1612
1798
  if (!page)
1613
1799
  { return; }
1614
1800
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
1615
1801
  var pageEl = document.getElementById(page.path);
1616
1802
  if (pageEl) {
1617
- pageEl.style.display = 'none';
1618
- page.onHide();
1803
+ if (this.hideTimer) {
1804
+ clearTimeout(this.hideTimer);
1805
+ this.hideTimer = null;
1806
+ setDisplay(this.lastHidePage, 'none');
1807
+ }
1808
+ this.lastHidePage = pageEl;
1809
+ this.hideTimer = setTimeout(function () {
1810
+ this$1.hideTimer = null;
1811
+ setDisplay(this$1.lastHidePage, 'none');
1812
+ }, this.animationDelay);
1813
+ (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1619
1814
  }
1620
1815
  else {
1621
- setTimeout(function () { return hidePage(page); }, 0);
1816
+ setTimeout(function () { return this$1.hide(page); }, 0);
1622
1817
  }
1623
- }
1818
+ };
1819
+ PageHandler.prototype.addAnimation = function addAnimation (pageEl, first) {
1820
+ if ( first === void 0 ) first = false;
1821
+
1822
+ if (!pageEl)
1823
+ { return; }
1824
+ if (this.animation && !first) {
1825
+ setTimeout(function () {
1826
+ pageEl.classList.add('taro_page_show');
1827
+ }, this.animationDelay);
1828
+ }
1829
+ else {
1830
+ pageEl.classList.add('taro_page_show');
1831
+ }
1832
+ };
1833
+
1834
+ Object.defineProperties( PageHandler.prototype, prototypeAccessors$1 );
1624
1835
 
1625
1836
  /* eslint-disable dot-notation */
1626
1837
  function createRouter(app, config, framework) {
1627
- var _a;
1628
- init(config);
1629
- var routes = [];
1630
- var alias = (_a = config.router.customRoutes) !== null && _a !== void 0 ? _a : {};
1838
+ var _a, _b, _c;
1839
+ var handler = new PageHandler(config);
1631
1840
  var runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
1632
- setRoutesAlias(alias);
1633
- for (var i = 0; i < config.routes.length; i++) {
1634
- var route = config.routes[i];
1635
- var path = addLeadingSlash(route.path);
1636
- routes.push({
1637
- path: alias[path] || path,
1638
- action: route.load
1639
- });
1640
- }
1641
- var basename = config.router.basename;
1841
+ routesAlias.set(handler.router.customRoutes);
1842
+ var basename = handler.router.basename;
1843
+ var routes = handler.routes.map(function (route) { return ({
1844
+ path: routesAlias.getAll(addLeadingSlash(route.path)),
1845
+ action: route.load
1846
+ }); });
1847
+ var entryPagePath = config.entryPagePath || ((_a = routes[0].path) === null || _a === void 0 ? void 0 : _a[0]);
1642
1848
  var router = new UniversalRouter(routes, { baseUrl: basename || '' });
1643
- app.onLaunch();
1849
+ (_b = app.onLaunch) === null || _b === void 0 ? void 0 : _b.call(app);
1644
1850
  var render = async function (ref) {
1645
1851
  var location = ref.location;
1646
1852
  var action = ref.action;
1647
1853
 
1648
1854
  var _a, _b, _c, _d;
1649
- routerConfig.router.pathname = location.pathname;
1855
+ handler.pathname = location.pathname;
1650
1856
  var element;
1651
1857
  try {
1652
- element = await router.resolve(config.router.forcePath || location.pathname);
1858
+ element = await router.resolve(handler.router.forcePath || handler.pathname);
1653
1859
  }
1654
1860
  catch (error) {
1655
1861
  if (error.status === 404) {
1656
1862
  (_a = app.onPageNotFound) === null || _a === void 0 ? void 0 : _a.call(app, {
1657
- path: location.pathname
1863
+ path: handler.pathname
1658
1864
  });
1659
1865
  }
1660
1866
  else {
@@ -1663,14 +1869,11 @@ function createRouter(app, config, framework) {
1663
1869
  }
1664
1870
  if (!element)
1665
1871
  { return; }
1666
- var pageConfig = config.routes.find(function (r) {
1667
- var path = addLeadingSlash(r.path);
1668
- return path === location.pathname || alias[path] === location.pathname;
1669
- });
1872
+ var pageConfig = handler.pageConfig;
1670
1873
  var enablePullDownRefresh = false;
1671
1874
  runtime.eventCenter.trigger('__taroRouterChange', {
1672
1875
  toLocation: {
1673
- path: location.pathname
1876
+ path: handler.pathname
1674
1877
  }
1675
1878
  });
1676
1879
  if (pageConfig) {
@@ -1678,53 +1881,56 @@ function createRouter(app, config, framework) {
1678
1881
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1679
1882
  }
1680
1883
  var currentPage = runtime.Current.page;
1681
- var pathname = location.pathname;
1884
+ var pathname = handler.pathname;
1682
1885
  var shouldLoad = false;
1683
1886
  if (action === 'POP') {
1684
1887
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
1685
1888
  var prevIndex = stacks.getPrevIndex(pathname);
1686
1889
  var delta = stacks.getDelta(pathname);
1687
- unloadPage(currentPage, delta);
1890
+ handler.unload(currentPage, delta, prevIndex > -1);
1688
1891
  if (prevIndex > -1) {
1689
- showPage(stacks.getItem(prevIndex), pageConfig, prevIndex);
1892
+ handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
1690
1893
  }
1691
1894
  else {
1692
1895
  shouldLoad = true;
1693
1896
  }
1694
1897
  }
1695
- else if (action === 'PUSH') {
1696
- hidePage(currentPage);
1697
- shouldLoad = true;
1698
- }
1699
- else if (action === 'REPLACE') {
1700
- if (isTabBar(config)) {
1701
- hidePage(currentPage);
1702
- var prevIndex$1 = stacks.getPrevIndex(pathname);
1898
+ else {
1899
+ if (handler.isTabBar) {
1900
+ if (handler.isSamePage(currentPage))
1901
+ { return; }
1902
+ var prevIndex$1 = stacks.getPrevIndex(pathname, 0);
1903
+ handler.hide(currentPage);
1703
1904
  if (prevIndex$1 > -1) {
1704
1905
  // NOTE: tabbar 页且之前出现过,直接复用
1705
- return showPage(stacks.getItem(prevIndex$1), pageConfig, prevIndex$1);
1906
+ return handler.show(stacks.getItem(prevIndex$1), pageConfig, prevIndex$1);
1706
1907
  }
1707
1908
  }
1708
- else {
1709
- unloadPage(currentPage);
1909
+ else if (action === 'REPLACE') {
1910
+ var delta$1 = stacks.getDelta(pathname);
1911
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1912
+ handler.unload(currentPage, delta$1);
1913
+ }
1914
+ else if (action === 'PUSH') {
1915
+ handler.hide(currentPage);
1710
1916
  }
1711
1917
  shouldLoad = true;
1712
1918
  }
1713
- if (shouldLoad) {
1919
+ if (shouldLoad || stacks.length < 1) {
1714
1920
  var el = (_c = element.default) !== null && _c !== void 0 ? _c : element;
1715
1921
  var loadConfig = Object.assign({}, pageConfig);
1716
1922
  var stacksIndex = stacks.length;
1717
1923
  delete loadConfig['path'];
1718
1924
  delete loadConfig['load'];
1719
- var page = runtime.createPageConfig(enablePullDownRefresh ? (_d = runtimeHooks.createPullDownComponent) === null || _d === void 0 ? void 0 : _d.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname + runtime.stringify(qs(stacksIndex)), {}, loadConfig);
1720
- return loadPage(page, pageConfig, stacksIndex);
1925
+ var page = runtime.createPageConfig(enablePullDownRefresh ? (_d = runtimeHooks.createPullDownComponent) === null || _d === void 0 ? void 0 : _d.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
1926
+ return handler.load(page, pageConfig, stacksIndex);
1721
1927
  }
1722
1928
  };
1723
1929
  if (exports.history.location.pathname === '/') {
1724
- exports.history.replace(prependBasename(routes[0].path + exports.history.location.search));
1930
+ exports.history.replace(prependBasename(entryPagePath + exports.history.location.search));
1725
1931
  }
1726
1932
  render({ location: exports.history.location, action: r.Push });
1727
- app.onShow(qs(stacks.length));
1933
+ (_c = app.onShow) === null || _c === void 0 ? void 0 : _c.call(app, handler.getQuery(stacks.length));
1728
1934
  return exports.history.listen(render);
1729
1935
  }
1730
1936