wiz-frameworks
Version:
wizlong react framework
108 lines (100 loc) • 2.89 kB
JavaScript
/**
* 模版注册
* @author mll
* @date 2019-10-15
*/
import React from 'react';
import { utils_tool } from '../../tool';
var log = utils_tool.log;
var _templates = {};
/**
* 设置模版
* @param {*} templates
*/
export var setTemplates = function setTemplates(templates) {
if (templates) {
_templates = templates;
}
};
/**
* 获取模版
* @param {*} key
*/
export var getTemplates = function getTemplates(key) {
var t = _templates[key];
return t ? t : null;
};
/**
* 获取全部模版
* @param {*} key
*/
export var findAll = function findAll(key) {
return _templates;
};
/**
* 根据配置获得由模版生成的组件
* 支持array/obj
* @param {*} config
*/
export var getComponentByConfig = function getComponentByConfig(config, _this) {
if (!config) {
log.error('templateUtils: 请传入正确的配置');
return null;
} else {
if (config instanceof Array) {
var r = [];
config.forEach(function (e) {
r.push(buildConfigToComponent(e));
});
return r;
} else {
return buildConfigToComponent(config, _this);
}
}
};
/**
* 解析配置生成组件
* @param {*} config
*
*/
var buildConfigToComponent = function buildConfigToComponent(config, _this) {
if (config) {
var name = config['name'];
var params = config['params'];
if (name && params) {
if (_this && _this['props'] && _this['props']['_datas']) {
params['_datas'] = _this['props']['_datas'];
}
if (_this && _this['props'] && _this['props']['doAction']) {
params['doAction'] = _this['props']['doAction'];
}
if (_this && _this['props']) {
params['loading'] = _this['props']['loading'];
}
if (_this && _this['jumpPage']) {
params['jumpPage'] = _this['jumpPage'];
}
if (_this && _this['accessBtns']) {
params['accessBtns'] = _this['accessBtns'];
}
if (_this && _this['params']) {
params['params'] = _this['params'];
}
var Node = getTemplates(name);
if (Node) {
var children = params['childrenParams'];
if (children) {
var childrenNode = getComponentByConfig(children, _this);
params['children'] = childrenNode;
delete params['childrenParams'];
}
return React.createElement(Node, params);
} else {
log.error('templateUtils: 根据 ' + name + ' 获取组件失败,请在项目启动时配置相应组件');
}
} else {
return config;
}
}
return null;
};