elmer-ui-core
Version:
web app framework
244 lines (243 loc) • 10.6 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
var elmer_redux_1 = require("elmer-redux");
var Component_1 = require("../../core/Component");
var globalUtil_1 = require("../../init/globalUtil");
var injectable_1 = require("../../inject/injectable");
var propsValidation_1 = require("../../propsValidation");
require("./Route");
var RouterService_1 = require("./RouterService");
var Router = (function (_super) {
__extends(Router, _super);
function Router(props, context) {
var _this = _super.call(this, props) || this;
_this.currentPage = {};
_this.firstCheck = false;
_this.state = {
isAjaxLoading: false,
isNeedAjax: false,
loadingPercent: "0%",
url: ""
};
var children = props.children || [];
var routes = [];
children.map(function (tmpChild) {
if (tmpChild.tagName === "eui-route") {
var tmpRoute = {
component: tmpChild.props["component"],
path: tmpChild.props["path"],
props: tmpChild.props.props || {}
};
routes.push(tmpRoute);
}
});
_this.sourceRouters = routes;
_this.firstCheck = true;
_this.context = context;
if (!globalUtil_1.getGlobalState("euiRouter")) {
globalUtil_1.defineGlobalState("euiRouter", {
hashRouter: props.hashRouter,
title: "euiRouter",
toParams: null,
version: "1.0.0"
}, true);
}
_this.state.url = props.url;
return _this;
}
Router.prototype.$willMount = function () {
this.model.data.dispose();
};
Router.prototype.$onPropsChanged = function (props) {
if (props.url !== this.state.url) {
this.onRouterChanged(props.url || location.href || "", this.state.url, props.param);
}
};
Router.prototype.$inject = function () {
var _this = this;
var _a, _b, _c, _d;
console.log("Router对象model注入成功,初始化Route参数");
var linkUrl = undefined !== this.state.url && null !== this.state.url ? this.state.url : (location.href || "");
this.model.data.C404 = this.props.C404;
this.model.data.initConfig(this.sourceRouters, this.props.hashRouter);
this.model.data.setBindRouteComponent(this);
this.model.data.refreshUrl(linkUrl);
this.model.data.dispatch = function (state) {
return _this.redux.dispatch(state);
};
var updateRouter = this.model.data.checkRoutersVisible(linkUrl, null, null);
if (!this.isEqual(updateRouter, this.router)) {
this.router = updateRouter;
if (undefined === ((_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.renderStore) === null || _b === void 0 ? void 0 : _b.options) || !((_d = (_c = this.context.renderStore) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.isRSV)) {
var ApiData = this.model.data.getRouterRequests(updateRouter);
if (!globalUtil_1.getGlobalState("RenderInServer") && Object.keys(ApiData).length > 0) {
this.state.isAjaxLoading = true;
this.state.isNeedAjax = true;
this.state.loadingPercent = "0%";
this.model.data.ajaxAll(ApiData, {
onCompleted: function () {
_this.setState({
isAjaxLoading: false
});
},
onDownloadProgress: function (event) {
_this.setState({
loadingPercent: parseInt(((event.loaded / event.total) * 100).toString(), 10) + "%"
});
}
}).then(function () {
_this.setState({
isAjaxLoading: false
}, true);
}).catch(function () {
_this.setState({
isAjaxLoading: false
}, true);
});
}
else {
this.state.isAjaxLoading = false;
this.state.isNeedAjax = false;
}
}
}
};
Router.prototype.onRouterChanged = function (newUrl, oldUrl, param) {
var _this = this;
var updateRouter = this.model.data.checkRoutersVisible(newUrl, oldUrl, param);
if (!this.isEqual(updateRouter, this.router)) {
var ApiData = this.model.data.getRouterRequests(updateRouter);
var skipResult = param ? param[RouterService_1.ROUTER_SKIP_API_ACTION] : null;
this.router = updateRouter;
if (Object.keys(ApiData).length > 0 && (undefined === skipResult || null === skipResult || (null !== skipResult && undefined !== skipResult && !skipResult))) {
this.setState({
isAjaxLoading: true,
isNeedAjax: true,
loadingPercent: "0%"
});
this.model.data.ajaxAll(ApiData, {
onCompleted: function () {
_this.setState({
isAjaxLoading: false
}, true);
},
onDownloadProgress: function (event) {
_this.setState({
loadingPercent: parseInt(((event.loaded / event.total) * 100).toString(), 10) + "%"
}, true);
}
}).then(function () {
_this.setState({
isAjaxLoading: false
}, true);
}).catch(function (option) {
});
}
else {
this.setState({
isAjaxLoading: false,
isNeedAjax: false
}, true);
}
ApiData = null;
skipResult = null;
}
};
Router.prototype.render = function () {
var initRouterCode = "";
if (this.router) {
initRouterCode = "<" + this.router.component + " ex:props=\"{{router.props}}\" />";
}
return initRouterCode +
'<div em:if="state.isNeedAjax && state.isAjaxLoading" class="eui-router-progress"><div style="width:{{state.loadingPercent}};"></div></div>';
};
Router.propType = {
C404: {
defaultValue: "eui-404",
description: "修改404默认页面",
rule: propsValidation_1.PropTypes.string
},
hashRouter: {
defaultValue: true,
description: "开启hash路由",
rule: propsValidation_1.PropTypes.bool
},
param: {
description: "定义传递的路由参数",
rule: propsValidation_1.PropTypes.any
},
url: {
description: "当前路由解析地址",
rule: propsValidation_1.PropTypes.string
}
};
Router.contextType = {
renderStore: {
description: "渲染入口传递参数,ElmerUI.render",
rule: propsValidation_1.PropTypes.object.isRequired
}
};
__decorate([
injectable_1.autowired(elmer_redux_1.ReduxController),
__metadata("design:type", elmer_redux_1.ReduxController)
], Router.prototype, "redux", void 0);
Router = __decorate([
injectable_1.declareComponent({
selector: "router",
model: {
data: RouterService_1.RouterService
},
connect: {
mapDispatchToProps: function (dispatch) {
return {
updateApiData: function (actionType, data) {
dispatch({
data: data,
type: actionType
});
}
};
}
}
}),
__metadata("design:paramtypes", [Object, Object])
], Router);
return Router;
}(Component_1.Component));
exports.Router = Router;
__exportStar(require("./withRoter"), exports);