@tarojs/router 3.5.0-alpha.13 → 3.5.0-alpha.16

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/index.esm.js CHANGED
@@ -1,12 +1,41 @@
1
1
  import { createBrowserHistory, createHashHistory, Action, parsePath } from 'history';
2
- import { requestAnimationFrame, Current, container, SERVICE_IDENTIFIER, eventCenter, createPageConfig, stringify } from '@tarojs/runtime';
3
- import UniversalRouter from 'universal-router';
2
+ import { Current, eventCenter, createPageConfig, hooks, stringify, requestAnimationFrame } from '@tarojs/runtime';
3
+ import MobileDetect from 'mobile-detect';
4
4
  import queryString from 'query-string';
5
5
  import { initTabBarApis } from '@tarojs/taro';
6
+ import UniversalRouter from 'universal-router';
7
+
8
+ /*! *****************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
6
32
 
33
+ // export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
34
+ // export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
7
35
  const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
8
36
  const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
9
- const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
37
+ const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
38
+ const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
10
39
  class RoutesAlias {
11
40
  constructor() {
12
41
  this.conf = [];
@@ -169,31 +198,6 @@ function prependBasename(url = '') {
169
198
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
170
199
  }
171
200
 
172
- /*! *****************************************************************************
173
- Copyright (c) Microsoft Corporation.
174
-
175
- Permission to use, copy, modify, and/or distribute this software for any
176
- purpose with or without fee is hereby granted.
177
-
178
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
179
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
180
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
181
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
182
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
183
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
184
- PERFORMANCE OF THIS SOFTWARE.
185
- ***************************************************************************** */
186
-
187
- function __awaiter(thisArg, _arguments, P, generator) {
188
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
189
- return new (P || (P = Promise))(function (resolve, reject) {
190
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
191
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
192
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
193
- step((generator = generator.apply(thisArg, _arguments || [])).next());
194
- });
195
- }
196
-
197
201
  class Stacks {
198
202
  constructor() {
199
203
  this.stacks = [];
@@ -343,34 +347,35 @@ function getCurrentPages() {
343
347
  return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
344
348
  }
345
349
 
346
- /**
347
- * 插入页面动画需要的样式
348
- */
349
- function loadAnimateStyle(ms = 300) {
350
- const css = `
351
- .taro_router .taro_page {
352
- position: absolute;
353
- left: 0;
354
- top: 0;
355
- width: 100%;
356
- height: 100%;
357
- background-color: #fff;
358
- transform: translate(100%, 0);
359
- transition: transform ${ms}ms;
360
- z-index: 0;
350
+ let md;
351
+ let preTitle = document.title;
352
+ let isLoadDdEntry = false;
353
+ function getMobileDetect() {
354
+ if (!md) {
355
+ md = new MobileDetect(navigator.userAgent);
356
+ }
357
+ return md;
361
358
  }
362
-
363
- .taro_router .taro_page.taro_tabbar_page,
364
- .taro_router .taro_page.taro_page_show.taro_page_stationed {
365
- transform: none;
359
+ function setTitle(title) {
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ if (preTitle === title)
362
+ return title;
363
+ document.title = title;
364
+ preTitle = title;
365
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
366
+ if (!isLoadDdEntry) {
367
+ isLoadDdEntry = true;
368
+ require('dingtalk-jsapi/platform');
369
+ }
370
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
371
+ setDingTitle({ title });
372
+ }
373
+ return title;
374
+ });
366
375
  }
367
-
368
- .taro_router .taro_page.taro_page_show {
369
- transform: translate(0, 0);
370
- }`;
371
- const style = document.createElement('style');
372
- style.innerHTML = css;
373
- document.getElementsByTagName('head')[0].appendChild(style);
376
+ function isDingTalk() {
377
+ const md = getMobileDetect();
378
+ return md.match(/DingTalk/ig);
374
379
  }
375
380
 
376
381
  let pageResizeFn;
@@ -394,7 +399,8 @@ function bindPageScroll(page, pageEl, distance = 50) {
394
399
  pageDOM = pageEl;
395
400
  let isReachBottom = false;
396
401
  pageScrollFn = function () {
397
- page.onPageScroll && page.onPageScroll({
402
+ var _a;
403
+ (_a = page.onPageScroll) === null || _a === void 0 ? void 0 : _a.call(page, {
398
404
  scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
399
405
  });
400
406
  if (isReachBottom && getOffset() > distance) {
@@ -446,6 +452,192 @@ function initTabbar(config) {
446
452
  initTabBarApis(config);
447
453
  }
448
454
 
455
+ /* eslint-disable dot-notation */
456
+ class MultiPageHandler {
457
+ constructor(config) {
458
+ this.config = config;
459
+ this.mount();
460
+ }
461
+ get appId() { return 'app'; }
462
+ get router() { return this.config.router || {}; }
463
+ get routerMode() { return this.router.mode || 'hash'; }
464
+ get customRoutes() { return this.router.customRoutes || {}; }
465
+ get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
466
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
467
+ set pathname(p) { this.router.pathname = p; }
468
+ get pathname() { return this.router.pathname; }
469
+ get basename() { return this.router.basename || ''; }
470
+ get pageConfig() { return this.config.route; }
471
+ get isTabBar() {
472
+ var _a;
473
+ const routePath = stripBasename(this.pathname, this.basename);
474
+ const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
475
+ if (typeof target === 'string') {
476
+ return target === routePath;
477
+ }
478
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
479
+ return target.includes(routePath);
480
+ }
481
+ return false;
482
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
483
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
484
+ }
485
+ get search() { return location.search.substr(1); }
486
+ getQuery(search = '', options = {}) {
487
+ search = search ? `${search}&${this.search}` : this.search;
488
+ const query = search
489
+ ? queryString.parse(search)
490
+ : {};
491
+ return Object.assign(Object.assign({}, query), options);
492
+ }
493
+ mount() {
494
+ var _a;
495
+ setHistoryMode(this.routerMode, this.router.basename);
496
+ (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
497
+ const app = document.createElement('div');
498
+ app.id = this.appId;
499
+ app.classList.add('taro_router');
500
+ if (this.tabBarList.length > 1) {
501
+ const container = document.createElement('div');
502
+ container.classList.add('taro-tabbar__container');
503
+ container.id = 'container';
504
+ const panel = document.createElement('div');
505
+ panel.classList.add('taro-tabbar__panel');
506
+ panel.appendChild(app);
507
+ container.appendChild(panel);
508
+ document.body.appendChild(container);
509
+ initTabbar(this.config);
510
+ }
511
+ else {
512
+ document.body.appendChild(app);
513
+ }
514
+ }
515
+ onReady(page, onLoad = true) {
516
+ var _a;
517
+ const pageEl = this.getPageContainer(page);
518
+ if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
519
+ const el = pageEl.firstElementChild;
520
+ (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
521
+ onLoad && (pageEl['__page'] = page);
522
+ }
523
+ }
524
+ load(page, pageConfig = {}) {
525
+ var _a;
526
+ if (!page)
527
+ return;
528
+ (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
529
+ var _a;
530
+ const pageEl = this.getPageContainer(page);
531
+ this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
532
+ this.onReady(page, true);
533
+ (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
534
+ this.bindPageEvents(page, pageEl, pageConfig);
535
+ });
536
+ }
537
+ getPageContainer(page) {
538
+ var _a;
539
+ const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
540
+ const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
541
+ if (page) {
542
+ return document.querySelector(`.taro_page#${id}`);
543
+ }
544
+ const el = (id
545
+ ? document.querySelector(`.taro_page#${id}`)
546
+ : document.querySelector('.taro_page') ||
547
+ document.querySelector('.taro_router'));
548
+ return el || window;
549
+ }
550
+ bindPageEvents(page, pageEl, config = {}) {
551
+ var _a;
552
+ if (!pageEl) {
553
+ pageEl = this.getPageContainer();
554
+ }
555
+ const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
556
+ bindPageScroll(page, pageEl, distance);
557
+ bindPageResize(page);
558
+ }
559
+ }
560
+
561
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
562
+ /** Note: 关于多页面应用
563
+ * - 需要配置路由映射(根目录跳转、404 页面……)
564
+ * - app.onPageNotFound 事件不支持
565
+ * - 应用生命周期可能多次触发
566
+ * - TabBar 会多次加载
567
+ * - 不支持路由动画
568
+ */
569
+ function createMultiRouter(app, config, framework) {
570
+ var _a, _b, _c, _d, _e, _f;
571
+ return __awaiter(this, void 0, void 0, function* () {
572
+ RouterConfig.config = config;
573
+ const handler = new MultiPageHandler(config);
574
+ const launchParam = handler.getQuery();
575
+ (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
576
+ app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
577
+ const pathName = config.pageName;
578
+ const pageConfig = handler.pageConfig;
579
+ let element;
580
+ try {
581
+ element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
582
+ }
583
+ catch (error) {
584
+ throw new Error(error);
585
+ }
586
+ if (!element)
587
+ return;
588
+ let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
589
+ eventCenter.trigger('__taroRouterChange', {
590
+ toLocation: {
591
+ path: pathName
592
+ }
593
+ });
594
+ if (pageConfig) {
595
+ setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
596
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
597
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
598
+ }
599
+ }
600
+ const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
601
+ const loadConfig = Object.assign({}, pageConfig);
602
+ delete loadConfig['path'];
603
+ delete loadConfig['load'];
604
+ const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, config.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
605
+ handler.load(page, pageConfig);
606
+ (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
607
+ });
608
+ }
609
+
610
+ /**
611
+ * 插入页面动画需要的样式
612
+ */
613
+ function loadAnimateStyle(ms = 300) {
614
+ const css = `
615
+ .taro_router .taro_page {
616
+ position: absolute;
617
+ left: 0;
618
+ top: 0;
619
+ width: 100%;
620
+ height: 100%;
621
+ background-color: #fff;
622
+ transform: translate(100%, 0);
623
+ transition: transform ${ms}ms;
624
+ z-index: 0;
625
+ }
626
+
627
+ .taro_router .taro_page.taro_tabbar_page,
628
+ .taro_router .taro_page.taro_page_show.taro_page_stationed {
629
+ transform: none;
630
+ }
631
+
632
+ .taro_router .taro_page.taro_page_show {
633
+ transform: translate(0, 0);
634
+ }`;
635
+ const style = document.createElement('style');
636
+ style.innerHTML = css;
637
+ document.getElementsByTagName('head')[0].appendChild(style);
638
+ }
639
+
640
+ /* eslint-disable dot-notation */
449
641
  function setDisplay(el, type = '') {
450
642
  if (el) {
451
643
  el.style.display = type;
@@ -458,10 +650,10 @@ class PageHandler {
458
650
  this.mount();
459
651
  }
460
652
  get appId() { return 'app'; }
461
- get router() { return this.config.router; }
653
+ get router() { return this.config.router || {}; }
462
654
  get routerMode() { return this.router.mode || 'hash'; }
463
655
  get customRoutes() { return this.router.customRoutes || {}; }
464
- get routes() { return this.config.routes; }
656
+ get routes() { return this.config.routes || []; }
465
657
  get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
466
658
  get PullDownRefresh() { return this.config.PullDownRefresh; }
467
659
  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; }
@@ -508,7 +700,7 @@ class PageHandler {
508
700
  }
509
701
  return false;
510
702
  })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
511
- return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
703
+ return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
512
704
  }
513
705
  isSamePage(page) {
514
706
  const routePath = stripBasename(this.pathname, this.basename);
@@ -722,7 +914,6 @@ function createRouter(app, config, framework) {
722
914
  var _a, _b;
723
915
  RouterConfig.config = config;
724
916
  const handler = new PageHandler(config);
725
- const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
726
917
  routesAlias.set(handler.router.customRoutes);
727
918
  const basename = handler.router.basename;
728
919
  const routes = handler.routes.map(route => {
@@ -736,6 +927,7 @@ function createRouter(app, config, framework) {
736
927
  const router = new UniversalRouter(routes, { baseUrl: basename || '' });
737
928
  const launchParam = handler.getQuery(stacks.length);
738
929
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
930
+ app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
739
931
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
740
932
  var _c, _d, _e, _f, _g;
741
933
  handler.pathname = decodeURI(location.pathname);
@@ -765,6 +957,7 @@ function createRouter(app, config, framework) {
765
957
  });
766
958
  if (pageConfig) {
767
959
  document.title = (_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title;
960
+ setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
768
961
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
769
962
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
770
963
  }
@@ -776,12 +969,15 @@ function createRouter(app, config, framework) {
776
969
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
777
970
  const prevIndex = stacks.getPrevIndex(pathname);
778
971
  const delta = stacks.getDelta(pathname);
779
- handler.unload(currentPage, delta, prevIndex > -1);
780
- if (prevIndex > -1) {
781
- handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
782
- }
783
- else {
784
- shouldLoad = true;
972
+ // NOTE: Safari 内核浏览器在非应用页面返回上一页时,会触发额外的 POP 事件,此处需避免当前页面被错误卸载
973
+ if (currentPage !== stacks.getItem(prevIndex)) {
974
+ handler.unload(currentPage, delta, prevIndex > -1);
975
+ if (prevIndex > -1) {
976
+ handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
977
+ }
978
+ else {
979
+ shouldLoad = true;
980
+ }
785
981
  }
786
982
  }
787
983
  else {
@@ -806,12 +1002,12 @@ function createRouter(app, config, framework) {
806
1002
  shouldLoad = true;
807
1003
  }
808
1004
  if (shouldLoad || stacks.length < 1) {
809
- const el = (_f = element.default) !== null && _f !== void 0 ? _f : element;
1005
+ const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
810
1006
  const loadConfig = Object.assign({}, pageConfig);
811
1007
  const stacksIndex = stacks.length;
812
1008
  delete loadConfig['path'];
813
1009
  delete loadConfig['load'];
814
- const page = createPageConfig(enablePullDownRefresh ? (_g = runtimeHooks.createPullDownComponent) === null || _g === void 0 ? void 0 : _g.call(runtimeHooks, el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
1010
+ const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
815
1011
  if (params)
816
1012
  page.options = params;
817
1013
  return handler.load(page, pageConfig, stacksIndex);
@@ -826,159 +1022,5 @@ function createRouter(app, config, framework) {
826
1022
  return history.listen(render);
827
1023
  }
828
1024
 
829
- class MultiPageHandler {
830
- constructor(config) {
831
- this.config = config;
832
- this.mount();
833
- }
834
- get appId() { return 'app'; }
835
- get router() { return this.config.router; }
836
- get routerMode() { return this.router.mode || 'hash'; }
837
- get customRoutes() { return this.router.customRoutes || {}; }
838
- get tabBarList() { var _a; return ((_a = this.config.tabBar) === null || _a === void 0 ? void 0 : _a.list) || []; }
839
- get PullDownRefresh() { return this.config.PullDownRefresh; }
840
- set pathname(p) { this.router.pathname = p; }
841
- get pathname() { return this.router.pathname; }
842
- get basename() { return this.router.basename || ''; }
843
- get pageConfig() { return this.config.route; }
844
- get isTabBar() {
845
- var _a;
846
- const routePath = stripBasename(this.pathname, this.basename);
847
- const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
848
- if (typeof target === 'string') {
849
- return target === routePath;
850
- }
851
- else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
852
- return target.includes(routePath);
853
- }
854
- return false;
855
- })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
856
- return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
857
- }
858
- get search() { return location.search.substr(1); }
859
- getQuery(search = '', options = {}) {
860
- search = search ? `${search}&${this.search}` : this.search;
861
- const query = search
862
- ? queryString.parse(search)
863
- : {};
864
- return Object.assign(Object.assign({}, query), options);
865
- }
866
- mount() {
867
- var _a;
868
- setHistoryMode(this.routerMode, this.router.basename);
869
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
870
- const app = document.createElement('div');
871
- app.id = this.appId;
872
- app.classList.add('taro_router');
873
- if (this.tabBarList.length > 1) {
874
- const container = document.createElement('div');
875
- container.classList.add('taro-tabbar__container');
876
- container.id = 'container';
877
- const panel = document.createElement('div');
878
- panel.classList.add('taro-tabbar__panel');
879
- panel.appendChild(app);
880
- container.appendChild(panel);
881
- document.body.appendChild(container);
882
- initTabbar(this.config);
883
- }
884
- else {
885
- document.body.appendChild(app);
886
- }
887
- }
888
- onReady(page, onLoad = true) {
889
- var _a;
890
- const pageEl = this.getPageContainer(page);
891
- if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
892
- const el = pageEl.firstElementChild;
893
- (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
894
- onLoad && (pageEl['__page'] = page);
895
- }
896
- }
897
- load(page, pageConfig = {}) {
898
- var _a;
899
- if (!page)
900
- return;
901
- (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
902
- var _a;
903
- const pageEl = this.getPageContainer(page);
904
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
905
- this.onReady(page, true);
906
- (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
907
- this.bindPageEvents(page, pageEl, pageConfig);
908
- });
909
- }
910
- getPageContainer(page) {
911
- var _a;
912
- const path = page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.page) === null || _a === void 0 ? void 0 : _a.path;
913
- const id = path === null || path === void 0 ? void 0 : path.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
914
- if (page) {
915
- return document.querySelector(`.taro_page#${id}`);
916
- }
917
- const el = (id
918
- ? document.querySelector(`.taro_page#${id}`)
919
- : document.querySelector('.taro_page') ||
920
- document.querySelector('.taro_router'));
921
- return el || window;
922
- }
923
- bindPageEvents(page, pageEl, config = {}) {
924
- var _a;
925
- if (!pageEl) {
926
- pageEl = this.getPageContainer();
927
- }
928
- const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
929
- bindPageScroll(page, pageEl, distance);
930
- bindPageResize(page);
931
- }
932
- }
933
-
934
- // TODO 支持多路由 (APP 生命周期仅触发一次)
935
- /** Note: 关于多页面应用
936
- * - 需要配置路由映射(根目录跳转、404 页面……)
937
- * - app.onPageNotFound 事件不支持
938
- * - 应用生命周期可能多次触发
939
- * - TabBar 会多次加载
940
- * - 不支持路由动画
941
- */
942
- function createMultiRouter(app, config, framework) {
943
- var _a, _b, _c, _d, _e, _f, _g;
944
- return __awaiter(this, void 0, void 0, function* () {
945
- RouterConfig.config = config;
946
- const handler = new MultiPageHandler(config);
947
- const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
948
- const launchParam = handler.getQuery();
949
- (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
950
- const pathName = config.pageName;
951
- const pageConfig = handler.pageConfig;
952
- let element;
953
- try {
954
- element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
955
- }
956
- catch (error) {
957
- throw new Error(error);
958
- }
959
- if (!element)
960
- return;
961
- let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
962
- eventCenter.trigger('__taroRouterChange', {
963
- toLocation: {
964
- path: pathName
965
- }
966
- });
967
- if (pageConfig) {
968
- document.title = (_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title;
969
- if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
970
- enablePullDownRefresh = pageConfig.enablePullDownRefresh;
971
- }
972
- }
973
- const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
974
- const loadConfig = Object.assign({}, pageConfig);
975
- delete loadConfig['path'];
976
- delete loadConfig['load'];
977
- const page = createPageConfig(enablePullDownRefresh ? (_f = runtimeHooks.createPullDownComponent) === null || _f === void 0 ? void 0 : _f.call(runtimeHooks, el, location.pathname, framework, config.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
978
- handler.load(page, pageConfig);
979
- (_g = app.onShow) === null || _g === void 0 ? void 0 : _g.call(app, launchParam);
980
- });
981
- }
982
-
983
1025
  export { createMultiRouter, createRouter, getCurrentPages, history, navigateBack, navigateTo, reLaunch, redirectTo, switchTab };
984
1026
  //# sourceMappingURL=index.esm.js.map