Draggable Connections Examples

A note on dragOptions and dropOptions

dragOptions

There are two methods in jsPlumb that allow you to configure an element from which Connections can be dragged - addEndpoint and makeSource. Each of these methods supports a dragOptions object is one of the parameters in its options. The allowed values in this object vary depending on the drag library in use: if you are using vanilla jsPlumb then the drag library is https://github.com/jsplumb/katavorio; otherwise with the jQuery flavour it is jQuery UI. dragOptions is passed directly through to the underlying library; check the docs for each of these to find out what options are available to you.

dropOptions

There are two methods that allow you to configure an element as a Connection drop target - addEndpoint and makeTarget. Where the source methods support dragOptions, these methods (and yes, one of these methods is the same as a method you'd use to configure a Connection source), these methods support a dropOptions parameter. Again, check the appropriate documentation for details on supported values in this object.

Examples

This is a list of examples of how to use jsPlumb to create Connections using drag and drop. The basic procedure is:

  1. Create Endpoints and register them on elements in your UI, or create a source Endpoint and then make some element a drop target
  2. Drag and Drop

There are plenty of options you can set when doing this...it will be easier to show you some examples:

var endpointOptions = { isSource:true, isTarget:true }; 
var window3Endpoint = jsPlumb.addEndpoint('window3', { anchor:"Top" }, endpointOptions );  

Notice here the usage of the three-argument addEndpoint - we can reuse endpointOptions with a different Anchor for another element. This is a useful practice to get into.

var window4Endpoint = jsPlumb.addEndpoint('window4', { anchor:"BottomCenter" }, endpointOptions );  

Now we have two Endpoints, both of which support drag and drop of new Connections. We can use these to make a programmatic Connection, too, though:

jsPlumb.connect({ 
  source:window3Endpoint,
  target:window4Endpoint,
  connector: [ "Bezier", { curviness:175 } ],
  paintStyle:{ lineWidth:25, strokeStyle:'yellow' }
});  
var endpointOptions = { 
      isSource:true, 
      isTarget:true,
      connector : "Straight",
      connectorStyle: { lineWidth:20, strokeStyle:'blue' },
      scope:"blueline",
      dragAllowedWhenFull:false  
}; 
var endpointOptions = {
      anchor:"Top", 
      isSource:true, 
      isTarget:true,
      connector : "Straight",
      connectorStyle: { lineWidth:20, strokeStyle:'blue' },
      scope:"blueline",
      dragAllowedWhenFull:false  
}; 
var endpointOptions = {
    anchor:[ "TopCenter", "BottomCenter" ], 
      isSource:true, 
      isTarget:true,
      connector : "Straight",
      connectorStyle: { lineWidth:20, strokeStyle:'blue' },
      scope:"blueline",
      dragAllowedWhenFull:false  
}; 
var endpointOptions = {
  anchors:[ "TopCenter", "BottomCenter" ], 
  isSource:true, 
  isTarget:true,
  connector : "Straight",
  connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  scope:"blueline",
  dragAllowedWhenFull:false  
}; 
var endpointOptions = { 
  isSource:true, 
  isTarget:true,
  endpoint: [ "Dot", { radius:30 } ],
  style:{fillStyle:'blue'},
  connector : "Straight",
  connectorStyle: { lineWidth:20, strokeStyle:'blue' },
  scope:"blueline",
  dropOptions:{ 
    drop:function(e, ui) { 
      alert('drop!'); 
    } 
  }  
}; 
var endpointOptions = { 
    isSource:true, 
    isTarget:true,
    endpoint: [ "Dot", {radius:30} ],
    style:{ fillStyle:'blue' },
    maxConnections:-1,
    connector : "Straight",
    connectorStyle: { lineWidth:20, strokeStyle:'blue' },
    scope:"blueline",
    dropOptions:{ 
          drop:function(e, ui) { 
            alert('drop!'); 
          } 
      }  
}; 
jsPlumb.addEndpoint("window1", { uuid:"abcdefg" }, endpointOptions );
jsPlumb.addEndpoint("window2", { uuid:"hijklmn" }, endpointOptions );
jsPlumb.connect({uuids:["abcdefg", "hijklmn"]});
var sourceEndpoint = { isSource:true, endpoint:[ "Dot", { radius:50 } ] };
var targetEndpoint = { endpoint:[ "Rectangle", { width:10, height:10 } ] };
jsPlumb.addEndpoint( "window1", sourceEndpoint );
jsPlumb.makeTarget( "window2", targetEndpoint );

Notice that the endpoint definition we use on the target window does not include the isTarget:true directive. jsPlumb ignores that flag when creating a Connection using an element as the target; but if you then tried to drag another connection to the Endpoint that was created, you would not be able to. To permit that, you would set isTarget:true on the targetEndpoint options defined above.