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.5 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.5 and 1.3.4
1.3.5 is mostly a performance and bugfix release, with a couple of new features. For that reason, I've left the list of changes between 1.3.4 and 1.3.3 in this file.
Backwards Compatibility
- The dynamicAnchors parameter as an argument to addEndpoint or connect calls has been removed. Just use anchor or anchors instead; jsPlumb will figure out you need Dynamic Anchors if you supply an array of possible locations
- jsPlumb.Defaults.Overlays is now applied to both Connections and Endpoints. This is because Endpoints now support Label overlays (see below). You should update your code to set jsPlumb.Defaults.ConnectionOverlays - unless you want your overlays to appear on Connections and Endpoints.
New Features
The following new features have been added:- support for the contextmenu event has been added to the jsPlumb object, Connections and Endpoints. See Events for more information.
- A new method, setSuspendDrawing, was added to jsPlumb. This allows you to instruct jsPlumb to not paint anything until you call this method again; it is useful for situations where you are adding a lot of connections at once but don't need to see them painted until everything has been added
- A new method, setHoverSuspended, was added to jsPlumb. This method allows you to instruct jsPlumb to not post hover events until you tell it to again. It can be useful when you're working with a lot of connections, because the occasional stray mouse event can creep through and cause your UI to flash like a 1970s disco.
- isHoverSuspended was also added.
- setLabel was re-implemented, properly this time. This is available on both Connections and Endpoints.
- getLabel was also re-implemented, correctly.
- labelLocation was added as a parameter on the setLabel function, for both Connections and Endpoints. For Connections the allowed values are a decimal between 0 and 1 inclusive, for Endpoints the allowed value is an array of two of these decimals, one for each axis
- Support for label overlays on Endpoints was added
- jsPlumb.Defaults.ConnectionOverlays default setting added
- jsPlumb.Defaults.EndpointOverlays default setting added
- Flowchart connectors now support the loopback case (in fact they received an overhaul in 1.3.5 and work better in all situations now)
Issues Resolved
- 169 - continuous anchors not tracking when existing connection dragged
- 173 - getCachedData method was effectively being bypassed
- 187 - flowchart connectors not symmetric
- 187 - dynamic anchors broken for Endpoints
- 189 - Endpoints draggable when they should not be
- 190 - getInstance regressions
- 191 - offsets calculated wrongly in certain situations
Changes between version 1.3.4 and 1.3.3
Version 1.3.4 was a fairly significant release, containing several bugfixes relating to positioning when elements with scrollbars or 'overflow:auto' are involved, a new type of connector, "Continuous" anchors, which are like Dynamic anchors on steroids, beforeDrag/beforeDrop interceptors, and a bunch of other bits and pieces such as the ability to make an entire div a connection source or target.
Backwards Compatibility
- Starting with this release, jsPlumb no longer automatically initialises any elements to be
draggable.
I thought about this long and hard and decided that it was too invasive for jsPlumb to assume that you wanted every connected element to be draggable. There were other complications, too, such as the fact that dragging was not initialised until you had at least one connection to an element - you could start out with a UI in which some elements were draggable, and others were not, and there did not seem, to the user, to be a decent reason for that.
You should use the jsPlumb.draggable(some selector) method to initialise elements as draggable now. It is still imperative that you use this method and not your underlying library's drag method, because otherwise jsPlumb will not know what's going on. Valid values for that selector are whatever your underlying library can support: $("someSelector") for jQuery, $$("someSelector") for MooTools, and Y.all("someSelector") for YUI3.
- The default renderer is now SVG, where supported, and VML elsewhere.
I'm of the opinion that SVG is a better option for almost every application using jsPlumb: it uses far less memory, has native mouse event support so is faster, and can be styled using CSS.
If you really want to use Canvas, call jsPlumb.setRenderMode(jsPlumb.CANVAS);.
Be aware that there is a jQuery bug with SVG in IE9 for all jQuery version from 1.6.x to 1.7.2: the hover events are not properly reported.
- The detachEverything method, which was deprecated in 1.3.2, has been removed from jsPlumb. Use detachEveryConnection instead. It does the same thing.
- The detachAll method, which was deprecated in 1.3.2, has been removed. Use detachAllConnections instead. It does the same thing.
- The labelStyle parameter has been removed from the Label overlay. You should use the cssClass parameter to set a class, and then control the appearance in your stylesheets.
New Features
-
StateMachine Connectors
These connectors look like the state machine connectors you might have seen in software like GraphViz. Support for "loopback" connections is included.
-
Continuous Anchors
Continuous anchors are anchors whose position is dynamically dependent on every connection that is attached to some element. If you're familiar with the existing Dynamic anchors in jsPlumb, these operate in pretty much the same way, with the exception that you do not need to nominate any specific locations yourself. They choose their position based on an element's orientation to the other element in a connection, and everything is ordered to keep the number of crossed connections to a minimum.
For more information, see the anchors section.
-
beforeDrag/beforeDrop interceptors
These interceptors allow you to decide whether or not to allow a new connection to be dragged or dropped before jsPlumb goes ahead with the action. They are documented in full in the interceptors section of the documentation.
-
makeSource/makeTarget methods
makeSource and makeTarget provide a means for you to nominate some entire element as a connection source or target. There are many applications in which this functionality makes more sense than having to register a dedicated Endpoint. Read more about these in the connections section below. 'transparent' is now supported as a fillStyle or strokeStyle.
Endpoint and Connector definitions now support a hoverClass parameter. This is the hover equivalent of the existing cssClass parameter.
The Image Endpoint now has an onload parameter, allowing you to supply a callback to run once the image has loaded
The Image Endpoint now has a setImage method, which also takes an optional onload parameter. See the API docs.
The Image Endpoint now has optional width and height parameters, allowing you to override the displayed size of the endpoint.
You can supply a parameters object to connect, addEndpoint, makeSource and makeTarget calls. This specifies an object literal of parameter values which will be present on resulting connections, available via the getParameter(String)method
Bugfixes for which there were not issues raised
- A mouse event bug with the SVG renderer was fixed; it was preventing mouse events from reaching SVG elements that were underneath other SVG elements
Issues Resolved
- 159 - do not add methods to the Array prototype
- 158 - use enclosing table element if an Endpoint is added to a table cell.
- 150 - hover over one Endpoint does not send the event to the other Endpoint in a Connection
- 145 - add getLabel/setLabel methods to Overlays
- 142 - document jsPlumb.connectionDetached event properly
- 139 - duplicate ids are sometimes given to Endpoints and Connections
- 132 - moving a Label Overlay does not remove its associated element
- 130 - positioning issues when in div with overflow:scroll
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.5-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.5-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.5-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 ], ... });
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 ] ];
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.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, 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.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, in which case you get a circle. These are the only Connectors for which the loopback case is supported. 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. However, a div element is added to the UI and so this Endpoint also support the css parameters: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.
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.
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 ], 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
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:
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, 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.
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", { endpoint:{ anchor:[ "Assign", { position:"Fixed" }] } });
Fixed is one of the two default position finders provided by jsPlumb. The other is Grid:
jsPlumb.makeTarget("someElement", { endpoint:{ anchor:[ "Assign", { position:"Grid", grid:[3,3] }] } });
To supply your own position finder to jsPlumb you first need to create the callback function. It takes N arguments and does stuff. This paragraph obviously needs to be finished.
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 WHERE IS THIS State Machine Connectors DemoHere'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", painStyle:{ 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 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 } } });
jsPlumb.makeSource("el1", { endpoint: { 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.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
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
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 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 callback is passed 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 callback is passed 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.
- beforeDrag This event is fired when a new connection is about to be dragged. Your callback function is passed the Connection that jsPlumb has just created. If you return false (or nothing) from this callback, the new Connection is aborted.
- beforeDrop This event is fired when a new connection has been dropped. Your callback function is passed the Connection that jsPlumb has just created. If you return false (or nothing) from this callback, the new Connection is aborted and removed from the UI.
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
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.
- - 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.5-all.js
Contains jsPlumb-1.3.5.js, jsPlumb-defaults-1.3.5.js, jsPlumb-renderers-canvas-1.3.5.js, jsPlumb-renderers-svg-1.3.5.js, jsPlumb-renderers-vml-1.3.5.js, jsPlumb-connectors-statemachine-1.3.5.js, jquery.jsPlumb-1.3.5.js and jsBezier-0.3-min.js
- jquery.jsPlumb-1.3.5-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.