84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Navigator = void 0;
|
|
const history_1 = require("history");
|
|
const navigator_common_1 = require("./navigator.common");
|
|
class Navigator extends navigator_common_1.Navigator {
|
|
history;
|
|
constructor() {
|
|
super();
|
|
this.history = (0, history_1.createBrowserHistory)();
|
|
}
|
|
/**
|
|
* 必须使用这个方法注入history才能和react-router兼容
|
|
* @param history
|
|
*/
|
|
setHistory(history) {
|
|
this.history = history;
|
|
}
|
|
getLocation() {
|
|
const { pathname } = this.history.location;
|
|
const url = pathname.replace(this.namespace, '').replace('//', '/');
|
|
return {
|
|
...this.history.location,
|
|
namespace: this.namespace,
|
|
url: url || '/',
|
|
};
|
|
}
|
|
getState() {
|
|
const { pathname, state, search } = this.getLocation();
|
|
const state2 = this.constructState(pathname, state, search);
|
|
return state2;
|
|
}
|
|
getUrlAndProps(options, state, disableNamespace) {
|
|
const { url, ...rest } = options;
|
|
const url2 = this.constructUrl(url, rest, disableNamespace);
|
|
const { pathname } = this.getLocation();
|
|
const state2 = Object.assign({}, state, {
|
|
oakFrom: pathname,
|
|
});
|
|
return {
|
|
url: url2,
|
|
props: state2,
|
|
};
|
|
}
|
|
async navigateTo(options, state, disableNamespace) {
|
|
if (!this.enter()) {
|
|
return;
|
|
}
|
|
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
|
|
this.history.push(url, props);
|
|
this.publish();
|
|
this.leave();
|
|
}
|
|
async redirectTo(options, state, disableNamespace) {
|
|
if (!this.enter()) {
|
|
return;
|
|
}
|
|
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
|
|
this.history.replace(url, props);
|
|
this.publish();
|
|
this.leave();
|
|
}
|
|
async switchTab(options, state, disableNamespace) {
|
|
console.error('浏览器无switchTab');
|
|
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
|
|
this.history.replace(url, props);
|
|
this.publish();
|
|
}
|
|
async navigateBack(delta) {
|
|
if (!this.enter()) {
|
|
return;
|
|
}
|
|
this.history.go(delta ? 0 - delta : -1);
|
|
this.publish();
|
|
this.leave();
|
|
}
|
|
navigateBackOrRedirectTo(options, state, disableNamespace) {
|
|
console.error('浏览器暂无法获得history堆栈');
|
|
this.history.back();
|
|
this.publish();
|
|
}
|
|
}
|
|
exports.Navigator = Navigator;
|