1 line
31 KiB
Plaintext
1 line
31 KiB
Plaintext
|
|
{"version":3,"file":"node.mjs","sources":["../../../../../../../packages/components/tree/src/model/node.ts"],"sourcesContent":["import { reactive } from 'vue'\nimport { isNil } from 'lodash-unified'\nimport {\n hasOwn,\n isArray,\n isBoolean,\n isFunction,\n isString,\n isUndefined,\n} from '@element-plus/utils'\nimport { NODE_KEY, markNodeData } from './util'\n\nimport type TreeStore from './tree-store'\nimport type { Nullable } from '@element-plus/utils'\nimport type {\n FakeNode,\n TreeKey,\n TreeNodeChildState,\n TreeNodeData,\n TreeNodeLoadedDefaultProps,\n TreeNodeOptions,\n} from '../tree.type'\n\nexport const getChildState = (node: Node[]): TreeNodeChildState => {\n let all = true\n let none = true\n let allWithoutDisable = true\n for (let i = 0, j = node.length; i < j; i++) {\n const n = node[i]\n if (n.checked !== true || n.indeterminate) {\n all = false\n if (!n.disabled) {\n allWithoutDisable = false\n }\n }\n if (n.checked !== false || n.indeterminate) {\n none = false\n }\n }\n\n return { all, none, allWithoutDisable, half: !all && !none }\n}\n\nconst reInitChecked = function (node: Node): void {\n if (node.childNodes.length === 0 || node.loading) return\n\n const { all, none, half } = getChildState(node.childNodes)\n if (all) {\n node.checked = true\n node.indeterminate = false\n } else if (half) {\n node.checked = false\n node.indeterminate = true\n } else if (none) {\n node.checked = false\n node.indeterminate = false\n }\n\n const parent = node.parent\n if (!parent || parent.level === 0) return\n\n if (!node.store.checkStrictly) {\n reInitChecked(parent)\n }\n}\n\nconst getPropertyFromData = function (node: Node, prop: string): any {\n const props = node.store.props\n const data = node.data || {}\n const config = (props as any)[prop]\n\n if (isFunction(config)) {\n return config(data, node)\n } else if (isString(config)) {\n return data[config]\n } else if (isUndefined(config)) {\n const dataProp = data[prop]\n return isUndefined(dataProp) ? '' : dataProp\n }\n}\n\nconst setCanFocus = function (childNodes: Node[], focus: boolean): void {\n childNodes.forEach((item) => {\n item.canFocus = focus\n setCanFocus(item.childNodes, focus)\n })\n}\n\nlet nodeIdSeed = 0\n\nclass Node {\n id: number\n text: string | null\n checked: boolean\n indeterminate: boolean\n data: TreeNodeData\n expanded: boolean\n parent: Node | null\n visible: boolean\n isCurrent: boolean\n store!: TreeStore\n isLeafByUser: boolean | undefined = undefined\n isLeaf: boolean | undefined = undefined\n canFocus: boolean\n\n level: number\n loaded: boolean\n childNodes: Node[]\n loading: boolean\n\n constructor(options: TreeNodeOptions) {\n this.id = nodeIdSeed++\n this.text = null\n this.checked = false\n this.indeterminate = false\n this.data = null as unknown as TreeNodeData\n this.expanded = false\n this.parent = null as Node | null\n this.visible = true\n this.isCurrent = false\n this.canFocus = false\n\n for (const name in options) {\n if (hasOwn(options, name)) {\n this[name] = options[name]\n }\n }\n\n // internal\n this.level = 0\n this.loaded = false\n this.childNodes = []\n this.loading = false\n\n if (this.parent) {\n this.level = this.parent.level + 1\n }\n }\n\n initialize() {\n const store = this.store\n if (!store) {\n throw new Error('[Node]store is required!')\n }\n store.registerNode(this)\n\n const props = store.props\n if (props && typeof props.isLeaf !== 'undefined') {\n const isLeaf = getPropertyFromData(this, 'isLeaf')\n if (isBoolean(isLeaf)) {\n this.isLeafByUser = isLeaf\n }\n }\n\n if (store.lazy !== true && this.data) {\n this.setData(this.data)\n\n if (store.defaultExpandAll) {\n this.expanded = true\n this.canFocus = true\n }\n } else if (\n this.level > 0 &&\n store.lazy &&\n store.defaultExpandAll &&
|