@opensig/opendesign
Version:
50 lines (49 loc) • 1.26 kB
JavaScript
import { VTree } from "../_utils/tree.mjs";
import { isUndefined, isString } from "../_utils/is.mjs";
class MenuTree extends VTree {
constructor(value, parent, children = []) {
super(value, parent, children);
}
addChild(options) {
const { value, parentVal } = options;
const node = {
value,
parent: null,
children: []
};
if (isUndefined(parentVal)) {
if (!this.hasSameNode(this.root.children, node.value)) {
node.parent = this.root;
this.root.children.push(node);
}
} else {
const parentNode = this.getNode(this.root, parentVal);
if (parentNode && !this.hasSameNode(parentNode.children, node.value)) {
node.parent = parentNode;
parentNode.children.push(node);
}
}
}
selectNode(val) {
const path = this.getPath(this.root, val, []) || [];
return path.map((node) => {
if (isString(node.value)) {
return node.value;
}
});
}
getSiblings(val) {
const node = this.getNode(this.root, val);
if (!node || !node.parent) {
return [];
}
return node.parent.children.map((item) => {
if (item.value !== val) {
return item.value;
}
});
}
}
export {
MenuTree as default
};