UNPKG

tav-ui

Version:
177 lines (172 loc) 5.77 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var lodashEs = require('lodash-es'); var treeHelper = require('../../../utils/helper/treeHelper2.js'); function useTree(treeDataRef, getFieldNames) { function getAllKeys(list) { const keys = []; const treeData = list || vue.unref(treeDataRef); const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return keys; for (let index = 0; index < treeData.length; index++) { const node = treeData[index]; keys.push(node[keyField]); const children = node[childrenField]; if (children && children.length) { keys.push(...getAllKeys(children)); } } return keys; } function getEnabledKeys(list) { const keys = []; const treeData = list || vue.unref(treeDataRef); const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return keys; for (let index = 0; index < treeData.length; index++) { const node = treeData[index]; node.disabled !== true && node.selectable !== false && keys.push(node[keyField]); const children = node[childrenField]; if (children && children.length) { keys.push(...getEnabledKeys(children)); } } return keys; } function getChildrenKeys(nodeKey, list) { const keys = []; const treeData = list || vue.unref(treeDataRef); const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return keys; for (let index = 0; index < treeData.length; index++) { const node = treeData[index]; const children = node[childrenField]; if (nodeKey === node[keyField]) { keys.push(node[keyField]); if (children && children.length) { keys.push(...getAllKeys(children)); } } else { if (children && children.length) { keys.push(...getChildrenKeys(nodeKey, children)); } } } return keys; } function updateNodeByKey(key, node, list) { if (!key) return; const treeData = list || vue.unref(treeDataRef); const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return; for (let index = 0; index < treeData.length; index++) { const element = treeData[index]; const children = element[childrenField]; if (element[keyField] === key) { treeData[index] = { ...treeData[index], ...node }; break; } else if (children && children.length) { updateNodeByKey(key, node, element[childrenField]); } } } function filterByLevel(level = 1, list, currentLevel = 1) { if (!level) { return []; } const res = []; const data = list || vue.unref(treeDataRef) || []; for (let index = 0; index < data.length; index++) { const item = data[index]; const { key: keyField, children: childrenField } = vue.unref(getFieldNames); const key = keyField ? item[keyField] : ""; const children = childrenField ? item[childrenField] : []; res.push(key); if (children && children.length && currentLevel < level) { currentLevel += 1; res.push(...filterByLevel(level, children, currentLevel)); } } return res; } function insertNodeByKey({ parentKey = null, node, push = "push" }) { const treeData = lodashEs.cloneDeep(vue.unref(treeDataRef)); if (!parentKey) { treeData[push](node); treeDataRef.value = treeData; return; } const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return; treeHelper.forEach(treeData, (treeItem) => { if (treeItem[keyField] === parentKey) { treeItem[childrenField] = treeItem[childrenField] || []; treeItem[childrenField][push](node); return true; } }); treeDataRef.value = treeData; } function insertNodesByKey({ parentKey = null, list, push = "push" }) { const treeData = lodashEs.cloneDeep(vue.unref(treeDataRef)); if (!list || list.length < 1) { return; } if (!parentKey) { for (let i = 0; i < list.length; i++) { treeData[push](list[i]); } } else { const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return; treeHelper.forEach(treeData, (treeItem) => { if (treeItem[keyField] === parentKey) { treeItem[childrenField] = treeItem[childrenField] || []; for (let i = 0; i < list.length; i++) { treeItem[childrenField][push](list[i]); } treeDataRef.value = treeData; return true; } }); } } function deleteNodeByKey(key, list) { if (!key) return; const treeData = list || vue.unref(treeDataRef); const { key: keyField, children: childrenField } = vue.unref(getFieldNames); if (!childrenField || !keyField) return; for (let index = 0; index < treeData.length; index++) { const element = treeData[index]; const children = element[childrenField]; if (element[keyField] === key) { treeData.splice(index, 1); break; } else if (children && children.length) { deleteNodeByKey(key, element[childrenField]); } } } return { deleteNodeByKey, insertNodeByKey, insertNodesByKey, filterByLevel, updateNodeByKey, getAllKeys, getChildrenKeys, getEnabledKeys }; } exports.useTree = useTree; //# sourceMappingURL=useTree2.js.map