@rxap/data-structure-tree
Version:
Provides a tree data structure with node manipulation capabilities, including adding, removing, and traversing nodes. It supports hierarchical data representation with features like expanding, collapsing, selecting, and deselecting nodes. The package al
197 lines • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Node = void 0;
const rxjs_1 = require("@rxap/rxjs");
const utilities_1 = require("@rxap/utilities");
class Node {
constructor(parent, item, children, _depth, onExpand, onCollapse, display = null, icon = null, type = null, onSelect = null, onDeselect = null, hasDetails = true, style = {},
/**
* Custom parameters passed to all child mode to transport non-standard
* information to allow custom implementations
*/
_parameters = null,
// TODO : move property before _parameters (refactor)
hidden = false) {
this.parent = parent;
this.item = item;
this._depth = _depth;
this.onExpand = onExpand;
this.onCollapse = onCollapse;
this.display = display;
this.icon = icon;
this.type = type;
this.onSelect = onSelect;
this.onDeselect = onDeselect;
this.hasDetails = hasDetails;
this.style = style;
this._parameters = _parameters;
this.hidden = hidden;
this.isLoading$ = new rxjs_1.ToggleSubject();
this._expanded = false;
this._selected = false;
this.hasChildren = false;
this._children = Object.freeze([]);
this.setChildren(children);
const identifier = (0, utilities_1.getIdentifierPropertyValue)(this.item);
if (identifier === null) {
throw new Error('Node item has not an identifier property');
}
this.id = identifier;
}
get depth() {
return this._depth;
}
get children() {
return this._children;
}
get hasVisibleChildren() {
return this._children.some(child => child.isVisible);
}
get isHidden() {
return this.hidden || (this.hasChildren && !this.hasVisibleChildren);
}
get isVisible() {
if (this.hasChildren) {
return !this.hidden || this.hasVisibleChildren;
}
else {
return !this.hidden;
}
}
get expanded() {
return this._expanded;
}
get selected() {
return this._selected;
}
get parameters() {
return this._parameters;
}
set parameters(parameters) {
this._parameters = parameters;
this.children.forEach(child => child.parameters = parameters);
}
static ToNode(parent, item, depth, onExpand, onCollapse, toDisplay = () => 'to display function not defined', getIcon = () => null, getType = () => null, onSelect = null, onDeselect = null, hasDetails = () => true, getStyle = () => ({}), parameters = null) {
var _a;
const node = new Node(parent, item, [], depth, onExpand, onCollapse, toDisplay(item), (0, utilities_1.coerceArray)(getIcon(item)), getType(item), onSelect, onDeselect, hasDetails(item), getStyle(item), parameters);
const children = ((_a = item.children) !== null && _a !== void 0 ? _a : []).map((child) => Node.ToNode(node, child, depth + 1, onExpand, onCollapse, toDisplay, getIcon, getType, onSelect, onDeselect, hasDetails, getStyle, parameters));
node.setChildren(children);
return node;
}
setDepth(depth) {
this._depth = depth;
this.children.forEach(child => child.setDepth(this.depth + 1));
}
addChild(child, end = false) {
const children = this.children.slice();
if (end) {
children.unshift(child);
}
else {
children.push(child);
}
this.setChildren(children);
}
addChildren(newChildren, end = false) {
const children = this.children.slice();
if (end) {
children.unshift(...newChildren);
}
else {
children.push(...newChildren);
}
this.setChildren(children);
}
setChildren(children) {
children.forEach(child => {
child.setDepth(this.depth + 1);
child.onCollapse = this.onCollapse;
child.onExpand = this.onExpand;
});
this._children = Object.freeze(children
.filter((child, index, nodes) => nodes.findIndex(node => node.id === child.id) === index)
// .sort((a, b) => a.hasChildren === b.hasChildren ? 0 : a.hasChildren ? -1 : 1),
);
this.hasChildren = this._children.length !== 0 || this.item.hasChildren;
}
clearChildren() {
this._children = Object.freeze([]);
this.hasChildren = this.item.hasChildren;
}
isNode(id) {
return this.id === id;
}
hasNode(id) {
return this.isNode(id) || this.children.some(child => child.hasNode(id));
}
getNode(id) {
if (this.isNode(id)) {
return this;
}
return this.children.map(child => child.getNode(id)).filter(Boolean)[0] || null;
}
toggleExpand(options = {}) {
if (this.expanded) {
return this.collapse(options);
}
else {
return this.expand(options);
}
}
expand(options = {}) {
this._expanded = true;
return this.onExpand(this, options);
}
select(options = {}) {
this._selected = true;
return this.onSelect ? this.onSelect(this, options) : Promise.resolve();
}
deselect(options = {}) {
this._selected = false;
return this.onDeselect ? this.onDeselect(this, options) : Promise.resolve();
}
toggleSelect() {
if (this.selected) {
return this.deselect();
}
else {
return this.select();
}
}
collapse(options = {}) {
this._expanded = false;
return this.onCollapse(this, options);
}
hide(options = {}) {
var _a;
this.hidden = true;
if (!options.onlySelf) {
if (options.forEachChild || options.forEachChildren) {
this.forEachChild(child => child.hide(Object.assign(Object.assign({}, options), { forEachChild: false, forEachChildren: !!options.forEachChildren })));
}
if (options.parent || options.parents) {
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.hide(Object.assign(Object.assign({}, options), { parent: false, parents: !!options.parents }));
}
}
}
show(options = {}) {
var _a;
this.hidden = false;
if (!options.onlySelf) {
if (options.forEachChild || options.forEachChildren) {
this.forEachChild(child => child.show(Object.assign(Object.assign({}, options), { forEachChild: false, forEachChildren: !!options.forEachChildren })));
}
if (options.parent || options.parents) {
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.show(Object.assign(Object.assign({}, options), { parent: false, parents: !!options.parents }));
}
}
}
forEachChild(fn) {
for (const child of this.children) {
fn(child);
child.forEachChild(fn);
}
}
}
exports.Node = Node;
//# sourceMappingURL=node.js.map