@daign/2d-pipeline
Version:
Two dimensional graphics pipeline.
153 lines (152 loc) • 5.19 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericNode = void 0;
var observable_1 = require("@daign/observable");
/**
* Class that describes a node in a tree data structure.
*/
var GenericNode = /** @class */ (function (_super) {
__extends(GenericNode, _super);
/**
* Constructor.
*/
function GenericNode() {
var _this = _super.call(this) || this;
/**
* Reference to the parent node.
*/
_this.parent = null;
/**
* References to the child nodes.
*/
_this.children = [];
/**
* Name that is used by the parent as a unique identifier for the child.
*/
_this.mappingName = null;
/**
* The child nodes referenced by their mapping name.
*/
_this.namedMapping = {};
return _this;
}
/**
* Append child node to this node.
* Will throw an error when a name is passed that is not unique within the parent.
* @param childNode The child node.
* @param name The name of the child for the mapping. Optional.
*/
GenericNode.prototype.appendChild = function (childNode, name) {
if (name && this.namedMapping[name]) {
throw new Error('Name is not unique.');
}
// Remove from previous parent if necessary.
if (childNode.parent) {
childNode.removeFromParent();
}
this.children.push(childNode);
childNode.parent = this;
if (name) {
childNode.mappingName = name;
this.namedMapping[name] = childNode;
}
this.propagateNotifyObservers();
};
/**
* Remove child node from this node.
* Will do nothing if the given node is not one of the child nodes.
* @param childNode The child node.
*/
GenericNode.prototype.removeChild = function (childNode) {
var index = this.children.indexOf(childNode);
if (index === -1) {
// The given node is not one of the child nodes.
return;
}
this.children.splice(index, 1);
childNode.parent = null;
if (childNode.mappingName) {
delete this.namedMapping[childNode.mappingName];
childNode.mappingName = null;
}
this.propagateNotifyObservers();
};
/**
* Remove all child nodes from this node.
*/
GenericNode.prototype.clearChildren = function () {
this.children.forEach(function (child) {
child.parent = null;
child.mappingName = null;
});
this.children = [];
this.namedMapping = {};
this.propagateNotifyObservers();
};
/**
* Remove this node from its parent.
* Will do nothing if node has no parent.
*/
GenericNode.prototype.removeFromParent = function () {
if (this.parent !== null) {
this.parent.removeChild(this);
}
};
/**
* Get a child node by its name in the mapping.
* Will throw an error if a child by this name does not exist.
* @param name The name of the child.
* @returns The child node.
*/
GenericNode.prototype.getChildByName = function (name) {
if (!this.namedMapping[name]) {
throw new Error('No child exists for the given name.');
}
return this.namedMapping[name];
};
/**
* Destroy this node and all its references.
*/
GenericNode.prototype.destroyNode = function () {
this.clearChildren();
this.removeFromParent();
this.clearObservers();
};
/**
* Destroy this node and all of its children.
*/
GenericNode.prototype.destroyRecursive = function () {
// Copy the references first because destroying clears the children.
var childReferencesCopy = this.children.slice();
this.destroyNode();
childReferencesCopy.forEach(function (child) {
child.destroyRecursive();
});
};
/**
* Call notifyObservers and propagate up the chain of ancestors.
*/
GenericNode.prototype.propagateNotifyObservers = function () {
this.notifyObservers();
if (this.parent !== null) {
this.parent.propagateNotifyObservers();
}
};
return GenericNode;
}(observable_1.Observable));
exports.GenericNode = GenericNode;