UNPKG

@daign/2d-pipeline

Version:
102 lines (101 loc) 4.04 kB
"use strict"; 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.View = void 0; var graphicNode_1 = require("./graphicNode"); var presentationNode_1 = require("./presentationNode"); /** * The view class that replicates a subtree of the graphic document and prepares it for rendering. */ var View = /** @class */ (function (_super) { __extends(View, _super); /** * Constructor. */ function View() { var _this = _super.call(this) || this; /** * The root of the subtree to be rendered. */ _this.anchorNode = null; /** * Callback to remove subscriptions when the source node changes. */ _this.anchorNodeSubscriptionRemover = null; /** * The representation of the view and root of the replicated tree. */ _this.viewPresentationNode = null; return _this; } /** * Set the anchor node of the view, that is the root of the subtree to be rendered. * @param anchorNode The anchor node. */ View.prototype.mountNode = function (anchorNode) { var _this = this; // Remove subscriptions on previous anchor node. if (this.anchorNodeSubscriptionRemover !== null) { this.anchorNodeSubscriptionRemover(); } this.anchorNode = anchorNode; // Rebuild the copy of the tree when the original document changes. var callback = function () { _this.replicateDocumentTree(); }; this.anchorNodeSubscriptionRemover = this.anchorNode.subscribeToChanges(callback); this.replicateDocumentTree(); }; /** * Creates a copy of the graphic document starting from the source node. */ View.prototype.replicateDocumentTree = function () { // Remove previously created copy. this.destroyDocumentTree(); // Start the creation if an anchor node exists. if (this.anchorNode !== null) { this.viewPresentationNode = new presentationNode_1.PresentationNode(this, this); this.replicateRecursive(this.viewPresentationNode, this.anchorNode); // Update the projection matrices when the tree has been created. this.viewPresentationNode.updateProjectionMatrices(); } }; /** * Recursively create copies of all graphic nodes. * @param parent The node that will be the parent of the source node's copy. * @param sourceNode The graphic node to copy. */ View.prototype.replicateRecursive = function (parent, sourceNode) { var _this = this; var presentationNode = new presentationNode_1.PresentationNode(this, sourceNode); parent.appendChild(presentationNode); sourceNode.children.forEach(function (sourceChild) { _this.replicateRecursive(presentationNode, sourceChild); }); }; /** * Remove a previously created presentation tree. */ View.prototype.destroyDocumentTree = function () { if (this.viewPresentationNode !== null) { this.viewPresentationNode.destroyRecursive(); this.viewPresentationNode = null; } }; return View; }(graphicNode_1.GraphicNode)); exports.View = View;