UNPKG

5.46 kBJavaScriptView Raw
1/* eslint-disable dot-notation */
2import { Current } from '@tarojs/runtime';
3import queryString from 'query-string';
4import { bindPageResize } from '../events/resize';
5import { bindPageScroll } from '../events/scroll';
6import { setHistoryMode } from '../history';
7import { initTabbar } from '../tabbar';
8import { addLeadingSlash, stripBasename } from '../utils';
9export default class MultiPageHandler {
10 constructor(config) {
11 this.config = config;
12 this.mount();
13 }
14 get appId() { return this.config.appId || '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 = addLeadingSlash(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 setHistoryMode(this.routerMode, this.router.basename);
48 const appId = this.appId;
49 let app = document.getElementById(appId);
50 let isPosition = true;
51 if (!app) {
52 app = document.createElement('div');
53 app.id = appId;
54 isPosition = false;
55 }
56 const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
57 app.classList.add('taro_router');
58 if (this.tabBarList.length > 1) {
59 const container = document.createElement('div');
60 container.classList.add('taro-tabbar__container');
61 container.id = 'container';
62 const panel = document.createElement('div');
63 panel.classList.add('taro-tabbar__panel');
64 panel.appendChild(app.cloneNode(true));
65 container.appendChild(panel);
66 if (!isPosition) {
67 appWrapper.appendChild(container);
68 }
69 else {
70 appWrapper.replaceChild(container, app);
71 }
72 initTabbar(this.config);
73 }
74 else {
75 if (!isPosition)
76 appWrapper.appendChild(app);
77 }
78 }
79 onReady(page, onLoad = true) {
80 var _a;
81 const pageEl = this.getPageContainer(page);
82 if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
83 const el = pageEl.firstElementChild;
84 (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
85 onLoad && (pageEl['__page'] = page);
86 }
87 }
88 load(page, pageConfig = {}) {
89 var _a;
90 if (!page)
91 return;
92 (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
93 var _a;
94 const pageEl = this.getPageContainer(page);
95 this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
96 this.onReady(page, true);
97 (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
98 this.bindPageEvents(page, pageEl, pageConfig);
99 });
100 }
101 getPageContainer(page) {
102 var _a;
103 const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
104 const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
105 if (page) {
106 return document.querySelector(`.taro_page#${id}`);
107 }
108 const el = (id
109 ? document.querySelector(`.taro_page#${id}`)
110 : document.querySelector('.taro_page') ||
111 document.querySelector('.taro_router'));
112 return el || window;
113 }
114 bindPageEvents(page, pageEl, config = {}) {
115 var _a;
116 if (!pageEl) {
117 pageEl = this.getPageContainer();
118 }
119 const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
120 bindPageScroll(page, pageEl, distance);
121 bindPageResize(page);
122 }
123}