How to lock the position of a cytoscape.js node within its parent node

孤街醉人 提交于 2019-12-06 06:48:37

问题


I want to lock the position of a node relative to its parent compound node, such that if I grab and drag the parent node, the child node moves with it, but the child is not individually grabbable. If I set the child to be ungrabbable and/or locked, then it doesn't move with its parent, but if I don't, it can be individually dragged, which I don't want. Can this be done?

Failing that, is there a way to programmatically grab/ungrab a node so that I can listen for a grab event and then grab the parent instead/as well?


回答1:


Compound grabbing/locking logic with children probably needs to be improved in general, but you should be able to achieve the effect you want today with the current version:

cy.nodes().nonorphans()
  .on('grab', function(){ this.ungrabify(); })
  .on('free', function(){ this.grabify(); })
;

This makes all nonorphan/child nodes not directly grabbable but still moveable with their parents.

Ref for improvements : https://github.com/cytoscape/cytoscape.js/issues/1074




回答2:


For posterity, the code that will do this in cytoscape-automove

let nodeList = cy.nodes().nonorphans();
for (let i=0; i < nodeList.length; i++) {
        let n = nodeList[i];
        let parent = n.parent()[0];
        let family = parent.children();
        family.add(parent);

        this.cy.automove({
            nodesMatching: family,
            reposition: 'drag',
            dragWith: n
        });
    }


来源:https://stackoverflow.com/questions/32130023/how-to-lock-the-position-of-a-cytoscape-js-node-within-its-parent-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!