@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.
- package/dist/animation.js +29 -0
- package/dist/api.js +103 -0
- package/dist/events/resize.js +13 -0
- package/dist/events/scroll.js +30 -0
- package/dist/history.js +100 -0
- package/dist/index.cjs.js +1000 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +984 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +4 -688
- package/dist/router/index.js +22 -0
- package/dist/router/mpa.js +60 -0
- package/dist/router/multi-page.js +111 -0
- package/dist/router/page.js +280 -0
- package/dist/router/spa.js +124 -0
- package/dist/router/stack.js +59 -0
- package/dist/tabbar.js +29 -0
- package/dist/utils.js +43 -0
- package/package.json +9 -8
- package/types/api.d.ts +15 -0
- package/types/history.d.ts +8 -0
- package/types/router.d.ts +32 -0
- package/dist/index.js.map +0 -1
- package/dist/router.esm.js +0 -673
- package/dist/router.esm.js.map +0 -1
- package/types/index.d.ts +0 -287
|
@@ -0,0 +1,1000 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
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
|
+
const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
|
|
17
|
+
const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
|
|
18
|
+
const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
|
|
19
|
+
class RoutesAlias {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.conf = [];
|
|
22
|
+
this.getConfig = (url = '') => {
|
|
23
|
+
const customRoute = this.conf.filter((arr) => {
|
|
24
|
+
return arr.includes(url);
|
|
25
|
+
});
|
|
26
|
+
return customRoute[0];
|
|
27
|
+
};
|
|
28
|
+
this.getOrigin = (url = '') => {
|
|
29
|
+
var _a;
|
|
30
|
+
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
|
|
31
|
+
};
|
|
32
|
+
this.getAlias = (url = '') => {
|
|
33
|
+
var _a;
|
|
34
|
+
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
|
|
35
|
+
};
|
|
36
|
+
this.getAll = (url = '') => {
|
|
37
|
+
return this.conf
|
|
38
|
+
.filter((arr) => arr.includes(url))
|
|
39
|
+
.reduceRight((p, a) => {
|
|
40
|
+
p.unshift(a[1]);
|
|
41
|
+
return p;
|
|
42
|
+
}, []);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
set(customRoutes = {}) {
|
|
46
|
+
for (let key in customRoutes) {
|
|
47
|
+
const path = customRoutes[key];
|
|
48
|
+
key = addLeadingSlash(key);
|
|
49
|
+
if (typeof path === 'string') {
|
|
50
|
+
this.conf.push([key, addLeadingSlash(path)]);
|
|
51
|
+
}
|
|
52
|
+
else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
|
|
53
|
+
this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const routesAlias = new RoutesAlias();
|
|
59
|
+
|
|
60
|
+
class RouterConfig {
|
|
61
|
+
static set config(e) {
|
|
62
|
+
this.__config = e;
|
|
63
|
+
}
|
|
64
|
+
static get config() {
|
|
65
|
+
return this.__config;
|
|
66
|
+
}
|
|
67
|
+
static get pages() {
|
|
68
|
+
return this.config.pages || [];
|
|
69
|
+
}
|
|
70
|
+
static get router() {
|
|
71
|
+
return this.config.router || {};
|
|
72
|
+
}
|
|
73
|
+
static get mode() {
|
|
74
|
+
return this.router.mode || 'hash';
|
|
75
|
+
}
|
|
76
|
+
static get customRoutes() { return this.router.customRoutes || {}; }
|
|
77
|
+
static isPage(url = '') {
|
|
78
|
+
return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
exports.history = void 0;
|
|
83
|
+
let basename = '/';
|
|
84
|
+
class MpaHistory {
|
|
85
|
+
constructor() {
|
|
86
|
+
this.back = window.history.back;
|
|
87
|
+
this.forward = window.history.forward;
|
|
88
|
+
this.pushState = this.eventState('pushState');
|
|
89
|
+
this.replaceState = this.eventState('replaceState');
|
|
90
|
+
}
|
|
91
|
+
get location() {
|
|
92
|
+
return {
|
|
93
|
+
pathname: window.location.pathname,
|
|
94
|
+
search: window.location.search,
|
|
95
|
+
hash: window.location.hash,
|
|
96
|
+
key: `${window.history.length}`,
|
|
97
|
+
state: window.history.state
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
createHref(_to) {
|
|
101
|
+
throw new Error('Method not implemented.');
|
|
102
|
+
}
|
|
103
|
+
parseUrl(to) {
|
|
104
|
+
let url = to.pathname || '';
|
|
105
|
+
if (RouterConfig.isPage(url)) {
|
|
106
|
+
url += '.html';
|
|
107
|
+
}
|
|
108
|
+
if (to.search) {
|
|
109
|
+
url += `?${to.search}`;
|
|
110
|
+
}
|
|
111
|
+
if (to.hash) {
|
|
112
|
+
url += `#${to.hash}`;
|
|
113
|
+
}
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
push(to, _state = {}) {
|
|
117
|
+
window.location.pathname = this.parseUrl(to);
|
|
118
|
+
// this.pushState(_state, '', this.parseUrl(to))
|
|
119
|
+
}
|
|
120
|
+
replace(to, _state = {}) {
|
|
121
|
+
window.location.replace(this.parseUrl(to));
|
|
122
|
+
// this.replaceState(_state, '', this.parseUrl(to))
|
|
123
|
+
}
|
|
124
|
+
go(delta) {
|
|
125
|
+
window.history.go(delta);
|
|
126
|
+
}
|
|
127
|
+
listen(listener) {
|
|
128
|
+
function callback(e) {
|
|
129
|
+
if (e.action === 'pushState') {
|
|
130
|
+
listener({ action: history.Action.Push, location: this.location });
|
|
131
|
+
}
|
|
132
|
+
else if (e.action === 'replaceState') {
|
|
133
|
+
listener({ action: history.Action.Replace, location: this.location });
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
// NOTE: 这里包括 back、forward、go 三种可能,并非是 POP 事件
|
|
137
|
+
listener({ action: history.Action.Pop, location: this.location });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
window.addEventListener('popstate', callback);
|
|
141
|
+
return () => {
|
|
142
|
+
window.removeEventListener('popstate', callback);
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
block(_blocker) {
|
|
146
|
+
throw new Error('Method not implemented.');
|
|
147
|
+
}
|
|
148
|
+
eventState(action) {
|
|
149
|
+
return (data, unused, url) => {
|
|
150
|
+
const wrapper = window.history[action](data, unused, url);
|
|
151
|
+
const evt = new Event(action);
|
|
152
|
+
evt.action = action;
|
|
153
|
+
evt.state = data;
|
|
154
|
+
evt.unused = unused;
|
|
155
|
+
evt.url = url;
|
|
156
|
+
window.dispatchEvent(evt);
|
|
157
|
+
return wrapper;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function setHistoryMode(mode, base = '/') {
|
|
162
|
+
const options = {
|
|
163
|
+
window
|
|
164
|
+
};
|
|
165
|
+
basename = base;
|
|
166
|
+
if (mode === 'browser') {
|
|
167
|
+
exports.history = history.createBrowserHistory(options);
|
|
168
|
+
}
|
|
169
|
+
else if (mode === 'multi') {
|
|
170
|
+
exports.history = new MpaHistory();
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
// default is hash
|
|
174
|
+
exports.history = history.createHashHistory(options);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function prependBasename(url = '') {
|
|
178
|
+
return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/*! *****************************************************************************
|
|
182
|
+
Copyright (c) Microsoft Corporation.
|
|
183
|
+
|
|
184
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
185
|
+
purpose with or without fee is hereby granted.
|
|
186
|
+
|
|
187
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
188
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
189
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
190
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
191
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
192
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
193
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
194
|
+
***************************************************************************** */
|
|
195
|
+
|
|
196
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
197
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
198
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
199
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
200
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
201
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
202
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
class Stacks {
|
|
207
|
+
constructor() {
|
|
208
|
+
this.stacks = [];
|
|
209
|
+
this.backDelta = 0;
|
|
210
|
+
}
|
|
211
|
+
set delta(delta) {
|
|
212
|
+
if (delta > 0) {
|
|
213
|
+
this.backDelta = delta;
|
|
214
|
+
}
|
|
215
|
+
else if (this.backDelta > 0) {
|
|
216
|
+
--this.backDelta;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this.backDelta = 0;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
get delta() {
|
|
223
|
+
return this.backDelta;
|
|
224
|
+
}
|
|
225
|
+
get length() {
|
|
226
|
+
return this.stacks.length;
|
|
227
|
+
}
|
|
228
|
+
get last() {
|
|
229
|
+
return this.stacks[this.length - 1];
|
|
230
|
+
}
|
|
231
|
+
get() {
|
|
232
|
+
return this.stacks;
|
|
233
|
+
}
|
|
234
|
+
getItem(index) {
|
|
235
|
+
return this.stacks[index];
|
|
236
|
+
}
|
|
237
|
+
getLastIndex(pathname, stateWith = 1) {
|
|
238
|
+
const list = [...this.stacks].reverse();
|
|
239
|
+
return list.findIndex((page, i) => { var _a; return i >= stateWith && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
|
|
240
|
+
}
|
|
241
|
+
getDelta(pathname) {
|
|
242
|
+
if (this.backDelta >= 1) {
|
|
243
|
+
return this.backDelta;
|
|
244
|
+
}
|
|
245
|
+
const index = this.getLastIndex(pathname);
|
|
246
|
+
// NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
|
|
247
|
+
return index > 0 ? index : 1;
|
|
248
|
+
}
|
|
249
|
+
getPrevIndex(pathname, stateWith = 1) {
|
|
250
|
+
const lastIndex = this.getLastIndex(pathname, stateWith);
|
|
251
|
+
if (lastIndex < 0) {
|
|
252
|
+
return -1;
|
|
253
|
+
}
|
|
254
|
+
return this.length - 1 - lastIndex;
|
|
255
|
+
}
|
|
256
|
+
pop() {
|
|
257
|
+
return this.stacks.pop();
|
|
258
|
+
}
|
|
259
|
+
push(page) {
|
|
260
|
+
return this.stacks.push(page);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const stacks = new Stacks();
|
|
264
|
+
|
|
265
|
+
function processNavigateUrl(option) {
|
|
266
|
+
var _a;
|
|
267
|
+
const pathPieces = history.parsePath(option.url);
|
|
268
|
+
// 处理相对路径
|
|
269
|
+
if ((_a = pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
|
|
270
|
+
const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
|
|
271
|
+
parts.pop();
|
|
272
|
+
pathPieces.pathname.split('/').forEach((item) => {
|
|
273
|
+
if (item === '.') {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
item === '..' ? parts.pop() : parts.push(item);
|
|
277
|
+
});
|
|
278
|
+
pathPieces.pathname = parts.join('/');
|
|
279
|
+
}
|
|
280
|
+
// 处理自定义路由
|
|
281
|
+
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
|
282
|
+
// 处理 basename
|
|
283
|
+
pathPieces.pathname = prependBasename(pathPieces.pathname);
|
|
284
|
+
// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
|
|
285
|
+
if (!pathPieces.search)
|
|
286
|
+
pathPieces.search = '';
|
|
287
|
+
return pathPieces;
|
|
288
|
+
}
|
|
289
|
+
function navigate(option, method) {
|
|
290
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
const { success, complete, fail } = option;
|
|
293
|
+
const unListen = exports.history.listen(() => {
|
|
294
|
+
const res = { errMsg: `${method}:ok` };
|
|
295
|
+
success === null || success === void 0 ? void 0 : success(res);
|
|
296
|
+
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
297
|
+
resolve(res);
|
|
298
|
+
unListen();
|
|
299
|
+
});
|
|
300
|
+
try {
|
|
301
|
+
if ('url' in option) {
|
|
302
|
+
const pathPieces = processNavigateUrl(option);
|
|
303
|
+
const state = { timestamp: Date.now() };
|
|
304
|
+
if (method === 'navigateTo') {
|
|
305
|
+
exports.history.push(pathPieces, state);
|
|
306
|
+
}
|
|
307
|
+
else if (method === 'redirectTo' || method === 'switchTab') {
|
|
308
|
+
exports.history.replace(pathPieces, state);
|
|
309
|
+
}
|
|
310
|
+
else if (method === 'reLaunch') {
|
|
311
|
+
stacks.delta = stacks.length;
|
|
312
|
+
exports.history.replace(pathPieces, state);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else if (method === 'navigateBack') {
|
|
316
|
+
stacks.delta = option.delta;
|
|
317
|
+
exports.history.go(-option.delta);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
const res = { errMsg: `${method}:fail ${error.message || error}` };
|
|
322
|
+
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
323
|
+
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
324
|
+
reject(res);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
function navigateTo(option) {
|
|
330
|
+
return navigate(option, 'navigateTo');
|
|
331
|
+
}
|
|
332
|
+
function redirectTo(option) {
|
|
333
|
+
return navigate(option, 'redirectTo');
|
|
334
|
+
}
|
|
335
|
+
function navigateBack(option = { delta: 1 }) {
|
|
336
|
+
if (!option.delta || option.delta < 1) {
|
|
337
|
+
option.delta = 1;
|
|
338
|
+
}
|
|
339
|
+
return navigate(option, 'navigateBack');
|
|
340
|
+
}
|
|
341
|
+
function switchTab(option) {
|
|
342
|
+
return navigate(option, 'switchTab');
|
|
343
|
+
}
|
|
344
|
+
function reLaunch(option) {
|
|
345
|
+
return navigate(option, 'reLaunch');
|
|
346
|
+
}
|
|
347
|
+
function getCurrentPages() {
|
|
348
|
+
if (process.env.NODE_ENV === 'development' && RouterConfig.mode === 'multi') {
|
|
349
|
+
console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
|
|
350
|
+
}
|
|
351
|
+
const pages = stacks.get();
|
|
352
|
+
return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 插入页面动画需要的样式
|
|
357
|
+
*/
|
|
358
|
+
function loadAnimateStyle(ms = 300) {
|
|
359
|
+
const css = `
|
|
360
|
+
.taro_router .taro_page {
|
|
361
|
+
position: absolute;
|
|
362
|
+
left: 0;
|
|
363
|
+
top: 0;
|
|
364
|
+
width: 100%;
|
|
365
|
+
height: 100%;
|
|
366
|
+
background-color: #fff;
|
|
367
|
+
transform: translate(100%, 0);
|
|
368
|
+
transition: transform ${ms}ms;
|
|
369
|
+
z-index: 0;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
.taro_router .taro_page.taro_tabbar_page,
|
|
373
|
+
.taro_router .taro_page.taro_page_show.taro_page_stationed {
|
|
374
|
+
transform: none;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
.taro_router .taro_page.taro_page_show {
|
|
378
|
+
transform: translate(0, 0);
|
|
379
|
+
}`;
|
|
380
|
+
const style = document.createElement('style');
|
|
381
|
+
style.innerHTML = css;
|
|
382
|
+
document.getElementsByTagName('head')[0].appendChild(style);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let pageResizeFn;
|
|
386
|
+
function bindPageResize(page) {
|
|
387
|
+
pageResizeFn && window.removeEventListener('resize', pageResizeFn);
|
|
388
|
+
pageResizeFn = function () {
|
|
389
|
+
page.onResize && page.onResize({
|
|
390
|
+
size: {
|
|
391
|
+
windowHeight: window.innerHeight,
|
|
392
|
+
windowWidth: window.innerWidth
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
window.addEventListener('resize', pageResizeFn, false);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
let pageScrollFn;
|
|
400
|
+
let pageDOM = window;
|
|
401
|
+
function bindPageScroll(page, pageEl, distance = 50) {
|
|
402
|
+
pageScrollFn && pageEl.removeEventListener('scroll', pageScrollFn);
|
|
403
|
+
pageDOM = pageEl;
|
|
404
|
+
let isReachBottom = false;
|
|
405
|
+
pageScrollFn = function () {
|
|
406
|
+
page.onPageScroll && page.onPageScroll({
|
|
407
|
+
scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
|
|
408
|
+
});
|
|
409
|
+
if (isReachBottom && getOffset() > distance) {
|
|
410
|
+
isReachBottom = false;
|
|
411
|
+
}
|
|
412
|
+
if (page.onReachBottom &&
|
|
413
|
+
!isReachBottom &&
|
|
414
|
+
getOffset() < distance) {
|
|
415
|
+
isReachBottom = true;
|
|
416
|
+
page.onReachBottom();
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
pageDOM.addEventListener('scroll', pageScrollFn, false);
|
|
420
|
+
}
|
|
421
|
+
function getOffset() {
|
|
422
|
+
if (pageDOM instanceof Window) {
|
|
423
|
+
return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// @ts-nocheck
|
|
431
|
+
function initTabbar(config) {
|
|
432
|
+
if (config.tabBar == null) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
// TODO: custom-tab-bar
|
|
436
|
+
const tabbar = document.createElement('taro-tabbar');
|
|
437
|
+
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
438
|
+
tabbar.conf = config.tabBar;
|
|
439
|
+
tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
|
|
440
|
+
const routerConfig = config.router;
|
|
441
|
+
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
442
|
+
if (routerConfig.customRoutes) {
|
|
443
|
+
tabbar.conf.custom = true;
|
|
444
|
+
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
tabbar.conf.custom = false;
|
|
448
|
+
tabbar.conf.customRoutes = {};
|
|
449
|
+
}
|
|
450
|
+
if (typeof routerConfig.basename !== 'undefined') {
|
|
451
|
+
tabbar.conf.basename = routerConfig.basename;
|
|
452
|
+
}
|
|
453
|
+
const container = document.getElementById('container');
|
|
454
|
+
container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
|
|
455
|
+
taro.initTabBarApis(config);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function setDisplay(el, type = '') {
|
|
459
|
+
if (el) {
|
|
460
|
+
el.style.display = type;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
class PageHandler {
|
|
464
|
+
constructor(config) {
|
|
465
|
+
this.defaultAnimation = { duration: 300, delay: 50 };
|
|
466
|
+
this.config = config;
|
|
467
|
+
this.mount();
|
|
468
|
+
}
|
|
469
|
+
get appId() { return 'app'; }
|
|
470
|
+
get router() { return this.config.router; }
|
|
471
|
+
get routerMode() { return this.router.mode || 'hash'; }
|
|
472
|
+
get customRoutes() { return this.router.customRoutes || {}; }
|
|
473
|
+
get routes() { return this.config.routes; }
|
|
474
|
+
get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
|
|
475
|
+
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
476
|
+
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; }
|
|
477
|
+
get animationDelay() {
|
|
478
|
+
var _a;
|
|
479
|
+
return (typeof this.animation === 'object'
|
|
480
|
+
? this.animation.delay
|
|
481
|
+
: this.animation
|
|
482
|
+
? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.delay
|
|
483
|
+
: 0) || 0;
|
|
484
|
+
}
|
|
485
|
+
get animationDuration() {
|
|
486
|
+
var _a;
|
|
487
|
+
return (typeof this.animation === 'object'
|
|
488
|
+
? this.animation.duration
|
|
489
|
+
: this.animation
|
|
490
|
+
? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.duration
|
|
491
|
+
: 0) || 0;
|
|
492
|
+
}
|
|
493
|
+
set pathname(p) { this.router.pathname = p; }
|
|
494
|
+
get pathname() { return this.router.pathname; }
|
|
495
|
+
get basename() { return this.router.basename || ''; }
|
|
496
|
+
get homePage() {
|
|
497
|
+
return this.config.entryPagePath || this.routes[0].path || this.basename;
|
|
498
|
+
}
|
|
499
|
+
get pageConfig() {
|
|
500
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
501
|
+
const homePage = this.homePage;
|
|
502
|
+
return this.routes.find(r => {
|
|
503
|
+
var _a;
|
|
504
|
+
const pagePath = addLeadingSlash(r.path);
|
|
505
|
+
return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
get isTabBar() {
|
|
509
|
+
var _a;
|
|
510
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
511
|
+
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
512
|
+
if (typeof target === 'string') {
|
|
513
|
+
return target === routePath;
|
|
514
|
+
}
|
|
515
|
+
else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
|
|
516
|
+
return target.includes(routePath);
|
|
517
|
+
}
|
|
518
|
+
return false;
|
|
519
|
+
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
520
|
+
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
521
|
+
}
|
|
522
|
+
isSamePage(page) {
|
|
523
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
524
|
+
const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
|
|
525
|
+
return pagePath.startsWith(routePath + '?');
|
|
526
|
+
}
|
|
527
|
+
get search() {
|
|
528
|
+
let search = '?';
|
|
529
|
+
if (this.routerMode === 'hash') {
|
|
530
|
+
const idx = location.hash.indexOf('?');
|
|
531
|
+
if (idx > -1) {
|
|
532
|
+
search = location.hash.slice(idx);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
search = location.search;
|
|
537
|
+
}
|
|
538
|
+
return search.substr(1);
|
|
539
|
+
}
|
|
540
|
+
getQuery(stamp = 0, search = '', options = {}) {
|
|
541
|
+
search = search ? `${search}&${this.search}` : this.search;
|
|
542
|
+
const query = search
|
|
543
|
+
? queryString__default["default"].parse(search, { decode: false })
|
|
544
|
+
: {};
|
|
545
|
+
query.stamp = stamp.toString();
|
|
546
|
+
return Object.assign(Object.assign({}, query), options);
|
|
547
|
+
}
|
|
548
|
+
mount() {
|
|
549
|
+
var _a;
|
|
550
|
+
setHistoryMode(this.routerMode, this.router.basename);
|
|
551
|
+
(_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
|
|
552
|
+
this.animation && loadAnimateStyle(this.animationDuration);
|
|
553
|
+
const app = document.createElement('div');
|
|
554
|
+
app.id = this.appId;
|
|
555
|
+
app.classList.add('taro_router');
|
|
556
|
+
if (this.tabBarList.length > 1) {
|
|
557
|
+
const container = document.createElement('div');
|
|
558
|
+
container.classList.add('taro-tabbar__container');
|
|
559
|
+
container.id = 'container';
|
|
560
|
+
const panel = document.createElement('div');
|
|
561
|
+
panel.classList.add('taro-tabbar__panel');
|
|
562
|
+
panel.appendChild(app);
|
|
563
|
+
container.appendChild(panel);
|
|
564
|
+
document.body.appendChild(container);
|
|
565
|
+
initTabbar(this.config);
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
document.body.appendChild(app);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
onReady(page, onLoad = true) {
|
|
572
|
+
var _a, _b;
|
|
573
|
+
const pageEl = this.getPageContainer(page);
|
|
574
|
+
if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
|
|
575
|
+
const el = pageEl.firstElementChild;
|
|
576
|
+
(_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(() => {
|
|
577
|
+
runtime.requestAnimationFrame(() => {
|
|
578
|
+
var _a;
|
|
579
|
+
(_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
580
|
+
pageEl['__isReady'] = true;
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
onLoad && (pageEl['__page'] = page);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
load(page, pageConfig = {}, stacksIndex = 0) {
|
|
587
|
+
var _a, _b;
|
|
588
|
+
if (!page)
|
|
589
|
+
return;
|
|
590
|
+
// NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
|
|
591
|
+
stacks.push(page);
|
|
592
|
+
const param = this.getQuery(stacks.length, '', page.options);
|
|
593
|
+
let pageEl = this.getPageContainer(page);
|
|
594
|
+
if (pageEl) {
|
|
595
|
+
setDisplay(pageEl);
|
|
596
|
+
this.isTabBar && pageEl.classList.add('taro_tabbar_page');
|
|
597
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
598
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
599
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
600
|
+
}
|
|
601
|
+
else {
|
|
602
|
+
(_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
|
|
603
|
+
var _a;
|
|
604
|
+
pageEl = this.getPageContainer(page);
|
|
605
|
+
this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
|
|
606
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
607
|
+
this.onReady(page, true);
|
|
608
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
609
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
unload(page, delta = 1, top = false) {
|
|
614
|
+
var _a, _b, _c;
|
|
615
|
+
if (!page)
|
|
616
|
+
return;
|
|
617
|
+
stacks.delta = --delta;
|
|
618
|
+
stacks.pop();
|
|
619
|
+
if (this.animation && top) {
|
|
620
|
+
if (this.unloadTimer) {
|
|
621
|
+
clearTimeout(this.unloadTimer);
|
|
622
|
+
(_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
623
|
+
this.unloadTimer = null;
|
|
624
|
+
}
|
|
625
|
+
this.lastUnloadPage = page;
|
|
626
|
+
const pageEl = this.getPageContainer(page);
|
|
627
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
|
|
628
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
|
|
629
|
+
this.unloadTimer = setTimeout(() => {
|
|
630
|
+
var _a, _b;
|
|
631
|
+
this.unloadTimer = null;
|
|
632
|
+
(_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
633
|
+
}, this.animationDuration);
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
const pageEl = this.getPageContainer(page);
|
|
637
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
|
|
638
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
|
|
639
|
+
(_c = page === null || page === void 0 ? void 0 : page.onUnload) === null || _c === void 0 ? void 0 : _c.call(page);
|
|
640
|
+
}
|
|
641
|
+
if (delta >= 1)
|
|
642
|
+
this.unload(stacks.last, delta);
|
|
643
|
+
}
|
|
644
|
+
show(page, pageConfig = {}, stacksIndex = 0) {
|
|
645
|
+
var _a, _b;
|
|
646
|
+
if (!page)
|
|
647
|
+
return;
|
|
648
|
+
const param = this.getQuery(stacks.length, '', page.options);
|
|
649
|
+
let pageEl = this.getPageContainer(page);
|
|
650
|
+
if (pageEl) {
|
|
651
|
+
setDisplay(pageEl);
|
|
652
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
653
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
654
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
655
|
+
}
|
|
656
|
+
else {
|
|
657
|
+
(_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
|
|
658
|
+
var _a;
|
|
659
|
+
pageEl = this.getPageContainer(page);
|
|
660
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
661
|
+
this.onReady(page, false);
|
|
662
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
663
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
hide(page) {
|
|
668
|
+
var _a;
|
|
669
|
+
if (!page)
|
|
670
|
+
return;
|
|
671
|
+
// NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
|
|
672
|
+
const pageEl = this.getPageContainer(page);
|
|
673
|
+
if (pageEl) {
|
|
674
|
+
if (this.hideTimer) {
|
|
675
|
+
clearTimeout(this.hideTimer);
|
|
676
|
+
this.hideTimer = null;
|
|
677
|
+
setDisplay(this.lastHidePage, 'none');
|
|
678
|
+
}
|
|
679
|
+
this.lastHidePage = pageEl;
|
|
680
|
+
this.hideTimer = setTimeout(() => {
|
|
681
|
+
this.hideTimer = null;
|
|
682
|
+
setDisplay(this.lastHidePage, 'none');
|
|
683
|
+
}, this.animationDuration + this.animationDelay);
|
|
684
|
+
(_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
setTimeout(() => this.hide(page), 0);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
addAnimation(pageEl, first = false) {
|
|
691
|
+
if (!pageEl)
|
|
692
|
+
return;
|
|
693
|
+
if (this.animation && !first) {
|
|
694
|
+
setTimeout(() => {
|
|
695
|
+
pageEl.classList.add('taro_page_show');
|
|
696
|
+
setTimeout(() => {
|
|
697
|
+
pageEl.classList.add('taro_page_stationed');
|
|
698
|
+
}, this.animationDuration);
|
|
699
|
+
}, this.animationDelay);
|
|
700
|
+
}
|
|
701
|
+
else {
|
|
702
|
+
pageEl.classList.add('taro_page_show');
|
|
703
|
+
pageEl.classList.add('taro_page_stationed');
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
getPageContainer(page) {
|
|
707
|
+
var _a;
|
|
708
|
+
const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
|
|
709
|
+
const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
710
|
+
if (page) {
|
|
711
|
+
return document.querySelector(`.taro_page#${id}`);
|
|
712
|
+
}
|
|
713
|
+
const el = (id
|
|
714
|
+
? document.querySelector(`.taro_page#${id}`)
|
|
715
|
+
: document.querySelector('.taro_page') ||
|
|
716
|
+
document.querySelector('.taro_router'));
|
|
717
|
+
return el || window;
|
|
718
|
+
}
|
|
719
|
+
bindPageEvents(page, pageEl, config = {}) {
|
|
720
|
+
var _a;
|
|
721
|
+
if (!pageEl) {
|
|
722
|
+
pageEl = this.getPageContainer();
|
|
723
|
+
}
|
|
724
|
+
const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
|
|
725
|
+
bindPageScroll(page, pageEl, distance);
|
|
726
|
+
bindPageResize(page);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function createRouter(app, config, framework) {
|
|
731
|
+
var _a, _b;
|
|
732
|
+
RouterConfig.config = config;
|
|
733
|
+
const handler = new PageHandler(config);
|
|
734
|
+
const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
|
|
735
|
+
routesAlias.set(handler.router.customRoutes);
|
|
736
|
+
const basename = handler.router.basename;
|
|
737
|
+
const routes = handler.routes.map(route => {
|
|
738
|
+
const routePath = addLeadingSlash(route.path);
|
|
739
|
+
const paths = routesAlias.getAll(routePath);
|
|
740
|
+
return {
|
|
741
|
+
path: paths.length < 1 ? routePath : paths,
|
|
742
|
+
action: route.load
|
|
743
|
+
};
|
|
744
|
+
});
|
|
745
|
+
const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
|
|
746
|
+
const launchParam = handler.getQuery(stacks.length);
|
|
747
|
+
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
748
|
+
const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
|
|
749
|
+
var _c, _d, _e, _f, _g;
|
|
750
|
+
handler.pathname = decodeURI(location.pathname);
|
|
751
|
+
let element, params;
|
|
752
|
+
try {
|
|
753
|
+
const result = yield router.resolve(handler.router.forcePath || handler.pathname);
|
|
754
|
+
[element, , params] = yield Promise.all(result);
|
|
755
|
+
}
|
|
756
|
+
catch (error) {
|
|
757
|
+
if (error.status === 404) {
|
|
758
|
+
(_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, {
|
|
759
|
+
path: handler.pathname
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
throw new Error(error);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
if (!element)
|
|
767
|
+
return;
|
|
768
|
+
const pageConfig = handler.pageConfig;
|
|
769
|
+
let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
|
|
770
|
+
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
771
|
+
toLocation: {
|
|
772
|
+
path: handler.pathname
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
if (pageConfig) {
|
|
776
|
+
document.title = (_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title;
|
|
777
|
+
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
778
|
+
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
const currentPage = runtime.Current.page;
|
|
782
|
+
const pathname = handler.pathname;
|
|
783
|
+
let shouldLoad = false;
|
|
784
|
+
if (action === 'POP') {
|
|
785
|
+
// NOTE: 浏览器事件退后多次时,该事件只会被触发一次
|
|
786
|
+
const prevIndex = stacks.getPrevIndex(pathname);
|
|
787
|
+
const delta = stacks.getDelta(pathname);
|
|
788
|
+
handler.unload(currentPage, delta, prevIndex > -1);
|
|
789
|
+
if (prevIndex > -1) {
|
|
790
|
+
handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
|
|
791
|
+
}
|
|
792
|
+
else {
|
|
793
|
+
shouldLoad = true;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
if (handler.isTabBar) {
|
|
798
|
+
if (handler.isSamePage(currentPage))
|
|
799
|
+
return;
|
|
800
|
+
const prevIndex = stacks.getPrevIndex(pathname, 0);
|
|
801
|
+
handler.hide(currentPage);
|
|
802
|
+
if (prevIndex > -1) {
|
|
803
|
+
// NOTE: tabbar 页且之前出现过,直接复用
|
|
804
|
+
return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
else if (action === 'REPLACE') {
|
|
808
|
+
const delta = stacks.getDelta(pathname);
|
|
809
|
+
// NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
|
|
810
|
+
handler.unload(currentPage, delta);
|
|
811
|
+
}
|
|
812
|
+
else if (action === 'PUSH') {
|
|
813
|
+
handler.hide(currentPage);
|
|
814
|
+
}
|
|
815
|
+
shouldLoad = true;
|
|
816
|
+
}
|
|
817
|
+
if (shouldLoad || stacks.length < 1) {
|
|
818
|
+
const el = (_f = element.default) !== null && _f !== void 0 ? _f : element;
|
|
819
|
+
const loadConfig = Object.assign({}, pageConfig);
|
|
820
|
+
const stacksIndex = stacks.length;
|
|
821
|
+
delete loadConfig['path'];
|
|
822
|
+
delete loadConfig['load'];
|
|
823
|
+
const page = runtime.createPageConfig(enablePullDownRefresh ? (_g = runtimeHooks.createPullDownComponent) === null || _g === void 0 ? void 0 : _g.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
|
|
824
|
+
if (params)
|
|
825
|
+
page.options = params;
|
|
826
|
+
return handler.load(page, pageConfig, stacksIndex);
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
const routePath = stripBasename(exports.history.location.pathname, handler.basename);
|
|
830
|
+
if (routePath === '/' || routePath === '') {
|
|
831
|
+
exports.history.replace(prependBasename(handler.homePage + exports.history.location.search));
|
|
832
|
+
}
|
|
833
|
+
render({ location: exports.history.location, action: history.Action.Push });
|
|
834
|
+
(_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
|
|
835
|
+
return exports.history.listen(render);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
class MultiPageHandler {
|
|
839
|
+
constructor(config) {
|
|
840
|
+
this.config = config;
|
|
841
|
+
this.mount();
|
|
842
|
+
}
|
|
843
|
+
get appId() { return 'app'; }
|
|
844
|
+
get router() { return this.config.router; }
|
|
845
|
+
get routerMode() { return this.router.mode || 'hash'; }
|
|
846
|
+
get customRoutes() { return this.router.customRoutes || {}; }
|
|
847
|
+
get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
|
|
848
|
+
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
849
|
+
set pathname(p) { this.router.pathname = p; }
|
|
850
|
+
get pathname() { return this.router.pathname; }
|
|
851
|
+
get basename() { return this.router.basename || ''; }
|
|
852
|
+
get pageConfig() { return this.config.route; }
|
|
853
|
+
get isTabBar() {
|
|
854
|
+
var _a;
|
|
855
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
856
|
+
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
857
|
+
if (typeof target === 'string') {
|
|
858
|
+
return target === routePath;
|
|
859
|
+
}
|
|
860
|
+
else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
|
|
861
|
+
return target.includes(routePath);
|
|
862
|
+
}
|
|
863
|
+
return false;
|
|
864
|
+
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
865
|
+
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
866
|
+
}
|
|
867
|
+
get search() { return location.search.substr(1); }
|
|
868
|
+
getQuery(search = '', options = {}) {
|
|
869
|
+
search = search ? `${search}&${this.search}` : this.search;
|
|
870
|
+
const query = search
|
|
871
|
+
? queryString__default["default"].parse(search)
|
|
872
|
+
: {};
|
|
873
|
+
return Object.assign(Object.assign({}, query), options);
|
|
874
|
+
}
|
|
875
|
+
mount() {
|
|
876
|
+
var _a;
|
|
877
|
+
setHistoryMode(this.routerMode, this.router.basename);
|
|
878
|
+
(_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
|
|
879
|
+
const app = document.createElement('div');
|
|
880
|
+
app.id = this.appId;
|
|
881
|
+
app.classList.add('taro_router');
|
|
882
|
+
if (this.tabBarList.length > 1) {
|
|
883
|
+
const container = document.createElement('div');
|
|
884
|
+
container.classList.add('taro-tabbar__container');
|
|
885
|
+
container.id = 'container';
|
|
886
|
+
const panel = document.createElement('div');
|
|
887
|
+
panel.classList.add('taro-tabbar__panel');
|
|
888
|
+
panel.appendChild(app);
|
|
889
|
+
container.appendChild(panel);
|
|
890
|
+
document.body.appendChild(container);
|
|
891
|
+
initTabbar(this.config);
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
document.body.appendChild(app);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
onReady(page, onLoad = true) {
|
|
898
|
+
var _a;
|
|
899
|
+
const pageEl = this.getPageContainer(page);
|
|
900
|
+
if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
|
|
901
|
+
const el = pageEl.firstElementChild;
|
|
902
|
+
(_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
|
|
903
|
+
onLoad && (pageEl['__page'] = page);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
load(page, pageConfig = {}) {
|
|
907
|
+
var _a;
|
|
908
|
+
if (!page)
|
|
909
|
+
return;
|
|
910
|
+
(_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
|
|
911
|
+
var _a;
|
|
912
|
+
const pageEl = this.getPageContainer(page);
|
|
913
|
+
this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
|
|
914
|
+
this.onReady(page, true);
|
|
915
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
916
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
getPageContainer(page) {
|
|
920
|
+
var _a;
|
|
921
|
+
const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
|
|
922
|
+
const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
923
|
+
if (page) {
|
|
924
|
+
return document.querySelector(`.taro_page#${id}`);
|
|
925
|
+
}
|
|
926
|
+
const el = (id
|
|
927
|
+
? document.querySelector(`.taro_page#${id}`)
|
|
928
|
+
: document.querySelector('.taro_page') ||
|
|
929
|
+
document.querySelector('.taro_router'));
|
|
930
|
+
return el || window;
|
|
931
|
+
}
|
|
932
|
+
bindPageEvents(page, pageEl, config = {}) {
|
|
933
|
+
var _a;
|
|
934
|
+
if (!pageEl) {
|
|
935
|
+
pageEl = this.getPageContainer();
|
|
936
|
+
}
|
|
937
|
+
const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
|
|
938
|
+
bindPageScroll(page, pageEl, distance);
|
|
939
|
+
bindPageResize(page);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// TODO 支持多路由 (APP 生命周期仅触发一次)
|
|
944
|
+
/** Note: 关于多页面应用
|
|
945
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
946
|
+
* - app.onPageNotFound 事件不支持
|
|
947
|
+
* - 应用生命周期可能多次触发
|
|
948
|
+
* - TabBar 会多次加载
|
|
949
|
+
* - 不支持路由动画
|
|
950
|
+
*/
|
|
951
|
+
function createMultiRouter(app, config, framework) {
|
|
952
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
953
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
954
|
+
RouterConfig.config = config;
|
|
955
|
+
const handler = new MultiPageHandler(config);
|
|
956
|
+
const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
|
|
957
|
+
const launchParam = handler.getQuery();
|
|
958
|
+
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
959
|
+
const pathName = config.pageName;
|
|
960
|
+
const pageConfig = handler.pageConfig;
|
|
961
|
+
let element;
|
|
962
|
+
try {
|
|
963
|
+
element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
|
|
964
|
+
}
|
|
965
|
+
catch (error) {
|
|
966
|
+
throw new Error(error);
|
|
967
|
+
}
|
|
968
|
+
if (!element)
|
|
969
|
+
return;
|
|
970
|
+
let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
|
|
971
|
+
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
972
|
+
toLocation: {
|
|
973
|
+
path: pathName
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
if (pageConfig) {
|
|
977
|
+
document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
|
|
978
|
+
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
979
|
+
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
|
|
983
|
+
const loadConfig = Object.assign({}, pageConfig);
|
|
984
|
+
delete loadConfig['path'];
|
|
985
|
+
delete loadConfig['load'];
|
|
986
|
+
const page = runtime.createPageConfig(enablePullDownRefresh ? (_f = runtimeHooks.createPullDownComponent) === null || _f === void 0 ? void 0 : _f.call(runtimeHooks, el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
|
|
987
|
+
handler.load(page, pageConfig);
|
|
988
|
+
(_g = app.onShow) === null || _g === void 0 ? void 0 : _g.call(app, launchParam);
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
exports.createMultiRouter = createMultiRouter;
|
|
993
|
+
exports.createRouter = createRouter;
|
|
994
|
+
exports.getCurrentPages = getCurrentPages;
|
|
995
|
+
exports.navigateBack = navigateBack;
|
|
996
|
+
exports.navigateTo = navigateTo;
|
|
997
|
+
exports.reLaunch = reLaunch;
|
|
998
|
+
exports.redirectTo = redirectTo;
|
|
999
|
+
exports.switchTab = switchTab;
|
|
1000
|
+
//# sourceMappingURL=index.cjs.js.map
|