Events
Events allow the Paper to report on actions happening internally, and give you the opportunity to react to and manipulate these actions. Events allow you to structure listeners to manipulate actions and unlock the full potential of Psiagram.
Examples
Before we dive into the methods and properties of an event, let's touch on some basic examples of what events can achieve.
Simple
First, let's look at a very simple example of a listener for an event.
In the example above, every time a Node is added to the Paper, it will log the ID of the added Node.
Cancelling events
However, where events really shine is by giving control over the event to any listeners. Here's an example of a listener that stops an event from continuing.
This listener checks the height, and if it exceeds 150px, it calls preventDefault to prevent the default action of the event (adding the Node to Paper) and stopPropagation to prevent the event from propagating to any additional listener functions. We will discuss the various methods and properties of an event below.
Transforming data
One final example, and we will get into the internals of an event. In this example, the listener actually updates the data that will be used by the default action of the event once it has gone through every listener.
For an AddNode event, the default action will be to add the Node to the paper at the coordinates specified. For a case like this, you don't want to prevent the default, nor do you want to stop the event from propagating (in case additional listeners do important actions based on the event). What you do want is to update the data that will be used by the default action once it is fired (once every listener has been called).
Event API
Each event has a set of properties and methods that allow you to view and manipulate the lifecycle of an event. When used together, these allow for complete control over the actions within Psiagram.
eventType
target
data
paper
defaultAction()
preventDefault()
stopPropagation()
Paper Events
There are various events that are fired by Paper whenever an action occurs. Each event has a specific target, data object and default action.
AddNode
Target
Data
Default action
Add the stored Node at the x
and y
coordinates.
Move Node
Target
Data
Default action
Move the stored Node from oldCoords
to coords
.
RemoveNode
Target
Data
Default action
Remove the target Node.
AddEdge
Target
Data
Default action
Add the Edge to the Paper.
MoveEdge
Target
Data
Default action
Move the Edge on the paper. If the source/target is a Node, then node
and midpoint
will be provided and point
will be null within data
. If it is a coordinate point, then node
and midpoint
will be null, and point
will be provided within data
. The default action is to trim the Edge at the end of either endpoint Node (if there is one), and then connect all of the coordinates.
RemoveEdge
Target
Data
Default action
Remove the target Edge.
PaperInit
Target
Data
Default action
UpdateActiveItem
Target
Data
Default action
Update the active item from oldActiveItem
to activeItem
. The old item could be null if there was none and an item is becoming active, or the current could be null if there is no current active item.
Last updated