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
32 KiB

3 months ago
  1. {"ast":null,"code":"import Node from './node.mjs';\nimport { getNodeKey } from './util.mjs';\nimport { hasOwn, isObject } from '@vue/shared';\nimport { isPropAbsent } from '../../../../utils/types.mjs';\nclass TreeStore {\n constructor(options) {\n this.currentNode = null;\n this.currentNodeKey = null;\n for (const option in options) {\n if (hasOwn(options, option)) {\n this[option] = options[option];\n }\n }\n this.nodesMap = {};\n }\n initialize() {\n this.root = new Node({\n data: this.data,\n store: this\n });\n this.root.initialize();\n if (this.lazy && this.load) {\n const loadFn = this.load;\n loadFn(this.root, data => {\n this.root.doCreateChildren(data);\n this._initDefaultCheckedNodes();\n });\n } else {\n this._initDefaultCheckedNodes();\n }\n }\n filter(value) {\n const filterNodeMethod = this.filterNodeMethod;\n const lazy = this.lazy;\n const traverse = function (node) {\n const childNodes = node.root ? node.root.childNodes : node.childNodes;\n childNodes.forEach(child => {\n child.visible = filterNodeMethod.call(child, value, child.data, child);\n traverse(child);\n });\n if (!node.visible && childNodes.length) {\n let allHidden = true;\n allHidden = !childNodes.some(child => child.visible);\n if (node.root) {\n node.root.visible = allHidden === false;\n } else {\n node.visible = allHidden === false;\n }\n }\n if (!value) return;\n if (node.visible && !node.isLeaf) {\n if (!lazy || node.loaded) {\n node.expand();\n }\n }\n };\n traverse(this);\n }\n setData(newVal) {\n const instanceChanged = newVal !== this.root.data;\n if (instanceChanged) {\n this.nodesMap = {};\n this.root.setData(newVal);\n this._initDefaultCheckedNodes();\n this.setCurrentNodeKey(this.currentNodeKey);\n } else {\n this.root.updateChildren();\n }\n }\n getNode(data) {\n if (data instanceof Node) return data;\n const key = isObject(data) ? getNodeKey(this.key, data) : data;\n return this.nodesMap[key] || null;\n }\n insertBefore(data, refData) {\n const refNode = this.getNode(refData);\n refNode.parent.insertBefore({\n data\n }, refNode);\n }\n insertAfter(data, refData) {\n const refNode = this.getNode(refData);\n refNode.parent.insertAfter({\n data\n }, refNode);\n }\n remove(data) {\n const node = this.getNode(data);\n if (node && node.parent) {\n if (node === this.currentNode) {\n this.currentNode = null;\n }\n node.parent.removeChild(node);\n }\n }\n append(data, parentData) {\n const parentNode = !isPropAbsent(parentData) ? this.getNode(parentData) : this.root;\n if (parentNode) {\n parentNode.insertChild({\n data\n });\n }\n }\n _initDefaultCheckedNodes() {\n const defaultCheckedKeys = this.defaultCheckedKeys || [];\n const nodesMap = this.nodesMap;\n defaultCheckedKeys.forEach(checkedKey => {\n const node = nodesMap[checkedKey];\n if (node) {\n node.setChecked(true, !this.checkStrictly);\n }\n });\n }\n _initDefaultCheckedNode(node) {\n const defaultCheckedKeys = this.defaultCheckedKeys || [];\n if (defaultCheckedKeys.includes(node.key)) {\n node.setChecked(true, !this.checkStrictly);\n }\n }\n setDefaultCheckedKey(newVal) {\n if (newVal !== this.defaultCheckedKeys) {\n this.defaultCheckedKeys = newVal;\n this._initDefaultCheckedNodes();\n }\n }\n registerNode(node) {\n const key = this.key;\n if (!node || !node.data) return;\n if (!key) {\n this.nodesMap[node.id] = node;\n } else {\n const nodeKey = node.key;\n if (nodeKey !== void 0) this.nodesMap[node.key] = node;\n }\n }\n deregisterNode(node) {\n const key = this.key;\n if (!key || !node || !node.data) return;\n node.childNodes.forEach(child => {\n this.deregisterNode(child);\