elmer-ui-core
Version:
web app framework
157 lines (156 loc) • 8.54 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 __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 __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.InjectComponent = void 0;
var elmer_common_1 = require("elmer-common");
var elmer_redux_1 = require("elmer-redux");
var i18nController_1 = require("../i18n/i18nController");
var globalUtil_1 = require("../init/globalUtil");
var injectable_1 = require("../inject/injectable");
var InjectComponent = (function (_super) {
__extends(InjectComponent, _super);
function InjectComponent() {
var _this = _super.call(this) || this;
_this.reduxController.checkInitStateData(globalUtil_1.getGlobalState, globalUtil_1.defineGlobalState);
_this.reduxController.setNotifyCallback(["$onPropsChanged", "$willReceiveProps"]);
return _this;
}
InjectComponent.prototype.run = function (targetComponent, ComponentClass, nodeData) {
this.checkPropTypes(targetComponent, ComponentClass);
};
InjectComponent.prototype.initComponent = function (targetComponent, ComponentClass, nodeData) {
this.i18nController.initI18nTranslate(targetComponent);
var reduxParam = ComponentClass.prototype.connect;
if (reduxParam && (reduxParam.mapStateToProps || reduxParam.mapDispatchToProps)) {
this.reduxController.checkInitComponents(targetComponent, ComponentClass.prototype.selector, nodeData);
}
};
InjectComponent.prototype.releaseComponent = function (targetComponent, nodeData) {
if (targetComponent) {
this.reduxController.removeComponent(targetComponent.selector, nodeData);
}
};
InjectComponent.prototype.beforeUpdateComponent = function (targetComponent, ComponentClass, props, nodeData) {
var reduxProps = this.reduxController.getStateByConnectSelector(ComponentClass.prototype.selector);
var dispatchValue = this.reduxController.getDispatchByConnectSelector(ComponentClass.prototype.selector);
this.isObject(reduxProps) && this.extend(props, reduxProps, true);
this.isObject(dispatchValue) && this.extend(props, dispatchValue, true);
this.setDefaultValue(props, ComponentClass.propType);
};
InjectComponent.prototype.beforeInitComponent = function (ComponentClass, props, nodeData) {
var reduxParam = ComponentClass.prototype.connect;
if (reduxParam && (reduxParam.mapStateToProps || reduxParam.mapDispatchToProps)) {
if (this.isEmpty(ComponentClass.prototype.selector)) {
this.defineReadOnlyProperty(ComponentClass.prototype, "selector", this.guid().replace(/\-/g, ""));
}
this.reduxController.connect(ComponentClass.prototype.selector, reduxParam.mapStateToProps, reduxParam.mapDispatchToProps);
var stateValue = this.reduxController.getStateByConnectSelector(ComponentClass.prototype.selector);
var dispatchValue = this.reduxController.getDispatchByConnectSelector(ComponentClass.prototype.selector);
stateValue && this.extend(props, stateValue, true);
dispatchValue && this.extend(props, dispatchValue, true);
}
this.setDefaultValue(props, ComponentClass.propType);
};
InjectComponent.prototype.setDefaultValue = function (props, checkRules) {
var _this = this;
if (this.isObject(props) && this.isObject(checkRules)) {
Object.keys(checkRules).map(function (propKey) {
if (_this.isObject(checkRules[propKey])) {
var tmpCheckRule = checkRules[propKey];
if (_this.isEmpty(props[propKey])) {
if (tmpCheckRule.defaultValue !== undefined) {
delete props[propKey];
_this.defineReadOnlyProperty(props, propKey, tmpCheckRule.defaultValue);
}
}
if (typeof tmpCheckRule.rule === "function" && tmpCheckRule.rule["type"] === "number") {
if (!isNaN(props[propKey]) && _this.isString(props[propKey])) {
var curValue = /\./.test(props[propKey]) ? parseFloat(props[propKey]) : parseInt(props[propKey], 10);
delete props[propKey];
_this.defineReadOnlyProperty(props, propKey, curValue);
}
}
}
});
}
};
InjectComponent.prototype.checkPropTypes = function (targetComponent, ComponentClass) {
var propTypes = ComponentClass["propType"] || {};
var propKeys = Object.keys(propTypes) || [];
if (propKeys.length > 0) {
this.checkPropTypesCallBack(targetComponent, propTypes);
}
};
InjectComponent.prototype.checkPropTypesCallBack = function (target, checkRules) {
var _this = this;
Object.keys(checkRules).map(function (tmpKey) {
var checkRuleData = checkRules[tmpKey];
if (_this.isFunction(checkRuleData)) {
_this.doCheckPropType(target, tmpKey, checkRuleData);
}
else if (_this.isObject(checkRuleData)) {
var checkData = checkRuleData;
if (_this.isFunction(checkData.rule)) {
_this.doCheckPropType(target, tmpKey, checkData.rule);
}
if (!_this.isEmpty(checkData.propertyKey)) {
target[checkData.propertyKey] = target.props[tmpKey];
}
if (!_this.isEmpty(checkData.stateKey)) {
target.state[checkData.stateKey] = target.props[tmpKey];
}
checkData = null;
}
checkRuleData = null;
});
};
InjectComponent.prototype.doCheckPropType = function (target, propertyKey, checkCallBack) {
var propValue = target.props[propertyKey];
this.isFunction(checkCallBack) && checkCallBack(propValue, {
error: function (msg, type) {
var tagName = target.humpToStr(target["selector"]);
var sMsg = "组件【" + tagName + "】属性【" + propertyKey + "】设置错误:" + msg;
console.error(sMsg, type);
},
propertyName: propertyKey,
propertyValue: propValue
});
};
__decorate([
injectable_1.autowired(i18nController_1.I18nController),
__metadata("design:type", i18nController_1.I18nController)
], InjectComponent.prototype, "i18nController", void 0);
__decorate([
injectable_1.autowired(elmer_redux_1.ReduxController, "ReduxController"),
__metadata("design:type", elmer_redux_1.ReduxController)
], InjectComponent.prototype, "reduxController", void 0);
InjectComponent = __decorate([
injectable_1.Injectable("InjectComponent"),
__metadata("design:paramtypes", [])
], InjectComponent);
return InjectComponent;
}(elmer_common_1.Common));
exports.InjectComponent = InjectComponent;