数通智联化工云平台
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.

1 line
17 KiB

3 years ago
{"version":3,"file":"useTree.mjs","sources":["../../../../../../../packages/components/tree-v2/src/composables/useTree.ts"],"sourcesContent":["import { computed, nextTick, ref, shallowRef, watch } from 'vue'\nimport { isObject } from '@element-plus/utils'\nimport {\n CURRENT_CHANGE,\n NODE_CLICK,\n NODE_COLLAPSE,\n NODE_EXPAND,\n TreeOptionsEnum,\n} from '../virtual-tree'\nimport { useCheck } from './useCheck'\nimport { useFilter } from './useFilter'\nimport type { SetupContext } from 'vue'\nimport type { treeEmits } from '../virtual-tree'\nimport type { CheckboxValueType } from '@element-plus/components/checkbox'\nimport type {\n Tree,\n TreeData,\n TreeKey,\n TreeNode,\n TreeNodeData,\n TreeProps,\n} from '../types'\n\nexport function useTree(\n props: TreeProps,\n emit: SetupContext<typeof treeEmits>['emit']\n) {\n const expandedKeySet = ref<Set<TreeKey>>(new Set(props.defaultExpandedKeys))\n const currentKey = ref<TreeKey | undefined>()\n const tree = shallowRef<Tree | undefined>()\n\n watch(\n () => props.currentNodeKey,\n (key) => {\n currentKey.value = key\n },\n {\n immediate: true,\n }\n )\n\n watch(\n () => props.data,\n (data: TreeData) => {\n setData(data)\n },\n {\n immediate: true,\n }\n )\n\n const {\n isIndeterminate,\n isChecked,\n toggleCheckbox,\n getCheckedKeys,\n getCheckedNodes,\n getHalfCheckedKeys,\n getHalfCheckedNodes,\n setChecked,\n setCheckedKeys,\n } = useCheck(props, tree)\n\n const { doFilter, hiddenNodeKeySet, isForceHiddenExpandIcon } = useFilter(\n props,\n tree\n )\n\n const valueKey = computed(() => {\n return props.props?.value || TreeOptionsEnum.KEY\n })\n const childrenKey = computed(() => {\n return props.props?.children || TreeOptionsEnum.CHILDREN\n })\n const disabledKey = computed(() => {\n return props.props?.disabled || TreeOptionsEnum.DISABLED\n })\n const labelKey = computed(() => {\n return props.props?.label || TreeOptionsEnum.LABEL\n })\n\n const flattenTree = computed(() => {\n const expandedKeys = expandedKeySet.value\n const hiddenKeys = hiddenNodeKeySet.value\n const flattenNodes: TreeNode[] = []\n const nodes = (tree.value && tree.value.treeNodes) || []\n function traverse() {\n const stack: TreeNode[] = []\n for (let i = nodes.length - 1; i >= 0; --i) {\n stack.push(nodes[i])\n }\n while (stack.length) {\n const node = stack.pop()\n if (!node) continue\n if (!hiddenKeys.has(node.key)) {\n flattenNodes.push(node)\n }\n // Only \"visible\" nodes will be rendered\n if (expandedKeys.has(node.key)) {\n const children = node.children\n if (children) {\n const length = children.length\n for (let i = length - 1; i >= 0; --i) {\n stack.push(children[i])\n }\n }\n }\n }\n }\n traverse()\n return flattenNodes\n })\n\n const isNotEmpty = computed(() => {\n return flattenTree.value.length > 0\n })\n\n function createTree(data: TreeData): Tree {\n const treeNodeMap: Map<TreeKey, TreeNode> = new Map()\n const levelTreeNodeMap: Map<number, TreeNode[]> = new Map()\n let maxLevel = 1\n function traverse(\n nodes: TreeData,\n level = 1,\n parent: TreeNode | undefined = undefined\n ) {\n const siblings: TreeNode[] = []\n for (const rawNode of nodes) {\n const value = getKey(rawNode)\n const node: TreeNode = {\n level,\n key: value,\n data: rawNode,\n }\n node.label = getLabel(rawNode)\n node.parent = parent\n const children = getChildren(rawNode)\n node.disabled = getDisabled(rawNode)\n node.isLeaf = !children || children.length === 0\n if (children && children.length) {\n node.children = traverse(children, level + 1, node)\n }\n siblings.push(node)\n treeNodeMap.set(value, node)\n if (!levelTreeNodeMap.