1 | import { __awaiter } from 'tslib';
|
2 | import { addLeadingSlash } from '@tarojs/runtime';
|
3 | import { EventChannel } from '@tarojs/shared';
|
4 | import { parsePath } from 'history';
|
5 | import { history, prependBasename } from './history.js';
|
6 | import { RouterConfig } from './router/index.js';
|
7 | import stacks from './router/stack.js';
|
8 | import { routesAlias } from './utils/index.js';
|
9 |
|
10 | const routeEvtChannel = EventChannel.routeChannel;
|
11 | function processNavigateUrl(option) {
|
12 | var _a;
|
13 | const pathPieces = parsePath(option.url);
|
14 |
|
15 | if ((_a = pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
|
16 | const parts = routesAlias.getOrigin(history.location.pathname).split('/');
|
17 | parts.pop();
|
18 | pathPieces.pathname.split('/').forEach((item) => {
|
19 | if (item === '.')
|
20 | return;
|
21 | item === '..' ? parts.pop() : parts.push(item);
|
22 | });
|
23 | pathPieces.pathname = parts.join('/');
|
24 | }
|
25 |
|
26 | pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
|
27 |
|
28 | pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
29 |
|
30 | pathPieces.pathname = prependBasename(pathPieces.pathname);
|
31 |
|
32 | if (!pathPieces.search)
|
33 | pathPieces.search = '';
|
34 | return pathPieces;
|
35 | }
|
36 | function navigate(option, method) {
|
37 | return __awaiter(this, void 0, void 0, function* () {
|
38 | return new Promise((resolve, reject) => {
|
39 | stacks.method = method;
|
40 | const { success, complete, fail } = option;
|
41 | const unListen = history.listen(() => {
|
42 | const res = { errMsg: `${method}:ok` };
|
43 | if (method === 'navigateTo') {
|
44 | res.eventChannel = routeEvtChannel;
|
45 | routeEvtChannel.addEvents(option.events);
|
46 | }
|
47 | success === null || success === void 0 ? void 0 : success(res);
|
48 | complete === null || complete === void 0 ? void 0 : complete(res);
|
49 | resolve(res);
|
50 | unListen();
|
51 | });
|
52 | try {
|
53 | if ('url' in option) {
|
54 | const pathPieces = processNavigateUrl(option);
|
55 | const state = { timestamp: Date.now() };
|
56 | if (method === 'navigateTo') {
|
57 |
|
58 | history.push(pathPieces, state);
|
59 | }
|
60 | else if (method === 'redirectTo' || method === 'switchTab') {
|
61 | history.replace(pathPieces, state);
|
62 | }
|
63 | else if (method === 'reLaunch') {
|
64 | stacks.delta = stacks.length;
|
65 | history.replace(pathPieces, state);
|
66 | }
|
67 | }
|
68 | else if (method === 'navigateBack') {
|
69 | stacks.delta = option.delta;
|
70 | if (stacks.length > option.delta) {
|
71 | history.go(-option.delta);
|
72 | }
|
73 | else {
|
74 | history.go(1 - stacks.length);
|
75 | }
|
76 | }
|
77 | }
|
78 | catch (error) {
|
79 | const res = { errMsg: `${method}:fail ${error.message || error}` };
|
80 | fail === null || fail === void 0 ? void 0 : fail(res);
|
81 | complete === null || complete === void 0 ? void 0 : complete(res);
|
82 | if (fail || complete) {
|
83 | return resolve(res);
|
84 | }
|
85 | else {
|
86 | return reject(res);
|
87 | }
|
88 | }
|
89 | });
|
90 | });
|
91 | }
|
92 | function navigateTo(option) {
|
93 | return navigate(option, 'navigateTo');
|
94 | }
|
95 | function redirectTo(option) {
|
96 | return navigate(option, 'redirectTo');
|
97 | }
|
98 | function navigateBack(option = { delta: 1 }) {
|
99 | if (!option.delta || option.delta < 1) {
|
100 | option.delta = 1;
|
101 | }
|
102 | return navigate(option, 'navigateBack');
|
103 | }
|
104 | function switchTab(option) {
|
105 | return navigate(option, 'switchTab');
|
106 | }
|
107 | function reLaunch(option) {
|
108 | return navigate(option, 'reLaunch');
|
109 | }
|
110 | function getCurrentPages() {
|
111 | if (process.env.NODE_ENV !== 'production' && RouterConfig.mode === 'multi') {
|
112 | console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
|
113 | }
|
114 | const pages = stacks.get();
|
115 | return pages.map(e => { var _a; return (Object.assign(Object.assign({}, e), { route: ((_a = e.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) || '' })); });
|
116 | }
|
117 |
|
118 | export { getCurrentPages, navigateBack, navigateTo, reLaunch, redirectTo, switchTab };
|