dragOptions
and dropOptions
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.
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.
This is a list of examples of how to use jsPlumb to create Connections using drag and drop. The basic procedure is:
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 };
window3
, specifying that it should be located in the top center of the element: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' }
});
scope
of blueline
. Also, this Endpoint mandates that once it is full, Connections can no longer be dragged from it (even if reattach
is specified on a Connection):var endpointOptions = {
isSource:true,
isTarget:true,
connector : "Straight",
connectorStyle: { lineWidth:20, strokeStyle:'blue' },
scope:"blueline",
dragAllowedWhenFull:false
};
TopCenter
. It creates Connections that are 20px wide straight lines, that are both a source and target of new Connections, and that have a scope
of blueline
. Also, this Endpoint mandates that once it is full, Connections can no longer be dragged from it (even if reattach
is specified on a Connection):var endpointOptions = {
anchor:"Top",
isSource:true,
isTarget:true,
connector : "Straight",
connectorStyle: { lineWidth:20, strokeStyle:'blue' },
scope:"blueline",
dragAllowedWhenFull:false
};
Top
or Bottom
. It creates Connections that are 20px wide straight lines, it is both a source and target of new Connections, and it has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can no longer be dragged from it (even if reattach
is specified on a Connection):var endpointOptions = {
anchor:[ "TopCenter", "BottomCenter" ],
isSource:true,
isTarget:true,
connector : "Straight",
connectorStyle: { lineWidth:20, strokeStyle:'blue' },
scope:"blueline",
dragAllowedWhenFull:false
};
anchors
instead of anchor
, if that makes you feel happier:var endpointOptions = {
anchors:[ "TopCenter", "BottomCenter" ],
isSource:true,
isTarget:true,
connector : "Straight",
connectorStyle: { lineWidth:20, strokeStyle:'blue' },
scope:"blueline",
dragAllowedWhenFull:false
};
scope
of blueline
, and has an event handler that pops up an alert on drop: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!');
}
}
};
maxConnections
being set to -1 means that the Endpoint has no maximum limit of Connections: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.