@tarojs/router 3.5.0-beta.2 → 3.5.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 插入页面动画需要的样式
3
+ */
4
+ export declare function loadAnimateStyle(ms?: number): void;
package/dist/api.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import Taro from '@tarojs/taro';
2
+ export declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
3
+ export declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
4
+ export declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
5
+ export declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
6
+ export declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
7
+ export declare function getCurrentPages(): Taro.Page[];
package/dist/api.js CHANGED
@@ -8,10 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { parsePath } from 'history';
11
- import stacks from './router/stack';
12
11
  import { history, prependBasename } from './history';
13
- import { routesAlias, addLeadingSlash } from './utils';
14
12
  import { RouterConfig } from './router';
13
+ import stacks from './router/stack';
14
+ import { addLeadingSlash, routesAlias } from './utils';
15
15
  function processNavigateUrl(option) {
16
16
  var _a;
17
17
  const pathPieces = parsePath(option.url);
@@ -0,0 +1,2 @@
1
+ import { PageInstance } from '@tarojs/runtime';
2
+ export declare function bindPageResize(page: PageInstance): void;
@@ -0,0 +1,2 @@
1
+ import { PageInstance } from '@tarojs/runtime';
2
+ export declare function bindPageScroll(page: PageInstance, pageEl: HTMLElement, distance?: number): void;
@@ -5,7 +5,8 @@ export function bindPageScroll(page, pageEl, distance = 50) {
5
5
  pageDOM = pageEl;
6
6
  let isReachBottom = false;
7
7
  pageScrollFn = function () {
8
- page.onPageScroll && page.onPageScroll({
8
+ var _a;
9
+ (_a = page.onPageScroll) === null || _a === void 0 ? void 0 : _a.call(page, {
9
10
  scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
10
11
  });
11
12
  if (isReachBottom && getOffset() > distance) {
@@ -0,0 +1,5 @@
1
+ import { IH5RouterConfig } from '@tarojs/taro/types/compile';
2
+ import { History } from 'history';
3
+ export declare let history: History;
4
+ export declare function setHistoryMode(mode?: IH5RouterConfig['mode'], base?: string): void;
5
+ export declare function prependBasename(url?: string): string;
@@ -0,0 +1,23 @@
1
+ import Taro from "@tarojs/taro";
2
+ import { History } from "history";
3
+ /* eslint-disable dot-notation */
4
+ import { AppInstance } from "@tarojs/runtime";
5
+ import { MpaRouterConfig, SpaRouterConfig } from "../../types/router";
6
+ declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
7
+ declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
8
+ declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
9
+ declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
10
+ declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
11
+ declare function getCurrentPages(): Taro.Page[];
12
+ declare let history: History;
13
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
14
+ /** Note: 关于多页面应用
15
+ * - 需要配置路由映射(根目录跳转、404 页面……)
16
+ * - app.onPageNotFound 事件不支持
17
+ * - 应用生命周期可能多次触发
18
+ * - TabBar 会多次加载
19
+ * - 不支持路由动画
20
+ */
21
+ declare function createMultiRouter(app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
22
+ declare function createRouter(app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
23
+ export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, createMultiRouter, createRouter };
package/dist/index.cjs.js CHANGED
@@ -4,18 +4,44 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var history = require('history');
6
6
  var runtime = require('@tarojs/runtime');
7
- var UniversalRouter = require('universal-router');
8
7
  var queryString = require('query-string');
9
8
  var taro = require('@tarojs/taro');
9
+ var UniversalRouter = require('universal-router');
10
10
 
11
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
12
 
13
- var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
14
13
  var queryString__default = /*#__PURE__*/_interopDefaultLegacy(queryString);
14
+ var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
15
+
16
+ /*! *****************************************************************************
17
+ Copyright (c) Microsoft Corporation.
18
+
19
+ Permission to use, copy, modify, and/or distribute this software for any
20
+ purpose with or without fee is hereby granted.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
23
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
25
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
26
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
27
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28
+ PERFORMANCE OF THIS SOFTWARE.
29
+ ***************************************************************************** */
30
+
31
+ function __awaiter(thisArg, _arguments, P, generator) {
32
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
33
+ return new (P || (P = Promise))(function (resolve, reject) {
34
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
35
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
36
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
37
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
38
+ });
39
+ }
15
40
 
16
41
  const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
17
42
  const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
18
43
  const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
44
+ const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
19
45
  class RoutesAlias {
20
46
  constructor() {
21
47
  this.conf = [];
@@ -178,31 +204,6 @@ function prependBasename(url = '') {
178
204
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
179
205
  }
180
206
 
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
207
  class Stacks {
207
208
  constructor() {
208
209
  this.stacks = [];
@@ -352,36 +353,6 @@ function getCurrentPages() {
352
353
  return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
353
354
  }
354
355
 
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
356
  let pageResizeFn;
386
357
  function bindPageResize(page) {
387
358
  pageResizeFn && window.removeEventListener('resize', pageResizeFn);
@@ -403,7 +374,8 @@ function bindPageScroll(page, pageEl, distance = 50) {
403
374
  pageDOM = pageEl;
404
375
  let isReachBottom = false;
405
376
  pageScrollFn = function () {
406
- page.onPageScroll && page.onPageScroll({
377
+ var _a;
378
+ (_a = page.onPageScroll) === null || _a === void 0 ? void 0 : _a.call(page, {
407
379
  scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
408
380
  });
409
381
  if (isReachBottom && getOffset() > distance) {
@@ -455,6 +427,192 @@ function initTabbar(config) {
455
427
  taro.initTabBarApis(config);
456
428
  }
457
429
 
430
+ /* eslint-disable dot-notation */
431
+ class MultiPageHandler {
432
+ constructor(config) {
433
+ this.config = config;
434
+ this.mount();
435
+ }
436
+ get appId() { return 'app'; }
437
+ get router() { return this.config.router; }
438
+ get routerMode() { return this.router.mode || 'hash'; }
439
+ get customRoutes() { return this.router.customRoutes || {}; }
440
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
441
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
442
+ set pathname(p) { this.router.pathname = p; }
443
+ get pathname() { return this.router.pathname; }
444
+ get basename() { return this.router.basename || ''; }
445
+ get pageConfig() { return this.config.route; }
446
+ get isTabBar() {
447
+ var _a;
448
+ const routePath = stripBasename(this.pathname, this.basename);
449
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
450
+ if (typeof target === 'string') {
451
+ return target === routePath;
452
+ }
453
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
454
+ return target.includes(routePath);
455
+ }
456
+ return false;
457
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
458
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
459
+ }
460
+ get search() { return location.search.substr(1); }
461
+ getQuery(search = '', options = {}) {
462
+ search = search ? `${search}&${this.search}` : this.search;
463
+ const query = search
464
+ ? queryString__default["default"].parse(search)
465
+ : {};
466
+ return Object.assign(Object.assign({}, query), options);
467
+ }
468
+ mount() {
469
+ var _a;
470
+ setHistoryMode(this.routerMode, this.router.basename);
471
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
472
+ const app = document.createElement('div');
473
+ app.id = this.appId;
474
+ app.classList.add('taro_router');
475
+ if (this.tabBarList.length > 1) {
476
+ const container = document.createElement('div');
477
+ container.classList.add('taro-tabbar__container');
478
+ container.id = 'container';
479
+ const panel = document.createElement('div');
480
+ panel.classList.add('taro-tabbar__panel');
481
+ panel.appendChild(app);
482
+ container.appendChild(panel);
483
+ document.body.appendChild(container);
484
+ initTabbar(this.config);
485
+ }
486
+ else {
487
+ document.body.appendChild(app);
488
+ }
489
+ }
490
+ onReady(page, onLoad = true) {
491
+ var _a;
492
+ const pageEl = this.getPageContainer(page);
493
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
494
+ const el = pageEl.firstElementChild;
495
+ (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
496
+ onLoad && (pageEl['__page'] = page);
497
+ }
498
+ }
499
+ load(page, pageConfig = {}) {
500
+ var _a;
501
+ if (!page)
502
+ return;
503
+ (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
504
+ var _a;
505
+ const pageEl = this.getPageContainer(page);
506
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
507
+ this.onReady(page, true);
508
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
509
+ this.bindPageEvents(page, pageEl, pageConfig);
510
+ });
511
+ }
512
+ getPageContainer(page) {
513
+ var _a;
514
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
515
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
516
+ if (page) {
517
+ return document.querySelector(`.taro_page#${id}`);
518
+ }
519
+ const el = (id
520
+ ? document.querySelector(`.taro_page#${id}`)
521
+ : document.querySelector('.taro_page') ||
522
+ document.querySelector('.taro_router'));
523
+ return el || window;
524
+ }
525
+ bindPageEvents(page, pageEl, config = {}) {
526
+ var _a;
527
+ if (!pageEl) {
528
+ pageEl = this.getPageContainer();
529
+ }
530
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
531
+ bindPageScroll(page, pageEl, distance);
532
+ bindPageResize(page);
533
+ }
534
+ }
535
+
536
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
537
+ /** Note: 关于多页面应用
538
+ * - 需要配置路由映射(根目录跳转、404 页面……)
539
+ * - app.onPageNotFound 事件不支持
540
+ * - 应用生命周期可能多次触发
541
+ * - TabBar 会多次加载
542
+ * - 不支持路由动画
543
+ */
544
+ function createMultiRouter(app, config, framework) {
545
+ var _a, _b, _c, _d, _e, _f;
546
+ return __awaiter(this, void 0, void 0, function* () {
547
+ RouterConfig.config = config;
548
+ const handler = new MultiPageHandler(config);
549
+ const launchParam = handler.getQuery();
550
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
551
+ app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
552
+ const pathName = config.pageName;
553
+ const pageConfig = handler.pageConfig;
554
+ let element;
555
+ try {
556
+ element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
557
+ }
558
+ catch (error) {
559
+ throw new Error(error);
560
+ }
561
+ if (!element)
562
+ return;
563
+ let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
564
+ runtime.eventCenter.trigger('__taroRouterChange', {
565
+ toLocation: {
566
+ path: pathName
567
+ }
568
+ });
569
+ if (pageConfig) {
570
+ document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
571
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
572
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
573
+ }
574
+ }
575
+ const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
576
+ const loadConfig = Object.assign({}, pageConfig);
577
+ delete loadConfig['path'];
578
+ delete loadConfig['load'];
579
+ const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
580
+ handler.load(page, pageConfig);
581
+ (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
582
+ });
583
+ }
584
+
585
+ /**
586
+ * 插入页面动画需要的样式
587
+ */
588
+ function loadAnimateStyle(ms = 300) {
589
+ const css = `
590
+ .taro_router .taro_page {
591
+ position: absolute;
592
+ left: 0;
593
+ top: 0;
594
+ width: 100%;
595
+ height: 100%;
596
+ background-color: #fff;
597
+ transform: translate(100%, 0);
598
+ transition: transform ${ms}ms;
599
+ z-index: 0;
600
+ }
601
+
602
+ .taro_router .taro_page.taro_tabbar_page,
603
+ .taro_router .taro_page.taro_page_show.taro_page_stationed {
604
+ transform: none;
605
+ }
606
+
607
+ .taro_router .taro_page.taro_page_show {
608
+ transform: translate(0, 0);
609
+ }`;
610
+ const style = document.createElement('style');
611
+ style.innerHTML = css;
612
+ document.getElementsByTagName('head')[0].appendChild(style);
613
+ }
614
+
615
+ /* eslint-disable dot-notation */
458
616
  function setDisplay(el, type = '') {
459
617
  if (el) {
460
618
  el.style.display = type;
@@ -517,7 +675,7 @@ class PageHandler {
517
675
  }
518
676
  return false;
519
677
  })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
520
- return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
678
+ return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
521
679
  }
522
680
  isSamePage(page) {
523
681
  const routePath = stripBasename(this.pathname, this.basename);
@@ -744,6 +902,7 @@ function createRouter(app, config, framework) {
744
902
  const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
745
903
  const launchParam = handler.getQuery(stacks.length);
746
904
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
905
+ app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
747
906
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
748
907
  var _c, _d, _e, _f;
749
908
  handler.pathname = decodeURI(location.pathname);
@@ -834,159 +993,6 @@ function createRouter(app, config, framework) {
834
993
  return exports.history.listen(render);
835
994
  }
836
995
 
837
- class MultiPageHandler {
838
- constructor(config) {
839
- this.config = config;
840
- this.mount();
841
- }
842
- get appId() { return 'app'; }
843
- get router() { return this.config.router; }
844
- get routerMode() { return this.router.mode || 'hash'; }
845
- get customRoutes() { return this.router.customRoutes || {}; }
846
- get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
847
- get PullDownRefresh() { return this.config.PullDownRefresh; }
848
- set pathname(p) { this.router.pathname = p; }
849
- get pathname() { return this.router.pathname; }
850
- get basename() { return this.router.basename || ''; }
851
- get pageConfig() { return this.config.route; }
852
- get isTabBar() {
853
- var _a;
854
- const routePath = stripBasename(this.pathname, this.basename);
855
- const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
856
- if (typeof target === 'string') {
857
- return target === routePath;
858
- }
859
- else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
860
- return target.includes(routePath);
861
- }
862
- return false;
863
- })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
864
- return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
865
- }
866
- get search() { return location.search.substr(1); }
867
- getQuery(search = '', options = {}) {
868
- search = search ? `${search}&${this.search}` : this.search;
869
- const query = search
870
- ? queryString__default["default"].parse(search)
871
- : {};
872
- return Object.assign(Object.assign({}, query), options);
873
- }
874
- mount() {
875
- var _a;
876
- setHistoryMode(this.routerMode, this.router.basename);
877
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
878
- const app = document.createElement('div');
879
- app.id = this.appId;
880
- app.classList.add('taro_router');
881
- if (this.tabBarList.length > 1) {
882
- const container = document.createElement('div');
883
- container.classList.add('taro-tabbar__container');
884
- container.id = 'container';
885
- const panel = document.createElement('div');
886
- panel.classList.add('taro-tabbar__panel');
887
- panel.appendChild(app);
888
- container.appendChild(panel);
889
- document.body.appendChild(container);
890
- initTabbar(this.config);
891
- }
892
- else {
893
- document.body.appendChild(app);
894
- }
895
- }
896
- onReady(page, onLoad = true) {
897
- var _a;
898
- const pageEl = this.getPageContainer(page);
899
- if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
900
- const el = pageEl.firstElementChild;
901
- (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
902
- onLoad && (pageEl['__page'] = page);
903
- }
904
- }
905
- load(page, pageConfig = {}) {
906
- var _a;
907
- if (!page)
908
- return;
909
- (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
910
- var _a;
911
- const pageEl = this.getPageContainer(page);
912
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
913
- this.onReady(page, true);
914
- (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
915
- this.bindPageEvents(page, pageEl, pageConfig);
916
- });
917
- }
918
- getPageContainer(page) {
919
- var _a;
920
- const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path;
921
- const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
922
- if (page) {
923
- return document.querySelector(`.taro_page#${id}`);
924
- }
925
- const el = (id
926
- ? document.querySelector(`.taro_page#${id}`)
927
- : document.querySelector('.taro_page') ||
928
- document.querySelector('.taro_router'));
929
- return el || window;
930
- }
931
- bindPageEvents(page, pageEl, config = {}) {
932
- var _a;
933
- if (!pageEl) {
934
- pageEl = this.getPageContainer();
935
- }
936
- const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
937
- bindPageScroll(page, pageEl, distance);
938
- bindPageResize(page);
939
- }
940
- }
941
-
942
- // TODO 支持多路由 (APP 生命周期仅触发一次)
943
- /** Note: 关于多页面应用
944
- * - 需要配置路由映射(根目录跳转、404 页面……)
945
- * - app.onPageNotFound 事件不支持
946
- * - 应用生命周期可能多次触发
947
- * - TabBar 会多次加载
948
- * - 不支持路由动画
949
- */
950
- function createMultiRouter(app, config, framework) {
951
- var _a, _b, _c, _d, _e, _f;
952
- return __awaiter(this, void 0, void 0, function* () {
953
- RouterConfig.config = config;
954
- const handler = new MultiPageHandler(config);
955
- const launchParam = handler.getQuery();
956
- (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
957
- const pathName = config.pageName;
958
- const pageConfig = handler.pageConfig;
959
- let element;
960
- try {
961
- element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
962
- }
963
- catch (error) {
964
- throw new Error(error);
965
- }
966
- if (!element)
967
- return;
968
- let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
969
- runtime.eventCenter.trigger('__taroRouterChange', {
970
- toLocation: {
971
- path: pathName
972
- }
973
- });
974
- if (pageConfig) {
975
- document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
976
- if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
977
- enablePullDownRefresh = pageConfig.enablePullDownRefresh;
978
- }
979
- }
980
- const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
981
- const loadConfig = Object.assign({}, pageConfig);
982
- delete loadConfig['path'];
983
- delete loadConfig['load'];
984
- const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
985
- handler.load(page, pageConfig);
986
- (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
987
- });
988
- }
989
-
990
996
  exports.createMultiRouter = createMultiRouter;
991
997
  exports.createRouter = createRouter;
992
998
  exports.getCurrentPages = getCurrentPages;