Can777
Can777

Reputation: 156

Removing items from context menu in Forge viewer

I want to remove Show Properties item from context menu which was added V.7.38 and hold other options in the menu.

Although I'm able to remove older options like Isolate or Hide selected, I'm not able to remove Show Properties from menu.

viewer.registerContextMenuCallback('id', function (menu, st) {
    //remove the item from menu
})

This code sample works for older options. Is there any other way to remove newly added options like Show properties ? Thanks

Upvotes: 0

Views: 362

Answers (1)

Petr Broz
Petr Broz

Reputation: 9942

Removing a specific menu entry is a bit tricky today, because of these two reasons:

  1. Different viewer extensions might register their own menu callback before or after your callback.

  2. When the viewer iterates through all menu callbacks to be executed, it does so simply using for (const callbackId in viewer.contextMenuCallbacks) { ... } (where contextMenuCallbacks is a JavaScript object), which means that the order of execution is not deterministic.

In case of the "Show properties" menu entry, this one is added by a context menu callback called propertiesmanager, added by the built-in viewer extension called Autodesk.PropertiesManager. With that said, there are a couple of options to remove the menu entry, for example:

  • removing the propertiesmanager menu callback using viewer.unregisterContextMenuCallback('propertiesmanager')
  • trying to register your own menu callback as late as possible to improve the chance that it will be called after the propertiesmanager callback and that it can therefore remove the "Show properties" during runtime; I was able to achieve this by registering my own menu callback after the Autodesk.Viewing.GEOMETRY_LOADED_EVENT, but again, I cannot guarantee that this will always work as the enumeration of object keys (in point 2 above) is not deterministic.
  • modifying the behavior of the Autodesk.PropertiesManager extension, for example, by modifying its _onContextMenu method that handles the menu callback; this is quite hacky though...

Upvotes: 1

Related Questions