'How to create context menu in mxGraph when clicked on canvas ( null cell)?

On my application using mxGraph, I have a feature that enables a context menu when it is clicked on a cell and returns different options based on where it is clicked - edge or vertex. My code looks like this:

   graph.popupMenuHandler.factoryMethod = function (menu, cell, evt) {
     if (cell.edge) {
       menu.addItem("First edge option", null, function () {
         alert("This is the first option of edge ");
       });
       menu.addItem("Second edge option", null, function () {
         alert("This is the second option of edge ");
       });
     }
     if (cell.vertex) {
       menu.addItem("View Properties", null, function () {
         document.getElementById("property-panel").style["display"] = "block";
       });
       menu.addItem("View Measurements ", null, function () {
         if (cell.measurementName!= null) {
           openPopup(cell);
         } else {
           showAlert("This component does not have any plots!");
         }
       });
     }
   };

This works great, But I want to be able to right click on the blank canvas that is not a cell, ie, somewhere on the editor, but which does not contain any vertex or edge. The problem with that is, this method, works only when a cell is clicked, as it's parameters are menu, cell, and evt. Since there is no cell on a blank canvas, it gives out this following error: Uncaught TypeError: cell is null. Does anyone know if there is a better implementation, if there is an approach where I could create an empty cell (invisible) when the user right-clicks, and then fire up the context menu by passing the invisible cell?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source