src/explorer/ObjectsExplorer.js
import {TreeViewPlugin} from "@xeokit/xeokit-sdk/dist/xeokit-sdk.es.js";
import {TreeViewContextMenu} from "../contextMenus/TreeViewContextMenu.js";
import {Controller} from "../Controller.js";
/** @private */
class ObjectsExplorer extends Controller {
constructor(parent, cfg = {}) {
super(parent);
if (!cfg.objectsTabElement) {
throw "Missing config: objectsTabElement";
}
if (!cfg.showAllObjectsButtonElement) {
throw "Missing config: showAllObjectsButtonElement";
}
if (!cfg.hideAllObjectsButtonElement) {
throw "Missing config: hideAllObjectsButtonElement";
}
if (!cfg.objectsElement) {
throw "Missing config: objectsElement";
}
this._objectsTabElement = cfg.objectsTabElement;
this._showAllObjectsButtonElement = cfg.showAllObjectsButtonElement;
this._hideAllObjectsButtonElement = cfg.hideAllObjectsButtonElement;
this._objectsTabButtonElement = this._objectsTabElement.querySelector(".xeokit-tab-btn");
if (!this._objectsTabButtonElement) {
throw "Missing DOM element: ,xeokit-tab-btn";
}
const objectsElement = cfg.objectsElement;
this._treeView = new TreeViewPlugin(this.viewer, {
containerElement: objectsElement,
hierarchy: "containment",
autoAddModels: false,
pruneEmptyNodes: true
});
this._treeViewContextMenu = new TreeViewContextMenu(this.bimViewer, {
hideOnAction: true
});
this._treeView.on("contextmenu", (e) => {
this._treeViewContextMenu.context = {
bimViewer: this.bimViewer,
viewer: e.viewer,
treeViewPlugin: e.treeViewPlugin,
treeViewNode: e.treeViewNode
};
this._treeViewContextMenu.show(e.event.pageX, e.event.pageY);
});
this._treeView.on("nodeTitleClicked", (e) => {
const scene = this.viewer.scene;
const objectIds = [];
e.treeViewPlugin.withNodeTree(e.treeViewNode, (treeViewNode) => {
if (treeViewNode.objectId) {
objectIds.push(treeViewNode.objectId);
}
});
const checked = e.treeViewNode.checked;
if (checked) {
scene.setObjectsXRayed(objectIds, false);
scene.setObjectsVisible(objectIds, false);
scene.setObjectsPickable(objectIds, true);
} else {
scene.setObjectsXRayed(objectIds, false);
scene.setObjectsVisible(objectIds, true);
scene.setObjectsPickable(objectIds, true);
}
});
this._onModelLoaded = this.viewer.scene.on("modelLoaded", (modelId) => {
if (this.viewer.metaScene.metaModels[modelId]) {
const modelInfo = this.bimViewer._modelsExplorer.getModelInfo(modelId);
if (!modelInfo) {
return;
}
this._treeView.addModel(modelId, {
rootName: modelInfo.name
});
}
});
this._onModelUnloaded = this.viewer.scene.on("modelUnloaded", (modelId) => {
if (this.viewer.metaScene.metaModels[modelId]) {
this._treeView.removeModel(modelId);
}
});
this.bimViewer.on("reset", () => {
this._treeView.collapse();
});
}
setEnabled(enabled) {
if (!enabled) {
this._objectsTabButtonElement.classList.add("disabled");
this._showAllObjectsButtonElement.classList.add("disabled");
this._hideAllObjectsButtonElement.classList.add("disabled");
} else {
this._objectsTabButtonElement.classList.remove("disabled");
this._showAllObjectsButtonElement.classList.remove("disabled");
this._hideAllObjectsButtonElement.classList.remove("disabled");
}
}
expandTreeViewToDepth(depth) {
this._treeView.expandToDepth(depth);
}
showNodeInTreeView(objectId) {
this._treeView.collapse();
this._treeView.showNode(objectId);
}
unShowNodeInTreeView() {
this._treeView.unShowNode();
}
destroy() {
super.destroy();
this._treeView.destroy();
this._treeViewContextMenu.destroy();
this.viewer.scene.off(this._onModelLoaded);
this.viewer.scene.off(this._onModelUnloaded);
}
}
export {ObjectsExplorer};