Skip to content

Commit

Permalink
Models menu "Add", "Edit" and "Delete" options
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Oct 5, 2020
1 parent 5f38691 commit 5530395
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 56 deletions.
151 changes: 98 additions & 53 deletions src/contextMenus/ModelsContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,112 @@ import {ContextMenu} from "@xeokit/xeokit-sdk/src/extras/ContextMenu/ContextMenu

/**
* @private
* @param {*} cfg Configs
* @param {Boolean} [cfg.enableEditModels=false] Set true to show Add/Edit/Delete options in the menu.
*/
class ModelsContextMenu extends ContextMenu {

constructor(cfg = {}) {
super({
context: cfg.context,
items: [
[
{
title: "Load",
getEnabled: function (context) {
return (!context.bimViewer.isModelLoaded(context.modelId));
},
doAction: function (context) {
context.bimViewer.loadModel(context.modelId);
}

const enableEditModels = (!!cfg.enableEditModels);

const items = [
[
{
title: "Load",
getEnabled: function (context) {
return (!context.bimViewer.isModelLoaded(context.modelId));
},
{
title: "Unload",
getEnabled: function (context) {
return context.bimViewer.isModelLoaded(context.modelId);
},
doAction: function (context) {
context.bimViewer.unloadModel(context.modelId);
}
doAction: function (context) {
context.bimViewer.loadModel(context.modelId);
}
},
{
title: "Unload",
getEnabled: function (context) {
return context.bimViewer.isModelLoaded(context.modelId);
},
{
title: "Load All",
getEnabled: function (context) {
const bimViewer = context.bimViewer;
const modelIds = bimViewer.getModelIds();
const loadedModelIds = bimViewer.getLoadedModelIds();
return (loadedModelIds.length < modelIds.length);
},
doAction: function (context) {
context.bimViewer.loadAllModels();
}
doAction: function (context) {
context.bimViewer.unloadModel(context.modelId);
}
}
]
];

if (enableEditModels) {

items.push([
{
title: "Add",
getEnabled: function (context) {
return true;
},
{
title: "Unload All",
getEnabled: function (context) {
const loadedModelIds = context.bimViewer.getLoadedModelIds();
return (loadedModelIds.length > 0);
},
doAction: function (context) {
context.bimViewer.unloadAllModels();
}
doAction: function (context) {
context.bimViewer.addModel();
}
],
[
{
title: "Clear Slices",
getEnabled: function (context) {
return (context.bimViewer.getNumSections() > 0);
},
doAction: function (context) {
context.bimViewer.clearSections();
}
},
{
title: "Edit",
getEnabled: function (context) {
return true;
},
doAction: function (context) {
context.bimViewer.editModel(context.modelId);
}
]
]
})
},
{
title: "Delete",
getEnabled: function (context) {
return true;
},
doAction: function (context) {
context.bimViewer.deleteModel(context.modelId);
}
}
]);
}

items.push([
{
title: "Load All",
getEnabled: function (context) {
const bimViewer = context.bimViewer;
const modelIds = bimViewer.getModelIds();
const loadedModelIds = bimViewer.getLoadedModelIds();
return (loadedModelIds.length < modelIds.length);
},
doAction: function (context) {
context.bimViewer.loadAllModels();
}
},
{
title: "Unload All",
getEnabled: function (context) {
const loadedModelIds = context.bimViewer.getLoadedModelIds();
return (loadedModelIds.length > 0);
},
doAction: function (context) {
context.bimViewer.unloadAllModels();
}
}
]);

items.push([
{
title: "Clear Slices",
getEnabled: function (context) {
return (context.bimViewer.getNumSections() > 0);
},
doAction: function (context) {
context.bimViewer.clearSections();
}
}
]);

super({
context: cfg.context,
items: items
});
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/explorer/ModelsExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class ModelsExplorer extends Controller {
}

this._modelsTabElement = cfg.modelsTabElement;
this._loadModelsButtonElement = cfg.loadModelsButtonElement;
this._unloadModelsButtonElement = cfg.unloadModelsButtonElement;
this._addModelButtonElement = cfg.addModelButtonElement;
this._modelsElement = cfg.modelsElement;
this._modelsTabButtonElement = this._modelsTabElement.querySelector(".xeokit-tab-btn");

Expand All @@ -39,9 +41,12 @@ class ModelsExplorer extends Controller {
objectDefaults: ModelIFCObjectColors
});

this._modelsContextMenu = new ModelsContextMenu();
this._modelsContextMenu = new ModelsContextMenu({
enableEditModels: cfg.enableEditModels
});

this._modelsInfo = {};
this._numModels = 0;
this._numModelsLoaded = 0;
this._projectId = null;
}
Expand All @@ -51,7 +56,15 @@ class ModelsExplorer extends Controller {
this.unloadProject();
this._projectId = projectId;
this._modelsInfo = {};
this._numModels = 0;
this._parseProject(projectInfo, done);
if (this._numModelsLoaded < this._numModels) {
this._loadModelsButtonElement.classList.remove("disabled");
}
if (this._numModelsLoaded > 0) {
this._unloadModelsButtonElement.classList.remove("disabled");
}
this._addModelButtonElement.classList.remove("disabled");
}, (errMsg) => {
this.error(errMsg);
if (error) {
Expand All @@ -74,6 +87,7 @@ class ModelsExplorer extends Controller {
var html = "";
const modelsInfo = projectInfo.models || [];
this._modelsInfo = {};
this._numModels = modelsInfo.length;
for (let i = 0, len = modelsInfo.length; i < len; i++) {
const modelInfo = modelsInfo[i];
this._modelsInfo[modelInfo.id] = modelInfo;
Expand Down Expand Up @@ -150,7 +164,7 @@ class ModelsExplorer extends Controller {
const modelId = modelsLoaded.pop();
this.loadModel(modelId,
() => { // Done
this._loadNextModel(modelsLoaded, done);
this._loadNextModel(modelsLoaded, done);
},
() => { // Error - recover and attempt to load next model
this._loadNextModel(modelsLoaded, done);
Expand All @@ -163,7 +177,7 @@ class ModelsExplorer extends Controller {
done();
return;
}
this.bimViewer.setViewerState(viewerState, done);
this.bimViewer.setViewerState(viewerState, done);
}

unloadProject() {
Expand All @@ -179,7 +193,10 @@ class ModelsExplorer extends Controller {
}
this._modelsElement.innerHTML = "";
this._numModelsLoaded = 0;

this._loadModelsButtonElement.classList.add("disabled");
this._unloadModelsButtonElement.classList.add("disabled");
this._addModelButtonElement.classList.add("disabled");
const lastProjectId = this._projectId;
this._projectId = null;
this.fire("projectUnloaded", {
Expand Down Expand Up @@ -240,6 +257,11 @@ class ModelsExplorer extends Controller {
const aabb = scene.getAABB(scene.visibleObjectIds);
this._numModelsLoaded++;
this._unloadModelsButtonElement.classList.remove("disabled");
if (this._numModelsLoaded < this._numModels) {
this._loadModelsButtonElement.classList.remove("disabled");
} else {
this._loadModelsButtonElement.classList.add("disabled");
}
if (this._numModelsLoaded === 1) { // Jump camera to view-fit first model loaded
this.viewer.cameraFlight.jumpTo({
aabb: aabb
Expand Down Expand Up @@ -292,6 +314,11 @@ class ModelsExplorer extends Controller {
} else {
this._unloadModelsButtonElement.classList.add("disabled");
}
if (this._numModelsLoaded < this._numModels) {
this._loadModelsButtonElement.classList.remove("disabled");
} else {
this._loadModelsButtonElement.classList.add("disabled");
}
this.fire("modelUnloaded", modelId);
}

Expand Down

0 comments on commit 5530395

Please sign in to comment.