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.3 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 10.04
- Safari 4 on Mac Tiger
- Safari 4 on Windows Vista
- Safari 5.0.5 on Windows 7
- Opera 10.54 on Windows XP
Changes since version 1.3.1
Version 1.3.3 contains a few new methods for working with Overlays, and a couple of bug fixes. Since a few 1.3.x versions have been released in a short amount of time, I've left in the notes from 1.3.1, as there are a few backwards compatibility issues you may need to know about.
New Features
- added getOverlay(id) to Connection
- added hideOverlay(id) to Connection
- added showOverlay(id) to Connection
- added hide() to Overlay
- added show() to Overlay
- added setVisible(state) to Overlay
Issues Resolved
- 120 - dynamic anchors get confused by scrolled div
- 127 - original event was lost in the jsPlumb shortcut bind methods
Version 1.3.1 was a major new release of jsPlumb, containing several important new features, and a few changes that break backwards compatibility.
Major new features include:
- - introduced support for using SVG
- - removed dependency on Excanvas script for IE<9. Jsplumb draws VML directly now.
- - introduced support for mouse events (click/doubleclick/enter/exit) for all browsers
- - introduced support for turning an entire element into a drag target for new connections
Backwards Compatibility Issues:
-
- you cannot create Endpoints/Connectors/Overlays directly anymore. You must supply jsPlumb with a definition of what you want, and it creates the actual object. For example:
OLD: jsPlumb.Defaults.Endpoint = new jsPlumb.Endpoints.Dot(45); NEW: jsPlumb.Defaults.Endpoint = [ "Dot", { radius:45 } ];
-
- Bezier connection requires a js object as argument now, instead of a single int
OLD: connector: [ "Bezier", 150 ] NEW: connector: [ "Bezier", { curviness:150 } ]
-
- Dot Endpoint requires a js object as argument now, instead of a single int
OLD: endpoint: [ "Dot", 10 ] NEW: endpoint: [ "Dot", { radius:10 } ]
-
- getConnections has been reworked a little to make it easier to use. See the API documentation.
-
- The 'container' concept, as an argument to addEndpoint or connect, and in the jsPlumb defaults, has been removed. Created elements are now appended to the parent of the source element of a connection, or the parent of an element to which you are adding an Endpoint.
-
- The 'backgroundPaintStyle' concept has been replaced with 'outlineColor' and 'outlineWidth' members in paint style definitions. To use these, you must supply at least 'outlineWidth'; if 'outlineColor' is not supplied, jsPlumb will use black by default.
-
- Functionality to drag an entire connection, by clicking and dragging the connector line, which was introduced for some browsers in 1.2.6, has been removed. I had several requests for a way to disable this, and got the distinct impression it was not very popular.
-
- The 'setDefaultNewCanvasSize' method on jsPlumb has been removed. jsPlumb no longer uses excanvas to render VML; this method was there to support edge cases of VML usage. You probably were not even aware of this method's existence.
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.6.0/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.3-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.3-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.3-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.CANVAS as the render mode, falling back to jsPlumb.VML if the browser is IE<9. You can change the render mode by making this call:
jsPlumb.setRenderMode(jsPlumb.SVG);
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
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 three types of these available as defaults - a Bezier curve, a straight line, and 'flowchart' 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. 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 ], ... });
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 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 ] ];
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.
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" }; 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 }, 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 three connector implementations - a straight line, a Bezier curve, and "flowchart" connectors. The default connector is the Bezier curve.
You optionally specify a Connector by setting the 'connector' property on a call to jsPlumb.connect or jsPlumb.addEndpoint(s). 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, we refer you to Wikipedia.
Straight Connector
The Straight Connector draws a straight line between the two endpoints. No constructor arguments are supported; use the endpointStyle argument to a connect or addEndpoint 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.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 three Endpoint implementations - Dot, Rectangle and Image. You optionally specify Endpoint properties using the endpoint parameter in a call to jsPlumb.connect, jsPlumb.addEndpoint or jsPlumb.makeTarget.
As with Connectors and Overlays, you specify Endpoints using the syntax described above in Connector, Endpoint & Overlay Definitions.
The three available Endpoint types, and their constructor arguments, are as follows:Dot Endpoint
This Endpoint draw a dot on the screen. It supports a single constructor parameter:radius - Optional; defaults to 10 pixels. Defines the radius of the dot.
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.
Image Endpoint
Draws an image from a given URL. This Endpoint supports one constructor parameter:src - Required. Specifies the URL of the image to use.
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.
You can specify one or more overlays when making a call to jsPlumb.connect or jsPlumb.addEndpoint (but not jsPlumb.makeTarget; overlays are always derived from what the source of a Connection defines) The two 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' will one day be used to support Endpoint Overlays:
jsPlumb.addEndpoint("someDiv", { ... 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.
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 also has 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 also has 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 ], AppendElementsToBody : false, Connector : "Bezier", DragOptions : { }, DropOptions : { }, Endpoint : "Dot", 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 : "canvas", 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
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);
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.
Your other option for creating a drag and drop target is to make an entire element a drop target, using the jsPlumb.makeTarget method. 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, 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.
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", painStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectionStyle : { strokeStyle:"#666" }, isTarget:true, dragOptions:{ scope:"dragScope" }, dropOptions:{ scope:"dropScope" } };
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 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({ source:"el1", target:"el2", 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 jsPlumb.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", { endpoint: { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 } } });
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.makeTarget also supports hover paint styles - here's the example from before, with one applied:
jsPlumb.makeTarget("el1", { endpoint: { paintStyle:{ fillStyle:"blue", outlineColor:"black", outlineWidth:1 }, hoverPaintStyle:{ fillStyle:"red" } } });
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
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.
Retrieving Connection Information
jsPlumb offers one fairly versatile method - getConnections - to retrieve information about the currently managed connections.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 ] }
- 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"});
Events
jsPlumb supports binding to several different events, both on Connections, Endpoints and Overlays, and also on the jsPlumb object itself. To bind events on any of these objects you use the jsPlumb.bind(object, event, callback) method, except in the case of Overlays, in which you provide the event listeners in the Overlay's constructor arguments (example below).jsPlumb Events
- jsPlumbConnection - notification a Connection was established. The callback is passed the new Connection as argument.
- jsPlumbConnectionDetached - notification a Connection was detached. The callback is passed the detached Connection as argument.
- 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.
Connection Events
- 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.
- 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
- 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.
- 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
jsPlumb.detachEverything();
- 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();
- 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)
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.
- - <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.2-min.js
These are the Bezier curve functions; they are maintained in a separate project called jsBezier
- jquery.jsPlumb-1.3.3-all.js
Contains jsPlumb-1.3.3.js, jsPlumb-defaults-1.3.3.js, jsPlumb-renderers-canvas-1.3.3.js, jsPlumb-renderers-svg-1.3.3.js, jsPlumb-renderers-vml-1.3.3.js, jquery.jsPlumb-1.3.3.js and jsBezier-0.2-min.js
- jquery.jsPlumb-1.3.3-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...