UNPKG

ng-layout-form

Version:

layout form

218 lines (215 loc) 9.37 kB
var _excluded = ["type"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } import { businessHelpRef, getRegisterComponent, getRegisterComponentProps, helpXtypeRef, LayConfWrap, NG } from 'ng-lib-tsx'; import { Comp_Info } from "../config/baseConfig"; export { LayConfWrap }; var emptyArray = []; /** * 根据配置数据获取对应配置的组件 * @param {object} item 组件配置项 * @returns {object} 组件 */ export function getComp(item) { if (!item.xtype) return { instance: Comp_Info['NGInput'].instance }; if (typeof item.xtype === 'function') return { instance: item.xtype }; if (item.xtype && _typeof(item.xtype) === 'object' && item.xtype.render && item.xtype.$$typeof) return { instance: item.xtype }; return function (_type) { var type = helpXtypeRef[_type] || _type; if (Comp_Info[type]) { if (['Input', 'NGInput'].includes(type) && ['prc', 'amt', 'amount', 'qty', 'rate', 'percent'].includes(item.type) && Comp_Info['NGInputNumber']) { return { instance: Comp_Info['NGInputNumber'].instance, props: Comp_Info['NGInputNumber'].defaultProps }; } return { instance: Comp_Info[type].instance, props: Comp_Info[type].defaultProps }; } var ins = getRegisterComponent(type, _type); if (ins) { return { instance: ins, props: getRegisterComponentProps(type, _type) }; } else if (businessHelpRef[type]) { var _businessHelpRef$type = businessHelpRef[type], helpType = _businessHelpRef$type.type, others = _objectWithoutProperties(_businessHelpRef$type, _excluded); return { instance: getRegisterComponent(helpType), props: _objectSpread(_objectSpread({}, others), getRegisterComponentProps(helpType)) }; } return { instance: Comp_Info['NGInput'].instance }; }(item.xtype); } /** * 获取组件对应的配置 * @returns {object} 组件的配置props * @param item * @param config * @param commonPropsType */ export function getCompPorps(item, config, commonPropsType) { var p = config; var t = Object.keys(p).reduce(function (acc, curr) { acc[curr] = item[curr] ? item[curr] : p[curr]['defaultValue']; return acc; }, item.antProps || {}); p['data'] && (t['data'] = (item === null || item === void 0 ? void 0 : item.data) || emptyArray); (item.xtype === 'ngCheckbox' || item.xtype === 'Checkbox') && delete t.value; return t; } /** * 格式化数据 * @param name * @param type * @param opt * @param key * @param initVals */ export function fomatValues(name, type, opt, key, initVals) { var vals = initVals; return merge; function merge(values) { if (_typeof(vals) !== _typeof(values)) { vals = values || vals; } else if (Array.isArray(vals)) { vals = vals.concat(values); } else if (_typeof(vals) === 'object') { vals = _objectSpread(_objectSpread({}, vals), values); } else { vals = values; } if (!values) return _defineProperty({}, name, _defineProperty({}, type, vals ? _defineProperty({ key: key }, opt, vals) : { key: key })); return merge; } } export function formFomatValues(key, initVals) { var vals = {}; var optType = 'newRow'; if (Object.prototype.toString.call(initVals) === '[object Object]') { initVals = [initVals]; } if (Array.isArray(initVals)) { vals = initVals.reduce(function (p, n) { return _objectSpread(_objectSpread({}, p), n); }, {}); return _defineProperty({}, 'form', _defineProperty({ key: key }, vals[key] ? 'modifiedRow' : 'newRow', vals)); } return merge; function merge(values) { if (_typeof(vals) !== _typeof(values)) { vals = values || vals; } else if (Array.isArray(vals)) { vals = vals.concat(values); } else if (_typeof(vals) === 'object') { vals = _objectSpread(_objectSpread({}, vals), values); } else { vals = values; } if (!values) { return _defineProperty({}, 'form', vals ? _defineProperty({ key: key }, optType, vals) : { key: key }); } if (values[key]) optType = 'modifiedRow'; return merge; } } export function listToObj(c) { if (!c) return {}; return c.reduce(function (acc, item) { acc[item.name] = item; return acc; }, {}); } export function getProperty(o, keys) { if (typeof keys === 'string' || typeof keys === 'number') { return o && o[keys]; } for (var i = 0; i < keys.length; i++) { if (!o) return; o = o[keys[i]]; } return o; } export function parseFunction(str) { if (str && typeof str === 'string') { return function () { try { var fn = new Function('return ' + str)(); return fn.apply(void 0, arguments); } catch (error) { console.log('erro:======', error, str); } }; } return str; } export function getObjValue(obj, pathKeys) { return NG.getObjValue(obj, pathKeys); } export function setObjValue(obj, pathKeys, value) { NG.setObjValue(obj, pathKeys, value); } export function getSubscribeFn(outRef) { return function (fn, type, name) { if (!outRef.current._compIns) { return function () {}; } if (name && name.length > 0) { var _outRef$current$inner, _outRef$current; var arr = NG.isArray(name) ? name : [name]; var loop = function loop(obj) { var prevKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; return Object.keys(obj).some(function (k) { if (NG.isObject(obj[k]) && loop(obj[k], k)) { return true; } return arr.includes(prevKey ? "".concat(prevKey, ".").concat(k) : k); }); }; return (_outRef$current$inner = (_outRef$current = outRef.current).innerSubscribe) === null || _outRef$current$inner === void 0 ? void 0 : _outRef$current$inner.call(_outRef$current, function (p) { fn(_objectSpread(_objectSpread({}, p), {}, { name: name })); }, type, function (_ref6) { var args = _ref6.args; return loop(args[0]); }); } else { var _outRef$current$inner2, _outRef$current2; return (_outRef$current$inner2 = (_outRef$current2 = outRef.current).innerSubscribe) === null || _outRef$current$inner2 === void 0 ? void 0 : _outRef$current$inner2.call(_outRef$current2, fn, type); } }; } export function isHelpField(type) { var _getRegisterComponent; return NG.isString(type) && ((_getRegisterComponent = getRegisterComponent(type)) === null || _getRegisterComponent === void 0 ? void 0 : _getRegisterComponent.isHelp); }