mxgraph

mxgraph block mxevent effect


I'm trying to add a simple 'confirm to remove dialog' on my mxgraph based app, but can't keep the remove event from actually happening when I want to cancel it. So far, I'm listening to mxEvent.REMOVE_CELLS, in my simplest approach I tried something like:

graph.addListener(mxEvent.REMOVE_CELLS, (sender, evt) => {
    evt.consume();
});

As far as I know, consume should keep the event from propagating, and as I understood to have effect at all, but the nodes are deleted no matter what. I even tried to undo immediatelly after the event, and still not working.

Is there even a straight forward way to keep an event from happening and apply a different logic instead


Solution

  • Finally, I ended up overriding mxGraph.removeCells to fire my own custom event, looks something like:

    mxGraph.prototype.removeCells = function(cells, includeEdges) {
      ...
    
      if (shouldntRemoveDirectly) {
        this.fireEvent(new mxEventObject('beforeRemoveLoop', 'cells', cells, 'includeEdges', includeEdges));
        return;
      }
    
      ...
    }