{"version":3,"file":"useCheck.js","sources":["../../../../../../../packages/components/tree-v2/src/composables/useCheck.ts"],"sourcesContent":["import { getCurrentInstance, nextTick, ref, watch } from 'vue'\nimport {\n NODE_CHECK,\n NODE_CHECK_CHANGE,\n SetOperationEnum,\n} from '../virtual-tree'\nimport type { CheckboxValueType } from '@element-plus/components/checkbox'\nimport type { Ref } from 'vue'\nimport type { Tree, TreeKey, TreeNode, TreeNodeData, TreeProps } from '../types'\n\nexport function useCheck(props: TreeProps, tree: Ref) {\n const checkedKeys = ref>(new Set())\n const indeterminateKeys = ref>(new Set())\n const { emit } = getCurrentInstance()!\n\n watch(\n [() => tree.value, () => props.defaultCheckedKeys],\n () => {\n return nextTick(() => {\n _setCheckedKeys(props.defaultCheckedKeys)\n })\n },\n {\n immediate: true,\n }\n )\n\n const updateCheckedKeys = () => {\n if (!tree.value || !props.showCheckbox || props.checkStrictly) {\n return\n }\n const { levelTreeNodeMap, maxLevel } = tree.value\n const checkedKeySet = checkedKeys.value\n const indeterminateKeySet = new Set()\n // It is easier to determine the indeterminate state by\n // traversing from bottom to top\n // leaf nodes not have indeterminate status and can be skipped\n for (let level = maxLevel - 1; level >= 1; --level) {\n const nodes = levelTreeNodeMap.get(level)\n if (!nodes) continue\n nodes.forEach((node) => {\n const children = node.children\n if (children) {\n // Whether all child nodes are selected\n let allChecked = true\n // Whether a child node is selected\n let hasChecked = false\n for (const childNode of children) {\n const key = childNode.key\n if (checkedKeySet.has(key)) {\n hasChecked = true\n } else if (indeterminateKeySet.has(key)) {\n allChecked = false\n hasChecked = true\n break\n } else {\n allChecked = false\n }\n }\n if (allChecked) {\n checkedKeySet.add(node.key)\n } else if (hasChecked) {\n indeterminateKeySet.add(node.key)\n checkedKeySet.delete(node.key)\n } else {\n checkedKeySet.delete(node.key)\n indeterminateKeySet.delete(node.key)\n }\n }\n })\n }\n indeterminateKeys.value = indeterminateKeySet\n }\n\n const isChecked = (node: TreeNode) => checkedKeys.value.has(node.key)\n\n const isIndeterminate = (node: TreeNode) =>\n indeterminateKeys.value.has(node.key)\n\n const toggleCheckbox = (\n node: TreeNode,\n isChecked: CheckboxValueType,\n nodeClick = true\n ) => {\n const checkedKeySet = checkedKeys.value\n const toggle = (node: TreeNode, checked: CheckboxValueType) => {\n checkedKeySet[checked ? SetOperationEnum.ADD : SetOperationEnum.DELETE](\n node.key\n )\n const children = node.children\n if (!props.checkStrictly && children) {\n children.forEach((childNode) => {\n if (!childNode.disabled) {\n toggle(childNode, checked)\n }\n })\n }\n }\n toggle(node, isChecked)\n updateCheckedKeys()\n if (nodeClick) {\n afterNodeCheck(node, isChecked)\n }\n }\n\n const afterNodeCheck = (node: TreeNode, checked: CheckboxValueType) => {\n const { checkedNodes, checkedKeys } = getChecked()\n const { halfCheckedNodes, halfCheckedKeys } = getHalfChecked()\n emit(NODE_CHECK, node.data, {\n checkedKeys,\n checkedNodes,\n halfCheckedKeys,\n halfCheckedNodes,\n })\n emit(NODE_CHECK_CHANGE, node.data, checked)\n }\n\n // expose\n function getCheckedKeys(leafOnly = false): TreeKey[] {\n return getChecked(leafOnly).checkedKeys\n }\n\n function getCheckedNodes(leafOnly = false): TreeNodeData[] {\n return getChecked(leafOnly).checkedNodes\n }\n\n function getHalfCheckedKeys(): TreeKey[] {\n return getHalfChecked().halfCheckedKeys\n }\n\n function getHalfCheckedNodes(): TreeNodeData[] {\n return getHalfChecked().halfCheckedNodes\n }\n\n function getChecked(leafOnly = false): {\n checkedKeys: TreeKey[]\n checkedNodes: TreeNodeData[]\n } {\n const checkedNodes: TreeNodeData[] = []\n const keys: TreeKey[] = []\n if (tree?.value && props.showCheckbox) {\n const { treeNodeMap } = tree.value\n checkedKeys.value.forEach((key) => {\n const node = treeNodeMap.get(key)\n if (node && (!leafOnly || (leafOnly && node.isLeaf))) {\n keys.push(key)\n checkedNodes.push(node.data)\n }\n })\n }\n return {\n checkedKeys: keys,\n checkedNodes,\n }\n }\n\n function getHalfChecked(): {\n halfCheckedKeys: TreeKey[]\n halfCheckedNodes: TreeNodeData[]\n } {\n const halfCheckedNodes: TreeNodeData[] = []\n const halfCheckedKeys: TreeKey[] = []\n if (tree?.value && props.showCheckbox) {\n const { treeNodeMap } = tree.value\n indeterminateKeys.value.forEach((key) => {\n const node = treeNodeMap.get(key)\n if (node) {\n halfCheckedKeys.push(key)\n halfCheckedNodes.push(node.data)\n }\n })\n }\n return {\n halfCheckedNodes,\n halfCheckedKeys,\n }\n }\n\n function setCheckedKeys(keys: TreeKey[]) {\n checkedKeys.value.clear()\n indeterminateKeys.value.clear()\n nextTick(() => {\n _setCheckedKeys(keys)\n })\n }\n\n function setChecked(key: TreeKey, isChecked: boolean) {\n if (tree?.value && props.showCheckbox) {\n const node = tree.value.treeNodeMap.get(key)\n if (node) {\n toggleCheckbox(node, isChecked, false)\n }\n }\n }\n\n function _setCheckedKeys(keys: TreeKey[]) {\n if (tree?.value) {\n const { treeNodeMap } = tree.value\n if (props.showCheckbox && treeNodeMap && keys) {\n for (const key of keys) {\n const node = treeNodeMap.get(key)\n if (node && !isChecked(node)) {\n toggleCheckbox(node, true, false)\n }\n }\n }\n }\n }\n\n return {\n updateCheckedKeys,\n toggleCheckbox,\n isChecked,\n isIndeterminate,\n // expose\n getCheckedKeys,\n getCheckedNodes,\n getHalfCheckedKeys,\n getHalfCheckedNodes,\n setChecked,\n setCheckedKeys,\n }\n}\n"],"names":["ref","getCurrentInstance","watch","nextTick","SetOperationEnum","NODE_CHECK","NODE_CHECK_CHANGE"],"mappings":";;;;;;;AAMO,SAAS,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE;AACtC,EAAE,MAAM,WAAW,GAAGA,OAAG,iBAAiB,IAAI,GAAG,EAAE,CAAC,CAAC;AACrD,EAAE,MAAM,iBAAiB,GAAGA,OAAG,iBAAiB,IAAI,GAAG,EAAE,CAAC,CAAC;AAC3D,EAAE,MAAM,EAAE,IAAI,EAAE,GAAGC,sBAAkB,EAAE,CAAC;AACxC,EAAEC,SAAK,CAAC,CAAC,M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