UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

99 lines (98 loc) 2.62 kB
"use strict"; import { CoreWalker } from "../../../../core/Walker"; export class HierarchyParentController { constructor(node) { this.node = node; this._parent = null; } parent() { return this._parent; } setParent(parent) { if (parent != this.node.parentController.parent()) { this._parent = parent; if (this._parent) { this.node.nameController.requestNameToParent(this.node.name()); } } } firstAncestorWithContext(context) { if (this._parent) { if (this._parent.context() == context) { return this._parent; } else { return this._parent.parentController.firstAncestorWithContext(context); } } return null; } findParent(callback) { if (this._parent) { if (callback(this._parent) == true) { return this._parent; } else { return this._parent.parentController.findParent(callback); } } return null; } path(relativeToParent) { const separator = CoreWalker.SEPARATOR; if (this._parent != null) { if (this._parent == relativeToParent) { return this.node.name(); } else { const parent_fullPath = this._parent.path(relativeToParent); if (parent_fullPath === separator) { return parent_fullPath + this.node.name(); } else { return parent_fullPath + separator + this.node.name(); } } } else { return separator; } } onSetParent() { if (this._on_set_parent_hooks) { for (const hook of this._on_set_parent_hooks) { hook(); } } } findNode(path) { if (path == null) { return null; } if (path == CoreWalker.CURRENT || path == CoreWalker.CURRENT_WITH_SLASH) { return this.node; } if (path == CoreWalker.PARENT || path == CoreWalker.PARENT_WITH_SLASH) { return this.node.parent(); } const separator = CoreWalker.SEPARATOR; if (path === separator) { return this.node.scene().root(); } if (path[0] === separator) { path = path.substring(1, path.length); return this.node.scene().root().node(path); } if (path.split) { const elements = path.split(separator); if (elements.length === 1) { const name = elements[0]; if (this.node.childrenController) { return this.node.childrenController.childByName(name); } else { return null; } } else { return CoreWalker.findNode(this.node, path); } } else { console.error("unexpected path given:", path); return null; } } }