d-render
Version:
数据渲染库 - form、table、search-form
120 lines (118 loc) • 4.6 kB
JavaScript
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
class EffectExecutor {
constructor(config = {}) {
this.config = config;
this.types = Object.keys(this.config);
}
setExecutor(type, executor) {
this.config[type] = {};
this.config[type].executor = executor;
this.types = Object.keys(this.config);
}
// 处理数组及字符型数据
compatibleEffect(effectConfig, type, param) {
const transformStrEffect = this.config[type].transformStrEffect;
const effect = transformStrEffect ? effectConfig[type] || effectConfig[`${type}Str`] : effectConfig[type];
return [].concat(effect).map((e) => {
let fn = e;
if (typeof e === "string") {
if (typeof transformStrEffect === "function") {
fn = transformStrEffect(e);
} else {
fn = () => {
console.warn(`[transformStrEffect warn]: ${type}\u672A\u914D\u7F6E\u5B57\u7B26\u4E32\u8F6C\u51FD\u6570\u7684\u65B9\u6CD5`);
};
}
}
return [fn, param];
});
}
getGlobalEffect(fieldConfig, type) {
return this.config[type].transformStrEffect ? fieldConfig[type] || fieldConfig[`${type}Str`] : fieldConfig[type];
}
getExecuteEffect(params, fieldConfig, globalParam) {
console.log("params", params);
const analysisEffects = this.types;
const executeObject = analysisEffects.reduce((acc, key) => {
acc[key] = [];
return acc;
}, {});
const globalEffectSign = analysisEffects.reduce((acc, key) => {
acc[key] = false;
return acc;
}, {});
const globalParams = params.filter((param) => {
return param.effect === void 0 || param.effect === true;
});
if (globalParams.length > 0) {
analysisEffects.forEach((key) => {
if (this.getGlobalEffect(fieldConfig, key))
globalEffectSign[key] = true;
});
params = params.filter((param) => {
return !(param.effect === void 0 || param.effect === true);
});
}
params.forEach((param) => {
const _a = param, { effect } = _a, privateParam = __objRest(_a, ["effect"]);
analysisEffects.forEach((key) => {
if (typeof effect[key] === "function") {
if (key !== "resetValue")
globalEffectSign[key] = true;
executeObject[key].push(...this.compatibleEffect(effect, key, privateParam));
} else if (effect[key] === true) {
if (key !== "resetValue") {
globalEffectSign[key] = true;
} else {
executeObject[key].push(...this.compatibleEffect(effect, key, privateParam));
}
}
});
});
analysisEffects.forEach((key) => {
if (globalEffectSign[key] && this.getGlobalEffect(fieldConfig, key)) {
executeObject[key].unshift(...this.compatibleEffect(fieldConfig, key, globalParam));
}
});
console.log("executeObject", executeObject);
return executeObject;
}
analysisEffects(effectParams, config, globalParam) {
console.log("effectParams", effectParams);
this.executeChangeValueEffect = globalParam.executeChangeValueEffect;
this.values = globalParam.values;
this.outValues = globalParam.outValues;
this.effectsConfig = this.getExecuteEffect(effectParams, config, globalParam);
}
execute(type, logKey, ...args) {
const condition = this.config[type].condition;
if (args[0].length > 0) {
if (!condition || condition && condition(...args)) {
this.config[type].executor(this.values, this.outValues, args[0]);
console.log(`%c[d-render]%c\u6267\u884C%c${logKey}.${type}`, "color: #e6a23c", "color: normal", "color: #67c23a");
} else {
console.log(`%c[d-render]%c\u4E0D\u7B26\u5408%c${logKey}.${type}%c\u6267\u884C\u6761\u4EF6`, "color: #e6a23c", "color: normal", "color: red", "color: normal", args[0]);
}
}
}
executeAll(logKey) {
Object.keys(this.effectsConfig).forEach((type) => {
this.execute(type, logKey, this.effectsConfig[type], this.executeChangeValueEffect);
});
}
}
export { EffectExecutor };