| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- import { nextTick } from 'vue';
- import { isNil } from 'lodash-unified';
- import Node from './node.mjs';
- import { getNodeKey } from './util.mjs';
- import { hasOwn, NOOP, isObject } from '@vue/shared';
- import { isPropAbsent } from '../../../../utils/types.mjs';
- class TreeStore {
- constructor(options) {
- this.lazy = false;
- this.checkStrictly = false;
- this.autoExpandParent = false;
- this.defaultExpandAll = false;
- this.checkDescendants = false;
- this.currentNode = null;
- this.currentNodeKey = null;
- for (const option in options) {
- if (hasOwn(options, option)) {
- this[option] = options[option];
- }
- }
- this.nodesMap = {};
- }
- initialize() {
- this.root = new Node({
- data: this.data,
- store: this
- });
- this.root.initialize();
- if (this.lazy && this.load) {
- const loadFn = this.load;
- loadFn(this.root, (data) => {
- this.root.doCreateChildren(data);
- this._initDefaultCheckedNodes();
- }, NOOP);
- } else {
- this._initDefaultCheckedNodes();
- }
- }
- filter(value) {
- const filterNodeMethod = this.filterNodeMethod;
- const lazy = this.lazy;
- const traverse = async function(node) {
- const childNodes = node.root ? node.root.childNodes : node.childNodes;
- for (const [index, child] of childNodes.entries()) {
- child.visible = !!(filterNodeMethod == null ? void 0 : filterNodeMethod.call(child, value, child.data, child));
- if (index % 80 === 0 && index > 0) {
- await nextTick();
- }
- await traverse(child);
- }
- if (!node.visible && childNodes.length) {
- let allHidden = true;
- allHidden = !childNodes.some((child) => child.visible);
- if (node.root) {
- node.root.visible = allHidden === false;
- } else {
- node.visible = allHidden === false;
- }
- }
- if (!value)
- return;
- if (node.visible && !node.isLeaf) {
- if (!lazy || node.loaded) {
- node.expand();
- }
- }
- };
- traverse(this);
- }
- setData(newVal) {
- const instanceChanged = newVal !== this.root.data;
- if (instanceChanged) {
- this.nodesMap = {};
- this.root.setData(newVal);
- this._initDefaultCheckedNodes();
- this.setCurrentNodeKey(this.currentNodeKey);
- } else {
- this.root.updateChildren();
- }
- }
- getNode(data) {
- if (data instanceof Node)
- return data;
- const key = isObject(data) ? getNodeKey(this.key, data) : data;
- return this.nodesMap[key] || null;
- }
- insertBefore(data, refData) {
- var _a;
- const refNode = this.getNode(refData);
- (_a = refNode.parent) == null ? void 0 : _a.insertBefore({ data }, refNode);
- }
- insertAfter(data, refData) {
- var _a;
- const refNode = this.getNode(refData);
- (_a = refNode.parent) == null ? void 0 : _a.insertAfter({ data }, refNode);
- }
- remove(data) {
- const node = this.getNode(data);
- if (node && node.parent) {
- if (node === this.currentNode) {
- this.currentNode = null;
- }
- node.parent.removeChild(node);
- }
- }
- append(data, parentData) {
- const parentNode = !isPropAbsent(parentData) ? this.getNode(parentData) : this.root;
- if (parentNode) {
- parentNode.insertChild({ data });
- }
- }
- _initDefaultCheckedNodes() {
- const defaultCheckedKeys = this.defaultCheckedKeys || [];
- const nodesMap = this.nodesMap;
- defaultCheckedKeys.forEach((checkedKey) => {
- const node = nodesMap[checkedKey];
- if (node) {
- node.setChecked(true, !this.checkStrictly);
- }
- });
- }
- _initDefaultCheckedNode(node) {
- const defaultCheckedKeys = this.defaultCheckedKeys || [];
- if (!isNil(node.key) && defaultCheckedKeys.includes(node.key)) {
- node.setChecked(true, !this.checkStrictly);
- }
- }
- setDefaultCheckedKey(newVal) {
- if (newVal !== this.defaultCheckedKeys) {
- this.defaultCheckedKeys = newVal;
- this._initDefaultCheckedNodes();
- }
- }
- registerNode(node) {
- const key = this.key;
- if (!node || !node.data)
- return;
- if (!key) {
- this.nodesMap[node.id] = node;
- } else {
- const nodeKey = node.key;
- if (!isNil(nodeKey))
- this.nodesMap[nodeKey] = node;
- }
- }
- deregisterNode(node) {
- const key = this.key;
- if (!key || !node || !node.data)
- return;
- node.childNodes.forEach((child) => {
- this.deregisterNode(child);
- });
- delete this.nodesMap[node.key];
- }
- getCheckedNodes(leafOnly = false, includeHalfChecked = false) {
- const checkedNodes = [];
- const traverse = function(node) {
- const childNodes = node.root ? node.root.childNodes : node.childNodes;
- childNodes.forEach((child) => {
- if ((child.checked || includeHalfChecked && child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) {
- checkedNodes.push(child.data);
- }
- traverse(child);
- });
- };
- traverse(this);
- return checkedNodes;
- }
- getCheckedKeys(leafOnly = false) {
- return this.getCheckedNodes(leafOnly).map((data) => (data || {})[this.key]);
- }
- getHalfCheckedNodes() {
- const nodes = [];
- const traverse = function(node) {
- const childNodes = node.root ? node.root.childNodes : node.childNodes;
- childNodes.forEach((child) => {
- if (child.indeterminate) {
- nodes.push(child.data);
- }
- traverse(child);
- });
- };
- traverse(this);
- return nodes;
- }
- getHalfCheckedKeys() {
- return this.getHalfCheckedNodes().map((data) => (data || {})[this.key]);
- }
- _getAllNodes() {
- const allNodes = [];
- const nodesMap = this.nodesMap;
- for (const nodeKey in nodesMap) {
- if (hasOwn(nodesMap, nodeKey)) {
- allNodes.push(nodesMap[nodeKey]);
- }
- }
- return allNodes;
- }
- updateChildren(key, data) {
- const node = this.nodesMap[key];
- if (!node)
- return;
- const childNodes = node.childNodes;
- for (let i = childNodes.length - 1; i >= 0; i--) {
- const child = childNodes[i];
- this.remove(child.data);
- }
- for (let i = 0, j = data.length; i < j; i++) {
- const child = data[i];
- this.append(child, node.data);
- }
- }
- _setCheckedKeys(key, leafOnly = false, checkedKeys) {
- const allNodes = this._getAllNodes().sort((a, b) => a.level - b.level);
- const cache = /* @__PURE__ */ Object.create(null);
- const keys = Object.keys(checkedKeys);
- allNodes.forEach((node) => node.setChecked(false, false));
- const cacheCheckedChild = (node) => {
- node.childNodes.forEach((child) => {
- var _a;
- cache[child.data[key]] = true;
- if ((_a = child.childNodes) == null ? void 0 : _a.length) {
- cacheCheckedChild(child);
- }
- });
- };
- for (let i = 0, j = allNodes.length; i < j; i++) {
- const node = allNodes[i];
- const nodeKey = node.data[key].toString();
- const checked = keys.includes(nodeKey);
- if (!checked) {
- if (node.checked && !cache[nodeKey]) {
- node.setChecked(false, false);
- }
- continue;
- }
- if (node.childNodes.length) {
- cacheCheckedChild(node);
- }
- if (node.isLeaf || this.checkStrictly) {
- node.setChecked(true, false);
- continue;
- }
- node.setChecked(true, true);
- if (leafOnly) {
- node.setChecked(false, false);
- const traverse = function(node2) {
- const childNodes = node2.childNodes;
- childNodes.forEach((child) => {
- if (!child.isLeaf) {
- child.setChecked(false, false);
- }
- traverse(child);
- });
- };
- traverse(node);
- }
- }
- }
- setCheckedNodes(array, leafOnly = false) {
- const key = this.key;
- const checkedKeys = {};
- array.forEach((item) => {
- checkedKeys[(item || {})[key]] = true;
- });
- this._setCheckedKeys(key, leafOnly, checkedKeys);
- }
- setCheckedKeys(keys, leafOnly = false) {
- this.defaultCheckedKeys = keys;
- const key = this.key;
- const checkedKeys = {};
- keys.forEach((key2) => {
- checkedKeys[key2] = true;
- });
- this._setCheckedKeys(key, leafOnly, checkedKeys);
- }
- setDefaultExpandedKeys(keys) {
- keys = keys || [];
- this.defaultExpandedKeys = keys;
- keys.forEach((key) => {
- const node = this.getNode(key);
- if (node)
- node.expand(null, this.autoExpandParent);
- });
- }
- setChecked(data, checked, deep) {
- const node = this.getNode(data);
- if (node) {
- node.setChecked(!!checked, deep);
- }
- }
- getCurrentNode() {
- return this.currentNode;
- }
- setCurrentNode(currentNode) {
- const prevCurrentNode = this.currentNode;
- if (prevCurrentNode) {
- prevCurrentNode.isCurrent = false;
- }
- this.currentNode = currentNode;
- this.currentNode.isCurrent = true;
- }
- setUserCurrentNode(node, shouldAutoExpandParent = true) {
- var _a;
- const key = node[this.key];
- const currNode = this.nodesMap[key];
- this.setCurrentNode(currNode);
- if (shouldAutoExpandParent && this.currentNode && this.currentNode.level > 1) {
- (_a = this.currentNode.parent) == null ? void 0 : _a.expand(null, true);
- }
- }
- setCurrentNodeKey(key, shouldAutoExpandParent = true) {
- var _a;
- this.currentNodeKey = key;
- if (isPropAbsent(key)) {
- this.currentNode && (this.currentNode.isCurrent = false);
- this.currentNode = null;
- return;
- }
- const node = this.getNode(key);
- if (node) {
- this.setCurrentNode(node);
- if (shouldAutoExpandParent && this.currentNode && this.currentNode.level > 1) {
- (_a = this.currentNode.parent) == null ? void 0 : _a.expand(null, true);
- }
- }
- }
- }
- export { TreeStore as default };
- //# sourceMappingURL=tree-store.mjs.map
|