UNPKG

@manuth/woltlab-compiler

Version:

A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components

176 lines 4.12 kB
import { NodeCollection } from "./NodeCollection.js"; /** * Represents a node. * * @template TItem * The type of the node-item. * * @template TOptions * The type of the options for generating nodes. */ export class Node { /** * Initializes a new instance of the {@link Node `Node<TItem, TOptions>`} class. * * @param options * The options for generating the object. * * @param generator * The generator-function for generating sub-nodes. */ constructor(options, generator) { /** * The id of the node. */ this.id = null; /** * The item of the node. */ this.item = null; /** * The parent of the node. */ this.parent = null; /** * The children of the node. */ this.nodes = new NodeCollection(this); if ((options.ID !== null) && (options.ID !== undefined)) { this.ID = options.ID; } this.Name = options.Name; if ((options.Item !== null) && (options.Item !== undefined)) { this.item = generator(this, options.Item); } if ((options.Parent !== null) && (options.Parent !== undefined)) { this.Parent = new Node(options.Parent, generator); } if ((options.Nodes !== null) && (options.Nodes !== undefined)) { for (let node of options.Nodes) { this.Nodes.push(new Node(node, generator)); } } } /** * Gets the parents of the node. */ get Parents() { let result = []; for (let node = this.Parent; node !== null; node = node.Parent) { result.push(node); } return result; } /** * Gets or sets the id of the node. */ get ID() { return this.id; } /** * @inheritdoc */ set ID(value) { this.id = value; } /** * Gets or sets the name of the node. */ get Name() { return this.name; } /** * @inheritdoc */ set Name(value) { this.name = value; } /** * Gets the full name of the node. */ get FullName() { return this.Parents.reverse().concat([this]).map((node) => node.Name).join("."); } /** * @inheritdoc */ get Item() { return this.item; } /** * @inheritdoc */ get Parent() { return this.parent; } /** * @inheritdoc */ set Parent(value) { var _a; if (this.Parent !== value) { if ((_a = this.Parent) === null || _a === void 0 ? void 0 : _a.Nodes.includes(this)) { this.Parent.Nodes.splice(this.Parent.Nodes.indexOf(this), 1); } if (value === null || value === void 0 ? void 0 : value.Nodes.includes(this)) { this.parent = value; } else { value.Nodes.push(this); } } } /** * @inheritdoc */ get Nodes() { return this.nodes; } /** * @inheritdoc * * @returns * All nodes inside this node. */ GetAllNodes() { let result = []; result.push(this); for (let node of this.Nodes) { result.push(...node.GetAllNodes()); } return result; } /** * @inheritdoc * * @returns * The objects of the node. */ GetObjects() { let result = {}; if (this.ID) { result[this.ID] = this; } if (this.Item) { Object.assign(result, this.Item.GetObjects()); } for (let node of this.Nodes) { Object.assign(result, node.GetObjects()); } return result; } /** * Returns a string which represents the current object. * * @returns * A string which represents the object. */ toString() { return this.FullName; } } //# sourceMappingURL=Node.js.map