Currently, JSAV supports two types of trees: common tree and binary tree. The structures form a “class” hierarchy: Tree ← Binary Tree and TreeNode ← BinaryTreeNode.
These functions of a JSAV instance initialize a new tree or binary tree. The returned tree instance is controlled through a collection of methods explained next.
Options that the optional parameter can specify:
horizontal vertical
. Possible horizontal values are “left”, “center”, “right” and vertical values “top”, “center”, “bottom”. Defaults to center center
. Only has an effect if relativeTo is
specified.center center
. Only
has an effect if relativeTo is specified.Removes the DOM element of this structure from the document. This is useful, for example, in re-initializing exercises when the existing structure needs to be removed.
Returns the value for the given CSS property. This function exists for all trees, nodes, and edges.
Animates the value of the given CSS property to value. This function exists for all trees, nodes, and edges.
Animates values of the CSS properties in the map to the given values. This function exists for all trees, nodes, and edges. For example
would animate the color and font-size properties of the tree.
Returns the ID of the structure. If optional parameter newId
is given, sets the ID of the structure. The given ID should be unique. This function exists for all trees, nodes, and edges.
Returns the root of this tree. If the optional node
parameter is given, the root of the tree is set. This function exists for all trees. If node
is a node the old root is replaced. The replaced root will also be hidden, unless option hide
is set to false
. If node
is a value the old value will be replaced in the root.
Creates a new node that can be added to this tree. “Subclasses” override this to create nodes suited for the tree, so this method should be used when creating new nodes. This function exists for all trees.
Returns the height of the tree. This function exists for all trees
This function (re)calculates the layout for the tree. Note, that the library does not do this automatically. That means that after changing the tree, you should call this manually at the end of each animation step. This function exists for all trees.
Make the tree invisible. This function exists for all trees. It recursively hides all the nodes and edges in the tree as well unless
option recursive
is set to false
.
Make the tree visible.This function exists for all trees. It recursively shows all the nodes and edges in the tree as well unless
option recursive
is set to false
.
Adds the CSS class className
to the tree and animates the changes.
Removes the CSS class className
from the tree and animates the changes.
Toggles the CSS class className
to the tree and animates the changes.
Return true/false based on if the tree has the CSS class className
.
There are functions to attach event handlers for the nodes and edges in the tree. The events
that can be
listened for are: click, dblclick, mousedown, mousemove, mouseup, mouseenter, and mouseleave.
See jQuery documentation for details on
the events. Every event handler gets as a parameter the jQuery event object. Inside the event
handler function, this
will refer to the JSAV node or edge object.
The function takes another, optional, parameter options that should be an object. It can be used to specify whether the event handler is for nodes or edges. By default, it is attached to only nodes.
Returns: a JSAV tree object. Thus, this method can be chained.
For example, to highlight an node on mouseenter and unhighlight on mouseleave, you can use:
To attach a handler to edges, you can do:
Similarly to arrays, you can also pass custom data to the handler. For example,
bt.click({"color": "blue"}, JSAV.utils._helpers.css);
would call the css
function with the given parameter.
To bind other events than the ones listed above, you can use the on
function. It takes
as the first parameter the name of the event. Multiple events can be bound by separating their names with
spaces. Other parameters are the same as for the shortcuts.
The following functions exist for both tree nodes and binary tree nodes.
Returns the value stored in this node. If the optional newValue
parameter is given, the value is set to the given value.
Returns the parent node of this node. If the optional newParent
parameter is given, the parent node is set.
Returns the edge that connects this node to its parent. If the optional newEdge
parameter is given, the edge to parent is set.
Returns the edge that connects this node to its child at pos
. Returns undefined
is no such child exists.
Returns the pos
:th child of this node.
Sets the pos
:th child to the given value. The node
can be a value or a node of correct type to the node. If value null
is given as node
, the child at that position is removed. Note, that this also hides the removed node and the edge.
The optional third argument options
should be an object. The following option(s) are supported:
edgeLabel
: specify a label shown on the edge connecting the new node to its parent.The function returns the node the child was added to.
Adds a child node to this node. The node
can be a value or a node of correct type to the node.
The options
parameter works like above.
The function returns the node the child was added to. So you can chain the calls to add multiple children to the same node:
This code would add children A, B, and C to the node
.
Removes the node from its parent. The node and its child nodes are hidden recursively, unless option hide
is set to false
.
Hides the node and the edge connecting it to the parent node. Also recursively hides all child nodes unless
option recursive
is set to false
.
Shows the node and the edge connecting it to the parent node. Also recursively shows all child nodes unless
option recursive false
is set to false
. Note, that if the tree is not visible, showing nodes will not have
any effect until the tree is set visible by calling show.
Returns the left child or undefined if node has no left child. If optional parameter node
is given, sets the left child. The parameter can be a value or a binary tree node. If value null
is given as node
, the left child is removed. Note, that this also hides the removed node and the edge.
The additional options
parameter works like for .child(...)
above.
Returns the right child or undefined if node has no right child. If optional parameter node
is given, sets the right child. The parameter can be a value or a binary tree node. Passing null
as node works similarly to the left function. The additional options
parameter works like for .child(...)
above.
Returns the edge that connects this node to its left child. Returns undefined
if node has no left child.
Returns the edge that connects this node to its right child. Returns undefined
if node has no right child.