Tree.js
Tree.js is a JavaScript library to build and manipulate hookable trees.
Installation
It is available with bower:
bower install tree.js
Then add the retrieved files to your HTML layout:
<!-- If you want to build hookable tree (see below) -->
You can also use it with RequireJS as an AMD module.
Usage
Simple tree
Create a tree
var myTree = Tree;
Find a node
var lebrac = myTree; // or var lebrac = myTree; // or var lebrac = myTree;
Get the raw data of a node
lebracdata // { name: 'lebrac', job: 'designer' }
Get an attribute
lebrac; // designer
Set an attribute
lebrac;lebrac ;
Get the path of a node
lebrac; // /dupuis/prunelle/lebrac
Get the parent of a node
var dupuis = lebracparent;dupuisname; // dupuisdupuisparent; // undefined
Append a child node
lebrac; lebrac;lebracparentparentparentname; // dupuis
Remove a node
lebrac;myTree; // undefined
Move a node
var lagaffe = myTree;lagaffe; // /dupuis/lagaffe
Get the children of a node
var children = myTree;children0name; // prunelle
Get a node visitor
In order to execute a callback on each node of the tree, you can use a visitor:
var visitor = myTree;;
Stringify a node
On any node you can call stringify
to serialize it. Internally it will use JSON.stringify with custom replacer to avoid circular references because of the _parent
private property:
myTree
Work with hooks
To work with hooks, you first need to add hook capacities to your tree:
var hookableTree = Tree;
Everything explained above is still true but the hookable operations will now return promises!
Because of that you need to include into your page Q
library. Otherwise you can specify another promises library by calling: hookableTree.promiseFactory(YOUR_FACTORY)
. It must expose the same API than Q
.
You can configure the timeout used for each listener by calling hookableTree.timeout(30000)
. Set it to 0
to disable it, default to 30000
.
Register a hook listener
There are 12 hooks available:
Hook | Description |
---|---|
HOOK_PRE_APPEND | Triggered when append is called and before applying it on the tree |
HOOK_POST_APPEND | Triggered when append is called and after applying it on the tree |
HOOK_ERROR_APPEND | Triggered when append is called and an error occured |
HOOK_PRE_REMOVE | Triggered when remove is called and before applying it on the tree |
HOOK_POST_REMOVE | Triggered when remove is called and after applying it on the tree |
HOOK_ERROR_REMOVE | Triggered when remove is called and an error occured |
HOOK_PRE_MOVE | Triggered when moveTo is called and before applying it on the tree |
HOOK_POST_MOVE | Triggered when moveTo is called and after applying it on the tree |
HOOK_ERROR_MOVE | Triggered when moveTo is called and an error occured |
HOOK_PRE_CLONE | Triggered when clone is called and before applying it on the tree |
HOOK_POST_CLONE | Triggered when clone is called and after applying it on the tree |
HOOK_ERROR_CLONE | Triggered when clone is called and an error occured |
To register a hook you need to call registerListener(string hook, function listener)
:
hookableTree;
Because of hooks, append
, remove
, move
, clone
will return promise, as show in this example:
hookableTree;
hookableTree;
hookableTree;
hookableTree;
Build
To rebuild the minified JavaScript you must run: make build
.
Tests
Install dependencies and run the unit tests:
make install
make test-spec
Contributing
All contributions are welcome and must pass the tests. If you add a new feature, please write tests for it.
License
This application is available under the MIT License, courtesy of marmelab.