@tarojs/router 3.5.0-canary.1 → 3.5.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.d.ts +4 -0
- package/dist/animation.js +29 -0
- package/dist/api.d.ts +7 -0
- package/dist/api.js +103 -0
- package/dist/events/resize.d.ts +2 -0
- package/dist/events/resize.js +13 -0
- package/dist/events/scroll.d.ts +2 -0
- package/dist/events/scroll.js +31 -0
- package/dist/history.d.ts +5 -0
- package/dist/history.js +100 -0
- package/dist/index.cjs.d.ts +23 -0
- package/dist/index.cjs.js +1043 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.d.ts +23 -0
- package/dist/index.esm.js +1026 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +4 -2674
- package/dist/router/index.d.ts +11 -0
- package/dist/router/index.js +22 -0
- package/dist/router/mpa.d.ts +10 -0
- package/dist/router/mpa.js +62 -0
- package/dist/router/multi-page.d.ts +27 -0
- package/dist/router/multi-page.js +112 -0
- package/dist/router/page.d.ts +42 -0
- package/dist/router/page.js +281 -0
- package/dist/router/spa.d.ts +3 -0
- package/dist/router/spa.js +130 -0
- package/dist/router/stack.d.ts +18 -0
- package/dist/router/stack.js +59 -0
- package/dist/tabbar.d.ts +2 -0
- package/dist/tabbar.js +29 -0
- package/dist/utils/index.d.ts +15 -0
- package/dist/utils/index.js +47 -0
- package/dist/utils/navigate.d.ts +5 -0
- package/dist/utils/navigate.js +44 -0
- package/package.json +18 -17
- package/types/api.d.ts +13 -0
- package/types/history.d.ts +6 -0
- package/types/router.d.ts +30 -0
- package/dist/index.js.map +0 -1
- package/dist/router.esm.js +0 -2650
- package/dist/router.esm.js.map +0 -1
- package/types/index.d.ts +0 -287
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router';
|
|
2
|
+
export declare class RouterConfig {
|
|
3
|
+
private static __config;
|
|
4
|
+
static set config(e: SpaRouterConfig | MpaRouterConfig);
|
|
5
|
+
static get config(): SpaRouterConfig | MpaRouterConfig;
|
|
6
|
+
static get pages(): string[];
|
|
7
|
+
static get router(): import("../../types/router").Router;
|
|
8
|
+
static get mode(): "hash" | "browser" | "multi";
|
|
9
|
+
static get customRoutes(): Record<string, string | string[]>;
|
|
10
|
+
static isPage(url?: string): boolean;
|
|
11
|
+
}
|
|
@@ -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,10 @@
|
|
|
1
|
+
import { AppInstance } from '@tarojs/runtime';
|
|
2
|
+
import type { MpaRouterConfig } from '../../types/router';
|
|
3
|
+
/** Note: 关于多页面应用
|
|
4
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
5
|
+
* - app.onPageNotFound 事件不支持
|
|
6
|
+
* - 应用生命周期可能多次触发
|
|
7
|
+
* - TabBar 会多次加载
|
|
8
|
+
* - 不支持路由动画
|
|
9
|
+
*/
|
|
10
|
+
export declare function createMultiRouter(app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
/* eslint-disable dot-notation */
|
|
11
|
+
import { createPageConfig, eventCenter, hooks, stringify } from '@tarojs/runtime';
|
|
12
|
+
import { setTitle } from '../utils/navigate';
|
|
13
|
+
import { RouterConfig } from '.';
|
|
14
|
+
import MultiPageHandler from './multi-page';
|
|
15
|
+
// TODO 支持多路由 (APP 生命周期仅触发一次)
|
|
16
|
+
/** Note: 关于多页面应用
|
|
17
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
18
|
+
* - app.onPageNotFound 事件不支持
|
|
19
|
+
* - 应用生命周期可能多次触发
|
|
20
|
+
* - TabBar 会多次加载
|
|
21
|
+
* - 不支持路由动画
|
|
22
|
+
*/
|
|
23
|
+
export function createMultiRouter(app, config, framework) {
|
|
24
|
+
var _a, _b, _c, _d, _e, _f;
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
RouterConfig.config = config;
|
|
27
|
+
const handler = new MultiPageHandler(config);
|
|
28
|
+
const launchParam = handler.getQuery();
|
|
29
|
+
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
30
|
+
app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
|
|
31
|
+
const pathName = config.pageName;
|
|
32
|
+
const pageConfig = handler.pageConfig;
|
|
33
|
+
let element;
|
|
34
|
+
try {
|
|
35
|
+
element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw new Error(error);
|
|
39
|
+
}
|
|
40
|
+
if (!element)
|
|
41
|
+
return;
|
|
42
|
+
let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
|
|
43
|
+
eventCenter.trigger('__taroRouterChange', {
|
|
44
|
+
toLocation: {
|
|
45
|
+
path: pathName
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (pageConfig) {
|
|
49
|
+
setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
|
|
50
|
+
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
51
|
+
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
|
|
55
|
+
const loadConfig = Object.assign({}, pageConfig);
|
|
56
|
+
delete loadConfig['path'];
|
|
57
|
+
delete loadConfig['load'];
|
|
58
|
+
const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, config.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
|
|
59
|
+
handler.load(page, pageConfig);
|
|
60
|
+
(_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PageInstance } from '@tarojs/runtime';
|
|
2
|
+
import type { PageConfig } from '@tarojs/taro';
|
|
3
|
+
import type { MpaRouterConfig, Route } from '../../types/router';
|
|
4
|
+
export default class MultiPageHandler {
|
|
5
|
+
protected config: MpaRouterConfig;
|
|
6
|
+
constructor(config: MpaRouterConfig);
|
|
7
|
+
get appId(): string;
|
|
8
|
+
get router(): import("../../types/router").Router;
|
|
9
|
+
get routerMode(): "hash" | "browser" | "multi";
|
|
10
|
+
get customRoutes(): Record<string, string | string[]>;
|
|
11
|
+
get tabBarList(): import("@tarojs/taro").TabBarItem[];
|
|
12
|
+
get PullDownRefresh(): any;
|
|
13
|
+
set pathname(p: string);
|
|
14
|
+
get pathname(): string;
|
|
15
|
+
get basename(): string;
|
|
16
|
+
get pageConfig(): Route;
|
|
17
|
+
get isTabBar(): boolean;
|
|
18
|
+
get search(): string;
|
|
19
|
+
getQuery(search?: string, options?: Record<string, unknown>): {
|
|
20
|
+
[x: string]: unknown;
|
|
21
|
+
};
|
|
22
|
+
mount(): void;
|
|
23
|
+
onReady(page: PageInstance, onLoad?: boolean): void;
|
|
24
|
+
load(page: PageInstance, pageConfig?: Route): void;
|
|
25
|
+
getPageContainer(page?: PageInstance | null): HTMLElement | null;
|
|
26
|
+
bindPageEvents(page: PageInstance, pageEl?: HTMLElement | null, config?: Partial<PageConfig>): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/* eslint-disable dot-notation */
|
|
2
|
+
import { Current } from '@tarojs/runtime';
|
|
3
|
+
import queryString from 'query-string';
|
|
4
|
+
import { bindPageResize } from '../events/resize';
|
|
5
|
+
import { bindPageScroll } from '../events/scroll';
|
|
6
|
+
import { setHistoryMode } from '../history';
|
|
7
|
+
import { initTabbar } from '../tabbar';
|
|
8
|
+
import { stripBasename } from '../utils';
|
|
9
|
+
export default class MultiPageHandler {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.mount();
|
|
13
|
+
}
|
|
14
|
+
get appId() { return 'app'; }
|
|
15
|
+
get router() { return this.config.router || {}; }
|
|
16
|
+
get routerMode() { return this.router.mode || 'hash'; }
|
|
17
|
+
get customRoutes() { return this.router.customRoutes || {}; }
|
|
18
|
+
get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
|
|
19
|
+
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
20
|
+
set pathname(p) { this.router.pathname = p; }
|
|
21
|
+
get pathname() { return this.router.pathname; }
|
|
22
|
+
get basename() { return this.router.basename || ''; }
|
|
23
|
+
get pageConfig() { return this.config.route; }
|
|
24
|
+
get isTabBar() {
|
|
25
|
+
var _a;
|
|
26
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
27
|
+
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
28
|
+
if (typeof target === 'string') {
|
|
29
|
+
return target === routePath;
|
|
30
|
+
}
|
|
31
|
+
else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
|
|
32
|
+
return target.includes(routePath);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
36
|
+
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
37
|
+
}
|
|
38
|
+
get search() { return location.search.substr(1); }
|
|
39
|
+
getQuery(search = '', options = {}) {
|
|
40
|
+
search = search ? `${search}&${this.search}` : this.search;
|
|
41
|
+
const query = search
|
|
42
|
+
? queryString.parse(search)
|
|
43
|
+
: {};
|
|
44
|
+
return Object.assign(Object.assign({}, query), options);
|
|
45
|
+
}
|
|
46
|
+
mount() {
|
|
47
|
+
var _a;
|
|
48
|
+
setHistoryMode(this.routerMode, this.router.basename);
|
|
49
|
+
(_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
|
|
50
|
+
const app = document.createElement('div');
|
|
51
|
+
app.id = this.appId;
|
|
52
|
+
app.classList.add('taro_router');
|
|
53
|
+
if (this.tabBarList.length > 1) {
|
|
54
|
+
const container = document.createElement('div');
|
|
55
|
+
container.classList.add('taro-tabbar__container');
|
|
56
|
+
container.id = 'container';
|
|
57
|
+
const panel = document.createElement('div');
|
|
58
|
+
panel.classList.add('taro-tabbar__panel');
|
|
59
|
+
panel.appendChild(app);
|
|
60
|
+
container.appendChild(panel);
|
|
61
|
+
document.body.appendChild(container);
|
|
62
|
+
initTabbar(this.config);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
document.body.appendChild(app);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
onReady(page, onLoad = true) {
|
|
69
|
+
var _a;
|
|
70
|
+
const pageEl = this.getPageContainer(page);
|
|
71
|
+
if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
|
|
72
|
+
const el = pageEl.firstElementChild;
|
|
73
|
+
(_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
|
|
74
|
+
onLoad && (pageEl['__page'] = page);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
load(page, pageConfig = {}) {
|
|
78
|
+
var _a;
|
|
79
|
+
if (!page)
|
|
80
|
+
return;
|
|
81
|
+
(_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
|
|
82
|
+
var _a;
|
|
83
|
+
const pageEl = this.getPageContainer(page);
|
|
84
|
+
this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
|
|
85
|
+
this.onReady(page, true);
|
|
86
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
87
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
getPageContainer(page) {
|
|
91
|
+
var _a;
|
|
92
|
+
const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
|
|
93
|
+
const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
94
|
+
if (page) {
|
|
95
|
+
return document.querySelector(`.taro_page#${id}`);
|
|
96
|
+
}
|
|
97
|
+
const el = (id
|
|
98
|
+
? document.querySelector(`.taro_page#${id}`)
|
|
99
|
+
: document.querySelector('.taro_page') ||
|
|
100
|
+
document.querySelector('.taro_router'));
|
|
101
|
+
return el || window;
|
|
102
|
+
}
|
|
103
|
+
bindPageEvents(page, pageEl, config = {}) {
|
|
104
|
+
var _a;
|
|
105
|
+
if (!pageEl) {
|
|
106
|
+
pageEl = this.getPageContainer();
|
|
107
|
+
}
|
|
108
|
+
const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
|
|
109
|
+
bindPageScroll(page, pageEl, distance);
|
|
110
|
+
bindPageResize(page);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { PageInstance } from '@tarojs/runtime';
|
|
2
|
+
import type { PageConfig, RouterAnimate } from '@tarojs/taro';
|
|
3
|
+
import type { Route, SpaRouterConfig } from '../../types/router';
|
|
4
|
+
export default class PageHandler {
|
|
5
|
+
protected config: SpaRouterConfig;
|
|
6
|
+
protected readonly defaultAnimation: RouterAnimate;
|
|
7
|
+
protected unloadTimer: ReturnType<typeof setTimeout> | null;
|
|
8
|
+
protected hideTimer: ReturnType<typeof setTimeout> | null;
|
|
9
|
+
protected lastHidePage: HTMLElement | null;
|
|
10
|
+
protected lastUnloadPage: PageInstance | null;
|
|
11
|
+
constructor(config: SpaRouterConfig);
|
|
12
|
+
get appId(): string;
|
|
13
|
+
get router(): import("../../types/router").Router;
|
|
14
|
+
get routerMode(): "hash" | "browser" | "multi";
|
|
15
|
+
get customRoutes(): Record<string, string | string[]>;
|
|
16
|
+
get routes(): Route[];
|
|
17
|
+
get tabBarList(): import("@tarojs/taro").TabBarItem[];
|
|
18
|
+
get PullDownRefresh(): any;
|
|
19
|
+
get animation(): boolean | RouterAnimate;
|
|
20
|
+
get animationDelay(): number;
|
|
21
|
+
get animationDuration(): number;
|
|
22
|
+
set pathname(p: string);
|
|
23
|
+
get pathname(): string;
|
|
24
|
+
get basename(): string;
|
|
25
|
+
get homePage(): string;
|
|
26
|
+
get pageConfig(): Route | undefined;
|
|
27
|
+
get isTabBar(): boolean;
|
|
28
|
+
isSamePage(page?: PageInstance | null): boolean;
|
|
29
|
+
get search(): string;
|
|
30
|
+
getQuery(stamp?: number, search?: string, options?: Record<string, unknown>): {
|
|
31
|
+
[x: string]: unknown;
|
|
32
|
+
};
|
|
33
|
+
mount(): void;
|
|
34
|
+
onReady(page: PageInstance, onLoad?: boolean): void;
|
|
35
|
+
load(page: PageInstance, pageConfig?: Route, stacksIndex?: number): void;
|
|
36
|
+
unload(page?: PageInstance | null, delta?: number, top?: boolean): void;
|
|
37
|
+
show(page?: PageInstance | null, pageConfig?: Route, stacksIndex?: number): void;
|
|
38
|
+
hide(page?: PageInstance | null): void;
|
|
39
|
+
addAnimation(pageEl?: HTMLElement | null, first?: boolean): void;
|
|
40
|
+
getPageContainer(page?: PageInstance | null): HTMLElement | null;
|
|
41
|
+
bindPageEvents(page: PageInstance, pageEl?: HTMLElement | null, config?: Partial<PageConfig>): void;
|
|
42
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/* eslint-disable dot-notation */
|
|
2
|
+
import { Current, requestAnimationFrame } from '@tarojs/runtime';
|
|
3
|
+
import queryString from 'query-string';
|
|
4
|
+
import { loadAnimateStyle } from '../animation';
|
|
5
|
+
import { bindPageResize } from '../events/resize';
|
|
6
|
+
import { bindPageScroll } from '../events/scroll';
|
|
7
|
+
import { setHistoryMode } from '../history';
|
|
8
|
+
import { initTabbar } from '../tabbar';
|
|
9
|
+
import { addLeadingSlash, routesAlias, stripBasename, stripTrailing } from '../utils';
|
|
10
|
+
import stacks from './stack';
|
|
11
|
+
function setDisplay(el, type = '') {
|
|
12
|
+
if (el) {
|
|
13
|
+
el.style.display = type;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export default class PageHandler {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.defaultAnimation = { duration: 300, delay: 50 };
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.mount();
|
|
21
|
+
}
|
|
22
|
+
get appId() { return 'app'; }
|
|
23
|
+
get router() { return this.config.router || {}; }
|
|
24
|
+
get routerMode() { return this.router.mode || 'hash'; }
|
|
25
|
+
get customRoutes() { return this.router.customRoutes || {}; }
|
|
26
|
+
get routes() { return this.config.routes || []; }
|
|
27
|
+
get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
|
|
28
|
+
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
29
|
+
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; }
|
|
30
|
+
get animationDelay() {
|
|
31
|
+
var _a;
|
|
32
|
+
return (typeof this.animation === 'object'
|
|
33
|
+
? this.animation.delay
|
|
34
|
+
: this.animation
|
|
35
|
+
? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.delay
|
|
36
|
+
: 0) || 0;
|
|
37
|
+
}
|
|
38
|
+
get animationDuration() {
|
|
39
|
+
var _a;
|
|
40
|
+
return (typeof this.animation === 'object'
|
|
41
|
+
? this.animation.duration
|
|
42
|
+
: this.animation
|
|
43
|
+
? (_a = this.defaultAnimation) === null || _a === void 0 ? void 0 : _a.duration
|
|
44
|
+
: 0) || 0;
|
|
45
|
+
}
|
|
46
|
+
set pathname(p) { this.router.pathname = p; }
|
|
47
|
+
get pathname() { return this.router.pathname; }
|
|
48
|
+
get basename() { return this.router.basename || ''; }
|
|
49
|
+
get homePage() {
|
|
50
|
+
return this.config.entryPagePath || this.routes[0].path || this.basename;
|
|
51
|
+
}
|
|
52
|
+
get pageConfig() {
|
|
53
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
54
|
+
const homePage = this.homePage;
|
|
55
|
+
return this.routes.find(r => {
|
|
56
|
+
var _a;
|
|
57
|
+
const pagePath = addLeadingSlash(r.path);
|
|
58
|
+
return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
get isTabBar() {
|
|
62
|
+
var _a;
|
|
63
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
64
|
+
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
65
|
+
if (typeof target === 'string') {
|
|
66
|
+
return target === routePath;
|
|
67
|
+
}
|
|
68
|
+
else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
|
|
69
|
+
return target.includes(routePath);
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
73
|
+
return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
|
|
74
|
+
}
|
|
75
|
+
isSamePage(page) {
|
|
76
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
77
|
+
const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
|
|
78
|
+
return pagePath.startsWith(routePath + '?');
|
|
79
|
+
}
|
|
80
|
+
get search() {
|
|
81
|
+
let search = '?';
|
|
82
|
+
if (this.routerMode === 'hash') {
|
|
83
|
+
const idx = location.hash.indexOf('?');
|
|
84
|
+
if (idx > -1) {
|
|
85
|
+
search = location.hash.slice(idx);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
search = location.search;
|
|
90
|
+
}
|
|
91
|
+
return search.substr(1);
|
|
92
|
+
}
|
|
93
|
+
getQuery(stamp = 0, search = '', options = {}) {
|
|
94
|
+
search = search ? `${search}&${this.search}` : this.search;
|
|
95
|
+
const query = search
|
|
96
|
+
? queryString.parse(search, { decode: false })
|
|
97
|
+
: {};
|
|
98
|
+
query.stamp = stamp.toString();
|
|
99
|
+
return Object.assign(Object.assign({}, query), options);
|
|
100
|
+
}
|
|
101
|
+
mount() {
|
|
102
|
+
var _a;
|
|
103
|
+
setHistoryMode(this.routerMode, this.router.basename);
|
|
104
|
+
(_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
|
|
105
|
+
this.animation && loadAnimateStyle(this.animationDuration);
|
|
106
|
+
const app = document.createElement('div');
|
|
107
|
+
app.id = this.appId;
|
|
108
|
+
app.classList.add('taro_router');
|
|
109
|
+
if (this.tabBarList.length > 1) {
|
|
110
|
+
const container = document.createElement('div');
|
|
111
|
+
container.classList.add('taro-tabbar__container');
|
|
112
|
+
container.id = 'container';
|
|
113
|
+
const panel = document.createElement('div');
|
|
114
|
+
panel.classList.add('taro-tabbar__panel');
|
|
115
|
+
panel.appendChild(app);
|
|
116
|
+
container.appendChild(panel);
|
|
117
|
+
document.body.appendChild(container);
|
|
118
|
+
initTabbar(this.config);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
document.body.appendChild(app);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
onReady(page, onLoad = true) {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
const pageEl = this.getPageContainer(page);
|
|
127
|
+
if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
|
|
128
|
+
const el = pageEl.firstElementChild;
|
|
129
|
+
(_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(() => {
|
|
130
|
+
requestAnimationFrame(() => {
|
|
131
|
+
var _a;
|
|
132
|
+
(_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
133
|
+
pageEl['__isReady'] = true;
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
onLoad && (pageEl['__page'] = page);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
load(page, pageConfig = {}, stacksIndex = 0) {
|
|
140
|
+
var _a, _b;
|
|
141
|
+
if (!page)
|
|
142
|
+
return;
|
|
143
|
+
// NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
|
|
144
|
+
stacks.push(page);
|
|
145
|
+
const param = this.getQuery(stacks.length, '', page.options);
|
|
146
|
+
let pageEl = this.getPageContainer(page);
|
|
147
|
+
if (pageEl) {
|
|
148
|
+
setDisplay(pageEl);
|
|
149
|
+
this.isTabBar && pageEl.classList.add('taro_tabbar_page');
|
|
150
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
151
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
152
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
(_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
|
|
156
|
+
var _a;
|
|
157
|
+
pageEl = this.getPageContainer(page);
|
|
158
|
+
this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
|
|
159
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
160
|
+
this.onReady(page, true);
|
|
161
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
162
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
unload(page, delta = 1, top = false) {
|
|
167
|
+
var _a, _b, _c;
|
|
168
|
+
if (!page)
|
|
169
|
+
return;
|
|
170
|
+
stacks.delta = --delta;
|
|
171
|
+
stacks.pop();
|
|
172
|
+
if (this.animation && top) {
|
|
173
|
+
if (this.unloadTimer) {
|
|
174
|
+
clearTimeout(this.unloadTimer);
|
|
175
|
+
(_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
176
|
+
this.unloadTimer = null;
|
|
177
|
+
}
|
|
178
|
+
this.lastUnloadPage = page;
|
|
179
|
+
const pageEl = this.getPageContainer(page);
|
|
180
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
|
|
181
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
|
|
182
|
+
this.unloadTimer = setTimeout(() => {
|
|
183
|
+
var _a, _b;
|
|
184
|
+
this.unloadTimer = null;
|
|
185
|
+
(_b = (_a = this.lastUnloadPage) === null || _a === void 0 ? void 0 : _a.onUnload) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
186
|
+
}, this.animationDuration);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
const pageEl = this.getPageContainer(page);
|
|
190
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
|
|
191
|
+
pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
|
|
192
|
+
(_c = page === null || page === void 0 ? void 0 : page.onUnload) === null || _c === void 0 ? void 0 : _c.call(page);
|
|
193
|
+
}
|
|
194
|
+
if (delta >= 1)
|
|
195
|
+
this.unload(stacks.last, delta);
|
|
196
|
+
}
|
|
197
|
+
show(page, pageConfig = {}, stacksIndex = 0) {
|
|
198
|
+
var _a, _b;
|
|
199
|
+
if (!page)
|
|
200
|
+
return;
|
|
201
|
+
const param = this.getQuery(stacks.length, '', page.options);
|
|
202
|
+
let pageEl = this.getPageContainer(page);
|
|
203
|
+
if (pageEl) {
|
|
204
|
+
setDisplay(pageEl);
|
|
205
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
206
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
207
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
(_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
|
|
211
|
+
var _a;
|
|
212
|
+
pageEl = this.getPageContainer(page);
|
|
213
|
+
this.addAnimation(pageEl, stacksIndex === 0);
|
|
214
|
+
this.onReady(page, false);
|
|
215
|
+
(_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
216
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
hide(page) {
|
|
221
|
+
var _a;
|
|
222
|
+
if (!page)
|
|
223
|
+
return;
|
|
224
|
+
// NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
|
|
225
|
+
const pageEl = this.getPageContainer(page);
|
|
226
|
+
if (pageEl) {
|
|
227
|
+
if (this.hideTimer) {
|
|
228
|
+
clearTimeout(this.hideTimer);
|
|
229
|
+
this.hideTimer = null;
|
|
230
|
+
setDisplay(this.lastHidePage, 'none');
|
|
231
|
+
}
|
|
232
|
+
this.lastHidePage = pageEl;
|
|
233
|
+
this.hideTimer = setTimeout(() => {
|
|
234
|
+
this.hideTimer = null;
|
|
235
|
+
setDisplay(this.lastHidePage, 'none');
|
|
236
|
+
}, this.animationDuration + this.animationDelay);
|
|
237
|
+
(_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
setTimeout(() => this.hide(page), 0);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
addAnimation(pageEl, first = false) {
|
|
244
|
+
if (!pageEl)
|
|
245
|
+
return;
|
|
246
|
+
if (this.animation && !first) {
|
|
247
|
+
setTimeout(() => {
|
|
248
|
+
pageEl.classList.add('taro_page_show');
|
|
249
|
+
setTimeout(() => {
|
|
250
|
+
pageEl.classList.add('taro_page_stationed');
|
|
251
|
+
}, this.animationDuration);
|
|
252
|
+
}, this.animationDelay);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
pageEl.classList.add('taro_page_show');
|
|
256
|
+
pageEl.classList.add('taro_page_stationed');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
getPageContainer(page) {
|
|
260
|
+
var _a;
|
|
261
|
+
const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
|
|
262
|
+
const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
263
|
+
if (page) {
|
|
264
|
+
return document.querySelector(`.taro_page#${id}`);
|
|
265
|
+
}
|
|
266
|
+
const el = (id
|
|
267
|
+
? document.querySelector(`.taro_page#${id}`)
|
|
268
|
+
: document.querySelector('.taro_page') ||
|
|
269
|
+
document.querySelector('.taro_router'));
|
|
270
|
+
return el || window;
|
|
271
|
+
}
|
|
272
|
+
bindPageEvents(page, pageEl, config = {}) {
|
|
273
|
+
var _a;
|
|
274
|
+
if (!pageEl) {
|
|
275
|
+
pageEl = this.getPageContainer();
|
|
276
|
+
}
|
|
277
|
+
const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
|
|
278
|
+
bindPageScroll(page, pageEl, distance);
|
|
279
|
+
bindPageResize(page);
|
|
280
|
+
}
|
|
281
|
+
}
|