Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dragfree dragfreeon events don't work when snapToGridDuringDrag is set to true #41

Open
darkhightech opened this issue Sep 26, 2019 · 3 comments

Comments

@darkhightech
Copy link

When setting the option snapToGridDuringDrag to true, I've noticed that dragfree and dragfreeon events are not being fired.

Example available at: https://codesandbox.io/embed/cytoscape-dagre-demo-7ge5o

Setting snapToGridDuringDrag to false will allow the event to get triggered.

@Suges
Copy link

Suges commented Jan 11, 2022

Also having this problem.

@rob-gordon
Copy link

+1

@feichin-noreja
Copy link

@hasanbalci can you have a look at my code?

My "solution" is: I added a didDrag to snap_during_drag.js and triggerd the dragfree event.

I dont know if there is a better way for this. I dont understand, why the default event from cytoscape is not triggerd correctly. Also dont know the differenct of dragfree and dragfreeon. Maybe you can help me to finalize this code?

`module.exports = function (cy, snap) {

var snapToGridDuringDrag = {};

var attachedNode;
var draggedNodes;

var startPos;
var endPos;

var didDrag = false;

snapToGridDuringDrag.onTapStartNode = function (e) {
    // If user intends to do box selection, then return. Related issue #28
    if (e.originalEvent.altKey || e.originalEvent.ctrlKey
            || e.originalEvent.metaKey || e.originalEvent.shiftKey){
        return;
    }

    var cyTarget = e.target || e.cyTarget;
    if (cyTarget.selected())
        draggedNodes = e.cy.$(":selected");
    else
        draggedNodes = cyTarget;

    startPos = e.position || e.cyPosition;

    if (cyTarget.grabbable() && !cyTarget.locked()){
      attachedNode = cyTarget;
      attachedNode.lock();
      //attachedNode.trigger("grab");
      cy.on("tapdrag", onTapDrag);
      cy.on("tapend", onTapEndNode);
    }
};

var onTapEndNode = function (e) {
    //attachedNode.trigger("free");
    cy.off("tapdrag", onTapDrag);
    cy.off("tapend", onTapEndNode);
    attachedNode.unlock();
    if (didDrag) {
        didDrag = false;
        attachedNode.trigger("dragfree");
    }
    e.preventDefault();
};

var getDist = function () {
    return {
        x: endPos.x - startPos.x,
        y: endPos.y - startPos.y
    }
};

var onTapDrag = function (e) {

    var nodePos = attachedNode.position();
    endPos = e.position || e.cyPosition;
    endPos = snap.snapPos(endPos);
    var dist = getDist();
    if (dist.x != 0 || dist.y != 0) {
        attachedNode.unlock();
        var nodes = draggedNodes.union(draggedNodes.descendants());

        nodes.filter(":childless").positions(function (node, i) {
            if(typeof node === "number") {
              node = i;
            }
            var pos = node.position();
            return snap.snapPos({
                x: pos.x + dist.x,
                y: pos.y + dist.y
            });
        });

        startPos = endPos;
        attachedNode.lock();
        attachedNode.trigger("drag");
        didDrag = true;
    }

};

return snapToGridDuringDrag;

};
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants