jsTree

A tree component based on jQuery.

Dependencies

Getting started

Including the files

Option 1: Including all the necessary source files

<!-- REQUIRED SCRIPTS AND STYLES --> <link rel="stylesheet" type="text/css" href="path-to-jsTree/tree_component.css" /> <script type="text/javascript" src="path-to-jsTree/css.js"></script> <script type="text/javascript" src="path-to-jsTree/jquery.js"></script> <script type="text/javascript" src="path-to-jsTree/jquery.listen.js"></script> <script type="text/javascript" src="path-to-jsTree/tree_component.js"></script> <!-- required for xml data type --> <script type="text/javascript" src="path-to-jsTree/sarissa.js"></script> <script type="text/javascript" src="path-to-jsTree/sarissa_ieemu_xpath.js"></script> <script type="text/javascript" src="path-to-jsTree/jquery.xslt.js"></script> <!-- required for metadata rule definitions --> <script type="text/javascript" src="path-to-jsTree/jquery.metadata.js"></script> <!-- required for cookie functionality --> <script type="text/javascript" src="path-to-jsTree/jquery.cookie.js"></script>

Option 2: Including the minified version

<!-- REQUIRED SCRIPTS AND STYLES --> <link rel="stylesheet" type="text/css" href="path-to-jsTree/tree_component.css" /> <script type="text/javascript" src="path-to-jsTree/tree_component.js"></script>

Creation

var tree = new tree_component(); tree.init(container_node,[{ options }]);

The init function takes two parameters:

Configuration

Description of all the possible properties of the options object (you can see the default values below).

You can only pass the values you need, which are different from the default configuration. (check out the examples).

data javascript object

The object contains the following properties:

dflt String

The id of the node, that is to be focused onload. If data is loaded asyncroniously, make sure the id will be loaded with the first request. Value could be false.

languages Array

An array of strings representing each language. The first in the array will be the default language (e.g. visible on load). The strings in this array will be used internally for CSS classes - make sure that they are valid CSS classnames. An empty array will mean no language versions.

path String

The path to the jsTree folder. If set to false - the script will auto-detect its path.

cookies requires Cookies plugin
Javascript object

If set to false - cookies will be disabled. Otherwise the possible properties of the object are:

ui Javascript object

Properties of the object are:

lang Javascript object

Object containing the possible translations of standard strings used in the code. Properties of the object are:

rules Javascript object

This object's properties define the interactive behaviour of the tree. Properties of the object are:

callback Javascript object

This object's properties define the callback functions to be executed upon various events. All properties of the object are functions:

Defaults

{ data : { type : "predefined", async : false, url : false, json : false }, dflt : false, languages : [], path : false, cookies : false, ui : { dots : true, rtl : false, animation : 0, hover_mode : true }, lang : { new_node : "New folder", loading : "Loading ..." }, rules : { multiple : false, metadata : false, type_attr : "rel", multitree : false, createat : "bottom", use_inline : false, clickable : "all", renameable : "all", deletable : "all", creatable : "all", draggable : "none", dragrules : "none" }, callback : { beforechange : function(NODE,TREE_OBJ) { return true }, beforemove : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, beforecreate : function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, beforerename : function(NODE,LANG,TREE_OBJ) { return true }, beforedelete : function(NODE,TREE_OBJ) { return true }, onchange : function(NODE,TREE_OBJ) { }, onrename : function(NODE,LANG,TREE_OBJ) { }, onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ) { }, oncopy : function(NODE,REF_NODE,TYPE,TREE_OBJ) { }, oncreate : function(NODE,REF_NODE,TYPE,TREE_OBJ) { }, ondelete : function(NODE, TREE_OBJ) { }, onopen : function(NODE, TREE_OBJ) { }, onclose : function(NODE, TREE_OBJ) { }, error : function(TEXT, TREE_OBJ) { }, ondblclk : function(NODE, TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); }, onrgtclk : function(NODE, TREE_OBJ, EV) { } } }

Data sources

Default is predefined HTML.

predefined HTML

Binds events and display only to the container element's content. Expected content is as follows:

<!-- SINGLE NODE --> <li id="node_identificator"> <a href="#">node_title</a> </li> <!-- NODE WITH CHILDREN --> <li id="node_identificator"> <a href="#" style="background-image:url(node_icon);">node_title</a> <ul> <!-- CHILD NODES --> </ul> </li>

If language versions are used:

<!-- SINGLE NODE --> <li id="node_identificator"> <a href="#" class="language_name">node_title</a> <a href="#" class="language_name">node_title</a> </li>

nested XML

All attributes of the item node get copied to the li node in the output HTML. If the hasChildren attribute is present, the output node is displayed as having children even if it does not (useful for async). If the state attribute is present the node is forced in the specified state.

<xml version="1.0" encoding="utf-8" ?> <root> <item [attribute="attribute_value" state="closed|open" hasChildren]> <content> <name icon="path_to_icon" lang="language_name"><![CDATA[node_title]]></name> <name icon="path_to_icon" lang="language_name"><![CDATA[node_title]]></name> </content> <!-- NESTED ITEM NODES --> </item> </root>

flat XML

All attributes of the item node get copied to the li node in the output HTML. If the hasChildren attribute is present, the output node is displayed as having children even if it does not (useful for async). If the state attribute is present the node is forced in the specified state.

<xml version="1.0" encoding="utf-8" ?> <root> <item id="node_identificator" parent_id="parent_node_identificator" [attribute="attribute_value" icon="path_to_icon" state="closed|open" hasChildren]> <content> <name icon="path_to_icon" lang="language_name"><![CDATA[node_title]]></name> <name icon="path_to_icon" lang="language_name"><![CDATA[node_title]]></name> </content> </item> <!-- NEXT ITEM NODES --> </root>

JSON

If state is "closed" the node is displayed as having children even if it does not.

No langauge versions

{ attributes: { id : "node_identificator", [attribute : "attribute_value"] }, state: "closed" or " ", data: "node_identificator", icons: "path_to_icon", children: [ // an array of child nodes objects ] }

With language versions:
Note, the data and icons array must be ordered in the same order as the passed languages array (see below)

{ attributes: { id : "node_identificator", [attribute : "attribute_value"] }, state: "closed" or " ", data: ["node_identificator", "node_identificator", ... ], icons: ["path_to_icon", "path_to_icon", ... ], children: [ // an array of child nodes objects ] }

Customization

CSS node icon

Globally for all trees

.tree li a { background-image:url(node_icon); }

Or for a specific tree

container_selector li a { background-image:url(node_icon); }

For a specific language

container_selector OR .tree li a.language_name { background-image:url(node_icon); }

For all closed nodes

container_selector OR .tree li.closed { background-image:url(node_icon); }

For all open nodes

container_selector OR .tree li.open { background-image:url(node_icon); }

For all leaf nodes

container_selector OR .tree li.leaf { background-image:url(node_icon); }

For all selected nodes

container_selector OR .tree li a.clicked { background-image:url(node_icon); }


You could also combine these to get more complex CSS selectors.

Predefined HTML node icon

If you are using predefined HTML you can specify the background-image inline for any <a> nodes

<li id="node_identificator"> <a href="#" style="background-image:url(node_icon);">node_title</a> </li>

XML data source node icon

If you are using XML as source - simply pass an icon attribute. Here is how it looks if you have no language versions:

<item icon="node_icon" ...>

If you have langauge versions you can specify different icons for each language:

<item ...> <content> <name lang="language_name" icon="node_icon" ...> ... </name> <name lang="language_name" icon="node_icon" ...> ... </name> </content> </item>

JSON data source node icon

If you are using JSON as source you can specify node icon with the icon attribute.

No language versions

{ attributes : { ... }, data : "...", icon : "node_icon" }

Language versions

{ attributes : { ... }, data : ["...", "...", ... ], icon : ["node_icon", "node_icon", ... ] }

Dots

You can choose whether to have dots or not, and toggle them on the fly (attach to event)

In the configuration

instance.init(container_node,{ ui: { dots : false | true } });

Runtime using built-in function

instance.toggleDots();

Runtime using jQuery

$(container_node).addClass("no_dots"); $(container_node).removeClass("no_dots"); $(container_node).toggleClass("no_dots");

In the HTML markup

<container_node class="no_dots"> ... </container_node>

Right to left alignment

In the configuration

instance.init(container_node,{ ui: { rtl : false | true } });

Runtime using jQuery

$(container_node).addClass("rtl"); $(container_node).removeClass("rtl"); $(container_node).toggleClass("rtl");

In the HTML markup

<container_node class="rtl"> ... </container_node>

Hotkeys

I found this jquery plugin extremely useful - check it out

$.hotkeys.add('key', { disableInInput: true }, function() { instance.function([arguments]); }); /* for example */ $.hotkeys.add('f2', { disableInInput: true }, function() { tree.rename(); }); $.hotkeys.add('n', { disableInInput: true }, function() { tree.create(); }); $.hotkeys.add('d', { disableInInput: true }, function() { tree.remove(); }); $.hotkeys.add('l', { disableInInput: true }, function() { tree.cycle_lang(); }); $.hotkeys.add('r', { disableInInput: true }, function() { tree.refresh(); }); $.hotkeys.add('up', { disableInInput: true }, function() { tree.get_prev(); }); $.hotkeys.add('down', { disableInInput: true }, function() { tree.get_next(); }); $.hotkeys.add('left', { disableInInput: true }, function() { tree.get_left(); }); $.hotkeys.add('right', { disableInInput: true }, function() { tree.get_right(); }); $.hotkeys.add('return', { disableInInput: true }, function() { tree.select_branch(); });

Functions

Expect shortly

Examples

Expect shortly