UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 10.7 kB
{"version":3,"file":"tree2.mjs","names":[],"sources":["../../../../../../packages/components/tree/src/tree.vue"],"sourcesContent":["<template>\n <div\n ref=\"el$\"\n :class=\"[\n ns.b(),\n ns.is('dragging', !!dragState.draggingNode),\n ns.is('drop-not-allow', !dragState.allowDrop),\n ns.is('drop-inner', dragState.dropType === 'inner'),\n { [ns.m('highlight-current')]: highlightCurrent },\n ]\"\n role=\"tree\"\n >\n <el-tree-node\n v-for=\"child in root.childNodes\"\n :key=\"getNodeKey(child)\"\n :node=\"child\"\n :props=\"props\"\n :accordion=\"accordion\"\n :render-after-expand=\"renderAfterExpand\"\n :show-checkbox=\"showCheckbox\"\n :render-content=\"renderContent\"\n @node-expand=\"handleNodeExpand\"\n />\n <div v-if=\"isEmpty\" :class=\"ns.e('empty-block')\">\n <slot name=\"empty\">\n <span :class=\"ns.e('empty-text')\">\n {{ emptyText ?? t('el.tree.emptyText') }}\n </span>\n </slot>\n </div>\n <div\n v-show=\"dragState.showDropIndicator\"\n ref=\"dropIndicator$\"\n :class=\"ns.e('drop-indicator')\"\n />\n </div>\n</template>\n\n<script lang=\"ts\">\nimport {\n computed,\n defineComponent,\n getCurrentInstance,\n provide,\n ref,\n watch,\n} from 'vue'\nimport { isEqual } from 'lodash-unified'\nimport { useLocale, useNamespace } from '@element-plus/hooks'\nimport { formItemContextKey } from '@element-plus/components/form'\nimport TreeStore from './model/tree-store'\nimport { getNodeKey as getNodeKeyUtil, handleCurrentChange } from './model/util'\nimport ElTreeNode from './tree-node.vue'\nimport { useNodeExpandEventBroadcast } from './model/useNodeExpandEventBroadcast'\nimport { useDragNodeHandler } from './model/useDragNode'\nimport { useKeydown } from './model/useKeydown'\nimport { ROOT_TREE_INJECTION_KEY } from './tokens'\nimport { treeEmits, treeProps } from './tree'\n\nimport type Node from './model/node'\nimport type { ComponentInternalInstance } from 'vue'\nimport type { Nullable } from '@element-plus/utils'\nimport type { FilterValue, TreeData, TreeKey, TreeNodeData } from './tree.type'\n\nexport default defineComponent({\n name: 'ElTree',\n components: { ElTreeNode },\n props: treeProps,\n emits: treeEmits,\n setup(props, ctx) {\n const { t } = useLocale()\n const ns = useNamespace('tree')\n\n const store = ref<TreeStore>(\n new TreeStore({\n key: props.nodeKey,\n data: props.data,\n lazy: props.lazy,\n props: props.props,\n load: props.load,\n currentNodeKey: props.currentNodeKey,\n checkStrictly: props.checkStrictly,\n checkDescendants: props.checkDescendants,\n defaultCheckedKeys: props.defaultCheckedKeys,\n defaultExpandedKeys: props.defaultExpandedKeys,\n autoExpandParent: props.autoExpandParent,\n defaultExpandAll: props.defaultExpandAll,\n filterNodeMethod: props.filterNodeMethod,\n })\n )\n\n store.value.initialize()\n\n const root = ref<Node>(store.value.root)\n const currentNode = ref<Node | null>(null)\n const el$ = ref<Nullable<HTMLElement>>(null)\n const dropIndicator$ = ref<Nullable<HTMLElement>>(null)\n\n const { broadcastExpanded } = useNodeExpandEventBroadcast(props)\n\n const { dragState } = useDragNodeHandler({\n props,\n ctx,\n el$,\n dropIndicator$,\n store,\n })\n\n useKeydown({ el$ }, store)\n\n const instance = getCurrentInstance()\n\n const isSelectTree = computed(() => {\n let parent = instance?.parent\n while (parent) {\n if (parent.type.name === 'ElTreeSelect') {\n return true\n }\n parent = parent.parent\n }\n return false\n })\n\n const isEmpty = computed(() => {\n const { childNodes } = root.value\n return (\n (!childNodes ||\n childNodes.length === 0 ||\n childNodes.every(({ visible }) => !visible)) &&\n !isSelectTree.value\n )\n })\n\n watch(\n () => props.currentNodeKey,\n (newVal) => {\n store.value.setCurrentNodeKey(newVal ?? null)\n }\n )\n\n watch(\n () => props.defaultCheckedKeys,\n (newVal, oldVal) => {\n if (isEqual(newVal, oldVal)) return\n\n store.value.setDefaultCheckedKey(newVal ?? [])\n }\n )\n\n watch(\n () => props.defaultExpandedKeys,\n (newVal) => {\n store.value.setDefaultExpandedKeys(newVal ?? [])\n }\n )\n\n watch(\n () => props.data,\n (newVal) => {\n store.value.setData(newVal)\n },\n { deep: true }\n )\n\n watch(\n () => props.checkStrictly,\n (newVal) => {\n store.value.checkStrictly = newVal\n }\n )\n\n const filter = (value: FilterValue) => {\n if (!props.filterNodeMethod)\n throw new Error('[Tree] filterNodeMethod is required when filter')\n store.value.filter(value)\n }\n\n const getNodeKey = (node: Node) => {\n return props.nodeKey ? getNodeKeyUtil(props.nodeKey, node.data) : node.id\n }\n\n const requireNodeKey = (methodName: string) => {\n if (!props.nodeKey) {\n throw new Error(`[Tree] nodeKey is required in ${methodName}`)\n }\n }\n\n const getNodePath = (data: TreeKey | TreeNodeData) => {\n requireNodeKey('getNodePath')\n\n const node = store.value.getNode(data)\n if (!node) return []\n const path = [node.data]\n let parent = node.parent\n while (parent && parent !== root.value) {\n path.push(parent.data)\n parent = parent.parent\n }\n return path.reverse()\n }\n\n const getCheckedNodes = (\n leafOnly?: boolean,\n includeHalfChecked?: boolean\n ): TreeNodeData[] => {\n return store.value.getCheckedNodes(leafOnly, includeHalfChecked)\n }\n\n const getCheckedKeys = (leafOnly?: boolean): TreeKey[] => {\n return store.value.getCheckedKeys(leafOnly)\n }\n\n const getCurrentNode = () => {\n const currentNode = store.value.getCurrentNode()\n return currentNode ? currentNode.data : null\n }\n\n const getCurrentKey = (): TreeKey | null => {\n requireNodeKey('getCurrentKey')\n\n const currentNode = getCurrentNode()\n return currentNode ? currentNode[props.nodeKey!] : null\n }\n\n const setCheckedNodes = (nodes: Node[], leafOnly?: boolean) => {\n requireNodeKey('setCheckedNodes')\n\n store.value.setCheckedNodes(nodes, leafOnly)\n }\n\n const setCheckedKeys = (keys: TreeKey[], leafOnly?: boolean) => {\n requireNodeKey('setCheckedKeys')\n\n store.value.setCheckedKeys(keys, leafOnly)\n }\n\n const setChecked = (\n data: TreeKey | TreeNodeData,\n checked: boolean,\n deep: boolean\n ) => {\n store.value.setChecked(data, checked, deep)\n }\n\n const getHalfCheckedNodes = (): TreeNodeData[] => {\n return store.value.getHalfCheckedNodes()\n }\n\n const getHalfCheckedKeys = (): TreeKey[] => {\n return store.value.getHalfCheckedKeys()\n }\n\n const setCurrentNode = (node: Node, shouldAutoExpandParent = true) => {\n requireNodeKey('setCurrentNode')\n\n handleCurrentChange(store, ctx.emit, () => {\n broadcastExpanded(node)\n store.value.setUserCurrentNode(node, shouldAutoExpandParent)\n })\n }\n\n const setCurrentKey = (\n key: TreeKey | null = null,\n shouldAutoExpandParent = true\n ) => {\n requireNodeKey('setCurrentKey')\n\n handleCurrentChange(store, ctx.emit, () => {\n broadcastExpanded()\n store.value.setCurrentNodeKey(key, shouldAutoExpandParent)\n })\n }\n\n const getNode = (data: TreeKey | TreeNodeData): Node => {\n return store.value.getNode(data)\n }\n\n const remove = (data: TreeNodeData | Node) => {\n store.value.remove(data)\n }\n\n const append = (\n data: TreeNodeData,\n parentNode: TreeNodeData | TreeKey | Node\n ) => {\n store.value.append(data, parentNode)\n }\n\n const insertBefore = (\n data: TreeNodeData,\n refNode: TreeKey | TreeNodeData | Node\n ) => {\n store.value.insertBefore(data, refNode)\n }\n\n const insertAfter = (\n data: TreeNodeData,\n refNode: TreeKey | TreeNodeData | Node\n ) => {\n store.value.insertAfter(data, refNode)\n }\n\n const handleNodeExpand = (\n nodeData: TreeNodeData,\n node: Node,\n instance: ComponentInternalInstance\n ) => {\n broadcastExpanded(node)\n ctx.emit('node-expand', nodeData, node, instance)\n }\n\n const updateKeyChildren = (key: TreeKey, data: TreeData) => {\n requireNodeKey('updateKeyChildren')\n\n store.value.updateChildren(key, data)\n }\n\n provide(ROOT_TREE_INJECTION_KEY, {\n ctx,\n props,\n store,\n root,\n currentNode,\n instance,\n })\n\n provide(formItemContextKey, undefined)\n\n return {\n ns,\n // ref\n store,\n root,\n currentNode,\n dragState,\n el$,\n dropIndicator$,\n\n // computed\n isEmpty,\n\n // methods\n filter,\n getNodeKey,\n getNodePath,\n getCheckedNodes,\n getCheckedKeys,\n getCurrentNode,\n getCurrentKey,\n setCheckedNodes,\n setCheckedKeys,\n setChecked,\n getHalfCheckedNodes,\n getHalfCheckedKeys,\n setCurrentNode,\n setCurrentKey,\n t,\n getNode,\n remove,\n append,\n insertBefore,\n insertAfter,\n handleNodeExpand,\n updateKeyChildren,\n }\n },\n})\n</script>\n"],"mappings":";;;;;;;qBACE,mBAkCM,OAAA;EAjCJ,KAAI;EACH,OAAK,eAAA;GAAU,KAAA,GAAG,GAAC;GAAU,KAAA,GAAG,GAAE,YAAA,CAAA,CAAe,KAAA,UAAU,aAAY;GAAS,KAAA,GAAG,GAAE,kBAAA,CAAoB,KAAA,UAAU,UAAS;GAAS,KAAA,GAAG,GAAE,cAAe,KAAA,UAAU,aAAQ,QAAA;GAAA,GAAwB,KAAA,GAAG,EAAC,oBAAA,GAAwB,KAAA,kBAAgB;GAAA,CAAA;EAOhP,MAAK;EAAA;oBAEL,mBAUE,UAAA,MAAA,WATgB,KAAA,KAAK,aAAd,UAAK;uBADd,YAUE,yBAAA;IARC,KAAK,KAAA,WAAW,MAAK;IACrB,MAAM;IACN,OAAO,KAAA;IACP,WAAW,KAAA;IACX,uBAAqB,KAAA;IACrB,iBAAe,KAAA;IACf,kBAAgB,KAAA;IAChB,cAAa,KAAA;IAAA,EAAA,MAAA,GAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA,CAAA;;EAEL,KAAA,WAAA,WAAA,EAAX,mBAMM,OAAA;GAAA,KAAA;GANe,OAAK,eAAE,KAAA,GAAG,EAAC,cAAA,CAAA;GAAA,GAC9B,WAIO,KAAA,QAAA,SAAA,EAAA,QAAA,CAHL,mBAEO,QAAA,EAFA,OAAK,eAAE,KAAA,GAAG,EAAC,aAAA,CAAA,EAAA,kBACb,KAAA,aAAa,KAAA,EAAC,oBAAA,CAAA,EAAA,EAAA,CAAA,CAAA;iBAIvB,mBAIE,OAAA;GAFA,KAAI;GACH,OAAK,eAAE,KAAA,GAAG,EAAC,iBAAA,CAAA;GAAA,qBAFJ,KAAA,UAAU,kBAAiB,CAAA,CAAA"}