Hi,
We use native HTML when implementing context menu for all elements including diagram.
If i user right click on the diagram, the context menu is show, but, then, if he moves the mouse OUTSIDE the diagram and click elsewhere on the screen, the context menu retains on the screen.
The context menu disappears only when:
I have put a break point at the lostFocus function.
When the user selects a menu item, the function is fired before the function that implements the menu item.
OK, here’s a partial solution that Simon came up with. Move that hiding code to a separate function:
function hideCX() {
if (myDiagram.currentTool instanceof go.ContextMenuTool) {
myDiagram.currentTool.doCancel();
}
}
Then in your showContextMenu function (that’s the name in the samples/customContextMenu.html` sample, but could be anything in your code) where you are showing the HTML for the context menu, add this:
function showContextMenu(obj, diagram, tool) {
. . .
// Optional: Use a `window` click listener with event capture to
// remove the context menu if the user clicks elsewhere on the page
window.addEventListener("click", hideCX, true);
}
Then clean up that listener when hiding the context menu:
function hideContextMenu() {
cxElement.classList.remove("show-menu");
// Optional: Use a `window` click listener with event capture to
// remove the context menu if the user clicks elsewhere on the page
window.removeEventListener("click", hideCX, true);
}
This should handle clicks anywhere else on your page, but not when changing focus to another window.