我想弄清楚如何動態添加端點錨到jsPlumb容器。jsPlumb - 每邊動態端點錨
我想只在左側有源端點,在右側有目標端點。
問題是,我無法找到任何方法來解決問題,而無需訴諸一些黑客,就像我現在正在做的那樣。
jsPlumb支持Continuous Anchors,但單個錨的位置將根據連接器之間的方向和連續錨的數量重新計算。這意味着源端點和目標端點可以共享容器的同一側,這是我想避免的。
這裏是一個jsFiddler code I came up with
這裏是我使用的破解,並重新計算錨定位自己的代碼的一部分(當添加按鈕被點擊),具有一定的越野車結果:(
function fixEndpoints(endpoints) {
//there are 2 types - input and output
var inputAr = $.grep(endpoints, function (elementOfArray, indexInArray) {
return elementOfArray.isSource; //input
});
var outputAr = $.grep(endpoints, function (elementOfArray, indexInArray) {
return elementOfArray.isTarget; //output
});
calculateEndpoint(inputAr, true);
calculateEndpoint(outputAr, false);
}
function calculateEndpoint(endpointArray, isInput) {
//multiplyer
var mult = 1/endpointArray.length;
for (var i = 0; i < endpointArray.length; i++) {
if (isInput) {
endpointArray[i].anchor.x = 1;
endpointArray[i].anchor.y = mult * i;//, 1, 0] };
}
else {
endpointArray[i].anchor.x = 0;
endpointArray[i].anchor.y = mult * i;//, -1, 0] };
}
}
}
//Add additional anchor
$(".button_add").live("click", function() {
var parentnode = $(this)[0].parentNode.parentNode;
jsPlumb.addEndpoint(
parentnode,
anEndpointSource
);
jsPlumb.addEndpoint(
parentnode,
anEndpointDestination
);
//get list of current endpoints
var endpoints = jsPlumb.getEndpoints(parentnode);
//fix endpoints
fixEndpoints(endpoints);
jsPlumb.recalculateOffsets();
jsPlumb.repaint(parentnode);
});
從上圖中可以看到,左側只有源端點(Dot)和右側(Box)只有目標端點,一旦添加了新端點,將根據錨點上的錨點數重新計算錨點e方。
這個工作,但仍然錯誤:位置只更新一次我移動容器和容器之間的連接也不正確。
我想吃點什麼,是爲它工作並連接正確的物品(最好使用正確的jsPlumb代碼,而不訴諸黑客)
很好:)有沒有辦法「刪除」(選擇)節點呢?我將創建一個狀態機編輯器w/jsplumb,但只是在動態地添加/刪除節點和連接而掙扎。一個jsfiddle將是如此之大:) – Dominik 2013-06-27 08:22:48