Replace existing node with external node

Hi,

In GoJS, we’d like to be able to replace existing nodes in a diagram with external nodes. We have a set of node “templates” which we drag onto to the main canvas, and we listen for the ExternalObjectsDropped events to place those nodes on the canvas. We’re able to drag new new nodes just fine, but we’d like to be able to replace existing nodes using a drag and drop event similar to mouseDrop.

The reason we’re not using mouseDrop for this is because we’d like to limit this behavior to dropping new node “templates” on the diagram. We don’t want to exhibit the node replacement behavior when moving existing nodes in the diagram.

Looking at the ExternalObjectsDropped event, I didn’t see any way to get the target node like there is for mouseDrop. Seems like just the source diagram and parts are available. Is there a way to get target information from ExternalObjectsDropped? Would using the mouseDrop event be preferred? If so, Is there a way to determine if the object being dropped in the mouseDrop event an external object?

Thanks for any help!

If you don’t want to use the GraphObject.mouseDrop event handler, you don’t have to, but then you need to find the object (a Node in your case) yourself by calling Diagram.findPartAt.

If you do use the mouseDrop event handler, you can see whether the drag-and-drop was an external one or not by checking the Diagram.currentTool to see if it’s a DraggingTool.

I couldn’t use mouseDrop on its own since we needed to distinguish between 1)external drops (from the templates) and 2) drops from dragging around existing nodes in the diagram. We need to ignore any drop events not coming from the templates.

To get this to work, I had to use the target node info found in the mouseDrop event, and then use that in the ExternalObjectsDropped event handler to update the appropriate node. It works, but was wondering if there was a better way to do this.

      $(go.Node, . . .,
        {
          mouseDrop: function(e, node) {
            if (e.diagram.currentTool instanceof go.DraggingTool) {
              // an internal drag-and-drop
            } else {
              // an external drag-and-drop from another Diagram
            }
          }
        },
        . . .