ivue-material-plus
Version:
A high quality UI components Library with Vue.js
318 lines (313 loc) • 8.93 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var shared = require('@vue/shared');
var node = require('./node.js');
var utils = require('../utils.js');
class TreeStore {
constructor(options) {
this.currentNodeKey = null;
this.currentNode = null;
for (const name in options) {
if (shared.hasOwn(options, name)) {
this[name] = options[name];
}
}
this.nodesMap = {};
}
initialize() {
this.root = new node["default"]({
data: this.data,
store: this
});
this.root.initialize();
if (this.lazy && this.load) {
this.load(this.root, (data) => {
this.root.createChildren(data);
this.initDefaultCheckedNodes();
});
} else {
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 (nodeKey !== void 0) {
this.nodesMap[node.key] = node;
}
}
}
setCurrentNode(currentNode) {
const preCurrentNode = this.currentNode;
if (preCurrentNode) {
preCurrentNode.isCurrent = false;
}
this.currentNode = currentNode;
this.currentNode.isCurrent = true;
}
getCheckedNodes(leafOnly = false, includeHalfChecked = false) {
const checkedNodes = [];
const traverse = (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 halfCheckedNodes = [];
const traverse = (node) => {
const childNodes = node.root ? node.root.childNodes : node.childNodes;
childNodes.forEach((child) => {
if (child.indeterminate) {
halfCheckedNodes.push(child.data);
}
traverse(child);
});
};
traverse(this);
return halfCheckedNodes;
}
getHalfCheckedKeys() {
return this.getHalfCheckedNodes().map((data) => (data || {})[this.key]);
}
initDefaultCheckedNodes() {
const defaultCheckedKeys = this.defaultCheckedKeys || [];
const nodesMap = this.nodesMap;
defaultCheckedKeys.forEach((checkedKey) => {
const node = nodesMap[checkedKey];
if (node) {
node.setChecked(true, !this.checkBoxStrictly);
}
});
}
initDefaultCheckedNode(node) {
const defaultCheckedKeys = this.defaultCheckedKeys || [];
if (defaultCheckedKeys.includes(node.key)) {
node.setChecked(true, !this.checkBoxStrictly);
}
}
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) {
const key = this.key;
const checkedKeys = {};
this.defaultCheckedKeys = keys;
keys.forEach((key2) => {
checkedKeys[key2] = true;
});
this._setCheckedKeys(key, leafOnly, checkedKeys);
}
_setCheckedKeys(key, leafOnly = false, checkedKeys) {
const allNodes = this.getAllNodes().sort((a, b) => b.level - a.level);
const parentKeys = {};
const keys = Object.keys(checkedKeys);
allNodes.forEach((node) => {
node.setChecked(false, false);
});
for (let i = 0; i < allNodes.length; i++) {
const node = allNodes[i];
const nodeKey = node.data[key].toString();
const checked = keys.includes(nodeKey);
if (!checked) {
if (node.checked && !parentKeys[nodeKey]) {
node.setChecked(false, false);
}
continue;
}
let parent = node.parent;
while (parent && parent.level > 0) {
parentKeys[parent.data[key]] = true;
parent = parent.parent;
}
if (node.isLeaf || this.checkBoxStrictly) {
node.setChecked(true, false);
continue;
}
node.setChecked(true, true);
if (leafOnly) {
node.setChecked(false, false);
const traverse = (node2) => {
const childNodes = node2.childNodes;
childNodes.forEach((child) => {
if (!child.isLeaf) {
child.setChecked(false, false);
}
traverse(child);
});
};
traverse(node);
}
}
}
getAllNodes() {
const allNodes = [];
const nodesMap = this.nodesMap;
for (const nodeKey in nodesMap) {
if (shared.hasOwn(nodesMap, nodeKey)) {
allNodes.push(nodesMap[nodeKey]);
}
}
return allNodes;
}
setData(value) {
const instanceChanged = value !== this.root.data;
if (instanceChanged) {
this.root.setData(value);
this.initDefaultCheckedNodes();
} else {
this.root.updateChildren();
}
}
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);
}
}
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 = parentData ? this.getNode(parentData) : this.root;
if (parentNode) {
parentNode.insertChild({ data });
}
}
getNode(data) {
if (data instanceof node["default"]) {
return data;
}
const key = shared.isObject(data) ? utils.getNodeKey(this.key, data) : data;
return this.nodesMap[key] || null;
}
deregisterNode(node) {
const key = this.key;
if (!key || !node || !node.data) {
return;
}
node.childNodes.forEach((child) => {
this.deregisterNode(child);
});
delete this.nodesMap[node.key];
}
setDefaultCheckedKey(value) {
if (value !== this.defaultCheckedKeys) {
this.defaultCheckedKeys = value;
this.initDefaultCheckedNodes();
}
}
setDefaultExpandedKeys(keys) {
keys = keys || [];
this.defaultExpandedKeys = keys;
keys.forEach((key) => {
const node = this.getNode(key);
if (node) {
node.expand(null, this.autoExpandParent);
}
});
}
setCurrentNodeKey(key, shouldAutoExpandParent = true) {
if (key === null || key === void 0) {
this.currentNode && (this.currentNode.isCurrent = false);
this.currentNode = null;
return;
}
const node = this.getNode(key);
if (node) {
this.setCurrentNode(node);
if (shouldAutoExpandParent && this.currentNode.level > 1) {
this.currentNode.parent.expand(null, true);
}
}
}
setUserCurrentNode(node, shouldAutoExpandParent = true) {
const key = node[this.key];
const currNode = this.nodesMap[key];
this.setCurrentNode(currNode);
if (shouldAutoExpandParent && this.currentNode.level > 1) {
this.currentNode.parent.expand(null, true);
}
}
filter(value) {
const filterNodeMethod = this.filterNodeMethod;
const lazy = this.lazy;
const traverse = (node) => {
const childNodes = node.root ? node.root.childNodes : node.childNodes;
childNodes.forEach((child) => {
child.visible = filterNodeMethod.call(child, value, child.data, child);
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 && !lazy) {
node.expand();
}
};
traverse(this);
}
insertBefore(data, refData) {
const refNode = this.getNode(refData);
refNode.parent.insertBefore({ data }, refNode);
}
insertAfter(data, refData) {
const refNode = this.getNode(refData);
refNode.parent.insertAfter({ data }, refNode);
}
setChecked(data, checked, deep) {
const node = this.getNode(data);
if (node) {
node.setChecked(!!checked, deep);
}
}
getCurrentNode() {
return this.currentNode;
}
}
exports["default"] = TreeStore;
//# sourceMappingURL=tree-store.js.map