@tarojs/router 3.4.8 → 3.5.0-alpha.10

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.
@@ -0,0 +1,22 @@
1
+ import { addLeadingSlash } from '../utils';
2
+ export class RouterConfig {
3
+ static set config(e) {
4
+ this.__config = e;
5
+ }
6
+ static get config() {
7
+ return this.__config;
8
+ }
9
+ static get pages() {
10
+ return this.config.pages || [];
11
+ }
12
+ static get router() {
13
+ return this.config.router || {};
14
+ }
15
+ static get mode() {
16
+ return this.router.mode || 'hash';
17
+ }
18
+ static get customRoutes() { return this.router.customRoutes || {}; }
19
+ static isPage(url = '') {
20
+ return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
21
+ }
22
+ }
@@ -0,0 +1,60 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { container, createPageConfig, eventCenter, SERVICE_IDENTIFIER, stringify } from '@tarojs/runtime';
11
+ import { RouterConfig } from '.';
12
+ import MultiPageHandler from './multi-page';
13
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
14
+ /** Note: 关于多页面应用
15
+ * - 需要配置路由映射(根目录跳转、404 页面……)
16
+ * - app.onPageNotFound 事件不支持
17
+ * - 应用生命周期可能多次触发
18
+ * - TabBar 会多次加载
19
+ * - 不支持路由动画
20
+ */
21
+ export function createMultiRouter(app, config, framework) {
22
+ var _a, _b, _c, _d, _e, _f, _g;
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ RouterConfig.config = config;
25
+ const handler = new MultiPageHandler(config);
26
+ const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
27
+ const launchParam = handler.getQuery();
28
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
29
+ const pathName = config.pageName;
30
+ const pageConfig = handler.pageConfig;
31
+ let element;
32
+ try {
33
+ element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
34
+ }
35
+ catch (error) {
36
+ throw new Error(error);
37
+ }
38
+ if (!element)
39
+ return;
40
+ let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
41
+ eventCenter.trigger('__taroRouterChange', {
42
+ toLocation: {
43
+ path: pathName
44
+ }
45
+ });
46
+ if (pageConfig) {
47
+ document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
48
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
49
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
50
+ }
51
+ }
52
+ const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
53
+ const loadConfig = Object.assign({}, pageConfig);
54
+ delete loadConfig['path'];
55
+ delete loadConfig['load'];
56
+ const page = createPageConfig(enablePullDownRefresh ? (_f = runtimeHooks.createPullDownComponent) === null || _f === void 0 ? void 0 : _f.call(runtimeHooks, el, location.pathname, framework, config.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
57
+ handler.load(page, pageConfig);
58
+ (_g = app.onShow) === null || _g === void 0 ? void 0 : _g.call(app, launchParam);
59
+ });
60
+ }
@@ -0,0 +1,111 @@
1
+ import { Current } from '@tarojs/runtime';
2
+ import queryString from 'query-string';
3
+ import { bindPageResize } from '../events/resize';
4
+ import { bindPageScroll } from '../events/scroll';
5
+ import { setHistoryMode } from '../history';
6
+ import { initTabbar } from '../tabbar';
7
+ import { stripBasename } from '../utils';
8
+ export default class MultiPageHandler {
9
+ constructor(config) {
10
+ this.config = config;
11
+ this.mount();
12
+ }
13
+ get appId() { return 'app'; }
14
+ get router() { return this.config.router; }
15
+ get routerMode() { return this.router.mode || 'hash'; }
16
+ get customRoutes() { return this.router.customRoutes || {}; }
17
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
18
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
19
+ set pathname(p) { this.router.pathname = p; }
20
+ get pathname() { return this.router.pathname; }
21
+ get basename() { return this.router.basename || ''; }
22
+ get pageConfig() { return this.config.route; }
23
+ get isTabBar() {
24
+ var _a;
25
+ const routePath = stripBasename(this.pathname, this.basename);
26
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
27
+ if (typeof target === 'string') {
28
+ return target === routePath;
29
+ }
30
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
31
+ return target.includes(routePath);
32
+ }
33
+ return false;
34
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
35
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
36
+ }
37
+ get search() { return location.search.substr(1); }
38
+ getQuery(search = '', options = {}) {
39
+ search = search ? `${search}&${this.search}` : this.search;
40
+ const query = search
41
+ ? queryString.parse(search)
42
+ : {};
43
+ return Object.assign(Object.assign({}, query), options);
44
+ }
45
+ mount() {
46
+ var _a;
47
+ setHistoryMode(this.routerMode, this.router.basename);
48
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
49
+ const app = document.createElement('div');
50
+ app.id = this.appId;
51
+ app.classList.add('taro_router');
52
+ if (this.tabBarList.length > 1) {
53
+ const container = document.createElement('div');
54
+ container.classList.add('taro-tabbar__container');
55
+ container.id = 'container';
56
+ const panel = document.createElement('div');
57
+ panel.classList.add('taro-tabbar__panel');
58
+ panel.appendChild(app);
59
+ container.appendChild(panel);
60
+ document.body.appendChild(container);
61
+ initTabbar(this.config);
62
+ }
63
+ else {
64
+ document.body.appendChild(app);
65
+ }
66
+ }
67
+ onReady(page, onLoad = true) {
68
+ var _a;
69
+ const pageEl = this.getPageContainer(page);
70
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
71
+ const el = pageEl.firstElementChild;
72
+ (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
73
+ onLoad && (pageEl['__page'] = page);
74
+ }
75
+ }
76
+ load(page, pageConfig = {}) {
77
+ var _a;
78
+ if (!page)
79
+ return;
80
+ (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
81
+ var _a;
82
+ const pageEl = this.getPageContainer(page);
83
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
84
+ this.onReady(page, true);
85
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
86
+ this.bindPageEvents(page, pageEl, pageConfig);
87
+ });
88
+ }
89
+ getPageContainer(page) {
90
+ var _a;
91
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
92
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
93
+ if (page) {
94
+ return document.querySelector(`.taro_page#${id}`);
95
+ }
96
+ const el = (id
97
+ ? document.querySelector(`.taro_page#${id}`)
98
+ : document.querySelector('.taro_page') ||
99
+ document.querySelector('.taro_router'));
100
+ return el || window;
101
+ }
102
+ bindPageEvents(page, pageEl, config = {}) {
103
+ var _a;
104
+ if (!pageEl) {
105
+ pageEl = this.getPageContainer();
106
+ }
107
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
108
+ bindPageScroll(page, pageEl, distance);
109
+ bindPageResize(page);
110
+ }
111
+ }
@@ -0,0 +1,280 @@
1
+ import { Current, requestAnimationFrame } from '@tarojs/runtime';
2
+ import queryString from 'query-string';
3
+ import { loadAnimateStyle } from '../animation';
4
+ import { bindPageResize } from '../events/resize';
5
+ import { bindPageScroll } from '../events/scroll';
6
+ import { setHistoryMode } from '../history';
7
+ import { initTabbar } from '../tabbar';
8
+ import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
9
+ import stacks from './stack';
10
+ function setDisplay(el, type = '') {
11
+ if (el) {
12
+ el.style.display = type;
13
+ }
14
+ }
15
+ export default class PageHandler {
16
+ constructor(config) {
17
+ this.defaultAnimation = { duration: 300, delay: 50 };
18
+ this.config = config;
19
+ this.mount();
20
+ }
21
+ get appId() { return 'app'; }
22
+ get router() { return this.config.router; }
23
+ get routerMode() { return this.router.mode || 'hash'; }
24
+ get customRoutes() { return this.router.customRoutes || {}; }
25
+ get routes() { return this.config.routes; }
26
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
27
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
28
+ get animation() { var _a, _b; return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.animation) !== null && _b !== void 0 ? _b : this.defaultAnimation; }
29
+ get animationDelay() {
30
+ var _a;
31
+ return (typeof this.animation === 'object'
32
+ ? this.animation.delay
33
+ : this.animation
34
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.delay
35
+ : 0) || 0;
36
+ }
37
+ get animationDuration() {
38
+ var _a;
39
+ return (typeof this.animation === 'object'
40
+ ? this.animation.duration
41
+ : this.animation
42
+ ? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.duration
43
+ : 0) || 0;
44
+ }
45
+ set pathname(p) { this.router.pathname = p; }
46
+ get pathname() { return this.router.pathname; }
47
+ get basename() { return this.router.basename || ''; }
48
+ get homePage() {
49
+ return this.config.entryPagePath || this.routes[0].path || this.basename;
50
+ }
51
+ get pageConfig() {
52
+ const routePath = stripBasename(this.pathname, this.basename);
53
+ const homePage = this.homePage;
54
+ return this.routes.find(r => {
55
+ var _a;
56
+ const pagePath = addLeadingSlash(r.path);
57
+ return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
58
+ });
59
+ }
60
+ get isTabBar() {
61
+ var _a;
62
+ const routePath = stripBasename(this.pathname, this.basename);
63
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
64
+ if (typeof target === 'string') {
65
+ return target === routePath;
66
+ }
67
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
68
+ return target.includes(routePath);
69
+ }
70
+ return false;
71
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
72
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
73
+ }
74
+ isSamePage(page) {
75
+ const routePath = stripBasename(this.pathname, this.basename);
76
+ const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
77
+ return pagePath.startsWith(routePath + '?');
78
+ }
79
+ get search() {
80
+ let search = '?';
81
+ if (this.routerMode === 'hash') {
82
+ const idx = location.hash.indexOf('?');
83
+ if (idx > -1) {
84
+ search = location.hash.slice(idx);
85
+ }
86
+ }
87
+ else {
88
+ search = location.search;
89
+ }
90
+ return search.substr(1);
91
+ }
92
+ getQuery(stamp = 0, search = '', options = {}) {
93
+ search = search ? `${search}&${this.search}` : this.search;
94
+ const query = search
95
+ ? queryString.parse(search, { decode: false })
96
+ : {};
97
+ query.stamp = stamp.toString();
98
+ return Object.assign(Object.assign({}, query), options);
99
+ }
100
+ mount() {
101
+ var _a;
102
+ setHistoryMode(this.routerMode, this.router.basename);
103
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
104
+ this.animation && loadAnimateStyle(this.animationDuration);
105
+ const app = document.createElement('div');
106
+ app.id = this.appId;
107
+ app.classList.add('taro_router');
108
+ if (this.tabBarList.length > 1) {
109
+ const container = document.createElement('div');
110
+ container.classList.add('taro-tabbar__container');
111
+ container.id = 'container';
112
+ const panel = document.createElement('div');
113
+ panel.classList.add('taro-tabbar__panel');
114
+ panel.appendChild(app);
115
+ container.appendChild(panel);
116
+ document.body.appendChild(container);
117
+ initTabbar(this.config);
118
+ }
119
+ else {
120
+ document.body.appendChild(app);
121
+ }
122
+ }
123
+ onReady(page, onLoad = true) {
124
+ var _a, _b;
125
+ const pageEl = this.getPageContainer(page);
126
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
127
+ const el = pageEl.firstElementChild;
128
+ (_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(() => {
129
+ requestAnimationFrame(() => {
130
+ var _a;
131
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
132
+ pageEl['__isReady'] = true;
133
+ });
134
+ });
135
+ onLoad && (pageEl['__page'] = page);
136
+ }
137
+ }
138
+ load(page, pageConfig = {}, stacksIndex = 0) {
139
+ var _a, _b;
140
+ if (!page)
141
+ return;
142
+ // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
143
+ stacks.push(page);
144
+ const param = this.getQuery(stacks.length, '', page.options);
145
+ let pageEl = this.getPageContainer(page);
146
+ if (pageEl) {
147
+ setDisplay(pageEl);
148
+ this.isTabBar && pageEl.classList.add('taro_tabbar_page');
149
+ this.addAnimation(pageEl, stacksIndex === 0);
150
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
151
+ this.bindPageEvents(page, pageEl, pageConfig);
152
+ }
153
+ else {
154
+ (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
155
+ var _a;
156
+ pageEl = this.getPageContainer(page);
157
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
158
+ this.addAnimation(pageEl, stacksIndex === 0);
159
+ this.onReady(page, true);
160
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
161
+ this.bindPageEvents(page, pageEl, pageConfig);
162
+ });
163
+ }
164
+ }
165
+ unload(page, delta = 1, top = false) {
166
+ var _a, _b, _c;
167
+ if (!page)
168
+ return;
169
+ stacks.delta = --delta;
170
+ stacks.pop();
171
+ if (this.animation && top) {
172
+ if (this.unloadTimer) {
173
+ clearTimeout(this.unloadTimer);
174
+ (_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
175
+ this.unloadTimer = null;
176
+ }
177
+ this.lastUnloadPage = page;
178
+ const pageEl = this.getPageContainer(page);
179
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
180
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
181
+ this.unloadTimer = setTimeout(() => {
182
+ var _a, _b;
183
+ this.unloadTimer = null;
184
+ (_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
185
+ }, this.animationDuration);
186
+ }
187
+ else {
188
+ const pageEl = this.getPageContainer(page);
189
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
190
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
191
+ (_c = page === null || page === void 0 ? void 0 : page.onUnload) === null || _c === void 0 ? void 0 : _c.call(page);
192
+ }
193
+ if (delta >= 1)
194
+ this.unload(stacks.last, delta);
195
+ }
196
+ show(page, pageConfig = {}, stacksIndex = 0) {
197
+ var _a, _b;
198
+ if (!page)
199
+ return;
200
+ const param = this.getQuery(stacks.length, '', page.options);
201
+ let pageEl = this.getPageContainer(page);
202
+ if (pageEl) {
203
+ setDisplay(pageEl);
204
+ this.addAnimation(pageEl, stacksIndex === 0);
205
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
206
+ this.bindPageEvents(page, pageEl, pageConfig);
207
+ }
208
+ else {
209
+ (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
210
+ var _a;
211
+ pageEl = this.getPageContainer(page);
212
+ this.addAnimation(pageEl, stacksIndex === 0);
213
+ this.onReady(page, false);
214
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
215
+ this.bindPageEvents(page, pageEl, pageConfig);
216
+ });
217
+ }
218
+ }
219
+ hide(page) {
220
+ var _a;
221
+ if (!page)
222
+ return;
223
+ // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
224
+ const pageEl = this.getPageContainer(page);
225
+ if (pageEl) {
226
+ if (this.hideTimer) {
227
+ clearTimeout(this.hideTimer);
228
+ this.hideTimer = null;
229
+ setDisplay(this.lastHidePage, 'none');
230
+ }
231
+ this.lastHidePage = pageEl;
232
+ this.hideTimer = setTimeout(() => {
233
+ this.hideTimer = null;
234
+ setDisplay(this.lastHidePage, 'none');
235
+ }, this.animationDuration + this.animationDelay);
236
+ (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
237
+ }
238
+ else {
239
+ setTimeout(() => this.hide(page), 0);
240
+ }
241
+ }
242
+ addAnimation(pageEl, first = false) {
243
+ if (!pageEl)
244
+ return;
245
+ if (this.animation && !first) {
246
+ setTimeout(() => {
247
+ pageEl.classList.add('taro_page_show');
248
+ setTimeout(() => {
249
+ pageEl.classList.add('taro_page_stationed');
250
+ }, this.animationDuration);
251
+ }, this.animationDelay);
252
+ }
253
+ else {
254
+ pageEl.classList.add('taro_page_show');
255
+ pageEl.classList.add('taro_page_stationed');
256
+ }
257
+ }
258
+ getPageContainer(page) {
259
+ var _a;
260
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
261
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
262
+ if (page) {
263
+ return document.querySelector(`.taro_page#${id}`);
264
+ }
265
+ const el = (id
266
+ ? document.querySelector(`.taro_page#${id}`)
267
+ : document.querySelector('.taro_page') ||
268
+ document.querySelector('.taro_router'));
269
+ return el || window;
270
+ }
271
+ bindPageEvents(page, pageEl, config = {}) {
272
+ var _a;
273
+ if (!pageEl) {
274
+ pageEl = this.getPageContainer();
275
+ }
276
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
277
+ bindPageScroll(page, pageEl, distance);
278
+ bindPageResize(page);
279
+ }
280
+ }
@@ -0,0 +1,124 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { container, createPageConfig, Current, eventCenter, SERVICE_IDENTIFIER, stringify } from '@tarojs/runtime';
11
+ import { Action as LocationAction } from 'history';
12
+ import UniversalRouter from 'universal-router';
13
+ import { history, prependBasename } from '../history';
14
+ import PageHandler from './page';
15
+ import stacks from './stack';
16
+ import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
17
+ import { RouterConfig } from '.';
18
+ export function createRouter(app, config, framework) {
19
+ var _a, _b;
20
+ RouterConfig.config = config;
21
+ const handler = new PageHandler(config);
22
+ const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
23
+ routesAlias.set(handler.router.customRoutes);
24
+ const basename = handler.router.basename;
25
+ const routes = handler.routes.map(route => {
26
+ const routePath = addLeadingSlash(route.path);
27
+ const paths = routesAlias.getAll(routePath);
28
+ return {
29
+ path: paths.length < 1 ? routePath : paths,
30
+ action: route.load
31
+ };
32
+ });
33
+ const router = new UniversalRouter(routes, { baseUrl: basename || '' });
34
+ const launchParam = handler.getQuery(stacks.length);
35
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
36
+ const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
37
+ var _c, _d, _e, _f, _g;
38
+ handler.pathname = decodeURI(location.pathname);
39
+ let element, params;
40
+ try {
41
+ const result = yield router.resolve(handler.router.forcePath || handler.pathname);
42
+ [element, , params] = yield Promise.all(result);
43
+ }
44
+ catch (error) {
45
+ if (error.status === 404) {
46
+ (_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, {
47
+ path: handler.pathname
48
+ });
49
+ }
50
+ else {
51
+ throw new Error(error);
52
+ }
53
+ }
54
+ if (!element)
55
+ return;
56
+ const pageConfig = handler.pageConfig;
57
+ let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
58
+ eventCenter.trigger('__taroRouterChange', {
59
+ toLocation: {
60
+ path: handler.pathname
61
+ }
62
+ });
63
+ if (pageConfig) {
64
+ document.title = (_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title;
65
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
66
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
67
+ }
68
+ }
69
+ const currentPage = Current.page;
70
+ const pathname = handler.pathname;
71
+ let shouldLoad = false;
72
+ if (action === 'POP') {
73
+ // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
74
+ const prevIndex = stacks.getPrevIndex(pathname);
75
+ const delta = stacks.getDelta(pathname);
76
+ handler.unload(currentPage, delta, prevIndex > -1);
77
+ if (prevIndex > -1) {
78
+ handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
79
+ }
80
+ else {
81
+ shouldLoad = true;
82
+ }
83
+ }
84
+ else {
85
+ if (handler.isTabBar) {
86
+ if (handler.isSamePage(currentPage))
87
+ return;
88
+ const prevIndex = stacks.getPrevIndex(pathname, 0);
89
+ handler.hide(currentPage);
90
+ if (prevIndex > -1) {
91
+ // NOTE: tabbar 页且之前出现过,直接复用
92
+ return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
93
+ }
94
+ }
95
+ else if (action === 'REPLACE') {
96
+ const delta = stacks.getDelta(pathname);
97
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
98
+ handler.unload(currentPage, delta);
99
+ }
100
+ else if (action === 'PUSH') {
101
+ handler.hide(currentPage);
102
+ }
103
+ shouldLoad = true;
104
+ }
105
+ if (shouldLoad || stacks.length < 1) {
106
+ const el = (_f = element.default) !== null && _f !== void 0 ? _f : element;
107
+ const loadConfig = Object.assign({}, pageConfig);
108
+ const stacksIndex = stacks.length;
109
+ delete loadConfig['path'];
110
+ delete loadConfig['load'];
111
+ const page = createPageConfig(enablePullDownRefresh ? (_g = runtimeHooks.createPullDownComponent) === null || _g === void 0 ? void 0 : _g.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
112
+ if (params)
113
+ page.options = params;
114
+ return handler.load(page, pageConfig, stacksIndex);
115
+ }
116
+ });
117
+ const routePath = stripBasename(history.location.pathname, handler.basename);
118
+ if (routePath === '/' || routePath === '') {
119
+ history.replace(prependBasename(handler.homePage + history.location.search));
120
+ }
121
+ render({ location: history.location, action: LocationAction.Push });
122
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
123
+ return history.listen(render);
124
+ }
@@ -0,0 +1,59 @@
1
+ class Stacks {
2
+ constructor() {
3
+ this.stacks = [];
4
+ this.backDelta = 0;
5
+ }
6
+ set delta(delta) {
7
+ if (delta > 0) {
8
+ this.backDelta = delta;
9
+ }
10
+ else if (this.backDelta > 0) {
11
+ --this.backDelta;
12
+ }
13
+ else {
14
+ this.backDelta = 0;
15
+ }
16
+ }
17
+ get delta() {
18
+ return this.backDelta;
19
+ }
20
+ get length() {
21
+ return this.stacks.length;
22
+ }
23
+ get last() {
24
+ return this.stacks[this.length - 1];
25
+ }
26
+ get() {
27
+ return this.stacks;
28
+ }
29
+ getItem(index) {
30
+ return this.stacks[index];
31
+ }
32
+ getLastIndex(pathname, stateWith = 1) {
33
+ const list = [...this.stacks].reverse();
34
+ return list.findIndex((page, i) => { var _a; return i >= stateWith && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
35
+ }
36
+ getDelta(pathname) {
37
+ if (this.backDelta >= 1) {
38
+ return this.backDelta;
39
+ }
40
+ const index = this.getLastIndex(pathname);
41
+ // NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
42
+ return index > 0 ? index : 1;
43
+ }
44
+ getPrevIndex(pathname, stateWith = 1) {
45
+ const lastIndex = this.getLastIndex(pathname, stateWith);
46
+ if (lastIndex < 0) {
47
+ return -1;
48
+ }
49
+ return this.length - 1 - lastIndex;
50
+ }
51
+ pop() {
52
+ return this.stacks.pop();
53
+ }
54
+ push(page) {
55
+ return this.stacks.push(page);
56
+ }
57
+ }
58
+ const stacks = new Stacks();
59
+ export default stacks;