@opensig/opendesign
Version:
60 lines (59 loc) • 1.76 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
class VTree {
constructor(value, parent, children = []) {
__publicField(this, "root");
this.root = {
value,
parent,
children
};
}
getNode(node, val) {
if (node.value === val) {
return node;
}
const children = node.children;
for (let i = 0, len = children.length; i < len; i++) {
const rlt = this.getNode(children[i], val);
if (rlt) {
return rlt;
}
}
}
getPath(node, val, path) {
const children = node.children;
for (let i = 0, len = children.length; i < len; i++) {
const child = children[i];
if (child.value === val) {
return [...path, child];
}
const rlt = this.getPath(child, val, [...path, child]);
if (rlt) {
return rlt;
}
}
}
hasSameNode(nodes, val) {
return nodes.some((item) => item.value === val);
}
addNode(node) {
const parent = node.parent;
if (!parent) {
if (!this.hasSameNode(this.root.children, node.value)) {
node.parent = this.root;
this.root.children.push(node);
}
} else {
const parentNode = this.getNode(this.root, parent.value);
if (parentNode && !this.hasSameNode(parentNode.children, node.value)) {
node.parent = parentNode;
parentNode.children.push(node);
}
}
}
}
exports.VTree = VTree;