elmer-ui-core
Version:
web app framework
69 lines (68 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withRouter = void 0;
var RouterService_1 = require("./RouterService");
var withRouter = function (childComponent, Fn) {
if (Object.prototype.toString.call(childComponent) === "[object Function]") {
childComponent.prototype.redirect = function (path, params) {
var now = new Date();
var toParam = params || {};
toParam.time = now.getTime();
var obj = Fn(RouterService_1.RouterService);
var toRouter = obj.checkRoutersVisible(path, null, toParam);
var ApiData = obj.getRouterRequests(toRouter);
var toPath = path;
if (obj.hashRouter) {
if (!(/^\#\/.*/.test(path))) {
toPath = "#/" + path.replace(/^\//, "");
}
}
if (Object.keys(ApiData).length > 0) {
toParam[RouterService_1.ROUTER_SKIP_API_ACTION] = true;
obj.setState({
isAjaxLoading: true,
isNeedAjax: true,
loadingPercent: "0%"
});
obj.ajaxAll(ApiData, {
option: {
obj: obj
},
onCompleted: function (option) {
option.obj.setState({
isAjaxLoading: false
});
history.pushState(toParam, null, toPath);
},
onDownloadProgress: function (event, option) {
option.obj.setState({
loadingPercent: parseInt(((event.loaded / event.total) * 100).toString(), 10) + "%"
});
}
}).then(function (option) {
var _a;
(_a = option.obj) === null || _a === void 0 ? void 0 : _a.setState({
isAjaxLoading: false
});
}).catch(function (option) {
var _a;
(_a = option.obj) === null || _a === void 0 ? void 0 : _a.setState({
isAjaxLoading: false
});
});
}
else {
history.pushState(toParam, null, toPath);
}
obj = null;
ApiData = null;
toRouter = null;
};
childComponent.prototype.hashRouter = Fn(RouterService_1.RouterService).hashRouter;
return childComponent;
}
else {
throw new Error("withRouter只能用于Component");
}
};
exports.withRouter = withRouter;