@tarojs/router 3.4.5 → 3.5.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation.js +29 -0
- package/dist/api.js +91 -0
- package/dist/events/resize.js +13 -0
- package/dist/events/scroll.js +30 -0
- package/dist/history.js +99 -0
- package/dist/{router.esm.js → index.cjs.js} +375 -110
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.js +4 -688
- package/dist/router/index.js +23 -0
- package/dist/router/mpa.js +49 -0
- package/dist/router/multi-page.js +105 -0
- package/dist/router/page.js +265 -0
- package/dist/router/spa.js +110 -0
- package/dist/router/stack.js +57 -0
- package/dist/tabbar.js +29 -0
- package/dist/utils.js +39 -0
- package/package.json +9 -9
- package/types/index.d.ts +2 -1
- package/dist/index.js.map +0 -1
- package/dist/router.esm.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,688 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var history = require('history');
|
|
6
|
-
var runtime = require('@tarojs/runtime');
|
|
7
|
-
var UniversalRouter = require('universal-router');
|
|
8
|
-
var queryString = require('query-string');
|
|
9
|
-
var taro = require('@tarojs/taro');
|
|
10
|
-
|
|
11
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
-
|
|
13
|
-
var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
|
|
14
|
-
var queryString__default = /*#__PURE__*/_interopDefaultLegacy(queryString);
|
|
15
|
-
|
|
16
|
-
exports.history = void 0;
|
|
17
|
-
let basename = '/';
|
|
18
|
-
function setHistoryMode(mode, base = '/') {
|
|
19
|
-
const options = {
|
|
20
|
-
window
|
|
21
|
-
};
|
|
22
|
-
basename = base;
|
|
23
|
-
if (mode === 'browser') {
|
|
24
|
-
exports.history = history.createBrowserHistory(options);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
// default is hash
|
|
28
|
-
exports.history = history.createHashHistory(options);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function prependBasename(url = '') {
|
|
32
|
-
return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
|
|
33
|
-
}
|
|
34
|
-
const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path);
|
|
35
|
-
const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
|
|
36
|
-
|
|
37
|
-
class Stacks {
|
|
38
|
-
stacks = [];
|
|
39
|
-
backDelta = 0;
|
|
40
|
-
set delta(delta) {
|
|
41
|
-
if (delta > 0) {
|
|
42
|
-
this.backDelta = delta;
|
|
43
|
-
}
|
|
44
|
-
else if (this.backDelta > 0) {
|
|
45
|
-
--this.backDelta;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
this.backDelta = 0;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
get delta() {
|
|
52
|
-
return this.backDelta;
|
|
53
|
-
}
|
|
54
|
-
get length() {
|
|
55
|
-
return this.stacks.length;
|
|
56
|
-
}
|
|
57
|
-
get last() {
|
|
58
|
-
return this.stacks[this.length - 1];
|
|
59
|
-
}
|
|
60
|
-
get() {
|
|
61
|
-
return this.stacks;
|
|
62
|
-
}
|
|
63
|
-
getItem(index) {
|
|
64
|
-
return this.stacks[index];
|
|
65
|
-
}
|
|
66
|
-
getLastIndex(pathname, stateWith = 1) {
|
|
67
|
-
const list = [...this.stacks].reverse();
|
|
68
|
-
return list.findIndex((page, i) => i >= stateWith && page.path?.replace(/\?.*/g, '') === pathname);
|
|
69
|
-
}
|
|
70
|
-
getDelta(pathname) {
|
|
71
|
-
if (this.backDelta >= 1) {
|
|
72
|
-
return this.backDelta;
|
|
73
|
-
}
|
|
74
|
-
const index = this.getLastIndex(pathname);
|
|
75
|
-
// NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
|
|
76
|
-
return index > 0 ? index : 1;
|
|
77
|
-
}
|
|
78
|
-
getPrevIndex(pathname, stateWith = 1) {
|
|
79
|
-
const lastIndex = this.getLastIndex(pathname, stateWith);
|
|
80
|
-
if (lastIndex < 0) {
|
|
81
|
-
return -1;
|
|
82
|
-
}
|
|
83
|
-
return this.length - 1 - lastIndex;
|
|
84
|
-
}
|
|
85
|
-
pop() {
|
|
86
|
-
return this.stacks.pop();
|
|
87
|
-
}
|
|
88
|
-
push(page) {
|
|
89
|
-
return this.stacks.push(page);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
const stacks = new Stacks();
|
|
93
|
-
|
|
94
|
-
function addLeadingSlash(path) {
|
|
95
|
-
if (path == null) {
|
|
96
|
-
return '';
|
|
97
|
-
}
|
|
98
|
-
return path.charAt(0) === '/' ? path : '/' + path;
|
|
99
|
-
}
|
|
100
|
-
class RoutesAlias {
|
|
101
|
-
conf = [];
|
|
102
|
-
set(customRoutes = {}) {
|
|
103
|
-
for (let key in customRoutes) {
|
|
104
|
-
const path = customRoutes[key];
|
|
105
|
-
key = addLeadingSlash(key);
|
|
106
|
-
if (typeof path === 'string') {
|
|
107
|
-
this.conf.push([key, addLeadingSlash(path)]);
|
|
108
|
-
}
|
|
109
|
-
else if (path?.length > 0) {
|
|
110
|
-
this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
getConfig = (url = '') => {
|
|
115
|
-
const customRoute = this.conf.filter((arr) => {
|
|
116
|
-
return arr.includes(url);
|
|
117
|
-
});
|
|
118
|
-
return customRoute[0];
|
|
119
|
-
};
|
|
120
|
-
getOrigin = (url = '') => {
|
|
121
|
-
return this.getConfig(url)?.[0] || url;
|
|
122
|
-
};
|
|
123
|
-
getAlias = (url = '') => {
|
|
124
|
-
return this.getConfig(url)?.[1] || url;
|
|
125
|
-
};
|
|
126
|
-
getAll = (url = '') => {
|
|
127
|
-
return this.conf.filter((arr) => {
|
|
128
|
-
return arr.includes(url);
|
|
129
|
-
}).reduceRight((p, a) => {
|
|
130
|
-
p.unshift(a[1]);
|
|
131
|
-
return p;
|
|
132
|
-
}, [url]);
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const routesAlias = new RoutesAlias();
|
|
136
|
-
|
|
137
|
-
function processNavigateUrl(option) {
|
|
138
|
-
const pathPieces = history.parsePath(option.url);
|
|
139
|
-
// 处理相对路径
|
|
140
|
-
if (pathPieces.pathname?.includes('./')) {
|
|
141
|
-
const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
|
|
142
|
-
parts.pop();
|
|
143
|
-
pathPieces.pathname.split('/').forEach((item) => {
|
|
144
|
-
if (item === '.') {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
item === '..' ? parts.pop() : parts.push(item);
|
|
148
|
-
});
|
|
149
|
-
pathPieces.pathname = parts.join('/');
|
|
150
|
-
}
|
|
151
|
-
// 处理自定义路由
|
|
152
|
-
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
|
153
|
-
// 处理 basename
|
|
154
|
-
pathPieces.pathname = prependBasename(pathPieces.pathname);
|
|
155
|
-
// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
|
|
156
|
-
if (!pathPieces.search)
|
|
157
|
-
pathPieces.search = '';
|
|
158
|
-
return pathPieces;
|
|
159
|
-
}
|
|
160
|
-
async function navigate(option, method) {
|
|
161
|
-
return new Promise((resolve, reject) => {
|
|
162
|
-
const { success, complete, fail } = option;
|
|
163
|
-
const unListen = exports.history.listen(() => {
|
|
164
|
-
const res = { errMsg: `${method}:ok` };
|
|
165
|
-
success?.(res);
|
|
166
|
-
complete?.(res);
|
|
167
|
-
resolve(res);
|
|
168
|
-
unListen();
|
|
169
|
-
});
|
|
170
|
-
try {
|
|
171
|
-
if ('url' in option) {
|
|
172
|
-
const pathPieces = processNavigateUrl(option);
|
|
173
|
-
const state = { timestamp: Date.now() };
|
|
174
|
-
if (method === 'navigateTo') {
|
|
175
|
-
exports.history.push(pathPieces, state);
|
|
176
|
-
}
|
|
177
|
-
else if (method === 'redirectTo' || method === 'switchTab') {
|
|
178
|
-
exports.history.replace(pathPieces, state);
|
|
179
|
-
}
|
|
180
|
-
else if (method === 'reLaunch') {
|
|
181
|
-
stacks.delta = stacks.length;
|
|
182
|
-
exports.history.replace(pathPieces, state);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
else if (method === 'navigateBack') {
|
|
186
|
-
stacks.delta = option.delta;
|
|
187
|
-
exports.history.go(-option.delta);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
const res = { errMsg: `${method}:fail ${error.message || error}` };
|
|
192
|
-
fail?.(res);
|
|
193
|
-
complete?.(res);
|
|
194
|
-
reject(res);
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
function navigateTo(option) {
|
|
199
|
-
return navigate(option, 'navigateTo');
|
|
200
|
-
}
|
|
201
|
-
function redirectTo(option) {
|
|
202
|
-
return navigate(option, 'redirectTo');
|
|
203
|
-
}
|
|
204
|
-
function navigateBack(options = { delta: 1 }) {
|
|
205
|
-
if (!options.delta || options.delta < 1) {
|
|
206
|
-
options.delta = 1;
|
|
207
|
-
}
|
|
208
|
-
return navigate(options, 'navigateBack');
|
|
209
|
-
}
|
|
210
|
-
function switchTab(option) {
|
|
211
|
-
return navigate(option, 'switchTab');
|
|
212
|
-
}
|
|
213
|
-
function reLaunch(option) {
|
|
214
|
-
return navigate(option, 'reLaunch');
|
|
215
|
-
}
|
|
216
|
-
function getCurrentPages() {
|
|
217
|
-
const pages = stacks.get();
|
|
218
|
-
return pages.map(e => ({ ...e, route: e.path || '' }));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* 插入页面动画需要的样式
|
|
223
|
-
*/
|
|
224
|
-
function loadAnimateStyle(ms = 300) {
|
|
225
|
-
const css = `
|
|
226
|
-
.taro_router .taro_page {
|
|
227
|
-
position: absolute;
|
|
228
|
-
left: 0;
|
|
229
|
-
top: 0;
|
|
230
|
-
width: 100%;
|
|
231
|
-
height: 100%;
|
|
232
|
-
background-color: #fff;
|
|
233
|
-
transform: translate(100%, 0);
|
|
234
|
-
transition: transform ${ms}ms;
|
|
235
|
-
z-index: 0;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
.taro_router .taro_page.taro_tabbar_page,
|
|
239
|
-
.taro_router .taro_page.taro_page_show.taro_page_stationed {
|
|
240
|
-
transform: none;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
.taro_router .taro_page.taro_page_show {
|
|
244
|
-
transform: translate(0, 0);
|
|
245
|
-
}`;
|
|
246
|
-
const style = document.createElement('style');
|
|
247
|
-
style.innerHTML = css;
|
|
248
|
-
document.getElementsByTagName('head')[0].appendChild(style);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// @ts-nocheck
|
|
252
|
-
function initTabbar(config) {
|
|
253
|
-
if (config.tabBar == null) {
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
// TODO: custom-tab-bar
|
|
257
|
-
const tabbar = document.createElement('taro-tabbar');
|
|
258
|
-
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
259
|
-
tabbar.conf = config.tabBar;
|
|
260
|
-
tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
|
|
261
|
-
const routerConfig = config.router;
|
|
262
|
-
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
263
|
-
if (routerConfig.customRoutes) {
|
|
264
|
-
tabbar.conf.custom = true;
|
|
265
|
-
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
tabbar.conf.custom = false;
|
|
269
|
-
tabbar.conf.customRoutes = {};
|
|
270
|
-
}
|
|
271
|
-
if (typeof routerConfig.basename !== 'undefined') {
|
|
272
|
-
tabbar.conf.basename = routerConfig.basename;
|
|
273
|
-
}
|
|
274
|
-
const container = document.getElementById('container');
|
|
275
|
-
container?.appendChild(tabbar);
|
|
276
|
-
taro.initTabBarApis(config);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
let pageResizeFn;
|
|
280
|
-
function bindPageResize(page) {
|
|
281
|
-
window.removeEventListener('resize', pageResizeFn);
|
|
282
|
-
pageResizeFn = function () {
|
|
283
|
-
page.onResize && page.onResize({
|
|
284
|
-
size: {
|
|
285
|
-
windowHeight: window.innerHeight,
|
|
286
|
-
windowWidth: window.innerWidth
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
};
|
|
290
|
-
window.addEventListener('resize', pageResizeFn, false);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
let pageScrollFn;
|
|
294
|
-
let pageDOM = window;
|
|
295
|
-
function bindPageScroll(page, pageEl, distance = 50) {
|
|
296
|
-
pageEl.removeEventListener('scroll', pageScrollFn);
|
|
297
|
-
pageDOM = pageEl;
|
|
298
|
-
let isReachBottom = false;
|
|
299
|
-
pageScrollFn = function () {
|
|
300
|
-
page.onPageScroll && page.onPageScroll({
|
|
301
|
-
scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
|
|
302
|
-
});
|
|
303
|
-
if (isReachBottom && getOffset() > distance) {
|
|
304
|
-
isReachBottom = false;
|
|
305
|
-
}
|
|
306
|
-
if (page.onReachBottom &&
|
|
307
|
-
!isReachBottom &&
|
|
308
|
-
getOffset() < distance) {
|
|
309
|
-
isReachBottom = true;
|
|
310
|
-
page.onReachBottom();
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
pageDOM.addEventListener('scroll', pageScrollFn, false);
|
|
314
|
-
}
|
|
315
|
-
function getOffset() {
|
|
316
|
-
if (pageDOM instanceof Window) {
|
|
317
|
-
return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
function setDisplay(el, type = '') {
|
|
325
|
-
if (el) {
|
|
326
|
-
el.style.display = type;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
class PageHandler {
|
|
330
|
-
config;
|
|
331
|
-
defaultAnimation = { duration: 300, delay: 50 };
|
|
332
|
-
unloadTimer;
|
|
333
|
-
hideTimer;
|
|
334
|
-
lastHidePage;
|
|
335
|
-
lastUnloadPage;
|
|
336
|
-
constructor(config) {
|
|
337
|
-
this.config = config;
|
|
338
|
-
this.mount();
|
|
339
|
-
}
|
|
340
|
-
get appId() { return 'app'; }
|
|
341
|
-
get router() { return this.config.router; }
|
|
342
|
-
get routerMode() { return this.router.mode || 'hash'; }
|
|
343
|
-
get customRoutes() { return this.router.customRoutes || {}; }
|
|
344
|
-
get routes() { return this.config.routes; }
|
|
345
|
-
get tabBarList() { return this.config.tabBar?.list || []; }
|
|
346
|
-
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
347
|
-
get animation() { return this.config?.animation ?? this.defaultAnimation; }
|
|
348
|
-
get animationDelay() {
|
|
349
|
-
return (typeof this.animation === 'object'
|
|
350
|
-
? this.animation.delay
|
|
351
|
-
: this.animation
|
|
352
|
-
? this.defaultAnimation?.delay
|
|
353
|
-
: 0) || 0;
|
|
354
|
-
}
|
|
355
|
-
get animationDuration() {
|
|
356
|
-
return (typeof this.animation === 'object'
|
|
357
|
-
? this.animation.duration
|
|
358
|
-
: this.animation
|
|
359
|
-
? this.defaultAnimation?.duration
|
|
360
|
-
: 0) || 0;
|
|
361
|
-
}
|
|
362
|
-
set pathname(p) { this.router.pathname = p; }
|
|
363
|
-
get pathname() { return this.router.pathname; }
|
|
364
|
-
get basename() { return this.router.basename || ''; }
|
|
365
|
-
get pageConfig() {
|
|
366
|
-
return this.routes.find(r => {
|
|
367
|
-
const routePath = stripBasename(this.pathname, this.basename);
|
|
368
|
-
const pagePath = addLeadingSlash(r.path);
|
|
369
|
-
return pagePath === routePath || routesAlias.getConfig(pagePath)?.includes(routePath);
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
get isTabBar() {
|
|
373
|
-
const routePath = stripBasename(this.pathname, this.basename);
|
|
374
|
-
const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
|
|
375
|
-
if (typeof target === 'string') {
|
|
376
|
-
return target === routePath;
|
|
377
|
-
}
|
|
378
|
-
else if (target?.length > 0) {
|
|
379
|
-
return target.includes(routePath);
|
|
380
|
-
}
|
|
381
|
-
return false;
|
|
382
|
-
})?.[0] || routePath;
|
|
383
|
-
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
384
|
-
}
|
|
385
|
-
isSamePage(page) {
|
|
386
|
-
const routePath = stripBasename(this.pathname, this.basename);
|
|
387
|
-
const pagePath = stripBasename(page?.path, this.basename);
|
|
388
|
-
return pagePath.startsWith(routePath + '?');
|
|
389
|
-
}
|
|
390
|
-
get search() {
|
|
391
|
-
let search = '?';
|
|
392
|
-
if (this.routerMode === 'hash') {
|
|
393
|
-
const idx = location.hash.indexOf('?');
|
|
394
|
-
if (idx > -1) {
|
|
395
|
-
search = location.hash.slice(idx);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
search = location.search;
|
|
400
|
-
}
|
|
401
|
-
return search.substr(1);
|
|
402
|
-
}
|
|
403
|
-
getQuery(stamp = 0, search = '', options = {}) {
|
|
404
|
-
search = search ? `${search}&${this.search}` : this.search;
|
|
405
|
-
const query = search
|
|
406
|
-
? queryString__default["default"].parse(search, { decode: false })
|
|
407
|
-
: {};
|
|
408
|
-
query.stamp = stamp.toString();
|
|
409
|
-
return { ...query, ...options };
|
|
410
|
-
}
|
|
411
|
-
mount() {
|
|
412
|
-
setHistoryMode(this.routerMode, this.router.basename);
|
|
413
|
-
document.getElementById('app')?.remove();
|
|
414
|
-
this.animation && loadAnimateStyle(this.animationDuration);
|
|
415
|
-
const app = document.createElement('div');
|
|
416
|
-
app.id = this.appId;
|
|
417
|
-
app.classList.add('taro_router');
|
|
418
|
-
if (this.tabBarList.length > 1) {
|
|
419
|
-
const container = document.createElement('div');
|
|
420
|
-
container.classList.add('taro-tabbar__container');
|
|
421
|
-
container.id = 'container';
|
|
422
|
-
const panel = document.createElement('div');
|
|
423
|
-
panel.classList.add('taro-tabbar__panel');
|
|
424
|
-
panel.appendChild(app);
|
|
425
|
-
container.appendChild(panel);
|
|
426
|
-
document.body.appendChild(container);
|
|
427
|
-
initTabbar(this.config);
|
|
428
|
-
}
|
|
429
|
-
else {
|
|
430
|
-
document.body.appendChild(app);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
onReady(page, onLoad = true) {
|
|
434
|
-
const pageEl = this.getPageContainer(page);
|
|
435
|
-
if (pageEl && !pageEl?.['__isReady']) {
|
|
436
|
-
const el = pageEl.firstElementChild;
|
|
437
|
-
el?.['componentOnReady']?.()?.then(() => {
|
|
438
|
-
runtime.requestAnimationFrame(() => {
|
|
439
|
-
page.onReady?.();
|
|
440
|
-
pageEl['__isReady'] = true;
|
|
441
|
-
});
|
|
442
|
-
});
|
|
443
|
-
onLoad && (pageEl['__page'] = page);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
load(page, pageConfig = {}, stacksIndex = 0) {
|
|
447
|
-
if (!page)
|
|
448
|
-
return;
|
|
449
|
-
// NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
|
|
450
|
-
stacks.push(page);
|
|
451
|
-
const param = this.getQuery(stacks.length, '', page.options);
|
|
452
|
-
let pageEl = this.getPageContainer(page);
|
|
453
|
-
if (pageEl) {
|
|
454
|
-
setDisplay(pageEl);
|
|
455
|
-
this.isTabBar && pageEl.classList.add('taro_tabbar_page');
|
|
456
|
-
this.addAnimation(pageEl, stacksIndex === 0);
|
|
457
|
-
page.onShow?.();
|
|
458
|
-
this.bindPageEvents(page, pageEl, pageConfig);
|
|
459
|
-
}
|
|
460
|
-
else {
|
|
461
|
-
page.onLoad?.(param, () => {
|
|
462
|
-
pageEl = this.getPageContainer(page);
|
|
463
|
-
this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
|
|
464
|
-
this.addAnimation(pageEl, stacksIndex === 0);
|
|
465
|
-
this.onReady(page, true);
|
|
466
|
-
page.onShow?.();
|
|
467
|
-
this.bindPageEvents(page, pageEl, pageConfig);
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
unload(page, delta = 1, top = false) {
|
|
472
|
-
if (!page)
|
|
473
|
-
return;
|
|
474
|
-
stacks.delta = --delta;
|
|
475
|
-
stacks.pop();
|
|
476
|
-
if (this.animation && top) {
|
|
477
|
-
if (this.unloadTimer) {
|
|
478
|
-
clearTimeout(this.unloadTimer);
|
|
479
|
-
this.lastUnloadPage?.onUnload?.();
|
|
480
|
-
this.unloadTimer = null;
|
|
481
|
-
}
|
|
482
|
-
this.lastUnloadPage = page;
|
|
483
|
-
const pageEl = this.getPageContainer(page);
|
|
484
|
-
pageEl?.classList.remove('taro_page_stationed');
|
|
485
|
-
pageEl?.classList.remove('taro_page_show');
|
|
486
|
-
this.unloadTimer = setTimeout(() => {
|
|
487
|
-
this.unloadTimer = null;
|
|
488
|
-
this.lastUnloadPage?.onUnload?.();
|
|
489
|
-
}, this.animationDuration);
|
|
490
|
-
}
|
|
491
|
-
else {
|
|
492
|
-
const pageEl = this.getPageContainer(page);
|
|
493
|
-
pageEl?.classList.remove('taro_page_stationed');
|
|
494
|
-
pageEl?.classList.remove('taro_page_show');
|
|
495
|
-
page?.onUnload?.();
|
|
496
|
-
}
|
|
497
|
-
if (delta >= 1)
|
|
498
|
-
this.unload(stacks.last, delta);
|
|
499
|
-
}
|
|
500
|
-
show(page, pageConfig = {}, stacksIndex = 0) {
|
|
501
|
-
if (!page)
|
|
502
|
-
return;
|
|
503
|
-
const param = this.getQuery(stacks.length, '', page.options);
|
|
504
|
-
let pageEl = this.getPageContainer(page);
|
|
505
|
-
if (pageEl) {
|
|
506
|
-
setDisplay(pageEl);
|
|
507
|
-
this.addAnimation(pageEl, stacksIndex === 0);
|
|
508
|
-
page.onShow?.();
|
|
509
|
-
this.bindPageEvents(page, pageEl, pageConfig);
|
|
510
|
-
}
|
|
511
|
-
else {
|
|
512
|
-
page.onLoad?.(param, () => {
|
|
513
|
-
pageEl = this.getPageContainer(page);
|
|
514
|
-
this.addAnimation(pageEl, stacksIndex === 0);
|
|
515
|
-
this.onReady(page, false);
|
|
516
|
-
page.onShow?.();
|
|
517
|
-
this.bindPageEvents(page, pageEl, pageConfig);
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
hide(page) {
|
|
522
|
-
if (!page)
|
|
523
|
-
return;
|
|
524
|
-
// NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
|
|
525
|
-
const pageEl = this.getPageContainer(page);
|
|
526
|
-
if (pageEl) {
|
|
527
|
-
if (this.hideTimer) {
|
|
528
|
-
clearTimeout(this.hideTimer);
|
|
529
|
-
this.hideTimer = null;
|
|
530
|
-
setDisplay(this.lastHidePage, 'none');
|
|
531
|
-
}
|
|
532
|
-
this.lastHidePage = pageEl;
|
|
533
|
-
this.hideTimer = setTimeout(() => {
|
|
534
|
-
this.hideTimer = null;
|
|
535
|
-
setDisplay(this.lastHidePage, 'none');
|
|
536
|
-
}, this.animationDuration + this.animationDelay);
|
|
537
|
-
page.onHide?.();
|
|
538
|
-
}
|
|
539
|
-
else {
|
|
540
|
-
setTimeout(() => this.hide(page), 0);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
addAnimation(pageEl, first = false) {
|
|
544
|
-
if (!pageEl)
|
|
545
|
-
return;
|
|
546
|
-
if (this.animation && !first) {
|
|
547
|
-
setTimeout(() => {
|
|
548
|
-
pageEl.classList.add('taro_page_show');
|
|
549
|
-
setTimeout(() => {
|
|
550
|
-
pageEl.classList.add('taro_page_stationed');
|
|
551
|
-
}, this.animationDuration);
|
|
552
|
-
}, this.animationDelay);
|
|
553
|
-
}
|
|
554
|
-
else {
|
|
555
|
-
pageEl.classList.add('taro_page_show');
|
|
556
|
-
pageEl.classList.add('taro_page_stationed');
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
getPageContainer(page) {
|
|
560
|
-
const path = page ? page?.path : runtime.Current.page?.path;
|
|
561
|
-
const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
562
|
-
if (page) {
|
|
563
|
-
return document.querySelector(`.taro_page#${id}`);
|
|
564
|
-
}
|
|
565
|
-
const el = (id
|
|
566
|
-
? document.querySelector(`.taro_page#${id}`)
|
|
567
|
-
: document.querySelector('.taro_page') ||
|
|
568
|
-
document.querySelector('.taro_router'));
|
|
569
|
-
return el || window;
|
|
570
|
-
}
|
|
571
|
-
bindPageEvents(page, pageEl, config = {}) {
|
|
572
|
-
if (!pageEl) {
|
|
573
|
-
pageEl = this.getPageContainer();
|
|
574
|
-
}
|
|
575
|
-
const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
|
|
576
|
-
bindPageScroll(page, pageEl, distance);
|
|
577
|
-
bindPageResize(page);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
/* eslint-disable dot-notation */
|
|
582
|
-
function createRouter(app, config, framework) {
|
|
583
|
-
const handler = new PageHandler(config);
|
|
584
|
-
const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
|
|
585
|
-
routesAlias.set(handler.router.customRoutes);
|
|
586
|
-
const basename = handler.router.basename;
|
|
587
|
-
const routes = handler.routes.map(route => ({
|
|
588
|
-
path: routesAlias.getAll(addLeadingSlash(route.path)),
|
|
589
|
-
action: route.load
|
|
590
|
-
}));
|
|
591
|
-
const entryPagePath = config.entryPagePath || routes[0].path?.[0];
|
|
592
|
-
const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
|
|
593
|
-
const launchParam = handler.getQuery(stacks.length);
|
|
594
|
-
app.onLaunch?.(launchParam);
|
|
595
|
-
const render = async ({ location, action }) => {
|
|
596
|
-
handler.pathname = location.pathname;
|
|
597
|
-
let element;
|
|
598
|
-
try {
|
|
599
|
-
element = await router.resolve(handler.router.forcePath || handler.pathname);
|
|
600
|
-
}
|
|
601
|
-
catch (error) {
|
|
602
|
-
if (error.status === 404) {
|
|
603
|
-
app.onPageNotFound?.({
|
|
604
|
-
path: handler.pathname
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
else {
|
|
608
|
-
throw new Error(error);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
if (!element)
|
|
612
|
-
return;
|
|
613
|
-
const pageConfig = handler.pageConfig;
|
|
614
|
-
let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
|
|
615
|
-
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
616
|
-
toLocation: {
|
|
617
|
-
path: handler.pathname
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
if (pageConfig) {
|
|
621
|
-
document.title = pageConfig.navigationBarTitleText ?? document.title;
|
|
622
|
-
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
623
|
-
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
const currentPage = runtime.Current.page;
|
|
627
|
-
const pathname = handler.pathname;
|
|
628
|
-
let shouldLoad = false;
|
|
629
|
-
if (action === 'POP') {
|
|
630
|
-
// NOTE: 浏览器事件退后多次时,该事件只会被触发一次
|
|
631
|
-
const prevIndex = stacks.getPrevIndex(pathname);
|
|
632
|
-
const delta = stacks.getDelta(pathname);
|
|
633
|
-
handler.unload(currentPage, delta, prevIndex > -1);
|
|
634
|
-
if (prevIndex > -1) {
|
|
635
|
-
handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
|
|
636
|
-
}
|
|
637
|
-
else {
|
|
638
|
-
shouldLoad = true;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
else {
|
|
642
|
-
if (handler.isTabBar) {
|
|
643
|
-
if (handler.isSamePage(currentPage))
|
|
644
|
-
return;
|
|
645
|
-
const prevIndex = stacks.getPrevIndex(pathname, 0);
|
|
646
|
-
handler.hide(currentPage);
|
|
647
|
-
if (prevIndex > -1) {
|
|
648
|
-
// NOTE: tabbar 页且之前出现过,直接复用
|
|
649
|
-
return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
else if (action === 'REPLACE') {
|
|
653
|
-
const delta = stacks.getDelta(pathname);
|
|
654
|
-
// NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
|
|
655
|
-
handler.unload(currentPage, delta);
|
|
656
|
-
}
|
|
657
|
-
else if (action === 'PUSH') {
|
|
658
|
-
handler.hide(currentPage);
|
|
659
|
-
}
|
|
660
|
-
shouldLoad = true;
|
|
661
|
-
}
|
|
662
|
-
if (shouldLoad || stacks.length < 1) {
|
|
663
|
-
const el = element.default ?? element;
|
|
664
|
-
const loadConfig = { ...pageConfig };
|
|
665
|
-
const stacksIndex = stacks.length;
|
|
666
|
-
delete loadConfig['path'];
|
|
667
|
-
delete loadConfig['load'];
|
|
668
|
-
const page = runtime.createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
|
|
669
|
-
return handler.load(page, pageConfig, stacksIndex);
|
|
670
|
-
}
|
|
671
|
-
};
|
|
672
|
-
const stripped = stripBasename(exports.history.location.pathname, handler.basename);
|
|
673
|
-
if (stripped === '/' || stripped === '') {
|
|
674
|
-
exports.history.replace(prependBasename(entryPagePath + exports.history.location.search));
|
|
675
|
-
}
|
|
676
|
-
render({ location: exports.history.location, action: history.Action.Push });
|
|
677
|
-
app.onShow?.(launchParam);
|
|
678
|
-
return exports.history.listen(render);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
exports.createRouter = createRouter;
|
|
682
|
-
exports.getCurrentPages = getCurrentPages;
|
|
683
|
-
exports.navigateBack = navigateBack;
|
|
684
|
-
exports.navigateTo = navigateTo;
|
|
685
|
-
exports.reLaunch = reLaunch;
|
|
686
|
-
exports.redirectTo = redirectTo;
|
|
687
|
-
exports.switchTab = switchTab;
|
|
688
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { history } from './history';
|
|
2
|
+
export * from './api';
|
|
3
|
+
export { createRouter } from './router/spa';
|
|
4
|
+
export { createMultiRouter } from './router/mpa';
|