UNPKG

shineout

Version:

Shein 前端组件库

93 lines (76 loc) 2.78 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread"; import immer from 'immer'; import { deepClone } from './clone'; export var getFilterTree = function getFilterTree(treeNodes, filterFunc, filterExpandKeys, keyFunc, childrenKey, showHitDescendants, firstMatchNode, _temp) { if (childrenKey === void 0) { childrenKey = 'children'; } var _ref = _temp === void 0 ? {} : _temp, advanced = _ref.advanced; var mapFilteredNodeToData = function mapFilteredNodeToData(node) { if (!node) return null; var match = false; if (filterFunc(node)) { if (firstMatchNode) firstMatchNode(node); match = true; } var children = (node[childrenKey] || []).map(mapFilteredNodeToData).filter(function (n) { return n; }); if (children.length || match) { var _objectSpread2; var key = keyFunc(node); if (filterExpandKeys && children.length > 0) filterExpandKeys.push(key); if (!node[childrenKey]) return node; var childNodes = showHitDescendants && match ? node[childrenKey] || [] : children; if (advanced && match && children.length > 0) childNodes = children; return _objectSpread({}, node, (_objectSpread2 = {}, _objectSpread2[childrenKey] = childNodes, _objectSpread2)); } return null; }; return treeNodes.map(mapFilteredNodeToData).filter(function (node) { return node; }); }; export var getFlattenTree = function getFlattenTree(data, childrenKey, wide) { if (childrenKey === void 0) { childrenKey = 'children'; } var arr = []; var flatten = function flatten(list, path) { list.forEach(function (item) { var children = item[childrenKey]; if (children && children.length > 0) { var clonedPath = [].concat(path); clonedPath.push(item); if (wide) arr.push(clonedPath); flatten(children, clonedPath); } else { arr.push([].concat(path, [item])); } }); }; flatten(data, []); return arr; }; export var mergeFilteredTree = function mergeFilteredTree(filterDatum, rawDatum, tiledId) { var filterData = filterDatum.data; var childrenKey = filterDatum.childrenKey; if (tiledId.length === 0) return filterData; var recursion = function recursion(node) { var nodeKey = filterDatum.getKey(node); if (tiledId.indexOf(nodeKey) >= 0) { node[childrenKey] = deepClone(rawDatum.getDataById(nodeKey)[childrenKey] || []); } else { var item = filterDatum.getDataById(nodeKey); if (item && item[childrenKey]) { node[childrenKey] = deepClone(item[childrenKey] || []); } } var children = node[childrenKey] || []; children.map(recursion); }; return immer(filterData, function (draft) { draft.map(recursion); }); };