基於maxkfranz的回答,這種方法將允許通過拖入父節點來添加父節點。 綁定tapend事件(觸發釋放時)到孤兒節點(沒有父母)在其中檢測,如果鼠標光標處於父節點的邊界框。
//dragging orphan nodes into parents
cy.on('tapend', ':orphan', function(event){
var node = event.cyTarget;
var mouse = event.cyRenderedPosition;
cy.$(':parent').each(function(i, ele){
//ensure we dont drag into self
if (ele !== node){
//check if we dragged into a compound node
var pos = ele.renderedBoundingBox();
if (mouse.x > pos.x1 && mouse.x < pos.x2){
if (mouse.y > pos.y1 && mouse.y < pos.y2){
console.log("Move ", node.id(), " into ", ele.id());
node.move({
parent: ele.id()
});
}
}
}
});
});
您可能需要使用foreach循環替代,打破node.move(後環),只需在重疊的節點和輕微的性能增加的情況下。
感謝您的回答,但即時尋找一個具體的答案如何解決我的問題。當我將元素e1拖到另一個元素e2上時,e2不會拋出dragover事件。我期待一個例子來構建這樣的東西:[link](https://gojs.net/latest/samples/regrouping.html) –