woltlab-compiler
Version:
A compiler for WoltLab-Package Archives and WoltLab-Package Components
156 lines • 3.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const NodeCollection_1 = require("./NodeCollection");
/**
* Represents a node.
*/
class Node {
/**
* Initializes a new instance of the `Node` 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_1.NodeCollection(this);
if (!util_1.isNullOrUndefined(options.ID)) {
this.ID = options.ID;
}
this.Name = options.Name;
if (!util_1.isNullOrUndefined(options.Item)) {
this.item = generator(this, options.Item);
}
if (!util_1.isNullOrUndefined(options.Parent)) {
this.Parent = new Node(options.Parent, generator);
}
if (!util_1.isNullOrUndefined(options.Nodes)) {
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;
}
set ID(value) {
this.id = value;
}
/**
* Gets or sets the name of the node.
*/
get Name() {
return this.name;
}
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) {
if (this.Parent !== value) {
if (!util_1.isNullOrUndefined(this.Parent) &&
this.Parent.Nodes.includes(this)) {
this.Parent.Nodes.splice(this.Parent.Nodes.indexOf(this), 1);
}
if (util_1.isNullOrUndefined(value) ||
value.Nodes.includes(this)) {
this.parent = value;
}
else {
value.Nodes.push(this);
}
}
}
/**
* @inheritdoc
*/
get Nodes() {
return this.nodes;
}
/**
* @inheritdoc
*/
GetAllNodes() {
let result = [];
result.push(this);
for (let node of this.Nodes) {
result.push(...node.GetAllNodes());
}
return result;
}
/**
* @inheritdoc
*/
GetObjects() {
let result = {};
if (!util_1.isNullOrUndefined(this.ID)) {
result[this.ID] = this;
}
if (!util_1.isNullOrUndefined(this.Item)) {
Object.assign(result, this.Item.GetObjects());
}
for (let node of this.Nodes) {
Object.assign(result, node.GetObjects());
}
return result;
}
/**
* Returns a string that represents the current object.
*/
toString() {
return this.FullName;
}
}
exports.Node = Node;
//# sourceMappingURL=Node.js.map