What’s new in jsPlumb 1.2.6

jsPlumb 1.2.6 was released today, with several bugfixes, an improved syntax for specifying Endpoints, Connectors and Overlays, mouse events when using Canvas, Flowchart Connectors, and a couple of other new pieces of functionality.  Here’s a run-down.

Shorthand Syntax for Endpoints, Connectors and Overlays

In previous versions of jsPlumb you may have got used to writing code like this:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  connector:new jsPlumb.Connectors.Bezier(68),
  endpoints:[ new jsPlumb.Endpoints.Dot({radius:12}),
              new jsPlumb.Endpoints.Rectangle({width:20, height:30}) 
  ]
});

That’s a lot of typing. 1.2.6 introduces a shorthand syntax designed to reduce the incidence of RSI amongst jsPlumb users. The equivalent code now looks like this:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  connector:["Bezier",68],
  endpoints:[["Dot",{radius:12}], ["Rectangle",{width:20,height:30}]]
});

If you are happy using the default parameter values for some component, you can use an even terser syntax:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  connector:"Bezier",
  endpoints:[ "Dot", "Rectangle" ]
});

The same syntax can be used for Overlays. For instance, you used to write something like:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  overlays:[
   new jsPlumb.Overlays.Arrow({location:0.1, width:10}),
   new jsPlumb.Overlays.Label({label:"foo", location:0.6})
  ]
});

which now would look like this:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  overlays:[
   [ "Arrow", {location:0.1, width:10} ],
   [ "Label", {label:"foo", location:0.6} ]
  ]
});

The same terser syntax applies to Overlays, if you are happy to be using the defaults, as it does for Endpoints and Connectors.

Flowchart Connectors

These are what the name suggests: connectors that consist of a series of horizontal or vertical segments, as you’d see on a flowchart. They support one parameter, minStubLength, which tells jsPlumb what the shortest acceptable value is for the stub that begins each connection.

To use these, taking the example code from above, you’d do this:

jsPlumb.connect({
  source:"someDiv",
  target:"someOtherDiv",
  connector:[ "Flowchart", {minStubLength:40} ],
  endpoints:[ "Dot", "Rectangle" ]
});

Note that here I have set the ‘minStubLength’ argument to tell jsPlumb the stubs need to be at least 40px long. This will make more sense if you checkout the demo.

Mouse Events

1.2.6 introduces support for the following mouse events, but only when you’re using Canvas, meaning not in IE prior to version 9.  The next release of jsPlumb will support mouse events for all browsers.

Handlers for these events can be registered on Endpoints and Connectors, using the bind method:

var connection = jsPlumb.connect(...some options...);
connection.bind("click", function(c) {
  console.log("c is a Connection object.  it connects " + c.sourceId + " to " + c.targetId);
});

As I was about to release 1.2.6 I realised, from using the API myself, that it would probably be more convenient to be able to bind a click handler on jsPlumb itself, to be notified of click events on any Endpoint or Connector. This functionality will be included in the next jsPlumb release.

This new functionality allows jsPlumb to support ‘hover’ styles for Endpoints and Connections - just like the CSS :hover pseudo class. For example:

jsPlumb.connect({
  source:"someDiv", 
  target:"someOtherDiv",
  paintStyle:{strokeStyle:"blue", lineWidth:5},
  hoverPaintStyle:{strokeStyle:"red", lineWidth:7}
});

Acceptable arguments for ‘hoverPaintStyle’ are whatever is acceptable for ‘paintStyle’.

Miscellaneous bits & pieces

  1. jsplumb posted this