UNPKG

syber-lowcode-business-materials

Version:
276 lines (272 loc) 8.73 kB
import _ from 'lodash'; var TreeDataHandler = /*#__PURE__*/function () { function TreeDataHandler(props) { var _this = this; /** * 搜索树数据 * @param treeData 搜索的数据 * @param filter 过滤条件 * @param once 是否搜索到一个,就直接终止并返回结果 * @param childField 子集键名 * @return {Array} */ this.searchTreeNodes = function (treeData, filter, once, childField) { if (childField === void 0) { childField = _this.childField; } var arr = []; if (treeData) { var _find = function find(treeNode) { if (treeNode) { if (filter(treeNode)) { arr.push(treeNode); } if (once && arr.length > 0) { return true; } if (treeNode[childField]) { for (var i = 0, l = treeNode[childField].length; i < l; i++) { if (_find(treeNode[childField][i])) { return true; } } } return false; } else { return false; } }; if (Array.isArray(treeData)) { for (var i = 0, l = treeData.length; i < l; i++) { _find(treeData[i]); if (once && arr.length > 0) { break; } } } else { _find(treeData); } return arr; } else { return arr; } }; /** * 通过特殊字段搜索目标节点 * @param treeData 搜索的数据 * @param matchRule 匹配规则,eg:{field:"key", value:"123456789"} * @param childField 子集键名 * @return {*} */ this.searchTreeNodeByField = function (treeData, matchRule, childField) { if (childField === void 0) { childField = _this.childField; } var result = _this.searchTreeNodes(treeData, function (action) { return action[matchRule.field] === matchRule.value; }, true, childField); if (result && result.length > 0) { return result[0]; } else { return null; } }; /** * 获取从根到目标节点经过的所有节点 * @param treeData 搜索的数据 * @param filter * @param childField 子集键名 * @return {*} */ this.getNodeDeepPath = function (treeData, filter, childField) { if (childField === void 0) { childField = _this.childField; } console.log('childField', childField); console.log('this.childField', _this.childField); var pathArr = []; if (treeData) { var _find2 = function find(treeNode) { if (treeNode) { pathArr.push(treeNode); if (filter(treeNode)) { return true; } else if (treeNode[childField]) { for (var i = 0, l = treeNode[childField].length; i < l; i++) { if (_find2(treeNode[childField][i])) { return true; } } } //没找到,把最后一个剔除 pathArr.pop(); return false; } else { return false; } }; if (Array.isArray(treeData)) { for (var i = 0, l = treeData.length; i < l; i++) { if (_find2(treeData[i])) { break; } } } else { _find2(treeData); } return pathArr; } else { return pathArr; } }; this.getFull = function (treeData, _id, childField, matchField) { if (childField === void 0) { childField = _this.childField; } if (matchField === void 0) { matchField = '_id'; } var path = _this.getNodeDeepPath(treeData, function (o) { return o[matchField] === _id; }, childField); var fullTitle = ''; var fullPath = []; if (path.length >= 0) { path.forEach(function (item) { fullTitle += item.title + '/'; if (item.type === 'key' || item.type === 'prop') { fullPath.push({ _id: item._id, isArray: item.isArray, title: item.title, dataType: item.dataType, mdId: item.mdId }); } }); //去头去尾 fullTitle = fullTitle.slice(fullTitle.indexOf('/') + 1); fullTitle = fullTitle.slice(0, fullTitle.length - 1); } return { fullPath: fullPath, fullTitle: fullTitle }; }; //树结构里递归子集的键名 this.childField = props && props.childField || 'children'; } /** * 根据传入的字段对数据排序 * @param treeData 需要排序的数据,可以是数组,也可以是对象 * @param sortField 排序字段,如果是多个,传入数组,eg:["time","number"] * @param sortMode 升序或降序 asc:升序 desc:降序 * 如果sortField是数组,则sortMode也可以是数组,一一对应,eg:["asc","desc"] * @param childField 子集键名 //如果不想排序子集,可以填一个不存在的键,比如:noChild */ var _proto = TreeDataHandler.prototype; _proto.sortByField = function sortByField(treeData, sortField, sortMode, childField) { if (sortMode === void 0) { sortMode = 'desc'; } if (childField === void 0) { childField = this.childField; } if (!sortField) { return treeData; } var _sortAction = function sortAction(data) { if (Array.isArray(data)) { //当前层排序 data = _.orderBy(data, sortField, sortMode); return data.map(function (item) { if (item[childField]) { item[childField] = _sortAction(item[childField]); } return item; }); } else { if (data[childField]) { data[childField] = _sortAction(data[childField]); } return data; } }; return _sortAction(treeData); } /** * 替换字段,返回新的数据 * @param treeData 用于匹配的原数据 * @param matchRule 新数据的键和值来源规则 eg:{key:"_id",title:"name",children:"scriptModules"} _id 替换成 key * @param changeType 修改类型 replace:替换键 new:新的数据只保留规则定义的键 add:在原数据上增加规则定义的键 * @param childField 子集键名 * */; _proto.changeField = function changeField(treeData, matchRule, changeType, childField) { if (changeType === void 0) { changeType = 'replace'; } if (childField === void 0) { childField = this.childField; } // console.log('changeField', treeData); var newTreeData = JSON.parse(JSON.stringify(treeData)); if (!matchRule) { return newTreeData; } //每一个节点 var handleAction = function handleAction(data) { var newItem = changeType === 'new' ? {} : data; for (var key in matchRule) { if (data.hasOwnProperty(matchRule[key])) { newItem[key] = data[matchRule[key]]; if (changeType === 'replace') { delete newItem[matchRule[key]]; } } } return newItem; }; var _changeAction = function changeAction(data) { if (Array.isArray(data)) { return data.map(function (item) { //先替换子节点,否则 childField 替换不到 if (item[childField]) { item[childField] = _changeAction(item[childField]); } return handleAction(item); }); } else { //先替换子节点,否则 childField 替换不到 if (data[childField]) { data[childField] = _changeAction(data[childField]); } return handleAction(data); } }; return _changeAction(newTreeData); }; //递归处理每一项 _proto.recursionDeal = function recursionDeal(treeData, handleAction, childField) { if (childField === void 0) { childField = this.childField; } var newTreeData = JSON.parse(JSON.stringify(treeData)); //递归逻辑 var _changeAction2 = function changeAction(data) { if (Array.isArray(data)) { return data.map(function (item) { if (item[childField]) { item[childField] = _changeAction2(item[childField]); } return handleAction(item); }); } else { return handleAction(data); } }; return _changeAction2(newTreeData); }; return TreeDataHandler; }(); export default new TreeDataHandler();