Skip to main content

UI States

UI States#

A common use case is the requirement to temporarily paint a set of nodes/groups/ports and/or edges with some set of styles. The surface supports this through the concept of UI States.

First, an example. This is a snippet from the view parameter passed to a render call:

toolkitInstance.render(someElement, {  view:{      ... other view content...        "states":{      "mintyGreenState":{        "default":{          cssClass:"mintyGreen",                paintStyle:{ "strokeStyle":"#00FF00" },          endpointStyle: { fillStyle:"#00EE00" }        }      }    }  }});

Here we have declared a state called mintyGreenState. When this state is active, all nodes of type default to which it applies will have a CSS class of mintyGreen added, and all edges of type default will be painted with a stroke style of #00FF00 (for the eagle-eyed, not quite minty green, it's true). All ports - which are represented via endpoints - will have a fill style of #00EE00.

Note also that edges and ports will be assigned the CSS class of mintyGreen. So you need to ensure that any CSS rules related to UI states are specific enough that you do not accidentally apply node/group styles to a connection.

caution

When you call updateEdge, updateNode or updateGroup on a Toolkit instance, any edge, node or group states that are active for the given object will be cancelled and it will revert to its default state (albeit with the new data you provided).


Applying UI States#

You can apply a state to any of the following:

Applying state to the entire dataset#

surface.activateState("mintyGreenState")          // in the absence of a second argument, the state is applied to the entire dataset.

Applying state to a Toolkit's current selection#

surface.activateState("mintyGreenState", toolkit.getSelection())

Applying state to an ad-hoc selection#

const selection = toolkit.select(["node1", "node2"])surface.activateState("mintyGreenState", selection)

Applying state to a path#

const path = toolkit.getPath({source:"node1", target:"node2"})surface.activateState("mintyGreenState", path)

Methods available#

  • activateState(stateId, [target])

Activate the given state, optionally on the given target. If target is not supplied then the state will be applied to the entire dataset.

  • deactivateState(stateId, [target])

Deactivate the given state, optionally on the given target. If target is not supplied then the state deactivation will be applied to the entire dataset.

  • resetState()

Deactivates all states across the entire dataset.


Wildcard states#

In the above example, the state specification applied to all nodes, groups, edges and ports that have a type of default. Should you wish to indicate that a state can be applied to any object, you can use a wildcard:

  "states":{    "mintyGreenState":{      "*":{        cssClass:"mintyGreen"      },      "aSpecificType":{        cssClass:"FOO"      }    }  }

In this example, which contains only cssClass directives (remember that the cssClass will be added to all nodes, groups, ports and edges), when mintyGreenState is activated, all objects will get the mintyGreen CSS class. But objects of type aSpecificType will get the FOO CSS class also.