尝试将节点扩展为null parent

问题描述 投票:1回答:1

我正在使用cytoscape.js和cytoscape.js-expand-collapse在我的图形结构中创建动态层次结构。我希望能够动态创建一个可能被扩展,删除或可能与其他节点重新合并的折叠(合并)节点。每当我打电话给nodes.move({parent:null})时我都遇到了麻烦。它们与图形分离,我无法将它们重新附加到新的parentNode。如果我在它们上面调用restore,我会看到错误,表示它们已经存在于图形中。

mergeunmerge本身在没有现有折叠节点的简单情况下工作正常。但是,对已经包含复合折叠节点的内容调用merge会破坏事物。我将获得生成的合并节点,而没有先前折叠的合并候选项的子节点

更新#1我意识到我的问题是返回错误的移动节点。在集合上调用.move会返回一组新节点。所以unmerge应该返回那些。

更新#2 cytoscape-expand-collapse实用程序在父节点折叠/展开期间通过将元边缘上的'originalEnds'数据prop中的节点数据丢去来进行一些内部簿记。因此,如果我现在通过将节点移入/移出父节点来动态地改变图形,则这些originalEnds指针会失去同步,从而导致无限循环。我正在做一些额外的节点跟踪我自己作为一种解决方法。

function merge(nodes){
    //if one of the nodes is a compound itself, first uncollapse, then merge
    var collapsed = nodes.filter(function(c){return typeof c.data('collapsedChildren')!=='undefined';});
    if (collapsed.length>0) {
        // for now just assume collapsed is a length of 1
        var unmerged = unmerge(collapsed); //unmerged should be now the former collapsed children
        nodes = nodes.subtract(collapsed).union(unmerged.nodes());
    }
    var parentNode = cy.add({group:'nodes', data: {id: parentID}});
    nodes.move({parent: parentID});
    collapseApi.collapse(parentNode);
    return parentNode;
}

function unmerge(parentNode){
    collapseApi.expand(parentNode);
    var children = parentNode.children();
    var moved = children.move({parent:null});
    //at this point children become "detached" from the graph
    // .removed() returns true, however, calling restore() logs an error and they are still technically in the graph
    parentNode.remove();
    return moved;
}
cytoscape.js
1个回答
0
投票

ele.move()在cytoscape中具有更方便的实现> = 3.4:元素被就地修改而不是创建替换元素。

使用ele.move()返回的集合的旧代码仍然有效。通过不必使用返回的集合,可以简化新代码。

© www.soinside.com 2019 - 2024. All rights reserved.