@wufengteam/inputs
Version:
平台提供的右侧属性编辑器,需要在主工程中注册
390 lines • 16.3 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { ARRAY_LIST } from '../constant';
/* eslint-disable no-restricted-syntax */
export var convertToArray = function convertToArray(objectData) {
var _a;
var list = [];
if (objectData && JSON.stringify(objectData) !== '{}') {
(_a = Object.keys(objectData)) === null || _a === void 0 ? void 0 : _a.forEach(function (key) {
if (key !== 'selectNode') {
// 非选中的节点,映射关系
if (key === null || key === void 0 ? void 0 : key.includes('.')) {
var bean = list.find(function (item) {
return item.fieldName === key.split('.')[0];
});
if (bean && (bean === null || bean === void 0 ? void 0 : bean.children)) {
bean.children.push({
fieldName: key,
paramName: objectData[key]
});
} else {
bean.children = [{
fieldName: key,
paramName: objectData[key]
}];
}
} else {
list.push({
fieldName: key,
paramName: objectData[key]
});
}
}
});
}
return list;
};
export var convertToObject = function convertToObject(list) {
var objectData = {};
if (list && list.length) {
list === null || list === void 0 ? void 0 : list.forEach(function (item) {
var _a;
objectData[item.fieldName] = item.paramName;
if (item === null || item === void 0 ? void 0 : item.children) {
(_a = item === null || item === void 0 ? void 0 : item.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
objectData[child.fieldName] = child.paramName;
});
}
});
}
return objectData;
};
/**
* 为外部服务添加key和parentKey
* @param tree
* @param parentKey
* @returns
*/
export var addKeysAndParentKeys = function addKeysAndParentKeys(tree) {
var parentKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '1';
if (Array.isArray(tree)) {
return tree === null || tree === void 0 ? void 0 : tree.map(function (node, index) {
// 为当前节点设置 key 和 parentKey
var key = "".concat(parentKey, "-").concat(index);
var newNode = Object.assign(Object.assign({}, node), {
key: key,
parentKey: parentKey
});
// 如果节点有子节点,则递归调用 addKeysAndParentKeys
if (newNode && (newNode === null || newNode === void 0 ? void 0 : newNode.children) && Array.isArray(newNode === null || newNode === void 0 ? void 0 : newNode.children)) {
newNode.children = addKeysAndParentKeys(newNode.children, key);
}
return newNode;
});
}
var key = "".concat(parentKey, "-", 0);
var newNode = Object.assign(Object.assign({}, tree), {
key: key,
parentKey: parentKey
});
if (newNode.children && Array.isArray(newNode.children)) {
newNode.children = addKeysAndParentKeys(newNode.children, key);
}
return newNode;
};
export var changeChildren = function changeChildren(list) {
list === null || list === void 0 ? void 0 : list.forEach(function (outerObject) {
var _a;
// 兼容外部集成服务,多返回了Objecy层级
(_a = outerObject === null || outerObject === void 0 ? void 0 : outerObject.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
if ((child === null || child === void 0 ? void 0 : child.attrType) === 'object') {
var newChildren = (child === null || child === void 0 ? void 0 : child.children) || []; // 使用可选链操作符防止undefined错误
// 替换最外层的children
outerObject.children = newChildren;
}
});
});
};
// 定义一个函数来遍历树状数组并输出所需属性
export var traverseTree = function traverseTree(nodes) {
return nodes === null || nodes === void 0 ? void 0 : nodes.map(function (node) {
// 只保留 paramName、paramType、paramValue 和 children 属性
var newNode = {
paramName: node.code,
paramType: node.paramType,
paramValue: node.paramValue,
children: node.children && traverseTree(node.children) || [] // 递归处理子节点
};
return newNode;
});
};
/**
* 判断入参是否有设置序列号
* @param tree
* @param vRowId
* @returns
*/
export var hasNodeWithParamValue = function hasNodeWithParamValue(tree, vRowId) {
function search(node) {
if (node.paramValue === vRowId) {
return true;
}
if (node.children && node.children.length > 0) {
var _iterator = _createForOfIteratorHelper(node.children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
if (search(child)) {
return true;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return false;
}
var _iterator2 = _createForOfIteratorHelper(tree),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var rootNode = _step2.value;
if (search(rootNode)) {
return true;
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return false;
};
/**
* 用于设置已选的组件
* @param resultArray
* @param nodes
* @returns
*/
export var getTreeParamValue = function getTreeParamValue(resultArray, nodes) {
var _iterator3 = _createForOfIteratorHelper(nodes),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var node = _step3.value;
// 检查当前节点的paramValue是否为空
if (node.paramValue && ((node === null || node === void 0 ? void 0 : node.paramType) !== 'fixed' || !(node === null || node === void 0 ? void 0 : node.paramType))) {
resultArray.push({
key: node === null || node === void 0 ? void 0 : node.code,
value: node.paramValue
}); // 将节点添加到结果数组中(这里使用了浅拷贝)
}
// 如果节点有children属性,则递归遍历children
if ((node === null || node === void 0 ? void 0 : node.children) && Array.isArray(node === null || node === void 0 ? void 0 : node.children)) {
getTreeParamValue(resultArray, node.children);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return resultArray;
};
// 函数:根据key将b数组的值赋给a数组
export function mergeTrees(treeA, treeB) {
var mapB = treeB === null || treeB === void 0 ? void 0 : treeB.reduce(function (acc, item) {
acc[item === null || item === void 0 ? void 0 : item.key] = item;
return acc;
}, {});
function merge(nodeA) {
var _a;
if (!nodeA) return;
var matchB = mapB[nodeA === null || nodeA === void 0 ? void 0 : nodeA.key];
if (matchB) {
// 这里可以选择性地合并值,或者直接赋值
// 例如,如果只想复制某个特定的属性:
nodeA.paramValue = matchB.paramValue; // 假设我们想要添加或覆盖 'value' 属性
// 如果想要合并所有属性(除了key),则可以使用 Object.assign 或展开运算符
// Object.assign(nodeA, { ...matchB, [key]: undefined }); // 排除key属性
}
if ((nodeA === null || nodeA === void 0 ? void 0 : nodeA.children) && Array.isArray(nodeA === null || nodeA === void 0 ? void 0 : nodeA.children)) {
(_a = nodeA === null || nodeA === void 0 ? void 0 : nodeA.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
return merge(child);
});
}
}
treeA === null || treeA === void 0 ? void 0 : treeA.forEach(function (node) {
return merge(node);
});
}
export function mergeTreesToSetValues(a, b) {
return b === null || b === void 0 ? void 0 : b.map(function (bItem) {
var _a, _b;
// 在数组a中寻找匹配项
var aMatch = a === null || a === void 0 ? void 0 : a.find(function (aItem) {
return aItem.code === bItem.paramName;
});
if (aMatch) {
// 合并属性,使用浅拷贝避免直接修改原对象
var merged = Object.assign(Object.assign({}, aMatch), bItem);
// 如果两边都有children,则递归处理
if ((aMatch === null || aMatch === void 0 ? void 0 : aMatch.children) && (bItem === null || bItem === void 0 ? void 0 : bItem.children)) {
var mergedChildren = mergeTreesToSetValues(aMatch.children, bItem.children);
// 仅当合并后的children为空数组时,删除children属性
if ((mergedChildren === null || mergedChildren === void 0 ? void 0 : mergedChildren.length) > 0) {
merged.children = mergedChildren;
} else {
delete merged.children;
}
} else if ((bItem === null || bItem === void 0 ? void 0 : bItem.children) && ((_a = bItem === null || bItem === void 0 ? void 0 : bItem.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
// 如果只有bItem有children并且不为空,则直接赋值
merged.children = bItem.children;
} else if ((aMatch === null || aMatch === void 0 ? void 0 : aMatch.children) && ((_b = aMatch === null || aMatch === void 0 ? void 0 : aMatch.children) === null || _b === void 0 ? void 0 : _b.length) > 0) {
// 如果只有aMatch有children并且不为空,则直接赋值
merged.children = aMatch.children;
} else {
// 如果两边都没有children或者都为空,则删除这个属性
delete merged.children;
}
return merged;
}
// 如果没有找到匹配项,返回bItem本身
return bItem;
});
}
export function checkTree(tree) {
var names = [];
function traverse(nodeList) {
// eslint-disable-next-line prefer-const
var _iterator4 = _createForOfIteratorHelper(nodeList),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var node = _step4.value;
// 检查当前节点
if ((node.paramValue === '' || node.paramValue === undefined) && node.paramType !== 'association') {
names.push(node.name || (node === null || node === void 0 ? void 0 : node.code));
}
// 如果存在children,则递归处理
if (node.children && node.children.length > 0) {
traverse(node.children);
}
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
}
// 开始遍历
traverse(tree);
// 根据names数组判断返回结果
if (names.length > 0) {
return {
result: false,
names: names
};
}
return {
result: true
};
}
export var checkParamsArray = function checkParamsArray(list) {
var count = list === null || list === void 0 ? void 0 : list.filter(function (item) {
return ARRAY_LIST.includes(item === null || item === void 0 ? void 0 : item.attrType);
});
return (count === null || count === void 0 ? void 0 : count.length) === 1;
};
// 假设每个节点都有一个唯一的 'key' 属性来标识它们
export function isNodeInDataSource(node, dataSource) {
// 遍历 dataSource 来查找是否有节点与 node 相同
return dataSource === null || dataSource === void 0 ? void 0 : dataSource.some(function (dsNode) {
return dsNode.key === node.key;
});
}
function extractKeys(node, key, set) {
var _a;
// 将当前节点的key加入到set中
set.add(node[key]);
// 递归处理子节点
if (node.children) {
(_a = node.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
return extractKeys(child, key, set);
});
}
}
export function getAddParamsData(paramsList, dataSource) {
var key = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'key';
// 创建一个Set用于快速查找dataSource中的key
// 创建一个Set用于快速查找dataSource中的所有层级key
var dataSourceKeys = new Set();
dataSource === null || dataSource === void 0 ? void 0 : dataSource.forEach(function (node) {
return extractKeys(node, key, dataSourceKeys);
});
/**
* 递归过滤函数
* @param {Array} nodes - 当前处理的节点列表
* @returns {Array} 返回过滤后的节点列表
*/
function filterNodes(nodes) {
var filteredNodes = [];
if (nodes && Array.isArray(nodes)) {
var _iterator5 = _createForOfIteratorHelper(nodes),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var node = _step5.value;
// 如果当前节点不在dataSource中,则直接考虑其子节点是否需要保留
if (!dataSourceKeys.has(node[key])) {
// 处理子节点
var children = node.children ? filterNodes(node.children) : [];
// 只有当至少有一个子节点被保留时,或者没有子节点的情况下,才保留当前节点
if (children.length > 0 || !node.children) {
var newNode = Object.assign({}, node);
if (children.length > 0) {
newNode.children = children;
}
filteredNodes.push(newNode);
}
} else if (node.children && node.children.length > 0) {
// 如果当前节点在dataSource中,检查是否有符合条件的子节点
var _children = filterNodes(node.children);
if (_children.length > 0) {
// 即使当前节点不符合条件,如果有符合条件的子节点,也要保留这些子节点信息
filteredNodes.push(Object.assign(Object.assign({}, node), {
children: _children
}));
}
}
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
}
return filteredNodes;
}
// 对根节点执行过滤
return filterNodes(paramsList);
}
export function extractNodeValues(nodes) {
var codeKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'code';
var paramValueKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'paramValue';
if (!nodes) {
return [];
}
var result = [];
function recurse(currentNodes) {
currentNodes === null || currentNodes === void 0 ? void 0 : currentNodes.forEach(function (node) {
// 创建新对象并添加到结果数组中
var transformedNode = {
key: node[codeKey],
value: node[paramValueKey]
};
result.push(transformedNode);
// 如果存在子节点,则递归处理
if (node.children && node.children.length > 0) {
recurse(node.children);
}
});
}
recurse(nodes);
return result;
}