Quantcast
Channel: GoJS - Northwoods Software
Viewing all articles
Browse latest Browse all 6968

Replace node on mouseDrop

$
0
0

@ewoudenberg wrote:

Dear Support:

(I'm referring to the Planogram's usage of mouseDrop as a starting point).

I'd like offer a special case of dragging shapes from the palette wherein if you drag a palette shape onto an existing diagram node, it "replaces" that node. All that means in our case is to change the value of a field in the data section of the part on the diagram.

Here's my code:

    // helper definitions for node templates

    nodeStyle() {
    return [
        {
            mouseDragEnter: function(e, node) {
                e.handled = true;
                node.isSelected = true;
            },
            mouseDragLeave: function(e, node) {
                node.isSelected = false;
            },
            mouseDrop: function(e, node) {
                let droppedPart = node.diagram.selection.first().part;
                if (droppedPart.data._isPaletteNode) {
                    let existingPart = node.part;
                    node.diagram.model.startTransaction("perform drop-in replacement");
                    node.diagram.model.setDataProperty(existingPart.data, 'class', droppedPart.data.class);
                    node.diagram.model.commitTransaction("perform drop-in replacement");
                }
                node.diagram.currentTool.doCancel();
            },

            //width: 100,
            resizable: true,
            locationSpot: go.Spot.Center,
            fromSpot: go.Spot.AllSides, toSpot: go.Spot.AllSides,
            fromLinkable: true, toLinkable: true,
            fromLinkableDuplicates: true, toLinkableDuplicates: true

        },
        // TwoWay Binding of the desiredSize
        new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify),
        // The Node.location comes from the "loc" property of the node data,
        // converted by the Point.parse static method.
        // If the Node.location is changed, it updates the "loc" property of the node data,
        // converting back using the Point.stringify static method.
        new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify)
    ];
},

I'm using "doCancel" at the end because I don't want the normal "ExternalObjectsDropped" event handler to run. This is simply because in this case I don't want a new node, I just want to use information from the node being dropped to update the existing node.

Unfortunately this isn't working perfectly (I think the doCancel is messing things up). This approach seems awkward and there must be a better way.

I tried setting e.handled = true in mouseDrop, but that did no prevent the ExternalObjectsDropped event handler from being called.

Thank you

Posts: 6

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6968

Trending Articles