You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
23 KiB

1 month ago
  1. {"ast":null,"code":"import { ref, getCurrentInstance, computed, watch, unref } from 'vue';\nimport { getRowIdentity, walkTreeNode } from '../util.mjs';\nimport { isArray } from '@vue/shared';\nimport { isUndefined } from '../../../../utils/types.mjs';\nfunction useTree(watcherData) {\n const expandRowKeys = ref([]);\n const treeData = ref({});\n const indent = ref(16);\n const lazy = ref(false);\n const lazyTreeNodeMap = ref({});\n const lazyColumnIdentifier = ref(\"hasChildren\");\n const childrenColumnName = ref(\"children\");\n const checkStrictly = ref(false);\n const instance = getCurrentInstance();\n const normalizedData = computed(() => {\n if (!watcherData.rowKey.value) return {};\n const data = watcherData.data.value || [];\n return normalize(data);\n });\n const normalizedLazyNode = computed(() => {\n const rowKey = watcherData.rowKey.value;\n const keys = Object.keys(lazyTreeNodeMap.value);\n const res = {};\n if (!keys.length) return res;\n keys.forEach(key => {\n if (lazyTreeNodeMap.value[key].length) {\n const item = {\n children: []\n };\n lazyTreeNodeMap.value[key].forEach(row => {\n const currentRowKey = getRowIdentity(row, rowKey);\n item.children.push(currentRowKey);\n if (row[lazyColumnIdentifier.value] && !res[currentRowKey]) {\n res[currentRowKey] = {\n children: []\n };\n }\n });\n res[key] = item;\n }\n });\n return res;\n });\n const normalize = data => {\n const rowKey = watcherData.rowKey.value;\n const res = {};\n walkTreeNode(data, (parent, children, level) => {\n const parentId = getRowIdentity(parent, rowKey);\n if (isArray(children)) {\n res[parentId] = {\n children: children.map(row => getRowIdentity(row, rowKey)),\n level\n };\n } else if (lazy.value) {\n res[parentId] = {\n children: [],\n lazy: true,\n level\n };\n }\n }, childrenColumnName.value, lazyColumnIdentifier.value);\n return res;\n };\n const updateTreeData = (ifChangeExpandRowKeys = false, ifExpandAll = (_a => (_a = instance.store) == null ? void 0 : _a.states.defaultExpandAll.value)()) => {\n var _a2;\n const nested = normalizedData.value;\n const normalizedLazyNode_ = normalizedLazyNode.value;\n const keys = Object.keys(nested);\n const newTreeData = {};\n if (keys.length) {\n const oldTreeData = unref(treeData);\n const rootLazyRowKeys = [];\n const getExpanded = (oldValue, key) => {\n if (ifChangeExpandRowKeys) {\n if (expandRowKeys.value) {\n return ifExpandAll || expandRowKeys.value.includes(key);\n } else {\n return !!(ifExpandAll || (oldValue == null ? void 0 : oldValue.expanded));\n }\n } else {\n const included = ifExpandAll || expandRowKeys.value && expandRowKeys.value.includes(key);\n return !!((oldValue == null ? void 0 : oldValue.expanded) || included);\n }\n };\n keys.forEach(key => {\n const oldValue = oldTreeData[key];\n const newValue = {\n ...nested[key]\n };\n newValue.expanded = getExpanded(oldValue, key);\n if (newValue.lazy) {\n const {\n loaded = false,\n loading = false\n } = oldValue || {};\n newValue.loaded = !!loaded;\n newValue.loading = !!loading;\n rootLazyRowKeys.push(key);\n }\n newTreeData[key] = newValue;\n });\n const lazyKeys = Object.keys(normalizedLazyNode_);\n if (lazy.value && lazyKeys.length && rootLazyRowKeys.length) {\n lazyKeys.forEach(key => {\n const oldValue = oldTreeData[key];\n const lazyNodeChildren = normalizedLazyNode_[key].children;\n if (rootLazyRowKeys.includes(key)) {\n if (newTreeData[key].children.length !== 0) {\n throw new Error(\"[ElTable]children must be an empty ar