2014-10-27 49 views
0

我正嘗試在我的連接上創建動態端點作爲覆蓋並遇到問題。我想模擬一下這個人有在這裏SO:JsPlumb動態端點作爲連接覆蓋

jsPlumb connecting custom overlays - endpoint not moved

但是,不管我怎麼努力做時,我得到了這一點:

var overlay_div = $(connection.overlays[0].canvas); 

我無法連接得到認可。我試圖把這個邏輯放在綁定連接中,但是當試圖建立連接覆蓋時,這並不起作用。任何協助這將是非常有益的。

+0

請創建一個小提琴,演示問題。 – 2014-10-27 19:47:32

+0

好吧,這裏是一個小提琴的開始:http://jsfiddle.net/janessaallen/c3b514wf/9/ – jallen 2014-10-27 20:01:25

+0

'未捕獲的ReferenceError:loadFlowchartNodes未定義'&'connectionNode()'創建一個端點,但沒有元素要附加端點,而應該返回一個'div'元素,它可以充當每個連接的自定義覆蓋。 – 2014-10-27 20:11:57

回答

0

http://jsfiddle.net/nitincool4urchat/c3b514wf/14/

首先,創建自定義元素疊加

其次,確保這些元素具有唯一的ID

三,綁定到connection事件來創建這些定製覆蓋端點。

jsPlumb.ready(function() { 
     // setup some defaults for jsPlumb. 
     instance = jsPlumb.getInstance({ 
      Endpoint : ["Dot", {radius:2}], 
      HoverPaintStyle : {strokeStyle:"#1e8151", lineWidth:4 }, 
      ConnectionOverlays : [ 
       [ "Arrow", { 
        location:1, 
        id:"arrow", 
        length:14, 
        foldback:0.8 
       }] 
       ,["Custom", { 
        create: function(component) { 
         return connectionNode(); 
        }, 
        location:0.5 
       }] 
       //,[ "Label", { label:"Connect To", id:"label", cssClass:"aLabel" }] 
      ], 
      Container: "flowchart-demo" 
     }); 


     var relationEndpoint = { 
      endpoint: ["Dot", { radius: 2 }], 
      isSource: true, 
      connector: ["Flowchart", { stub: [40, 60], cornerRadius: 5, alwaysRespectStubs: true }], 
      connectorStyle: { strokeStyle: "#5c96bc", lineWidth: 4, outlineColor: "transparent", outlineWidth: 4 }, 
      maxConnections: 5, 
      onMaxConnections: function (info, e) { 
       alert("Maximum connections (" + info.maxConnections + ") reached"); 
      }, 
      isTarget: true, 
      dropOptions: { 
       tolerance: "touch", 
       hoverClass: "dropHover", 
       activeClass: "dragActive" 
      }, 
      beforeDetach: function (conn) { 
       return confirm("Detach connection?"); 
      } 
     }; 

     function connectionNode() { 
      //var overlay_div = $(connection.ConnectionOverlays[0].canvas); 
      //jsPlumb.addEndpoint({ anchor: ["Perimeter", { shape: "Rectangle" }] }, relationEndpoint); 
      return $("<div>Custom</div>",{id:Date.now()}).css({border:'1px solid black',background:'green'}); 
     } 


     var windows = jsPlumb.getSelector(".flowchart-demo .window"); 

     instance.draggable(windows); 

     instance.bind("connection", function(info) { 
      console.dir(info.connection); 
      console.dir(info.connection.getOverlays()); 
      console.dir(info.connection.getOverlays()[1].canvas); 
      var overlay_div = $(info.connection.getOverlays()[1].canvas);   
      jsPlumb.addEndpoint(overlay_div,{ anchor: ["Perimeter", { shape: "Rectangle" }] }, relationEndpoint);   
     }); 


     // suspend drawing and initialise. 
     instance.doWhileSuspended(function() { 
      var isFilterSupported = instance.isDragFilterSupported(); 
      // make each ".ep" div a source and give it some parameters to work with. here we tell it 
      // to use a Continuous anchor and the StateMachine connectors, and also we give it the 
      // connector's paint style. note that in this demo the strokeStyle is dynamically generated, 
      // which prevents us from just setting a jsPlumb.Defaults.PaintStyle. but that is what i 
      // would recommend you do. Note also here that we use the 'filter' option to tell jsPlumb 
      // which parts of the element should actually respond to a drag start. 
      // here we test the capabilities of the library, to see if we 
      // can provide a `filter` (our preference, support by vanilla 
      // jsPlumb and the jQuery version), or if that is not supported, 
      // a `parent` (YUI and MooTools). I want to make it perfectly 
      // clear that `filter` is better. Use filter when you can. 
      if (isFilterSupported) { 
       instance.makeSource(windows, { 
        filter:".ep", 
        anchor:"Continuous", 
        connector: ["Flowchart", { stub: [40, 60], cornerRadius: 5, alwaysRespectStubs: true }], 
        connectorStyle:{ strokeStyle:"#5c96bc", lineWidth:4, outlineColor:"transparent", outlineWidth:4 }, 
        maxConnections:5, 
        onMaxConnections:function(info, e) { 
         alert("Maximum connections (" + info.maxConnections + ") reached"); 
        } 
       }); 
      } 
      else { 
       var eps = jsPlumb.getSelector(".ep"); 
       for (var i = 0; i < eps.length; i++) { 
        var e = eps[i], p = e.parentNode; 
        instance.makeSource(e, { 
         parent:p, 
         anchor:"Continuous", 
         connector: ["Flowchart", { stub: [40, 60], cornerRadius: 5, alwaysRespectStubs: true }], 
         connectorStyle:{ strokeStyle:"#5c96bc",lineWidth:4, outlineColor:"transparent", outlineWidth:4 }, 
         maxConnections:5, 
         onMaxConnections:function(info, e) { 
          alert("Maximum connections (" + info.maxConnections + ") reached"); 
         } 
        }); 
       } 
      } 
     }); 

     // initialise all '.w' elements as connection targets. 
     instance.makeTarget(windows, { 
      dropOptions:{ hoverClass:"dragHover" }, 
      anchor:"Continuous", 
      allowLoopback:true, 
      anchor:"Continuous" 
     }); 

     jsPlumb.fire("jsPlumbDemoLoaded", instance); 

    }); 
+0

這不就是覆蓋div應該是什麼?要附加端點的div? – jallen 2014-10-27 20:38:17

+0

對不起,不明白。 – 2014-10-28 05:17:50

+0

jsPlumbToolkit的Simon剛剛寫信給我說,不能將端點添加爲覆蓋層。 :-( – jallen 2014-10-28 11:51:15