Summary
jsPlumb allows you to connect elements on the screen using SVG, Canvas or VML, depending on the capabilities of the browser.It can be used with jQuery, MooTools or YUI3 (or another library of your choice if you feel like implementing an adapter for it).
Browser Compatibility
jsPlumb 1.3.7 has been tested on the following browsers:
- IE 6 on Windows XP
- IE 7 on Windows XP
- IE 8 on Windows XP
- Firefox 3.5.8 on Windows XP
- IE 9 on Windows 7
- Chrome 12 on Windows 7
- Firefox 3.5.8 on Windows 7
- Firefox 3.6.3 on Ubuntu 10.04
- Chrome on Ubuntu 11.04
- Safari 4 on Mac Leopard
- Chrome on Mac Leopard
- Safari 4 on Windows Vista
- Safari 5.0.5 on Windows 7
- Opera 10.54 on Windows XP
Changes between 1.3.7 and 1.3.6
Backwards Compatibility
There are no backwards compatibility issues between 1.3.7 and 1.3.6.New Features
The following new features have been added:- new method jsPlumb.select provides a fluid interface to work with lists of Connections. This is similar to the existing getConnections method, but the return value of jsPlumb.select supports all of the setter/getter operations that are available on Connection, executing that method on each Connection in the returned list. The method is also chainable. So you can now do things like this:
jsPlumb.select({source:"d1"}).setHover(true).showOverlays();
- makeTarget now supports a beforeDrop interceptor
- getOverlays method added to Connection and Endpoint
- showOverlays/hideOverlays methods added to Connection and Endpoint
- getConnections now support an optional second argument that returns results as a flat array instead of an object with scopes as keys. But if you're going to use this you might want to just use select instead...
- getConnections now supports a value of '*' for any of source, target or scope - a wildcard, meaning "any value".
- new methods setId and setIdChanged let you either tell jsPlumb to change an element's id, or that an element's id has changed. You should use these methods if you need to change element ids as jsPlumb uses element id internally.
Issues Resolved
- 207 - pass original event back in connection detached callback
- 209 - endpoint drag not creating connection after setEnabled(false) and then setEnabled(true)
- 210 - overlays not being hidden when setVisible(false) called
- 211 - IE8 JS error with overlays and continuous anchors
- 212 - change the id of an element
- 214 - beforeDetach gives wrong targetId when being reattached
Setup
Required Imports
jQuery
- jQuery 1.3.x or higher.
- jQuery UI 1.7.x or 1.8.x (if you wish to support drag and drop).
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script> <script type="text/javascript" src="PATH_TO/jquery.jsPlumb-1.3.7-all-min.js "></script>
MooTools
- MooTools Core 1.2.4 or higher. jsPlumb has been tested with 1.2.4 and 1.3.3.
- Drag.Move from MooTools More 1.2.4.4 or higher (if you wish to support drag and drop).
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.3.3/mootools-yui-compressed.js"></script> <script type="text/javascript" src="PATH_TO_MOO_TOOLS_MORE_1_3_2_1"></script> <script type="text/javascript" src="PATH_TO/mootools.jsPlumb-1.3.7-all-min.js "></script>
YUI3
- YUI 3.3.x. jsPlumb has been tested on 3.3.0 only; it is possible other 3.x.x versions will work.
<script type="text/javascript" src="http://yui.yahooapis.com/3.3.0/build/simpleyui/simpleyui-min.js"></script> <script type="text/javascript" src="PATH_TO/yui.jsPlumb-1.3.7-all-min.js "></script>
Render Mode
jsPlumb can use SVG, HTML5 Canvas elements or VML to render the various objects it adds to the display. Most modern browsers support Canvas and SVG; IE<9 browsers support neither.By default, jsPlumb attempts to use jsPlumb.SVG as the render mode (prior to 1.3.4, the default renderer was Canvas), falling back to jsPlumb.VML if the browser is IE<9. You can change the render mode by making this call:
jsPlumb.setRenderMode(jsPlumb.CANVAS);
The jsPlumb.setRenderMode method returns you the render mode that jsPlumb actually ended up setting. Valid values for setRenderMode are:
- jsPlumb.CANVAS
- jsPlumb.SVG
- jsPlumb.VML
Performance Considerations
SVG uses less memory than Canvas and has support for mouse events built in, so I recommend using SVG unless you have a really good reason for wanting Canvas.
Initializing jsPlumb
You should not start making calls to jsPlumb until the DOM has been initialized - perhaps no surprises there. With YUI, though, the asynchronous nature of the script loading process means that you are not guaranteed that jsPlumb is ready as soon as the DOM is. To handle this, you should bind to the "ready" event on jsPlumb (or the instance of jsPlumb you are working with):jsPlumb.bind("ready", function() { ... // your jsPlumb related init code goes here ... });
jsPlumb.ready(function() { ... // your jsPlumb related init code goes here ... });
For jQuery and MooTools you do not actually need to do this; by the time the DOM ready event is fired in each of those libraries you can be sure all the JS we need has been loaded. But in terms of explicitly calling out that you are waiting for jsPlumb, it seems like a good practice to bind to the "ready" event.
If you bind to the "ready" event after jsPlumb has already been initialized, your callback will be executed immediately.
Unloading jsPlumb
jsPlumb offers a method you can call when your page is unloading. You should do this to insure against memory leaks. You configure it like this:<body onunload="jsPlumb.unload();"> ... </body>
Multiple jsPlumb instances
jsPlumb is registered on the browser's Window by default, providing one static instance for the whole page to use. Should you need to, though, you can instantiate independent instances of jsPlumb, using the getInstance method, for example:var firstInstance = jsPlumb.getInstance();
firstInstance.Defaults.Connector = [ "Bezier", { curviness: 150 } ]; firstInstance.Defaults.Anchors = [ "TopCenter", "BottomCenter" ]; firstInstance.connect({source:"element1", target:"element2", scope:"someScope" });
var secondInstance = jsPlumb.getInstance({ PaintStyle:{ lineWidth:6, strokeStyle:"#567567", outlineColor:"black", outlineWidth:1 }, Connector:[ "Bezier", { curviness: 30 } ], Endpoint:[ "Dot", { radius:5 } ], EndpointStyle : { fillStyle: "#567567" }, Anchor : [ 0.5, 0.5, 1, 1 ] }); secondInstance.connect({ source:"element4", target:"element3", scope:"someScope" });
Z-index Considerations
You need to pay attention to the z-indices of the various parts of your UI when using jsPlumb, in particular to ensure that the elements that jsPlumb adds to the DOM do not overlay other parts of the interface.
jsPlumb adds an element to the DOM for each Endpoint, Connector and Overlay. So for a connection having visible Endpoints at each end and a label in the middle, jsPlumb adds four elements to the DOM. The actual elements it adds depend on the renderer in use (Canvas/SVG/VML).
To help you organise z-indices correctly, jsPlumb adds a CSS class to each type of element it adds. They are as follows:
type of element | class added |
Endpoint | _jsPlumb_endpoint |
Connector | _jsPlumb_connector |
Overlay | _jsPlumb_overlay |
Where does jsPlumb add elements?
It's important to understand where in the DOM jsPlumb will add any elements it creates, as it has a bearing on the markup you can use with jsPlumb.
Prior to 1.3.0, jsPlumb added everything to the end of the body element. This has the advantage of being the most flexible arrangement in terms of supporting what elements can be connected, but in certain use cases produced unexpected results. Consider the arrangement where you have some connected elements in a tab: you would expect jsPlumb to add elements inside the tab, so that when the user switches tabs and the current one is hidden, all the jsPlumb stuff is hidden too. But when the elements are on the body, this does not happen!
For this reason, from 1.3.0 onwards, jsPlumb's default behaviour is to attach Endpoint elements to the parent of the element the Endpoint is attached to (not the actual element the Endpoint is attached to), and to attach Connector elements to the parent of the source Endpoint in the Connection. This results in all the elements of the Connection being siblings inside the same parent node, which jsPlumb requires, for various reasons arising from the trade off between doing lots of maths and the performance of the whole thing.
In general, the default behaviour means that you cannot do things like this:
<body> <div id="container0"> <div id="node0"></div> </div> <div id="container1"> <div id="node1"></div> </div> </body> <script type="text/javascript"> var e0 = jsPlumb.addEndpoint("node0"), e1 = jsPlumb.addEndpoint("node1"); jsPlumb.connect({ source:e0, target:e1 }); </script>
because the elements created for 'e0' and 'e1' would have different parent nodes, and an attempt to make a Connection between them would not work. For the example just given, you would need to register the Endpoints on the 'container' divs (and then quite possibly throw away the 'node' divs):
<body> <div id="container0"> </div> <div id="container1"> </div> </body> <script type="text/javascript"> var e0 = jsPlumb.addEndpoint("container0"), e1 = jsPlumb.addEndpoint("container1"); jsPlumb.connect({ source:e0, target:e1 }); </script>
Remember that you can use the anchor parameter to an addEndpoint call to specify where on an element you want an Endpoint to appear. Used in conjunction with CSS classes (discussed below), you can have an Endpoint on top of some element wherever you like.
Note regarding the drawEndpoints option on jsPlumb.connect: with the default behaviour, jsPlumb uses the offsetParent of the source endpoint in a connection to make final adjustments to the position of a connector. When drawEndpoints is set to false, there is no offsetParent of the source endpoint because it is not visible. If your connection lies inside some container other than the document body, the connector will not be able to take that container's offset into account, and will most likely not be in the right place. You should either use the "Blank" endpoint when you don't want to see one, or instruct jsPlumb to attach everything to the document body (see below).
Overriding the default behaviour
From version 1.3.3, the container concept was reintroduced into jsPlumb, because there are use cases in which the default behaviour makes it difficult or impossible to build the UI you want. You can instruct jsPlumb to use some element as the parent of everything jsPlumb adds to the UI through usage of the jsPlumb.Defaults.Container property, or you can set a container parameter on calls to addEndpoint or connect (if this is set and a default is also set, this default value is actually used. This may seem counter-intuitive, and could of course be changed if there is feedback from the community that this would be a good idea). Some examples:
Set a container to use as the default container, using a jQuery selector (you can supply MooTools/YUI selectors if that's the library you're using), and then add an Endpoint. The canvas (or SVG/VML element) created will be a child of the document body:jsPlumb.Defaults.Container = $("body"); ... jsPlumb.addEndpoint(someDiv, { endpoint options });
jsPlumb.Defaults.Container = "containerId"; ... jsPlumb.connect({ source:someDiv, target:someOtherDiv });
jsPlumb.connect({ source:someDiv, target:someOtherDiv, container:$("#containerId") });
jsPlumb.addEndpoint(someDiv, { ..., container:"containerId" });
Basic Concepts
jsPlumb is all about connecting things together, so the core abstraction in jsPlumb is the Connection object, which is itself broken down into these four concepts:- Anchor - a location, relative to an element's origin, at which an Endpoint can exist. You do not create these yourself; you
supply hints to the various jsPlumb functions, which create them as needed. They have no visual representation; they are a logical position only.
Anchors can be referenced by name, for the Anchors that jsPlumb ships with, or a four element array describing [ x, y, x orientation, y orientation ].
See the anchors section for more detail.
- Endpoint - the visual representation of one end of a Connection. You can create and attach these to elements yourself, which you are required to do to support drag and drop, or have jsPlumb create them when creating a Connection programmatically using jsPlumb.connect(...). You can also join two Endpoints programmatically, by passing them as arguments to jsPlumb.connect(...).
- Connector - the visual representation of the line connecting two elements in the page. jsPlumb has four types of these
available as defaults - a Bezier curve, a straight line, 'flowchart' connectors and 'state machine connectors. You do not interact with Connectors;
you just specify definitions of them when you need to - see Connector, Endpoint & Overlay Definitions
- Overlay - a UI component that is used to decorate a Connector, such as a Label, Arrow, etc.
One Connection is made up of two Endpoints, a Connector, and zero or more Overlays working together to join two elements.
jsPlumb's public API exposes only Connection and Endpoint, handling the creation and configuration of everything else internally. But you still need to be across the concepts encapsulated by Anchor, Connector and Overlay.
Anchors
An Anchor models the notion of where on an element a Connector should connect. There are three main types of Anchors:- 'Static' anchors - these are fixed to some point on an element and do not move. They can be specified using a string to identify one of the defaults that jsPlumb ships with, or an array describing the location (see below)
- 'Dynamic' anchors - these are lists of Static anchors from which jsPlumb picks the most appropriate one each time a Connection is painted. The algorithm used to determine the most appropriate anchor picks the one that is closest to the center of the other element in the Connection. A future version of jsPlumb might support a pluggable algorithm to make this decision.
- 'Continuous' anchors - These anchors, introduced in 1.3.4, are not fixed to any specific loction; they are assigned to one of the four faces of an element depending on that element's orientation to the other element in the associated Connection. Continuous anchors are slightly more computationally intensive than Static or Dynamic anchors because jsPlumb is required to calculate the position of every Connection during a paint cycle, rather than just Connections belonging to the element in motion. These anchors were designed for use with the StateMachine connector that was also introduced in 1.3.4, but will work with any combination of Connector and Endpoint.
Static Anchors
jsPlumb has nine default anchor locations you can use to specify where the Connectors connect to elements: these are the four corners of an element, the center of the element, and the midpoint of each edge of the element:- TopCenter
- TopRight
- RightMiddle
- BottomRight
- BottomCenter
- BottomLeft
- LeftMiddle
- TopLeft
jsPlumb.connect({...., anchor:"BottomCenter", ... });
jsPlumb.connect({...., anchor:[ 0.5, 1, 0, 1 ], ... });
jsPlumb.connect({...., anchor:[ 0.5, 1, 0, 1, 0, 50 ], ... });
Dynamic Anchors
These are Anchors that can be positioned in one of a number of locations, choosing the one that is most appropriate each time something moves or is painted in the UI.There is no special syntax to creating a DynamicAnchor; you just provide an array of individual Static Anchor specifications, eg:
var dynamicAnchors = [ [ 0.2, 0, 0, -1 ], [ 1, 0.2, 1, 0 ], [ 0.8, 1, 0, 1 ], [ 0, 0.8, -1, 0 ] ]; jsPlumb.connect({...., anchor:dynamicAnchors, ... });
Default Dynamic Anchor
jsPlumb provides a dynamic anchor called "AutoDefault" that chooses from TopCenter, RightMiddle, BottomCenter and LeftMiddle:jsPlumb.connect({...., anchor:"AutoDefault", ... });
Location Selection
The initial implementation of the algorithm that decides which location to choose just calculates which location is closest to the center of the other element in the connection. It is possible that future versions of jsPlumb could support more sophisticated choice algorithms, if the need arose.Draggable Connections
Dynamic Anchors and Draggable Connections can interoperate: jsPlumb locks the position of a dynamic anchor when you start to drag a connection from it, and unlocks it once the connection is either established or abandoned. At that point you may see the position of the dynamic anchor change, as jsPlumb optimises the connection.You can see this behaviour in the draggable connections demonstration, when you drag a connection from the blue endpoint on window 1 to the blue endpoint on window 3 - the connection is established and then window 1's blue endpoint jumps down to a location that is closer to window 3.
Continuous Anchors
As discussed above, these are anchors whose positions are calculated by jsPlumb according to the orientation between elements in a Connection, and also how many other Continuous anchors happen to be sharing the element. You specify that you want to use Continuous anchors using the string syntax you would use to specify one of the default Static Anchors, for example:jsPlumb.connect({ source:someDiv, target:someOtherDiv, anchor:"Continuous" });
jsPlumb.connect({ source:someDiv, target:someOtherDiv, anchors:["BottomCenter", "Continuous"] });
Note also that Continuous Anchors can be specified on addEndpoint calls:
jsPlumb.addEndpoint(someDiv, { anchor:"Continuous", paintStyle:{ fillStyle:"red" } });
jsPlumb.makeSource(someDiv, { anchor:"Continuous", paintStyle:{ fillStyle:"red" } }); jsPlumb.makeTarget(someDiv, { anchor:"Continuous", paintStyle:{ fillStyle:"red" } });
jsPlumb.Defaults.Anchor = "Continuous";
Connector, Endpoint & Overlay Definitions
Before we discuss Connector, Endpoints and Overlays, a quick word on definitions: whenever you need to define a Connector, Endpoint or Overlay, you must use a "definition" of it, rather than constructing one directly. This definition can be either a string that nominates the artifact you want to create:jsPlumb.connect({ source:"someDiv", target:"someOtherDiv", endpoint:"Rectangle" });
jsPlumb.connect({ source:"someDiv", target:"someOtherDiv", endpoint:[ "Rectangle", { cssClass:"myEndpoint", width:30, height:10 } ] });
var common = { cssClass : "myCssClass", hoverClass : "myHoverClass" }; jsPlumb.connect({ source:"someDiv", target:"someOtherDiv", endpoint:[ "Rectangle", { width:30, height:10 }, common ] });
var common = { cssClass:"myCssClass" }; jsPlumb.connect({ source:"someDiv", target:"someOtherDiv", endpoint:[ "Dot", { radius:5, hoverClass:"myEndpointHover" }, common ], connector:[ "Bezier", { curviness:100 }, common ], overlays: [ [ "Arrow", { foldback:0.2 }, common ], [ "Label", { cssClass:"labelClass" } ] ] });
Connectors
Connectors are the lines that actually join elements of the UI. jsPlumb has four connector implementations - a straight line, a Bezier curve, "flowchart", and "state machine". The default connector is the Bezier curve.
You optionally specify a Connector by setting the 'connector' property on a call to jsPlumb.connect, jsPlumb.addEndpoint(s), jsPlumb.makeSource or jsPlumb.makeTarget. If you do not supply a value for 'connector', the default will be used (see defaults).
You specify Connectors using the syntax described above in Connector, Endpoint & Overlay Definitions. Allowed constructor values for each Connector type are described below:
Bezier Connector
The Bezier Connector provides a cubic Bezier path between the two Endpoints. It supports a single constructor argument:
curviness - Optional; defaults to 150. This defines the distance in pixels that the Bezier's control points are situated from the anchor points. This does not mean that your connector will pass through a point at this distance from your curve. It is a hint to how you want the curve to travel. Rather than discuss Bezier curves at length here, because they are a complex topic, refer instead to Wikipedia.
Straight Connector
The Straight Connector draws a straight line between the two endpoints. No constructor arguments are supported; use the endpointStyle and/or cssClass/connectorClass to a connect, addEndpoint, makeSource or makeTarget call to control the appearance of one of these Connectors.
Flowchart Connector
This type of Connector draws a connection that consists of a series of vertical or horizontal segments - the classic flowchart look. A single constructor argument is supported:
stub - this is the minimum length, in pixels, of the initial stub that emanates from an Endpoint. This parameter is optional, and defaults to 30 pixels.This Connector supports Connection that start and end on the same element.
State Machine Connector
This Connector draws slightly curved lines (they are actually quadratic Bezier curves), similar to the State Machine connectors you may have seen in software like GraphViz. Connections in which some element is both the source and the target (so-called 'loopback') are supported by these Connectors (as they are with Flowchart Connectors); in this case you get a circle. Supported parameters are:
margin - Optional; defaults to 5. Defines the distance from the element that the connector begins/ends.
curviness - Optional, defaults to 10. This has a similar effect to the curviness parameter on Bezier curves.
proximityLimit - Optional, defaults to 80. The minimum distance between the two ends of the Connector before it paints itself as a straight line rather than a quadratic Bezier curve.
Endpoint Types
An Endpoint is the UI component that marks the location of an Anchor, ie. the place where a Connector joins an element. jsPlumb comes with four Endpoint implementations - Dot, Rectangle, Blank and Image. You optionally specify Endpoint properties using the endpoint parameter in a call to jsPlumb.connect, jsPlumb.addEndpoint, jsPlumb.makeSource or jsPlumb.makeTarget.
As with Connectors and Overlays, you specify Endpoints using the syntax described above in Connector, Endpoint & Overlay Definitions.
The four available Endpoint types, and their constructor arguments, are as follows:Dot Endpoint
This Endpoint draw a dot on the screen. It supports three constructor parameters:radius - Optional; defaults to 10 pixels. Defines the radius of the dot.
cssClass - Optional. A CSS class to attach to the element the Endpoint creates.
hoverClass - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the element or an attached Connection.
Rectangle Endpoint
Draws a rectangle. Supported constructor parameters are:width - Optional; defaults to 20 pixels. Defines the width of the rectangle.
height - Optional; defaults to 20 pixels. Defines the height of the rectangle.
cssClass - Optional. A CSS class to attach to the element the Endpoint creates.
hoverClass - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the element or an attached Connection.
Image Endpoint
Draws an image from a given URL. This Endpoint supports three constructor parameters:src - Required. Specifies the URL of the image to use.
cssClass - Optional. A CSS class to attach to the element the Endpoint creates.
hoverClass - Optional. A CSS class to attach to the element the Endpoint creates whenever the mouse is hovering over the element or an attached Connection.
Blank Endpoint
Does not draw anything visible to the user. As discussed previously, this Endpoint is probably not what you want if you need your users to be able to drag existing Connections - for that, use a Rectangle or Dot Endpoint ans assign to it a CSS class that causes it to be transparent.Overlay Types
Overlays are UI elements that are painted onto connections, such as labels or arrows. jsPlumb comes with four defaults:
- Arrow - a configurable arrow that is painted at some point along the connector. You can control the length and width of the Arrow, the 'foldback' point - a point the tail points fold back into, and the direction (allowed values are 1 and -1; 1 is the default and means point in the direction of the connection)
- Label - a configurable label that is painted at some point along the connector.
- PlainArrow - an Arrow shaped as a triangle, with no foldback.
- Diamond - as the name suggests, a diamond.
All Overlays support these two methods for getting/setting their location (location is a number between 0 and 1 inclusive, indicating some point along the path inscribed by the associated Connector):
- getLocation - returns the current location
- setLocation - sets the current location; valid values are decimals in the range 0-1 inclusive
You can specify one or more overlays when making a call to jsPlumb.connect, jsPlumb.addEndpoint or jsPlumb.makeSource (but not jsPlumb.makeTarget; overlays are always derived from what the source of a Connection defines) The three cases are discussed below:
1. Specifying one or more overlays on a jsPlumb.connect call. In this example we'll create an Arrow with the default options for an Arrow, and a label with the text "foo":jsPlumb.connect({ ... overlays:[ "Arrow", [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ] ], ... });
2. Specifying one or more overlays on a jsPlumb.addEndpoint call. Note in this example that we use the parameter 'connectorOverlays' and not 'overlays' as in the last example. This is because 'overlays' would refer to Endpoint Overlays:
jsPlumb.addEndpoint("someDiv", { ... connectorOverlays:[ [ "Arrow", { width:10, length:30, location:1, id:"arrow" } ], [ "Label", { label:"foo", id:"label" } ] ], ... });
3. Specifying one or more overlays on a jsPlumb.makeSource call. Note in this example that we again use the parameter 'connectorOverlays' and not 'overlays'. The 'endpoint' parameter to jsPlumb.makeSource supports everything you might pass to the second argument of a jsPlumb.addEndpoint call:
jsPlumb.makeSource("someDiv", { ... endpoint:{ connectorOverlays:[ [ "Arrow", { width:10, length:30, location:1, id:"arrow" } ], [ "Label", { label:"foo", id:"label" } ] ] } ... });
Arrow Overlay
This overlay draws an arrow, using four points: the head and two tail points, and a 'foldback' point, which permits the tail of the arrow to be indented. Available constructor arguments for this Overlay are
- width - width of the tail of the arrow
- length - distance from the tail of the arrow to the head
- location - where, as a proportional value from 0 to 1 inclusive, the Arrow should appear on the Connector
- direction - which way to point. Allowed values are 1 (the default, meaning forwards) and -1, meaning backwards
- foldback - how far along the axis of the arrow the tail points foldback in to. Default is 0.623.
- paintStyle - a style object in the form used for paintStyle values for Endpoints and Connectors
PlainArrow Overlay
This is just a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 1, meaning the tail of the Arrow is a flat edge. All of the constructor parameters from Arrow apply for PlainArrow.
Diamond Overlay
This is a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 2, meaning the Arrow turns into a Diamond. All of the constructor parameters from Arrow apply for PlainArrow.
Label Overlay
This provides a text label to decorate Connectors with. The available constructor arguments are:- label - The text to display. You can provide a function here instead of plain text: it is passed the Connection as an argument, and it should return a String.
- cssClass - Optional css class to use for the Label. This is now preferred over using the 'labelStyle' parameter.
- labelStyle - (deprecated, use cssClass instead) Optional arguments for the label's appearance. Valid entries in this JS object are:
- font - a font string in a format suitable for the Canvas element
- fillStyle - the color to fill the label's background with. Optional.
- color - the color of the label's text. Optional.
- padding - optional padding for the label. This is expressed as a proportion of the width of the label, not in pixels or ems.
- borderWidth - optional width in pixels for the label's border. Defaults to 0.
- borderStyle - optional. The color to paint the border, if there is one.
- location - As for Arrow Overlay. Where, proportionally from 0 to 1 inclusive, the label should appear.
var c = jsPlumb.connect({ source:"d1", target:"d2", overlays:[ [ "Label", {label:"FOO", id:"label"}] ] }); ... var label = c.getOverlay("label"); console.log("Label is currently", label.getLabel()); label.setLabel("BAR"); console.log("Label is now", label.getLabel());
Both Connections and Endpoints support Label Overlays, and because changing labels is quite a common operation, setLabel and getLabel methods have been added to these objects:
var conn = jsPlumb.connect({ source:"d1", target:"d2", label:"FOO" }); ... console.log("Label is currently", conn.getLabel()); conn.setLabel("BAR"); console.log("Label is now", conn.getLabel());
var conn = jsPlumb.connect({ source:"d1", target:"d2" }); ... conn.setLabel(function(c) { var s = new Date(); return s.getTime() + "milliseconds have elapsed since 01/01/1970"; }); console.log("Label is now", conn.getLabel());
Hiding/Showing Overlays
You can control the visibility of Overlays using the setVisible method of Overlays themselves, or with showOverlay(id) or hideOverlay(id) on a Connection.
Remember the id parameter that we specified in the examples above? This can be used to retrieve the Overlay from a Connection:
var connection = jsPlumb.connect({ ... overlays:[ "Arrow", [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ] ], ... }); // time passes var overlay = connection.getOverlay("myLabel"); // now you can hide this Overlay: overlay.setVisible(false); // there are also hide/show methods: overlay.show(); overlay.hide();
However, Connection and Endpoint also have two convenience methods you could use instead:
var connection = jsPlumb.connect({ ... overlays:[ "Arrow", [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ] ], ... }); // time passes connection.hideOverlay("myLabel"); // more time passes connection.showOverlay("myLabel");
Removing Overlays
Connection and Endpoint also have a removeOverlay method, that does what you might expect:
var connection = jsPlumb.connect({ ... overlays:[ "Arrow", [ "Label", { label:"foo", location:0.25 }, id:"myLabel" ] ], ... }); // time passes connection.removeOverlay("myLabel");
Defaults
The easiest way to set a look and feel for your plumbing is to override the defaults that jsPlumb uses. If you do not do this you are forced to provide your overridden values on every call. Every argument to the connect and addEndpoint methods has an associated default value in jsPlumb.The defaults that ship with jsPlumb are stored in jsPlumb.Defaults, which is a Javascript object. Valid entries, and their initial values, are:
Anchor : "BottomCenter", Anchors : [ null, null ], Connector : "Bezier", ConnectionOverlays : [ ], DragOptions : { }, DropOptions : { }, Endpoint : "Dot", EndpointOverlays : [ ], Endpoints : [ null, null ], EndpointStyle : { fillStyle : null }, EndpointStyles : [ null, null ], EndpointHoverStyle : null, EndpointHoverStyles : [ null, null ], HoverPaintStyle : null, LabelStyle : { color : "black" }, LogEnabled : false, Overlays : [ ], MaxConnections : 1, MouseEventsEnabled : true, PaintStyle : { lineWidth : 8, strokeStyle : "#456" }, RenderMode : "svg", Scope : "_jsPlumb_DefaultScope"
Note that in EndpointStyle, the default fillStyle is 'null'. This instructs jsPlumb to use the strokeStyle from the attached connector to fill the endpoint.
Note also that you can specify either or both (or neither) of 'EndpointStyle' and 'EndpointStyles'. This allows you to specify a different end point for each end of a connection. 'Endpoint' and 'Endpoints' use the same concept. jsPlumb will look first in the individual endpoint/endpoint style arrays, and then fall back to the single default version.
you can override these defaults by including this in a script somewhere:jsPlumb.Defaults.PaintStyle = { lineWidth:13, strokeStyle: 'rgba(200,0,0,100)' } jsPlumb.Defaults.DragOptions = { cursor: "crosshair" }; jsPlumb.Default.Endpoints = [ [ "Dot", 7 ], [ "Dot", 11 ] ]; jsPlumb.Defaults.EndpointStyles = [{ fillStyle:"#225588" }, { fillStyle:"#558822" }];
- - connectors are 13 pixels wide and painted with a semi-transparent red line
- - when dragging an element the crosshair cursor is used
- - the source endpoint is a dot of radius 7; the target endpoint is a dot of radius 11
- - the source endpoint is blue; the target endpoint is green
jsPlumb.importDefaults
From version 1.3.7 onwards there is a helper method called importDefaults that allows you to import a set of values with just one call to jsPlumb. Here's the previous example using importDefaults:jsPlumb.importDefaults({ PaintStyle : { lineWidth:13, strokeStyle: 'rgba(200,0,0,100)' }, DragOptions : { cursor: "crosshair" }, Endpoints : [ [ "Dot", 7 ], [ "Dot", 11 ] ], EndpointStyles : [{ fillStyle:"#225588" }, { fillStyle:"#558822" }] });
Connections
Programmatic Connections
The most simple connection you can make with jsPlumb looks like this:jsPlumb.connect({source:"element1", target:"element2"});
- - The type and appearance of each Endpoint in the Connection. jsPlumb's default for this is the "Dot" endpoint, of radius 10, with fill color "#456".
- - The Anchors that define where the connection's Endpoints appear on each element. The jsPlumb default is "BottomCenter"
- - Whether or not each Endpoint can be a source or target for new Connections. The default is false.
- - The type and appearance of the Connection's Connector. The default is a "Bezier" connector of line width 8, and color "#456".
So this call will result in an 8px Bezier, colored "#456", from the bottom center of 'element1' to the bottom center of 'element2', and each Endpoint will be a 10px radius Dot Endpoint, colored "#456".
Let's beef up this call a little and tell jsPlumb what sort of Endpoints we want, and where we want them:
jsPlumb.connect({ source:"element1", target:"element2", anchors:["RightMiddle", "LeftMiddle" ], endpoint:"Rectangle", endpointStyle:{ fillStyle: "yellow" } });
This is what we have told jsPlumb we want this time:
- anchors - this array tells jsPlumb where the source and target Endpoints should be located on their parent elements. In this case,
we use the shorthand syntax to name one of jsPlumb's default anchors; you can also specify custom locations (see anchors).
Instead of anchors you can use anchor, if you want the source and target Endpoints to be located at the
same place on their parent elements.
-
endpoint - this tells jsPlumb to use the "Rectangle" Endpoint for both the source and target of the Connection. As with anchors,
endpoint has a plural version that allows you to specify a different Endpoint for each end of the Connection.
- endpointStyle - this is the definition of the appearance of the Endpoint you specified above. Again, there is a plural equivalent of this that allows you to specify a different style for each end of the Connection. For more information about allowed values for this value, see Connector, Endpoint & Overlay Styles.
Reusing common settings between jsPlumb.connect calls
A fairly common situation you will find yourself in is wanting to create a bunch of Connections that have only minor differences between them. To support that, jsPlumb.connect takes an optional second argument. For example:
var common = { anchors:[ "BottomCenter", "TopCenter" ], endpoints:["Dot", "Blank" ] }; jsPlumb.connect({ source:"someElement", target:"someOtherElement" }, common); jsPlumb.connect({ source:"aThirdElement", target:"yetAnotherElement" }, common);
Endpoints created by jsPlumb.connect
If you supply an element id or selector for either the source or target , jsPlumb.connect will automatically create an Endpoint on the given element. These automatically created Endpoints are not marked as drag source or targets, and cannot be interacted with. For some situations this behaviour is perfectly fine, but for more interactive UIs you should set things up using the drag and drop method discussed below.
Given that jsPlumb.connect creates its own Endpoints in some circumstances, in order to avoid leaving orphaned Endpoints around the place, if the Connection is subsequently deleted, these created Endpoints are deleted too. Should you want to, you can override this behaviour by setting deleteEndpointsOnDetach to false in the connect call:
jsPlumb.connect({ source:"aThirdElement", target:"yetAnotherElement", deleteEndpointsOnDetach:false });
Connections using Drag and Drop
To support drag and drop connections, you first need to set a few things up. Every drag and drop connection needs at least a source Endpoint that the user can drag a connection from. Here's a simple example of how to create an Endpoint:var endpointOptions = { isSource:true }; var endpoint = jsPlumb.addEndpoint('elementId', endpointOptions);
Tip: use the three-argument addEndpoint method for common data
One thing that happens quite often is that you have an Endpoint whose appearance and behaviour is largely the same between usages on different elements, with just a few differences.var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true };
jsPlumb.addEndpoint('element1', { anchor:"BottomCenter" }, exampleGreyEndpointOptions)); jsPlumb.addEndpoint('element2', { anchor:"TopCenter" }, exampleGreyEndpointOptions));
Now that you have a source Endpoint, you need to either create a target Endpoint on some element, or notify jsPlumb that you wish to make an entire element a drop target. Let's look at how to attach a target Endpoint first:
var endpointOptions = { isTarget:true, endpoint:"Rectangle", paintStyle:{ fillStyle:"gray" } }; var endpoint = jsPlumb.addEndpoint("otherElementId", endpointOptions);
This Endpoint, a gray rectangle, has declared that it can act as a drop target for Connections.
jsPlumb also supports marking entire elements as drag sources or drag targets, using the makeSource and makeTarget methods. This is a useful function for certain applications. Each of these methods takes an Endpoint declaration which defines the Endpoint that jsPlumb will create after a Connection has been attached. Let's take a look at makeTarget first:
Elements as sources & targets
jsPlumb also supports turning entire elements into Connection sources and targets, using the methods makeSource and makeTarget. With these methods you mark an element as a source or target, and provide an Endpoint specification for jsPlumb to use when a Connection is established. makeSource also gives you the ability to mark some child element as the place from which you wish to drag Connections, but still have the Connection on the main element after it has been established.These methods honour the jsPlumb defaults - if, for example you set up the default Anchors to be this:
jsPlumb.Defaults.Anchors = [ "LeftMiddle", "BottomRight" ];
A further thing to note about makeSource and makeTarget is that from 1.3.7 onwards any prior calls to one of these methods is honoured by subsequent calls to jsPlumb.connect. This helps when you're building a UI that uses this functionality at runtime but which loads some initial data, and you want the statically loaded data to have the same appearance and behaviour as dynamically created connections (obviously quite a common use case).
For example:
jsPlumb.makeSource("el1", { anchor:"Continuous", endpoint:["Rectangle", { width:40, height:20 }] }); ... jsPlumb.connect({source:"el1", target:"el2"});
You can override this behaviour by setting a newConnection parameter on the connect call:
jsPlumb.makeSource("el1", { anchor:"Continuous", endpoint:["Rectangle", { width:40, height:20 }] }); ... jsPlumb.connect({ source:"el1", target:"el2", newConnection:true });
jsPlumb.makeTarget
This method takes two arguments, the first of which specifies some element (or list of elements); the second specifies the Endpoint you wish to create on that element whenever a Connection is established on it. In this example we will use the exact same target Endpoint we used before - the gray rectangle - but we will tell jsPlumb that the element "aTargetDiv" will be the drop target:
var endpointOptions = { isTarget:true, maxConnections:5, endpoint:"Rectangle", paintStyle:{ fillStyle:"gray" } }; jsPlumb.makeTarget("aTargetDiv", endpointOptions);
The allowed values in 'endpointOptions' are identical for both the jsPlumb.addEndpoint and jsPlumb.makeTarget methods, but makeTarget supports an extended anchor syntax that allows you more control over the location of the target endpoint. This is discussed below.
Notice in the 'endpointOptions' object above there is a 'isTarget' parameter set - this may seem incongruous, since you know you're going to make some element a target. Remember that the endpointOptions object is the information jsPlumb will use to create an Endpoint on the given target element each time a Connection is established to it. It takes the exact same format as you would pass to addEndpoint; makeTarget is essentially a deferred addEndpoint call followed by a connect call. So in this case, we're telling jsPlumb that any Endpoints it happens to create on some element that was configured by the makeTarget call are themselves Connection targets.
Unique Endpoint per Target
jsPlumb will create a new Endpoint using the supplied information every time a new Connection is established on the target element, by default, but you can override this behaviour and tell jsPlumb that it should create at most one Endpoint, which it should attempt to use for subsequent Connections:
var endpointOptions = { isTarget:true, uniqueEndpoint:true, endpoint:"Rectangle", paintStyle:{ fillStyle:"gray" } }; jsPlumb.makeTarget("aTargetDiv", endpointOptions);
Deleting Endpoints on Detach
By default, any Endpoints created using makeTarget have deleteEndpointsOnDetach set to true, which means that once all Connections to that Endpoint are removed, the Endpoint is deleted. You can override this by setting the flag to true on the makeTarget call:var endpointOptions = { isTarget:true, maxConnections:5, uniqueEndpoint:true, deleteEndpointsOnDetach:false, endpoint:"Rectangle", paintStyle:{ fillStyle:"gray" } }; jsPlumb.makeTarget("aTargetDiv", endpointOptions);
Target Anchors positions with makeTarget
When using the makeTarget method, jsPlumb allows you to provide a callback function to be used to determine the appropriate location of a target anchor for every new Connection dropped on the given target. It may be the case that you want to take some special action rather than just relying on one of the standard anchor mechanisms.
This is achieved through an extended anchor syntax (note that this syntax is not supported in the jsPlumb.addEndpoint method) that supplies a "positionFinder" to the anchor specification. jsPlumb provides two of these by default; you can register your own on jsPlumb and refer to them by name, or just supply a function. Here's a few examples:
Instruct jsPlumb to place the target anchor at the exact location at which the mouse button was released on the target element. Note that you tell jsPlumb the anchor is of type "Assign", and you then provide a "position" parameter, which can be the name of some position finder, or a position finder function itself:jsPlumb.makeTarget("someElement", { anchor:[ "Assign", { position:"Fixed" }] });
Fixed is one of the two default position finders provided by jsPlumb. The other is Grid:
jsPlumb.makeTarget("someElement", { anchor:[ "Assign", { position:"Grid", grid:[3,3] }] });
Supplying your own position finder
To supply your own position finder to jsPlumb you first need to create the callback function. First let's take a look at what the source code for the Grid position finder looks like:
function(eventOffset, elementOffset, elementSize, constructorParams) { var dx = eventOffset.left - elementOffset.left, dy = eventOffset.top - elementOffset.top, gx = elementSize[0] / (constructorParams.grid[0]), gy = elementSize[1] / (constructorParams.grid[1]), mx = Math.floor(dx / gx), my = Math.floor(dy / gy); return [ ((mx * gx) + (gx / 2)) / elementSize[0], ((my * gy) + (gy / 2)) / elementSize[1] ]; }
- eventOffset - Page left/top where the mouse button was released (a JS object containing left/top members like you get from a jQuery offset call)
- elementOffset - JS offset object containing offsets for the element on which the Connection is to be created
- elementSize - [width, height] array of the dimensions of the element on which the Connection is to be created
- constructorParams - the parameters that were passed to the Anchor's constructor. In the example given above, those parameters are 'position' and 'grid'; you can pass arbitrary parameters.
To make your own position finder you need to create a function that takes those four arguments and returns an [x, y] position for the anchor, for example:
jsPlumb.AnchorPositionFinders.MyFinder = function(dp, ep, es, params) { ... return [ some maths ]; };
jsPlumb.makeTarget("someElement", { anchor:[ "Assign", { position:"MyFinder", myCustomParameter:"foo", anInteger:5 }] });
jsPlumb.makeSource
There are two use cases supported by this method. The first is the case that you want to drag a Connection from the element itself and have an Endpoint attached to the element when a Connection is established. The second is a more specialised case: you want to drag a Connection from the element, but once the Connection is established you want jsPlumb to move it so that its source is on some other element. For an example of this second case, check out the State Machine Connectors demonstration.Here's an example code snippet for the basic use case of makeSource:
jsPlumb.makeSource(someDiv, { paintStyle:{ fillStyle:"yellow" }, endpoint:"Blank", anchor:"BottomCenter" });
Notice again that the second argument is the same as the second argument to an addEndpoint call. makeSource is, essentially, a type of addEndpoint call. In this example we have told jsPlumb that we will support dragging Connections directly from 'someDiv'. Whenever a Connection is established between 'someDiv' and some other element, jsPlumb assigns an Endpoint at BottomCenter of 'someDiv', fills it yellow, and sets that Endpoint as the newly created Connection's source.
Configuring an element to be an entire Connection source using makeSource means that the element cannnot itself be draggable. There would be no way for jsPlumb to distinguish between the user attempting to drag the element and attempting to drag a Connection from the element. If your UI works in such a way that that is acceptable then you'll be fine to use this method. But if you want to drag Connections from some element but also have the element be draggable itself, you might want to consider the second use case. First, a code snippet:
jsPlumb.makeSource("someConnectionSourceDiv", { paintStyle:{ fillStyle:"yellow" }, endpoint:"Blank", anchor:"BottomCenter", parent:"someDiv" });
The only difference here is the inclusion of the 'parent' parameter. It instructs jsPlumb to configure the source Endpoint on the element specified - in this case, "someDiv" (the value of this parameter may be a String id or a selector).
Again, I'd suggest taking a look at the State Machine Connectors demonstration for an example of this.
There are many things you can set in an Endpoint options object; for a thorough list see the API documentation for Endpoint.
Here's an example of specifying that you want an Arrow overlay halfway along any Connection dragged from this Endpoint:
var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true, connectorOverlays: [ [ "Arrow", { location:0.5 } ] ] };
var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true, connectorOverlays: [ [ "Arrow", { location:0.5 } ] ] anchor:[ "TopCenter","RightMiddle","BottomCenter","LeftMiddle" ] };
Drag Options
These are options that will be passed through to the supporting library's drag API. jsPlumb passes everything you supply here through, inserting wrapping functions if necessary for the various lifecycle events that jsPlumb needs to know about. So if, for example, you pass a function to be called when dragging starts, jsPlumb will wrap that function with a function that does what jsPlumb needs to do, then call yours.At the time of writing, jsPlumb supports jQuery, MooTools and YUI3, and each of those libraries uses different terminology. In addition, jQuery's API is more fully featured, providing easy support for setting the zIndex and opacity of elements being dragged, as well as the 'scope' for a drag/drop (allowing you to specify more than one type of drag/drop pair), and hover classes for when a draggable is on the move or over a droppable. If you're using jQuery you can of course just supply these values on the dragOptions; to make it easier, jsPlumb's MooTools and YUI3 adapters recognize these options and add appropriate callbacks for you.
Given that the options here are library-specific, and they are all well-documented, we're going to discuss just the three drag options that behave the same way in all (see below for hoverClass):
- opacity - the opacity of an element that is being dragged. Should be a fraction between 0 and 1 inclusive.
- zIndex - the zIndex of an element that is being dragged.
- scope - the scope of the draggable. can only be dropped on a droppable with the same scope. this is discussed below.
For more information about drag options, take a look at the jQuery, MooTools, or YUI3 docs.
NOTE: there is an issue in Chrome that affects the 'cursor' argument to drag options in jQuery. See these links:
http://forum.jquery.com/topic/draggable-cursor-option-does-not-work
http://forum.jquery.com/topic/chrome-text-select-cursor-on-drag
document.onselectstart = function () { return false; };
Drop Options
Drop options are treated by jsPlumb in the same way as drag options - they are passed through to the underlying library. MooTools does not have drop options like jQuery and YUI3 do; droppable functionality in MooTools is actually implemented by the Drag.Move class - the one used to initialise a draggable. But when you setup an Endpoint in jsPlumb you should ignore that fact, and treat droppables like you would in jQuery or YUI3. jsPlumb wires everything up for you under the hood.There are three jQuery droppable options that jsPlumb treats as shortcuts in MooTools and YUI3, for the sake of consistency:
- hoverClass - the CSS class to attach to the droppable when a draggable is hovering over it.
- activeClass - the CSS class to attach to the droppable when a draggable is, um, being dragged.
- scope - the scope of the draggable. The draggable can only be dropped on a droppable with the same scope. This is discussed below.
For more information about drop options when using jQuery, see here.
Drag and Drop Scope
jsPlumb borrowed the concept of 'scope' from jQuery's drag/drop implementation: the notion of which draggables can be dropped on which droppables. In jsPlumb you can provide a 'scope' entry when creating an Endpoint. Here's the example grey Endpoint example with 'scope' added:var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:"#666" }, isSource:true, connectionStyle : { strokeStyle:"#666" }, isTarget:true, scope:"exampleGreyConnection" };
jsPlumb.getDefaultScope();
jsPlumb.setDefaultScope("mySpecialDefaultScope");
var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectionStyle : { strokeStyle:"#666" }, isTarget:true, dragOptions:{ scope:"dragScope" }, dropOptions:{ scope:"dropScope" } };
Interceptors
Interceptors were new in jsPlumb 1.3.4. They are basically event handlers from which you can return a value that tells jsPlumb to abort what it is that it was doing. There are currently two interceptors supported - beforeDrop, which is called when the user has dropped a Connection onto some target, and beforeDetach, which is called when the user is attempting to detach a Connection.
Interceptors can be registered via the bind method on jsPlumb just like any other event listeners, and they can also be passed in to the jsPlumb.addEndpoint, jsPlumb.makeSource and jsPlumb.makeTarget methods. A future version of jsPlumb will support registering interceptors on Endpoints using the bind method as you can now with the jsPlumb object. Note that binding 'beforeDrop' (as an example) on jsPlumb itself is like a catch-all: it will be called every time a Connection is dropped on _any_ Endpoint. But passing a 'beforeDrop' callback into an Endpoint (and in the future, registering one using 'bind') constrains that callback to just the Endpoint in question.
Before Drop
This is called when the user has released the mouse button over a Connection target. Your callback is passed the a JS object containing these fields:- sourceId
- targetId
- scope
Returning false - or nothing - from this callback will cause the drag to be abandoned, and the new Connection removed from the UI.
Before Detach
This is called when the user has detached a Connection, which can happen for a number of reasons: by default, jsPlumb allows users to drag Connections off of target Endpoints, but this can also result from a programmatic 'detach' call. Every case is treated the same by jsPlumb, so in fact it is possible for you to write code that attempts to detach a Connection but then denies itself! You might want to be careful with that.Note that this interceptor is passed the actual Connection object; this is different from the beforeDrop interceptor discussed above: in this case, we've already got a Connection, but with beforeDrop we are yet to confirm that a Connection should be created.
Returning false - or nothing - from this callback will cause the detach to be abandoned, and the Connection will be reinstated/left on its current target.
Paint Styles
Defining the appearance of Connectors, Endpoints (and Overlays, but this is deprecated in favour of using CSS classes) is achieved through a 'paintStyle' (or a quite similar name...keep reading) object passed as a parameter to one of jsPlumb.connect, jsPlumb.addEndpoint, jsPlumb.makeSource or jsPlumb.makeTarget. Depending on the method you are calling, the parameter names vary.Connector Paint Styles
These are specified in a paintStyle parameter on a call to jsPlumb.connect:jsPlumb.connect({ source:"el1", target:"el2", paintStyle:{ strokeStyle:"blue", lineWidth:10 } });
jsPlumb.addEndpoint("el1", { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 } });
jsPlumb.makeSource("el1", { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 } });
Endpoint Paint Styles
These are specified in a paintStyle parameter on a call to addEndpoint. This is the example from just above:jsPlumb.addEndpoint("el1", { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 } });
jsPlumb.connect({ source:"el1", target:"el2", endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, paintStyle:{ strokeStyle:"blue", lineWidth:10 } });
jsPlumb.connect({ source:"el1", target:"el2", endpointStyles:[ { fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, { fillStyle:"green" } ], paintStyle:{ strokeStyle:"blue", lineWidth:10 } });
jsPlumb.makeTarget("el1", { ... paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, ... });
jsPlumb.makeSource("el1", { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 } parent:"someOtherDivIJustPutThisHereToRemindYouYouCanDoThis" });
Overlay Paint Styles
The preferred way to set paint styles for Overlays is to use the cssClass parameter in the constructor arguments of an Overlay definition.Paint Style Parameters
This is the full list of parameters you can set in a paintStyle object, but note that fillStyle is ignored by Connectors, and strokeStyle is ignored by Endpoints. Also, if you create a Connection using jsPlumb.connect and do not specify any Endpoint styles, the Endpoints will derive their fillStyle from the Connector's strokeStyle.fillStyle, strokeStyle and outlineColor can be specified using any valid CSS3 syntax.
- fillStyle - color for an Endpoint, eg. rgba(100,100,100,50), "blue", "#456", "#993355", rgb(34, 56, 78).
- strokeStyle - color for a Connector. see fillStyle examples.
- lineWidth - width of a Connector's line. An integer.
- outlineWidth - width of the outline for an Endpoint or Connector. An integer.
- outlineColor - color of the outline for an Endpoint or Connector. see fillStyle examples.
- dashstyle - VML and SVG only. This comes from VML, and allows you to create dashed or dotted lines. It has a
better syntax than the equivalent attribute in SVG (stroke-dasharray, discussed below), so jsPlumb supports this for both renderers.
The dashstyle attribute is specified as an array of strokes and spaces, where each value is some multiple of the width of
the Connector, and that's where it's better than SVG, which uses pixels.
The VML spec is a good place to find valid values for dashstyle. Note that jsPlumb does not support the string values for this attribute ("solid", "dashdot", etc).
In SVG render mode, jsPlumb uses the lineWidth parameter in conjunction with the values in a dashstyle attribute to create an appropriate value for stroke-dasharray.
- stroke-dasharray - SVG only. This is the SVG equivalent of dashstyle. The SVG spec discusses valid values for this parameter. But be aware that jsPlumb does not convert this into a valid dashstyle attribute when using the VML renderer. Better to use dashstyle.
- stroke-dashoffset - SVG only. This is used in SVG to specify how far into the dash pattern to start painting. For more information, see the SVG spec.
- joinstyle - VML and SVG only. As with dashstyle, this is a VML attribute that jsPlumb supports for both VML and SVG - jsPlumb turns this into a stroke-linejoin attribute when rendering with SVG. This attribute specifies how you want individual segments of connectors to be joined; the VML and SVG specs both have examples of this, of which many are the same between the two, which is why jsPlumb will automatically convert this attribute into the SVG equivalent.
- stroke-linejoin - SVG only. This is the equivalent of VML's joinstyle attribute, but as with stroke-dasharray, jsPlumb does not convert this into something approriate for VML. So, using joinstyle will enable you to support more browsers with less effort.
Hover Paint Styles
Connectors and Endpoints both support the concept of a "hover" paint style - a paint style to use when the mouse is hovering over the component. These are specified in the exact same format as paint styles discussed above, but hover paint styles also inherit any values from the main paint style. This is because you will typically want to just change the color, or perhaps outline color, of a Connector or Endpoint when the mouse is hovering, but leave everything else the same. So having hover paint styles inherit their values precludes you from having to define things in more than one place.The naming convention adopted for hover paint styles is pretty much to insert the word 'hover' into the corresponding main paint style parameters. Here are a couple of examples:
jsPlumb.connect({ source:"el1", target:"el2", paintStyle:{ strokeStyle:"blue", lineWidth:10 }, hoverPaintStyle:{ strokeStyle:"red" }, endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, endpointHoverStyle:{ fillStyle:"red" } });
jsPlumb.connect({ source:"el1", target:"el2", paintStyle:{ strokeStyle:"blue", lineWidth:10 }, hoverPaintStyle:{ strokeStyle:"red" }, endpointStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, endpointHoverStyles:[ { fillStyle:"red" }, { fillStyle:"yellow" } ] });
jsPlumb.addEndpoint("el1", { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, hoverPaintStyle:{ fillStyle:"red" }, connectorPaintStyle:{ strokeStyle:"blue", lineWidth:10 }, connectorHoverPaintStyle:{ strokeStyle:"red", outlineColor:"yellow", outlineWidth:1 } }); jsPlumb.makeSource("el2", { paintStyle:{ fillStyle:"transparent", outlineColor:"yellow", outlineWidth:1 }, hoverPaintStyle:{ fillStyle:"red" }, connectorPaintStyle:{ strokeStyle:"green", lineWidth:3 }, connectorHoverPaintStyle:{ strokeStyle:"#678", outlineColor:"yellow", outlineWidth:1 } }); jsPlumb.makeTarget("el3", { paintStyle:{ fillStyle:"transparent", outlineColor:"yellow", outlineWidth:1 }, hoverPaintStyle:{ fillStyle:"red" } });
Note that makeTarget does not support Connector parameters. It is for creating targets only; Connector parameters will be set by the source Endpoint in any Connections that are made to the element that you turned into a target by using this method.
Gradients
The Canvas and SVG renderers both support gradients. The VML renderer does not. jsPlumb uses its own syntax to define gradients, to abstract out the differences between the syntax required by canvas and that required by SVG.There are two types of gradients available - a 'linear' gradient, which consists of colored lines all going in one direction, and a 'radial' gradient, which consists of colored circles emanating from one circle to another. Because of their basic shape, jsPlumb supports only linear gradients for Connectors. But for Endpoints, jsPlumb supports both linear and radial gradients.
Connector gradients
To specify a linear gradient to use in a Connector, you must add a gradient object to your Connector's paintStyle, for instance:jsPlumb.connect({ source : "window2", target : "window3", paintStyle:{ gradient:{ stops:[[0,"green"], [1,"red"]] }, lineWidth:15 } });
Notice the gradient object and the stops list inside it - the gradient consists of an arbitrary number of these "color stops". Each color stop is comprised of two values - [position, color]. Position must be a decimal value between 0 and 1 (inclusive), and indicates where the color stop is situated as a fraction of the length of the entire gradient. Valid values for the colors in the stops list are the same as those that are valid for strokeStyle when describing a color.
As mentioned, the stops list can hold an arbitrary number of entries. Here's an example of a gradient that goes from red to blue to green, and back again through blue to red:jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle : { gradient:{ stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']] }, lineWidth : 15 } });
jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle:{ strokeStyle:'red', gradient:{ stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']] }, lineWidth:15 } });
Endpoint gradients
Endpoint gradients are specified using the same syntax as Connector gradients. You put the gradient specifier either in the endpoint member, or if you are specifying different Endpoints for each end of the Connector, in one or both of the values in the endpoints array. Also, this information applies to the case that you are creating standalone Endpoints that you will be configuring for drag and drop creation of new Connections.This is an example of an Endpoint gradient that is different for each Endpoint in the Connector. This comes from the main demo; it is the Connector joining Window 2 to Window 3:
var w23Stroke = 'rgb(189,11,11)'; jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle:{ lineWidth:8, strokeStyle:w23Stroke }, anchors:[ [0.3,1,0,1], "TopCenter" ], endpoint:"Rectangle", endpointStyles:[ { gradient : {stops:[[0, w23Stroke], [1, '#558822']] } }, { gradient : {stops:[[0, w23Stroke], [1, '#882255']] } } ] });
Applying the gradient in Endpoints
Only the Dot and Rectangle endpoints honour the presence of a gradient (and, remember, not in VML). The Image endpoint of course ignores a gradient as it does no painting of its own.The type of gradient you will see depends on the Endpoint type:
- Dot - renders a radial endpoint, with color stop 0 on the outside, progressing inwards as we move through color stops.
Radial gradients actually require more data than linear gradients - in a linear gradient we just move from one point to another, whereas in a radial gradient we move from one circle to another. By default, jsPlumb will render a radial gradient using a source circle of the same radius as the Endpoint itself, and a target circle of 1/3 of the radius of the Endpoint (both circles share the same center as the Endpoint itself). This circle will be offset by radius/2 in each direction.
You can supply your own values for these inside the gradient descriptor:
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect({ source : 'window3', target : 'window4', paintStyle:{ lineWidth:10, strokeStyle:w34Stroke }, anchors:[ "RightMiddle", "LeftMiddle" ], endpointStyle:{ gradient : { stops:[ [0, w34Stroke], [1, w34HlStroke] ], offset:37.5, innerRadius:40 }, radius:55 } });
It is also possible to specify the offset and inner radius as percentages - enter the values as strings with a '%' symbol on the end:
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect({ source : 'window3', target : 'window4', paintStyle:{ lineWidth:10, strokeStyle:w34Stroke }, anchors:[ "RightMiddle", "LeftMiddle" ], endpointStyle:{ gradient : { stops:[ [0, w34Stroke], [1, w34HlStroke] ], offset:'68%', innerRadius:'73%' }, radius:25 } });
- Rectangle - renders a linear endpoint, with color stop 0 closest to the end of the Connector
Styling SVG/VML using CSS
A nice way of controlling your UI's appearance is to make use of the fact that both SVG and VML can be styled using CSS.This section will be expanded in the next few weeks to give some decent examples of how to do so
CSS Class Reference
jsPlumb attaches classes to each of the UI components it creates.These class names are exposed on the jsPlumb object and can be overridden if you need to do so (see the third column in the table)
Component | CSS Class | jsPlumb Member |
---|---|---|
Connector | _jsPlumb_connector | connectorClass |
Endpoint | _jsPlumb_endpoint | endpointClass |
Overlay | _jsPlumb_overlay | overlayClass |
You would typically use these to establish appropriate z-indices for your UI.
Animation
jsPlumb offers an 'animate' function, which wraps the underlying animation engine for whichever library you happen to be using and inserts a callback for jsPlumb to repaint whatever it needs to at each step. You could of course do this yourself; it's a convenience method really.The method signature is:
jsPlumb.animate : function(el, properties, options)
- el - element id, or element object from the library you're using.
- properties - properties for the animation, such as duration etc.
- options - options for the animation, such as callbacks etc.
jQuery and the 'revert' option
jQuery offers a 'revert' option that you can use to instruct it to revert a drag under some condition. This is rendered in the UI as an animation that returns the drag object from its current location to wherever it started out. It's a nice feature. Unfortunately, the animation that runs the revert does not offer any lifecycle callback methods - no 'step', no 'complete' - so its not possible for jsPlumb to know that the revert animation is taking place.Retrieving Connection Information
There are two ways of retrieving connection information from jsPlumb. getConnectionsis the original method; from 1.3.7 onwards this method is supplemented by jsPlumb.select, a much more versatile variant.getConnections
Before you use this method you should understand jsPlumb's notion of 'scope' - documentation is here
Retrieving connections for a single scope
To do this, you call getConnections with either no arguments, in which case jsPlumb uses the default scope, or with a string specifying one scope
var connectionList = jsPlumb.getConnections(); // you get a list of Connection objects that are in the default scope.
var connectionList = jsPlumb.getConnections("myScope"); // you get a list of Connection objects that are in "myScope".
More advanced filtering
getConnections optionally takes a JS object specifying filter parameters, of which there are three:- scope - the scope(s) of the connection type(s) you wish to retrieve
- source - limits the returned connections to those that have this source id
- target - limits the returned connections to those that have this target id
The return value of a call to getConnection using a JS object as parameter varies on how many scopes you defined. If you defined only a single scope then jsPlumb returns you a list of Connections in that scope. Otherwise the return value is a dictionary whose keys are scope names, and whose values are lists of Connections. For example, the following call:
jsPlumb.getConnections({scope:["someScope", "someCustomScope"]});
{ "someScope" : [ 1..n Connections ], "someCustomScope": [ 1..m Connections ] }
jsPlumb.getConnections({scope:["someScope", "someCustomScope"]}, true);
[ 1..n Connections ]
- Get all connections:
var c = jsPlumb.getAllConnections();
- Get all connections for the default scope only (return value is a list):
var c = jsPlumb.getConnections();
- Get all connections for the given scope (return value is a list):
var c = jsPlumb.getConnections({scope:"myTestScope"});
- Get all connections for the given scopes (return value is a map of scope names to connection lists):
var c = jsPlumb.getConnections({scope:["myTestScope", "yourTestScope"]});
- Get all connections for the given source (return value is a map of scope names to connection lists):
var c = jsPlumb.getConnections({source:"mySourceElement"});
- Get all connections for the given sources (return value is a map of scope names to connection lists):
var c = jsPlumb.getConnections({source:["mySourceElement", "yourSourceElement"]});
- Get all connections for the given target (return value is a map of scope names to connection lists):
var c = jsPlumb.getConnections({target:"myTargetElement"});
- Get all connections for the given source and targets (return value is a map of scope names to connection lists):
var c = jsPlumb.getConnections({source:"mySourceElement", target:["target1", "target2"]});
- Get all connections for the given scope, with the given source and target (return value is a list of connections):
var c = jsPlumb.getConnections({scope:'myScope", source:"mySourceElement", target:"myTargetElement"});
Selecting Connections
Introduced in 1.3.7, jsPlumb.select provides a fluid interface for working with lists of Connections. The syntax used to specify which Connections you want is identical to that which you use for getConnections, but the return value is an object that supports most operations that you can perform on a Connection, and which is also chainable, for setter methods. Certain getter methods are also supported, but these are not chainable; they return an array consisting of all the Connections in the selection along with the return value for that Connection.This is the full list of setter operations supported by jsPlumb.select:
- addOverlay
- removeOverlay
- removeOverlays
- showOverlay
- hideOverlay
- showOverlays
- hideOverlays
- removeAllOverlays
- setLabel
- setPaintStyle
- setHoverPaintStyle
- setDetachable
- setConnector
- setParameter
- setParameters
- detach
- getLabel
- getOverlay
- isHover
- isDetachable
- getParameter
- getParameters
Some examples:
Select all Connections and set their hover state to be false:jsPlumb.select().setHover(false);
jsPlumb.select({source:"d1"}).removeAllOverlays();
jsPlumb.select({scope:"foo"}).setPaintStyle({ strokeStyle:"blue", lineWidth:5 });
jsPlumb.select({source:"d1"}).detach();
.each iterator
The return value of jsPlumb.select has a .each function that allows you to iterate through the list, performing an operation on each one:jsPlumb.select({scope:"foo"}).each(function(connection) { // do something });
jsPlumb.select({scope:"foo"}).each(function(connection) { // do something }).setHover(true);
Other properties/functions
- length - this member reports the number of Connections in the selection
- get(idx) - this function allows you to retrieve a Connection from the selection
Events
jsPlumb supports binding to several different events on Connections, Endpoints and Overlays, and also on the jsPlumb object itself.jsPlumb Events
To bind an event to jsPlumb itself, use jsPlumb.bind(event, callback):
jsPlumb.bind("jsPlumbConnection", function(info) { .. update your model in here, maybe. });
These are the events you can bind to on the jsPlumb class:
- jsPlumbConnection - notification a Connection was established.
The first argument to the callback is an object with the following properties:
{ connection : the new Connection. you can register listeners on this etc. sourceId : id of the source element in the Connection targetId : id of the target element in the Connection source : the source element in the Connection target : the target element in the Connection sourceEndpoint : the source Endpoint in the Connection targetEndpoint : the targetEndpoint in the Connection }
All of the source/target properties are actually available inside the Connection object, but - for one of those rubbish historical reasons - are provided separately because of a vagary of the jsPlumbConnectionDetached callback, which is discussed below.
- jsPlumbConnectionDetached - notification a Connection was detached.
As with jsPlumbConnection, the first argument to the callback is an object with the following properties:
{ connection : the Connection that was detached. sourceId : id of the source element in the Connection before it was detached targetId : id of the target element in the Connection before it was detached source : the source element in the Connection before it was detached target : the target element in the Connection before it was detached sourceEndpoint : the source Endpoint in the Connection before it was detached targetEndpoint : the targetEndpoint in the Connection before it was detached }
As mentioned above, the source/target properties are provided separately from the Connection, because this event is fired whenever a Connection is either detached and abandoned, or detached from some Endpoint and attached to another. In the latter case, the Connection that is passed to this callback is in an indeterminate state (that is, the Endpoints are still in the state they are in when dragging, and do not reflect static reality), and so the source/target properties give you the real story.
Like I said above, this is really just for rubbish historical reasons. I'm attempting to document my way out of it!
- click - notification a Connection was clicked. The callback is passed the Connection and the original mouse event.
- dblclick - notification a Connection was double-clicked. The callback is passed the Connection and the original mouse event.
- endpointClick - notification an Endpoint was clicked. The callback is passed the Endpoint and the original mouse event.
- endpointDblClick - notification an Endpoint was double-clicked. The callback is passed the Endpoint and the original mouse event.
- contextmenu - a right-click on some given component. jsPlumb will report right clicks on both Connections and Endpoints; the callback is passed the component that received the right-click.
- beforeDrop This event is fired when a new connection has been dropped. Your callback is passed a JS object with sourceId, targetId and scope in it. If you return false (or nothing) from this callback, the new Connection is aborted and removed from the UI.
- beforeDetach This event is fired when a Connection is about to be detached, for whatever reason. Your callback function is passed the Connection that jsPlumb has just created. Returning false from this interceptor aborts the Connection detach.
Connection Events
To bind to an event on a Connection, you also use the bind method:
var connection = jsPlumb.connect({source:"d1", target:"d2"}); connection.bind("click", function(conn) { console.log("you clicked on ", conn); });
These are the Connection events to which you can bind a listener:
- click - notification a Connection was clicked. The callback is passed the Connection and the original mouse event.
- dblclick - notification a Connection was double-clicked. The callback is passed the Connection and the original mouse event.
- contextmenu - a right-click on the Connection. The callback is passed the Connection and the original event.
- mouseenter - notification the mouse entered the Connection's path. The callback is passed the Connection and the original mouse event.
- mouseexit - notification the mouse exited the Connection's. The callback is passed the Connection and the original mouse event.
Endpoint Events
To bind to an event on a Endpoint, you again use the bind method:
var endpoint = jsPlumb.addEndpoint("d1", { someOptions } ); endpoint.bind("click", function(endpoint) { console.log("you clicked on ", endpoint); });
These are the Endpoint events to which you can bind a listener:
- click - notification an Endpoint was clicked. The callback is passed the Endpoint and the original mouse event.
- dblclick - notification an Endpoint was double-clicked. The callback is passed the Endpoint and the original mouse event.
- contextmenu - a right-click on the Endpoint. The callback is passed the Endpoint and the original event.
- mouseenter - notification the mouse entered the Endpoint. The callback is passed the Endpoint and the original mouse event.
- mouseexit - notification the mouse exited the Endpoint. The callback is passed the Endpoint and the original mouse event.
Overlay Events
Registering event listeners on an Overlay is a slightly different procedure - you provide them as arguments to the Overlay's constructor. This is because you never actually act on an 'Overlay' object. Here's how to register a click listener on an Overlay:jsPlumb.connect({ source:"el1", target:"el2", overlays:[ [ "Label", { events:{ click:function(labelOverlay, originalEvent) { alert("you clicked on the label overlay for this connection :" + labelOverlay.connection) } } }] ] });
jsPlumb.connect Examples
This section provides examples of how to use the programmatic API to establish Connections.The basic syntax of a call is that you execute 'connect', providing a source and a target, and optionally a paintStyle and preferences for where you want the plumbing to be anchored on each element, as well as the type of connector to use.
- Connect window1 to window2 with the default settings:
jsPlumb.connect({source:"window1", target:"window2"});
- Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint (remember the default Endpoint is a Dot):
jsPlumb.connect({ source:'window1', target:'window2', paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint:
jsPlumb.connect({ source:'window1', target:'window2', paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window3 to 'window4' with a 10 pixel wide, semi opaque blue Connector, anchored to the left middle of window3, and the right middle of window4, with a Rectangle endpoint of width 10 and height 8:
jsPlumb.connect({ source:'window3', target:'window4', paintStyle:{ lineWidth:10, strokeStyle:'rgba(0, 0, 200, 0.5)' }, anchors:["RightMiddle", "LeftMiddle"], endpoint:[ "Rectangle", { width:10, height:8 } ] });
- Connect window2 to window3 with a default Connector from the top center of window2 to the bottom center of window3, and rectangular endpoints:
jsPlumb.connect({ source:'window2', target:'window3', paintStyle:{lineWidth:8, strokeStyle:'rgb(189,11,11)'}, anchors:["BottomCenter", "TopCenter"], endpoint:"Rectangle" });
- Connect window1 to window2 with a 15 px wide yellow Bezier. endpoints are a slightly lighter shade of yellow.
jsPlumb.connect({ source:'window1', target:'window2', anchors:["BottomCenter", [0.75,0,0,-1]], paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window3 to window4 with a 10px wide blue-ish half transparent Bezier. put endpoints underneath the element they attach to.
the endpoints have a radial gradient. both ways of specifying gradient positioning are shown here.
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect( { source:'window3', target:'window4', paintStyle:{lineWidth:10, strokeStyle:w34Stroke}, anchors:["RightMiddle", "LeftMiddle"], endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:17.5, innerRadius:15 }, radius:35}, //endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:'78%', innerRadius:'73%'}, radius:35 }, endpointsOnTop:false } );
- Connect window2 to window3 with an 8px red Bezier and default rectangular endpoints. see also how the first anchor is
specified here - this is how you create anchors in locations jsPlumb does not offer shortcuts for.
the endpoints in this example have linear gradients applied.
var w23Stroke = 'rgb(189,11,11)'; jsPlumb.connect({ source:'window2', target:'window3', paintStyle:{lineWidth:8,strokeStyle:w23Stroke}, anchors:[[0.3,1,0,1], "TopCenter"], endpoint:"Rectangle", endpointStyles:[{ gradient : {stops:[[0, w23Stroke], [1, '#558822']] }}, { gradient : {stops:[[0, w23Stroke], [1, '#882255']] }}] });
- Connect window5 to window6 from center to center, 5px wide line that is green and half transparent. the endpoints are
125px in radius and spill out from underneath their elements.
jsPlumb.connect({ source:'window5', target:'window6', anchors:["Center", "Center"], paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'}, endpointStyle:{radius:125} });
- Connect window4 to window5 from bottom right to top left, with a 7px straight line purple connector, and an image as the endpoint,
placed on top of the element it is connected to.
jsPlumb.connect({ source:"window4", target:"window5", anchors:[ "BottomRight","TopLeft" ], paintStyle:{ lineWidth:7, strokeStyle:"rgb(131,8,135)" }, endpoint:[ "Image", { src:"http://morrisonpitt.com/jsPlumb/img/endpointTest1.png" } ], connector:"Straight" });
- Connect window5 to window6 between their center points with a semi-opaque connector, and 125px endpoints:
jsPlumb.connect({ source:"window5", target:"window6", anchors:[ "Center", "Center" ], paintStyle:{ lineWidth:5, strokeStyle:"rgba(0,255,0,0.5)" }, endpointStyle:{ radius:125 } });
- Connect window7 to window8 with a 10 pixel wide blue Connector, anchored on the top left of window7 and the bottom right of window8:
jsPlumb.connect({ source:"window7", target:"window8", paintStyle:{ lineWidth:10, strokeStyle:"blue" }, anchors:[ "TopLeft", "BottomRight" ] });
- Connect the bottom right corner of window4 to the top left corner of window5, with rectangular endpoints of size 40x40 and a hover color of light blue:
jsPlumb.connect({ source:"window4", target:"window5", anchors:["BottomRight","TopLeft"], paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'}, hoverPaintStyle:{ strokeStyle:"rgb(0, 0, 135)" }, endpointStyle:{ width:40, height:40 }, endpoint:"Rectangle", connector:"Straight" });
- Connect window1 to window2 with the default paint settings but provide some drag options (which are passed through to the underlying library's draggable call):
jsPlumb.connect({source:'window1', target:'window2', dragOptions:{cursor:'crosshair'}});
Draggable Connections Examples
This is a list of examples of how to use jsPlumb to create Connections using drag and drop.The basic procedure is:
- 1a. Create Endpoints and register them on elements in your UI
- 1b. alternatively, create a source endpoint and then make some element a drop target
- 2. Drag and Drop
- Define an Endpoint with default appearance, that is both a source and target of new Connections:
var endpointOptions = { isSource:true, isTarget:true };
- Register that Endpoint on window3, specifying that it should be located in the top center of the element:
var window3Endpoint = jsPlumb.addEndpoint('window3', { anchor:"TopCenter" }, endpointOptions );
- Now register that Endpoint on window4, specifying that it should be located in the bottom center of the element:
var window4Endpoint = jsPlumb.addEndpoint('window4', { anchor:"BottomCenter" }, endpointOptions );
- Connect window3 to window4 with a 25px wide yellow Bezier that has a 'curviness' of 175:
jsPlumb.connect({ source:window3Endpoint, target:window4Endpoint, connector: [ "Bezier", 175 ], paintStyle:{ lineWidth:25, strokeStyle:'yellow' } });
- Define an Endpoint that creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
and that 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 = { isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Define an Endpoint that will be anchored to "TopCenter". It creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
and that 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", isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Define an Endpoint that will create a dynamic anchor which can be positioned at "TopCenter" or "BottomCenter". 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 };
- Exactly the same as before, but shows how you can use "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 };
- Define an Endpoint that is a 30px blue dot, creates Connections that are 20px wide straight lines, is both a source and target of new Connections,
has a 'scope' of 'blueline', and has an event handler that pops up an alert (note: the event handler name means this example is jQuery - MooTools
and YUI3 use different event handler names):
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!'); } } };
- Same example as before, but this is for MooTools, and the Endpoint can support up to 5 connections (the default is 1):
var endpointOptions = { isSource:true, isTarget:true, endpoint: [ "Dot", { radius:30 } ], style:{ fillStyle:'blue' }, maxConnections:5, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dropOptions:{ onDrop:function(e, ui) { alert('drop!'); } } };
- Same example again, but 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:{ onDrop:function(e, ui) { alert('drop!'); } } };
- Same example again, but for YUI3. Note the drop callback is "drop:hit":
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:hit":function(e, ui) { alert('drop!'); } } };
- Assign a UUID to the endpoint options created above, and add as Endpoints to "window1" and "window2":
jsPlumb.addEndpoint("window1", { uuid:"abcdefg" }, endpointOptions ); jsPlumb.addEndpoint("window2", { uuid:"hijklmn" }, endpointOptions );
- Connect the two Endpoints we just registered on "window1" and "window2":
jsPlumb.connect({uuids:["abcdefg", "hijklmn"]});
- Create a source Endpoint, register it on some element, then make some other element a Connection target:
var sourceEndpoint = { isSource:true, endpoint:[ "Dot", { radius:50 } ] }; var targetEndpoint = { endpoint:[ "Rectangle", { width:10, height:10 } ] }; jsPlumb.addEndpoint( "window1", sourceEndpoint ); jsPlumb.makeTarget( "window2", targetEndpoint );
Utility Functions
- Detach window5 from all connections
jsPlumb.detachAll("window5");
- Hide all window5's connections
jsPlumb.hide("window5");
- Hide all window5's connections endpoints
jsPlumb.hide("window5", true);
- Show all window5's connections
jsPlumb.show("window5");
- Show all window5's connections and endpoints. Note that in the case that you call jsPlumb.show with two arguments, jsPlumb
will also not make a connection visible if it determines that the other endpoint in the connection is not visible.
jsPlumb.show("window5", hide);
- Toggle the visibility of window5's connections
jsPlumb.toggle("window5");
- Force repaint of all of window5's connections
jsPlumb.repaint("window5");
- Force repaint of all of window5, window6 and window11's connections
jsPlumb.repaint( [ "window5", "window6", "window11" ] );
- Force repaint of every connection
jsPlumb.repaintEverything();
- Detach every connection that the given instance of jsPlumb is managing
jsPlumb.detachEveryConnection();
- Detach all connections from "window1"
jsPlumb.detachAllConnections("window1");
- Remove the given Endpoint from element "window1", deleting its Connections.
jsPlumb.removeEndpoint("window1", someEndpoint);
- Remove all Endpoints for the element 'window1', deleting their Connections.
jsPlumb.removeAllEndpoints("window1");
- Removes every Endpoint managed by this instance of jsPlumb, deleting all Connections.
This is the same as jsPlumb.reset(), effectively, but it does not clear out the event listeners list.
jsPlumb.removeEveryEndpoint();
- Deletes the given Endpoint and all its Connections.
jsPlumb.deleteEndpoint(endpoint);
- Removes every endpoint, detaches every connection, and clears the event listeners list. Returns jsPlumb instance to its initial state.
jsPlumb.reset();
- Library-agnostic method to get a selector from the given selector specification. This function is used mostly by the jsPlumb demos, to cut down on repeating code across the different libraries. But perhaps you are writing something with jsPlumb that you also wisht to be library agnostic - this function can help you. The returned object, in each of jQuery, MooTools and YUI, has a ".each" iterator.
var selector = jsPlumb.getSelector(".someSelector .spec");
- Set window1 to be not draggable, no matter what some jsPlumb command may request.
jsPlumb.setDraggable("window1", false);
- Set window1 and window2 to be not draggable, no matter what some jsPlumb command may request.
jsPlumb.setDraggable(["window1","window2"], false);
- Sets whether or not elements that are connected are draggable by default.
The default for this is true.
jsPlumb.setDraggableByDefault(false);
- Initialises window1 as a draggable element (all libraries). Passes in an on drag callback
jsPlumb.draggable("window1");
- Initialises window1 and window2 as draggable elements (all libraries)
jsPlumb.draggable(["window1","window2"]);
- Initialises window1 as a draggable element (all libraries)
jsPlumb.draggable("window1");
- Initialises all elements with class 'window' as draggable elements (jQuery)
jsPlumb.draggable($(".window"));
- Initialises all elements with class 'window' as draggable elements (MooTools)
jsPlumb.draggable($$(".window"));
- Initialises all elements with class 'window' as draggable elements (YIU3)
jsPlumb.draggable(Y.all(".window"));
- Initialises window1 as a draggable element (jQuery)
jsPlumb.draggable($("#window1"));
- Initialises window1 as a draggable element (MooTools)
jsPlumb.draggable($("window1"));
- Initialises window1 as a draggable element (YUI3)
jsPlumb.draggable(Y.one("window1"));
Advanced Topics
Which files are which?
In development, jsPlumb is broken up into seven scripts:- - jsPlumb-x.x.x.js
This is the main jsPlumb engine.
- - jsPlumb-defaults-x.x.x.js
This contains the default Anchor, Endpoint, Connector and Overlay implementations.
- - jsPlumb-renderers-canvas-x.x.x.js
This contains the HTML5 canvas render code.
- - jsPlumb-renderers-svg-x.x.x.js
This contains the SVG render code.
- - jsPlumb-renderers-vml-x.x.x.js
This contains the VML render code.
- - jsPlumb-connectors-statemachine-x.x.x.js
This State Machine connector.
- - <LIBRARY_PREFIX>.jsPlumb-x.x.x.js
This contains library-specific helper methods. jsPlumb ships with three of these - one each for jQuery, MooTools and YUI3. See below for information on how to create a new library implementation.
- - jsBezier-0.3-min.js
These are the Bezier curve functions; they are maintained in a separate project called jsBezier
- jquery.jsPlumb-1.3.7-all.js
Contains jsPlumb-1.3.7.js, jsPlumb-defaults-1.3.7.js, jsPlumb-renderers-canvas-1.3.7.js, jsPlumb-renderers-svg-1.3.7.js, jsPlumb-renderers-vml-1.3.7.js, jsPlumb-connectors-statemachine-1.3.7.js, jquery.jsPlumb-1.3.7.js and jsBezier-0.3-min.js
- jquery.jsPlumb-1.3.7-all-min.js
A minified version of the script above (minified using the YUI Compressor)
Pluggable Library Support
Out of the box, jsPlumb can be run on top of jQuery, MooTools or YUI3. This is achieved by delegating several core methods - tasks such as finding an element by id, finding an element's position or dimensions, initialising a draggable, etc - to the library in question.To develop one of these, your test page should include the first two scripts discussed above, and then your own script containing your library specific functionality. The existing implementations may be documented well enough for you to create your own, but contact me if you need assistance. If you do this, it would be great to share it with everyone...
There is a group of people working on an ExtJS adapter currently. There is no schedule for when this will be completed though.