@tarojs/router 4.0.0-beta.7 → 4.0.0-beta.71

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.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { initNavigationBar } from './navigationBar.js';
1
2
  import { initTabbar } from './tabbar.js';
2
3
  export { getCurrentPages, navigateBack, navigateTo, reLaunch, redirectTo, switchTab } from './api.js';
3
4
  export { createMpaHistory, history, prependBasename, setHistory, setHistoryMode } from './history.js';
@@ -5,7 +6,7 @@ export { createMultiRouter } from './router/mpa.js';
5
6
  export { createRouter } from './router/spa.js';
6
7
  export { routesAlias } from './utils/index.js';
7
8
  export { createBrowserHistory, createHashHistory } from 'history';
8
- export { isDingTalk, isWeixin, setTitle } from './utils/navigate.js';
9
+ export { isDingTalk, isWeixin, setMpaTitle, setNavigationBarLoading, setNavigationBarStyle, setTitle } from './utils/navigate.js';
9
10
 
10
11
  function handleAppMount(config, _, appId = config.appId || 'app') {
11
12
  let app = document.getElementById(appId);
@@ -19,6 +20,7 @@ function handleAppMount(config, _, appId = config.appId || 'app') {
19
20
  app.classList.add('taro_router');
20
21
  if (!isPosition)
21
22
  appWrapper.appendChild(app);
23
+ initNavigationBar(config, appWrapper);
22
24
  }
23
25
  function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
24
26
  let app = document.getElementById(appId);
@@ -44,6 +46,7 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
44
46
  appWrapper.replaceChild(container, app);
45
47
  }
46
48
  initTabbar(config, history);
49
+ initNavigationBar(config, container);
47
50
  }
48
51
 
49
52
  export { handleAppMount, handleAppMountWithTabbar };
@@ -0,0 +1,3 @@
1
+ import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
2
+ declare function initNavigationBar(config: SpaRouterConfig | MpaRouterConfig, container: HTMLElement): void;
3
+ export { initNavigationBar };
@@ -0,0 +1,44 @@
1
+ import { loadNavigationBarStyle } from './style.js';
2
+
3
+ const home_svg_str = `
4
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
5
+ <path d="M23.8899 12.2737C23.8232 12.3584 23.7237 12.3997 23.6198 12.3974H20.7994V23.5996C20.7994 23.8194 20.6213 24 20.4001 24H14.7994C14.5791 24 14.4002 23.8194 14.4002 23.5996V15.6H9.59963V23.5996C9.59963 23.8194 9.42075 24 9.20033 24H3.59968C3.48981 24 3.38964 23.954 3.31764 23.8811C3.24495 23.8091 3.2004 23.7087 3.2004 23.5996V12.3975H0.398546V12.3967C0.296893 12.396 0.194446 12.3544 0.11579 12.2738C-0.0371146 12.114 -0.0400714 11.864 0.11579 11.7076L11.7201 0.117284C11.8767 -0.0390948 12.1298 -0.0390948 12.2863 0.117284L23.8899 11.7076C24.0465 11.864 24.0265 12.0995 23.8899 12.2737ZM12.0029 0.964625L1.37086 11.5854L3.59968 11.5839V11.5999C3.65537 11.5999 3.70804 11.611 3.75557 11.6307C3.89952 11.692 4.00046 11.8339 4.00046 11.9996V23.1991H8.79955V15.2003C8.79955 14.9789 8.97917 14.8002 9.20033 14.8002H14.7995C15.0207 14.8002 15.2003 14.9789 15.2003 15.2003V23.1991H20.0001V11.9996C20.0001 11.8339 20.1003 11.692 20.2443 11.6307C20.2918 11.611 20.3453 11.5999 20.4001 11.5999V11.5713L22.6193 11.5691L12.0029 0.964625Z" fill="currentColor"/>
6
+ </svg>
7
+ `;
8
+ const back_svg_str = `
9
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
10
+ <path d="M17.8206 22.9016L7.45515 11.8756L17.8206 1.09845C18.0598 0.849741 18.0598 0.435233 17.8206 0.186528C17.5814 -0.0621762 17.1827 -0.0621762 16.9435 0.186528L6.1794 11.4611C5.9402 11.7098 5.9402 12.1244 6.1794 12.3731L16.9435 23.8135C17.1827 24.0622 17.5814 24.0622 17.8206 23.8135C18.0598 23.5648 18.0598 23.1503 17.8206 22.9016Z" fill="currentColor"/>
11
+ </svg>
12
+ `;
13
+ const loading_svg_str = `
14
+ <svg t="1709608074670" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4741" width="24" height="24"><path d="M256 529.066667H85.333333a17.066667 17.066667 0 1 1 0-34.133334h170.666667a17.066667 17.066667 0 0 1 0 34.133334z" opacity=".278" p-id="4742"></path><path d="M99.84 640a17.066667 17.066667 0 0 1-4.437333-33.553067l164.693333-44.373333a17.066667 17.066667 0 1 1 8.891733 32.9728l-164.693333 44.373333a17.544533 17.544533 0 0 1-4.4544 0.580267z" opacity=".322" p-id="4743"></path><path d="M264.533333 462.523733a16.896 16.896 0 0 1-4.369066-0.580266l-164.693334-43.52a17.0496 17.0496 0 1 1 8.721067-32.989867l164.693333 43.52a17.066667 17.066667 0 1 1-4.352 33.570133z" opacity=".239" p-id="4744"></path><path d="M384.017067 307.2a17.032533 17.032533 0 0 1-14.7968-8.533333l-85.333334-147.626667a17.066667 17.066667 0 0 1 29.559467-17.083733l85.333333 147.626666A17.066667 17.066667 0 0 1 384.017067 307.2z" opacity=".122" p-id="4745"></path><path d="M639.982933 307.2a17.0496 17.0496 0 0 1-14.762666-25.6l85.333333-147.626667a17.066667 17.066667 0 1 1 29.559467 17.066667l-85.333334 147.626667a17.032533 17.032533 0 0 1-14.7968 8.533333z" opacity=".922" p-id="4746"></path><path d="M692.906667 347.306667a17.066667 17.066667 0 0 1-12.117334-29.098667l120.337067-121.173333a17.066667 17.066667 0 1 1 24.234667 24.046933l-120.337067 121.173333a17.1008 17.1008 0 0 1-12.117333 5.051734z" opacity=".878" p-id="4747"></path><path d="M733.883733 401.066667a17.066667 17.066667 0 0 1-8.5504-31.8464l147.626667-85.333334a17.0496 17.0496 0 1 1 17.066667 29.5424l-147.626667 85.333334a16.776533 16.776533 0 0 1-8.516267 2.304z" opacity=".839" p-id="4748"></path><path d="M512 273.066667a17.066667 17.066667 0 0 1-17.066667-17.066667V85.333333a17.066667 17.066667 0 0 1 34.133334 0v170.666667a17.066667 17.066667 0 0 1-17.066667 17.066667z" p-id="4749"></path><path d="M578.577067 281.6a17.066667 17.066667 0 0 1-16.520534-21.418667l43.52-164.693333a17.066667 17.066667 0 0 1 33.006934 8.721067l-43.52 164.693333a17.066667 17.066667 0 0 1-16.4864 12.6976z" opacity=".961" p-id="4750"></path><path d="M445.44 282.453333a17.066667 17.066667 0 0 1-16.469333-12.629333l-44.373334-164.693333a17.066667 17.066667 0 0 1 32.955734-8.891734l44.373333 164.693334a17.066667 17.066667 0 0 1-16.4864 21.521066z" opacity=".078" p-id="4751"></path><path d="M924.177067 640c-1.4848 0-2.9696-0.187733-4.4544-0.580267l-164.693334-44.373333a17.066667 17.066667 0 0 1 8.874667-32.9728l164.693333 44.373333a17.066667 17.066667 0 0 1-4.420266 33.553067z" opacity=".722" p-id="4752"></path><path d="M881.476267 742.4a17.015467 17.015467 0 0 1-8.482134-2.269867l-148.48-85.333333a17.0496 17.0496 0 1 1 16.9984-29.5936l148.48 85.333333a17.0496 17.0496 0 0 1-8.516266 31.863467z" opacity=".678" p-id="4753"></path><path d="M813.226667 830.293333a17.015467 17.015467 0 0 1-12.066134-5.000533l-120.337066-120.337067a17.0496 17.0496 0 1 1 24.132266-24.132266l120.337067 120.337066a17.0496 17.0496 0 0 1-12.066133 29.1328z" opacity=".639" p-id="4754"></path><path d="M938.666667 529.066667H768a17.066667 17.066667 0 1 1 0-34.133334h170.666667a17.066667 17.066667 0 1 1 0 34.133334z" opacity=".761" p-id="4755"></path><path d="M401.066667 941.226667a17.066667 17.066667 0 0 1-16.4864-21.504l44.373333-164.693334a17.066667 17.066667 0 1 1 32.955733 8.874667l-44.373333 164.693333a17.066667 17.066667 0 0 1-16.469333 12.629334z" opacity=".478" p-id="4756"></path><path d="M298.6496 898.56a17.066667 17.066667 0 0 1-14.779733-25.565867l85.333333-148.48a17.083733 17.083733 0 0 1 29.5936 16.9984l-85.333333 148.48a17.032533 17.032533 0 0 1-14.813867 8.567467z" opacity=".439" p-id="4757"></path><path d="M512 955.733333a17.066667 17.066667 0 0 1-17.066667-17.066666V768a17.066667 17.066667 0 1 1 34.133334 0v170.666667a17.066667 17.066667 0 0 1-17.066667 17.066666z" opacity=".522" p-id="4758"></path><path d="M725.3504 898.56a17.032533 17.032533 0 0 1-14.7968-8.533333l-85.333333-147.626667a17.066667 17.066667 0 0 1 29.559466-17.066667l85.333334 147.626667a17.066667 17.066667 0 0 1-14.762667 25.6z" opacity=".6" p-id="4759"></path><path d="M622.062933 942.08c-7.509333 0-14.421333-5.0176-16.469333-12.629333l-44.3904-164.693334a17.066667 17.066667 0 1 1 32.9728-8.874666l44.3904 164.693333a17.066667 17.066667 0 0 1-16.503467 21.504z" opacity=".561" p-id="4760"></path><path d="M759.4496 463.36a17.083733 17.083733 0 0 1-4.420267-33.553067l164.693334-44.373333a17.066667 17.066667 0 0 1 8.874666 32.955733l-164.693333 44.373334a16.657067 16.657067 0 0 1-4.4544 0.597333z" opacity=".702" p-id="4761"></path><path d="M330.24 347.306667a17.015467 17.015467 0 0 1-12.066133-5.000534l-120.32-120.32a17.0496 17.0496 0 1 1 24.132266-24.132266l120.32 120.32a17.0496 17.0496 0 0 1-12.066133 29.1328z" opacity=".161" p-id="4762"></path><path d="M290.116267 401.066667a17.032533 17.032533 0 0 1-8.533334-2.286934l-147.626666-85.333333a17.066667 17.066667 0 1 1 17.083733-29.5424l147.626667 85.333333a17.066667 17.066667 0 0 1-8.5504 31.829334z" opacity=".2" p-id="4763"></path><path d="M142.523733 742.4a17.066667 17.066667 0 0 1-8.567466-31.8464l147.626666-85.333333a17.066667 17.066667 0 1 1 17.083734 29.559466l-147.626667 85.333334a16.930133 16.930133 0 0 1-8.516267 2.286933z" opacity=".361" p-id="4764"></path><path d="M209.92 830.293333a17.066667 17.066667 0 0 1-12.117333-29.098666l120.32-121.173334a17.066667 17.066667 0 0 1 24.2176 24.029867l-120.32 121.1904a16.896 16.896 0 0 1-12.100267 5.051733z" opacity=".4" p-id="4765"></path></svg>
15
+ `;
16
+ function initNavigationBar(config, container) {
17
+ if (config.router.mode === 'multi')
18
+ return;
19
+ const navigationBar = document.createElement('div');
20
+ navigationBar.classList.add('taro-navigation-bar-no-icon');
21
+ const navigationBarBackBtn = document.createElement('div');
22
+ navigationBarBackBtn.classList.add('taro-navigation-bar-back');
23
+ const navigationBarHomeBtn = document.createElement('div');
24
+ navigationBarHomeBtn.classList.add('taro-navigation-bar-home');
25
+ navigationBarBackBtn.innerHTML = back_svg_str;
26
+ navigationBarHomeBtn.innerHTML = home_svg_str;
27
+ const navigationBarTitleWrap = document.createElement('div');
28
+ navigationBarTitleWrap.classList.add('taro-navigation-bar-title-wrap');
29
+ const navigationBarLoading = document.createElement('div');
30
+ navigationBarLoading.classList.add('taro-navigation-bar-loading');
31
+ navigationBarLoading.innerHTML = loading_svg_str;
32
+ const navigationBarTitle = document.createElement('div');
33
+ navigationBarTitle.classList.add('taro-navigation-bar-title');
34
+ navigationBarTitleWrap.appendChild(navigationBarLoading);
35
+ navigationBarTitleWrap.appendChild(navigationBarTitle);
36
+ navigationBar.appendChild(navigationBarHomeBtn);
37
+ navigationBar.appendChild(navigationBarBackBtn);
38
+ navigationBar.appendChild(navigationBarTitleWrap);
39
+ navigationBar.id = 'taro-navigation-bar';
40
+ container.prepend(navigationBar);
41
+ loadNavigationBarStyle();
42
+ }
43
+
44
+ export { initNavigationBar };
@@ -1,8 +1,9 @@
1
1
  import { __awaiter } from 'tslib';
2
- import { incrementId, eventCenter, createPageConfig, hooks, stringify } from '@tarojs/runtime';
3
- import { setTitle } from '../utils/navigate.js';
2
+ import { incrementId, eventCenter, createPageConfig, hooks, stringify, Current } from '@tarojs/runtime';
3
+ import '../utils/index.js';
4
4
  import { RouterConfig } from './index.js';
5
5
  import MultiPageHandler from './multi-page.js';
6
+ import { setMpaTitle } from '../utils/navigate.js';
6
7
 
7
8
  const createStampId = incrementId();
8
9
  const launchStampId = createStampId();
@@ -20,6 +21,7 @@ function createMultiRouter(history, app, config, framework) {
20
21
  if (typeof app.onUnhandledRejection === 'function') {
21
22
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
22
23
  }
24
+ eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
23
25
  RouterConfig.config = config;
24
26
  const handler = new MultiPageHandler(config, history);
25
27
  const launchParam = {
@@ -53,7 +55,7 @@ function createMultiRouter(history, app, config, framework) {
53
55
  return;
54
56
  let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
55
57
  if (pageConfig) {
56
- setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
58
+ setMpaTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
57
59
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
58
60
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
59
61
  }
@@ -65,6 +67,20 @@ function createMultiRouter(history, app, config, framework) {
65
67
  const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, pathName, framework, handler.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
66
68
  handler.load(page, pageConfig);
67
69
  (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
70
+ window.addEventListener('visibilitychange', () => {
71
+ var _a, _b, _c;
72
+ const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
73
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
74
+ const param = {};
75
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
76
+ Object.assign(param, launchParam, { path });
77
+ if (document.visibilityState === 'visible') {
78
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
79
+ }
80
+ else {
81
+ (_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
82
+ }
83
+ });
68
84
  });
69
85
  }
70
86
 
@@ -0,0 +1,36 @@
1
+ import PageHandler from "./page.js";
2
+ interface NavigationBarCache {
3
+ backgroundColor?: string;
4
+ fontColor?: string;
5
+ title?: string;
6
+ show?: boolean;
7
+ loading?: boolean;
8
+ }
9
+ declare class NavigationBarHandler {
10
+ pageContext: PageHandler;
11
+ navigationBarElement: HTMLElement;
12
+ cache: Record<string, NavigationBarCache>;
13
+ isLoadDdEntry: boolean;
14
+ constructor(pageContext: PageHandler);
15
+ private toHomeFn;
16
+ private backFn;
17
+ get homeBtnElement(): Element | null;
18
+ get backBtnElement(): Element | null;
19
+ get titleElement(): Element | null;
20
+ get loadingElement(): Element | null;
21
+ init(): void;
22
+ setNavigationBarElement(): void;
23
+ load(): void;
24
+ setCacheValue(): void;
25
+ setFnBtnState(): void;
26
+ shiftLoadingState(show: boolean): void;
27
+ setNavigationLoading(show?: boolean): void;
28
+ setNavigationBarBackground(backgroundColor?: string): void;
29
+ setNavigationBarTextStyle(fontColor?: string): void;
30
+ setTitle(title?: any): void;
31
+ fnBtnToggleToHome(): void;
32
+ fnBtnToggleToBack(): void;
33
+ fnBtnToggleToNone(): void;
34
+ setNavigationBarVisible(show?: any): void;
35
+ }
36
+ export { NavigationBarHandler as default };
@@ -0,0 +1,252 @@
1
+ import { eventCenter } from '@tarojs/runtime';
2
+ import { reLaunch, navigateBack } from '../api.js';
3
+ import '../utils/index.js';
4
+ import stacks from './stack.js';
5
+ import { isDingTalk } from '../utils/navigate.js';
6
+
7
+ class NavigationBarHandler {
8
+ constructor(pageContext) {
9
+ this.isLoadDdEntry = false;
10
+ this.cache = {};
11
+ this.pageContext = pageContext;
12
+ this.init();
13
+ eventCenter.on('__taroH5SetNavigationBarTitle', (title) => {
14
+ this.setTitle(title);
15
+ });
16
+ eventCenter.on('__taroH5setNavigationBarLoading', (loading) => {
17
+ this.setNavigationLoading(loading);
18
+ });
19
+ eventCenter.on('__taroH5setNavigationBarColor', ({ backgroundColor, frontColor }) => {
20
+ if (typeof backgroundColor === 'string')
21
+ this.setNavigationBarBackground(backgroundColor);
22
+ if (typeof frontColor === 'string')
23
+ this.setNavigationBarTextStyle(frontColor);
24
+ });
25
+ }
26
+ toHomeFn() {
27
+ reLaunch({ url: this.pageContext.homePage });
28
+ }
29
+ backFn() {
30
+ navigateBack();
31
+ }
32
+ get homeBtnElement() {
33
+ var _a;
34
+ if (!this.navigationBarElement)
35
+ return null;
36
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-home')) === null || _a === void 0 ? void 0 : _a[0];
37
+ }
38
+ get backBtnElement() {
39
+ var _a;
40
+ if (!this.navigationBarElement)
41
+ return null;
42
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-back')) === null || _a === void 0 ? void 0 : _a[0];
43
+ }
44
+ get titleElement() {
45
+ var _a;
46
+ if (!this.navigationBarElement)
47
+ return null;
48
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-title')) === null || _a === void 0 ? void 0 : _a[0];
49
+ }
50
+ get loadingElement() {
51
+ if (!this.navigationBarElement)
52
+ return null;
53
+ return this.navigationBarElement.getElementsByClassName('taro-navigation-bar-loading')[0];
54
+ }
55
+ init() {
56
+ var _a, _b;
57
+ this.setNavigationBarElement();
58
+ if (!this.navigationBarElement)
59
+ return;
60
+ (_a = this.homeBtnElement) === null || _a === void 0 ? void 0 : _a.addEventListener('click', this.toHomeFn.bind(this));
61
+ (_b = this.backBtnElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.backFn.bind(this));
62
+ }
63
+ setNavigationBarElement() {
64
+ this.navigationBarElement = document.getElementById('taro-navigation-bar');
65
+ }
66
+ load() {
67
+ this.setCacheValue();
68
+ this.setTitle();
69
+ this.setNavigationBarVisible();
70
+ this.setFnBtnState();
71
+ this.setNavigationBarBackground();
72
+ this.setNavigationBarTextStyle();
73
+ this.setNavigationLoading();
74
+ }
75
+ setCacheValue() {
76
+ const currentPage = this.pageContext.currentPage;
77
+ if (typeof this.cache[currentPage] !== 'object') {
78
+ this.cache[currentPage] = {};
79
+ }
80
+ }
81
+ setFnBtnState() {
82
+ const currentRouter = this.pageContext.currentPage;
83
+ if (this.pageContext.isTabBar(currentRouter) || this.pageContext.homePage === currentRouter) {
84
+ this.fnBtnToggleToNone();
85
+ }
86
+ else if (stacks.length > 1) {
87
+ this.fnBtnToggleToBack();
88
+ }
89
+ else {
90
+ this.fnBtnToggleToHome();
91
+ }
92
+ }
93
+ shiftLoadingState(show) {
94
+ if (!this.loadingElement)
95
+ return;
96
+ if (show) {
97
+ this.loadingElement.classList.add('taro-navigation-bar-loading-show');
98
+ }
99
+ else {
100
+ this.loadingElement.classList.remove('taro-navigation-bar-loading-show');
101
+ }
102
+ }
103
+ setNavigationLoading(show) {
104
+ var _a;
105
+ if (!this.navigationBarElement)
106
+ return;
107
+ const currentPage = this.pageContext.currentPage;
108
+ let isShow;
109
+ if (typeof show === 'boolean') {
110
+ isShow = show;
111
+ this.cache[currentPage] &&
112
+ (this.cache[currentPage].loading = isShow);
113
+ }
114
+ else {
115
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.loading;
116
+ if (typeof cacheValue === 'boolean') {
117
+ isShow = cacheValue;
118
+ }
119
+ else {
120
+ // 默认值为 false
121
+ isShow = false;
122
+ this.cache[currentPage] &&
123
+ (this.cache[currentPage].loading = isShow);
124
+ }
125
+ }
126
+ this.shiftLoadingState(isShow);
127
+ }
128
+ setNavigationBarBackground(backgroundColor) {
129
+ var _a, _b, _c;
130
+ if (!this.navigationBarElement)
131
+ return;
132
+ const currentPage = this.pageContext.currentPage;
133
+ let color;
134
+ if (typeof backgroundColor === 'string') {
135
+ color = backgroundColor;
136
+ this.cache[currentPage] &&
137
+ (this.cache[currentPage].backgroundColor = color);
138
+ }
139
+ else {
140
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.backgroundColor;
141
+ if (typeof cacheValue === 'string') {
142
+ color = cacheValue;
143
+ }
144
+ else {
145
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarBackgroundColor) || '#000000';
146
+ this.cache[currentPage] &&
147
+ (this.cache[currentPage].backgroundColor = color);
148
+ }
149
+ }
150
+ this.navigationBarElement.style.background = color;
151
+ }
152
+ setNavigationBarTextStyle(fontColor) {
153
+ var _a, _b, _c;
154
+ if (!this.navigationBarElement)
155
+ return;
156
+ const currentPage = this.pageContext.currentPage;
157
+ let color;
158
+ if (typeof fontColor === 'string') {
159
+ color = fontColor;
160
+ this.cache[currentPage] &&
161
+ (this.cache[currentPage].fontColor = color);
162
+ }
163
+ else {
164
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.fontColor;
165
+ if (typeof cacheValue === 'string') {
166
+ color = cacheValue;
167
+ }
168
+ else {
169
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarTextStyle) || 'white';
170
+ this.cache[currentPage] &&
171
+ (this.cache[currentPage].fontColor = color);
172
+ }
173
+ }
174
+ this.navigationBarElement.style.color = color;
175
+ }
176
+ setTitle(title) {
177
+ var _a, _b, _c;
178
+ const currentPage = this.pageContext.currentPage;
179
+ let proceedTitle;
180
+ if (typeof title === 'string') {
181
+ proceedTitle = title;
182
+ this.cache[currentPage] &&
183
+ (this.cache[currentPage].title = proceedTitle);
184
+ }
185
+ else {
186
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.title;
187
+ if (typeof cacheValue === 'string') {
188
+ proceedTitle = cacheValue;
189
+ }
190
+ else {
191
+ proceedTitle = (_c = (_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationBarTitleText) !== null && _c !== void 0 ? _c : document.title;
192
+ this.cache[currentPage] &&
193
+ (this.cache[currentPage].title = proceedTitle);
194
+ }
195
+ }
196
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
197
+ if (!this.isLoadDdEntry) {
198
+ this.isLoadDdEntry = true;
199
+ require('dingtalk-jsapi/platform');
200
+ }
201
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
202
+ setDingTitle({ proceedTitle });
203
+ }
204
+ document.title = proceedTitle;
205
+ if (!this.titleElement)
206
+ return;
207
+ this.titleElement.innerHTML = proceedTitle;
208
+ }
209
+ fnBtnToggleToHome() {
210
+ if (!this.navigationBarElement)
211
+ return;
212
+ this.navigationBarElement.classList.add('taro-navigation-bar-home-icon');
213
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
214
+ }
215
+ fnBtnToggleToBack() {
216
+ if (!this.navigationBarElement)
217
+ return;
218
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
219
+ this.navigationBarElement.classList.add('taro-navigation-bar-back-icon');
220
+ }
221
+ fnBtnToggleToNone() {
222
+ if (!this.navigationBarElement)
223
+ return;
224
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
225
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
226
+ }
227
+ setNavigationBarVisible(show) {
228
+ var _a, _b;
229
+ if (!this.navigationBarElement)
230
+ return;
231
+ let shouldShow;
232
+ if (typeof show === 'boolean') {
233
+ shouldShow = show;
234
+ }
235
+ else {
236
+ shouldShow = (_a = this.pageContext.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
237
+ if (typeof ((_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
238
+ shouldShow = this.pageContext.pageConfig.navigationStyle;
239
+ }
240
+ }
241
+ if (shouldShow === 'default') {
242
+ this.navigationBarElement.classList.add('taro-navigation-bar-show');
243
+ this.navigationBarElement.classList.remove('taro-navigation-bar-hide');
244
+ }
245
+ else {
246
+ this.navigationBarElement.classList.add('taro-navigation-bar-hide');
247
+ this.navigationBarElement.classList.remove('taro-navigation-bar-show');
248
+ }
249
+ }
250
+ }
251
+
252
+ export { NavigationBarHandler as default };
@@ -1,15 +1,17 @@
1
+ import NavigationBarHandler from "./navigation-bar.js";
1
2
  import { PageInstance } from '@tarojs/runtime';
2
3
  import { PageConfig, RouterAnimate } from '@tarojs/taro';
3
4
  import { History } from "../history.js";
4
5
  import { Route, SpaRouterConfig } from '../../types/router';
5
6
  declare class PageHandler {
6
7
  history: History;
7
- protected config: SpaRouterConfig;
8
+ config: SpaRouterConfig;
8
9
  protected readonly defaultAnimation: RouterAnimate;
9
10
  protected unloadTimer: ReturnType<typeof setTimeout> | null;
10
11
  protected hideTimer: ReturnType<typeof setTimeout> | null;
11
12
  protected lastHidePage: HTMLElement | null;
12
13
  protected lastUnloadPage: PageInstance | null;
14
+ protected navigationBarHandler: NavigationBarHandler;
13
15
  homePage: string;
14
16
  constructor(config: SpaRouterConfig, history: History);
15
17
  get currentPage(): string;
@@ -5,6 +5,7 @@ import { bindPageScroll } from '../events/scroll.js';
5
5
  import { setHistory, history } from '../history.js';
6
6
  import { loadAnimateStyle, loadRouterStyle } from '../style.js';
7
7
  import { routesAlias } from '../utils/index.js';
8
+ import NavigationBarHandler from './navigation-bar.js';
8
9
  import stacks from './stack.js';
9
10
 
10
11
  /* eslint-disable dot-notation */
@@ -15,6 +16,7 @@ class PageHandler {
15
16
  this.config = config;
16
17
  this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
17
18
  this.mount();
19
+ this.navigationBarHandler = new NavigationBarHandler(this);
18
20
  }
19
21
  get currentPage() {
20
22
  const routePath = getCurrentPage(this.routerMode, this.basename);
@@ -158,6 +160,7 @@ class PageHandler {
158
160
  this.isDefaultNavigationStyle() && pageEl.classList.add('taro_navigation_page');
159
161
  this.addAnimation(pageEl, pageNo === 0);
160
162
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
163
+ this.navigationBarHandler.load();
161
164
  this.bindPageEvents(page, pageConfig);
162
165
  this.triggerRouterChange();
163
166
  }
@@ -170,6 +173,7 @@ class PageHandler {
170
173
  this.isDefaultNavigationStyle() && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page'));
171
174
  this.addAnimation(pageEl, pageNo === 0);
172
175
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
176
+ this.navigationBarHandler.load();
173
177
  this.onReady(page, true);
174
178
  this.bindPageEvents(page, pageConfig);
175
179
  this.triggerRouterChange();
@@ -224,6 +228,7 @@ class PageHandler {
224
228
  pageEl.classList.remove('taro_page_shade');
225
229
  this.addAnimation(pageEl, pageNo === 0);
226
230
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
231
+ this.navigationBarHandler.load();
227
232
  this.bindPageEvents(page, pageConfig);
228
233
  this.triggerRouterChange();
229
234
  }
@@ -233,6 +238,7 @@ class PageHandler {
233
238
  pageEl = this.getPageContainer(page);
234
239
  this.addAnimation(pageEl, pageNo === 0);
235
240
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
241
+ this.navigationBarHandler.load();
236
242
  this.onReady(page, false);
237
243
  this.bindPageEvents(page, pageConfig);
238
244
  this.triggerRouterChange();
@@ -1,10 +1,9 @@
1
1
  import { __awaiter } from 'tslib';
2
- import { incrementId, addLeadingSlash, eventCenter, stripBasename, Current, createPageConfig, hooks, stringify } from '@tarojs/runtime';
2
+ import { incrementId, addLeadingSlash, eventCenter, stripBasename, Current, safeExecute, createPageConfig, hooks, stringify } from '@tarojs/runtime';
3
3
  import { Action } from 'history';
4
4
  import UniversalRouter from 'universal-router';
5
5
  import { prependBasename } from '../history.js';
6
6
  import { routesAlias } from '../utils/index.js';
7
- import { setTitle } from '../utils/navigate.js';
8
7
  import { RouterConfig } from './index.js';
9
8
  import PageHandler from './page.js';
10
9
  import stacks from './stack.js';
@@ -40,7 +39,7 @@ function createRouter(history, app, config, framework) {
40
39
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
41
40
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
42
41
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
43
- var _c, _d, _e, _f, _g, _h, _j, _k, _l;
42
+ var _c, _d, _e, _f, _g, _h, _j, _k;
44
43
  handler.pathname = decodeURI(location.pathname);
45
44
  if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
46
45
  window.scrollTo(0, 0);
@@ -80,7 +79,6 @@ function createRouter(history, app, config, framework) {
80
79
  let navigationBarTextStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationBarTextStyle) || 'white';
81
80
  let navigationBarBackgroundColor = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarBackgroundColor) || '#000000';
82
81
  if (pageConfig) {
83
- setTitle((_j = pageConfig.navigationBarTitleText) !== null && _j !== void 0 ? _j : document.title);
84
82
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
85
83
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
86
84
  }
@@ -97,7 +95,7 @@ function createRouter(history, app, config, framework) {
97
95
  eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
98
96
  const currentPage = Current.page;
99
97
  const pathname = handler.pathname;
100
- const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
98
+ const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
101
99
  const cacheTabs = stacks.getTabs();
102
100
  let shouldLoad = false;
103
101
  stacks.method = '';
@@ -163,7 +161,7 @@ function createRouter(history, app, config, framework) {
163
161
  shouldLoad = true;
164
162
  }
165
163
  if (shouldLoad || stacks.length < 1) {
166
- const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
164
+ const el = (_k = element.default) !== null && _k !== void 0 ? _k : element;
167
165
  const loadConfig = Object.assign({}, pageConfig);
168
166
  const stacksIndex = stacks.length;
169
167
  delete loadConfig['path'];
@@ -188,6 +186,26 @@ function createRouter(history, app, config, framework) {
188
186
  }
189
187
  render({ location: history.location, action: Action.Push });
190
188
  (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
189
+ window.addEventListener('visibilitychange', () => {
190
+ var _a, _b, _c, _d, _e, _f, _g;
191
+ const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
192
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
193
+ const param = {};
194
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
195
+ Object.assign(param, launchParam, { path });
196
+ if (document.visibilityState === 'visible') {
197
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
198
+ // 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
199
+ (_d = (_c = Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
200
+ }
201
+ else {
202
+ // 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
203
+ if ((_e = Current.page) === null || _e === void 0 ? void 0 : _e.path) {
204
+ safeExecute((_f = Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
205
+ }
206
+ (_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
207
+ }
208
+ });
191
209
  return history.listen(render);
192
210
  }
193
211
 
package/dist/style.d.ts CHANGED
@@ -6,5 +6,9 @@ declare function loadAnimateStyle(ms?: number): void;
6
6
  * 插入路由相关样式
7
7
  */
8
8
  declare function loadRouterStyle(enableTabBar: boolean, enableWindowScroll: boolean): void;
9
+ /**
10
+ * 插入导航栏相关的样式
11
+ */
12
+ declare function loadNavigationBarStyle(): void;
9
13
  declare function addStyle(css: any): void;
10
- export { loadAnimateStyle, loadRouterStyle, addStyle };
14
+ export { loadAnimateStyle, loadRouterStyle, loadNavigationBarStyle, addStyle };