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