Skip to main content

Querying jsPlumb

Connections#

Selecting and operating on a list of connections#

The select function provides a fluid interface for working with lists of Connections. The syntax used to specify which connections you want is identical to that which you use for getConnections, but the return value is an object that supports most operations that you can perform on a connection, and which is also chainable, for setter methods. Certain getter methods are also supported, but these are not chainable; they return an array consisting of all the connections in the selection along with the return value for that connection.

Setter Operations#

This is the full list of setter operations supported by select(..):

  • addClass
  • removeClass
  • addOverlay
  • removeOverlay
  • removeOverlays
  • showOverlay
  • hideOverlay
  • showOverlays
  • hideOverlays
  • removeAllOverlays
  • setLabel
  • setPaintStyle
  • setHoverPaintStyle
  • setDetachable
  • setReattach
  • setConnector
  • setParameter
  • setParameters
  • detach
  • repaint
  • setType
  • addType
  • removeType
  • toggleType
  • setVisible

Each of these operations returns a selector that can be chained.

Getter Operations#

This is the full list of getter operations supported by select(..):

  • getLabel
  • getOverlay
  • isHover
  • isVisible
  • isDetachable
  • isReattach
  • getParameter
  • getParameters
  • getType
  • hasType

Each of these operations returns an array whose entries are [ value, Connection ] arrays, where value is the return value from the given connection. Remember that the return values from a getter are not chainable, but a getter may be called at the end of a chain of setters.

Select examples#

  • Select all connections and set their hover state to be false:
instance.select().setHover(false)
  • Select all connections from "d1" and remove all overlays:
instance.select({source:"d1"}).removeAllOverlays()
  • Select all connections in scope "foo" and set their paint style to be a thick blue line:
instance.select({scope:"foo"}).setPaintStyle({        stroke:"blue",         strokeWidth:5 });
  • Select all connections from "d1" and delete them:
instance.select({source:"d1"}).deleteAll()
  • Select all connections and add the classes "foo" and "bar" to them :
instance.select().addClass("foo bar")
  • Select all connections and remove the class "foo" from them :
instance.select().removeClass("foo")

Iterating through results#

The return value of select(..) has a .each function that allows you to iterate through the list, performing an operation on each one:

instance.select({scope:"foo"}).each((connection) => {        // do something });

.each is itself chainable:

instance.select({scope:"foo"}).each((connection) => {           // do something }).setHover(true);

Other properties/functions#

  • length - this member reports the number of connections in the selection
  • get(idx) - this function allows you to retrieve a connection from the selection

Retrieving static connection lists#

As well as the select method, jsPlumb has another method that can be used to retrieve static lists of connections - getConnections. The return value of this method is not chainable and does not offer operations on the connections in the way that the return value of select does.

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

const connectionList = instance.getConnections(); // you get a list of connection objects that are in the default scope.

Compare this with:

const connectionList = instance.getConnections("myScope");     // you get a list of connection objects that are in "myScope".

Filtering by source, target and/or scope#

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

Each of these three parameters may be supplied as a string, which for source and target is an element id and for scope is the name of the scope, or a list of strings. Also, you can pass "*" in as the value for any of these - a wildcard, meaning any value. See the examples below.

Important: 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:

instance.getConnections({  scope:["someScope", "someCustomScope"]});

would result in this output:

{  "someScope" : [ 1..n Connections ],  "someCustomScope": [ 1..m Connections ]}

There is an optional second parameter that tells getConnections to flatten its output and just return you an array. The previous example with this parameter would look like this:

instance.getConnections({  scope:["someScope", "someCustomScope"]}, true);

...and would result in this output:

[ 1..n Connections ]

The following examples show the various ways you can get connection information:

  • Get all connections:
instance.getAllConnections();
  • Get all connections for the default scope only(return value is a list):
instance.getConnections();
  • Get all connections for the given scope (return value is a list):
instance.getConnections({scope:"myTestScope"});
  • Get all connections for the given scopes (return value is a list if only connections for the default scope exist, otherwise its a map of scope names to connection lists):
instance.getConnections({    scope:["myTestScope", "yourTestScope"]});
  • Get all connections for the given source (return value is a list if only connections for the default scope exist, otherwise its a map of scope names to connection lists):
instance.getConnections({    source:"mySourceElement"});
  • Get all connections for the given sources (return value is a list if only connections for the default scope exist, otherwise its a map of scope names to connection lists):
instance.getConnections({    source:["mySourceElement", "yourSourceElement"]});
  • Get all connections for the given target (return value is a list if only connections for the default scope exist, otherwise its a map of scope names to connection lists):
instance.getConnections({    target:"myTargetElement"});
  • Get all connections for the given source and targets (return value is a list if only connections for the default scope exist, otherwise its a map of scope names to connection lists):
instance.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):
instance.getConnections({        scope:'myScope",         source:"mySourceElement",         target:"myTargetElement"});

Selecting and operating on a list of endpoints#

selectEndpoints(..) provides a fluid interface for working with lists of endpoints.

The syntax used to specify which Endpoints you want is identical to that which you use for select(..), and the return value is an object that supports most operations that you can perform on an endpoint (and which is also chainable, for setter methods). Certain getter methods are also supported, but these are not chainable; they return an array consisting of all the endpoints in the selection along with the return value for that endpoint.

Four parameters are supported byselectEndpoints - each of these except scope can be provided as either a string, a selector, a DOM element, or an array of a mixture of these types. scope can be provided as either a string or an array of strings:

  • element - element(s) to get both source and target endpoints from
  • source - element(s) to get source endpoints from
  • target - element(s) to get target endpoints from
  • scope - scope(s) for endpoints to retrieve

Setter Operations#

This is the full list of setter operations supported by selectEndpoints(..):

  • addClass
  • removeClass
  • addOverlay
  • removeOverlay
  • removeOverlays
  • showOverlay
  • hideOverlay
  • showOverlays
  • hideOverlays
  • removeAllOverlays
  • setLabel
  • setPaintStyle
  • setHoverPaintStyle
  • setConnector
  • setParameter
  • setParameters
  • repaint
  • setType
  • addType
  • removeType
  • toggleType
  • setVisible
  • setAnchor

Each of these operations returns a selector that can be chained.

Getter Operations#

This is the full list of getter operations supported by selectEndpoints(..):

  • getLabel
  • getOverlay
  • isHover
  • isVisible
  • getParameter
  • getParameters
  • getType
  • hasType
  • getAnchor

Each of these operations returns an array whose entries are [ value, Endpoint ] arrays, where 'value' is the return value from the given endpoint. Remember that the return values from a getter are not chainable, but a getter may be called at the end of a chain of setters.

Other methods/properties (not chainable):#

  • delete - deletes the endpoints in the selection
  • detachAll - detaches all connections from the endpoints in the selection
  • length - this member reports the number of endpoints in the selection
  • get(idx) - this function allows you to retrieve an endpoint from the selection

Examples#

  • Select all endpoints and set their hover state to be false:
instance.selectEndpoints().setHover(false);
  • Select all source endpoints on "d1" and remove all overlays:
instance.selectEndpoints({source:"d1"}).removeAllOverlays();
  • Select all source endpoints on "d1" and add the classes "foo" and "bar" to them :
instance.selectEndpoints({source:"d1"}).addClass("foo bar");
  • Select all source endpoints on "d1" and remove the class "foo" from them :
instance.selectEndpoints({source:"d1"}).removeClass("foo");
  • Select all endpoints in scope "foo" and set their fill style to be blue:
instance.selectEndpoints({ scope:"foo" }).setPaintStyle({ fill:"blue" });
  • Select all endpoints from "d1" and detach their connections:
instance.selectEndpoints({source:"d1"}).detachAll();
.each iterator#

The return value of selectEndpoints(..) also has a .each function that allows you to iterate through the list, performing an operation on each one:

instance.selectEndpoints({scope:"foo"}).each((endpoint) => {            // do something });

.each is itself chainable:

instance.selectEndpoints({scope:"foo"}).each((endpoint) => {            // do something 
}).setHover(true);