Main Page | Directories | File List | File Members

tree.js File Reference

Go to the source code of this file.

Variables

Zapatec Tree
Zapatec Tree all = {}
Zapatec Tree prototype createTree
Zapatec Tree prototype createItem
Zapatec Tree prototype makeNode
Zapatec Tree prototype insertBefore
Zapatec Tree prototype removeChild
Zapatec Tree prototype item_addDefaultIcon
Zapatec Tree prototype item_addIcon
Zapatec Tree prototype itemClicked
Zapatec Tree prototype toggleItem
Zapatec Tree prototype collapseAll
Zapatec Tree prototype expandAll
Zapatec Tree prototype toggleAll
Zapatec Tree prototype sync
Zapatec Tree prototype destroy
Zapatec Tree prototype _getTree
Zapatec Tree prototype onItemSelect = function() {}
Zapatec Tree onItemToggle


Variable Documentation

Zapatec Tree
 

The Zapatec.Tree object constructor. Pass to it the ID of an UL element (or a reference to the element should you have it already) and an optional configuration object. This function creates and initializes the tree widget according to data existent in the nested list, and applies the configuration specified.

The configuration object may contain the following options (the following shows default values):

 {
    hiliteSelectedNode : true,     // boolean
    compact            : false,    // boolean
    dynamic            : false,    // boolean
    initLevel          : false,    // false or number
    defaultIcons       : null      // null or string
 }

  • hiliteSelectedNode -- if false is passed, the tree will not highlight the currently selected node.
  • compact -- if true is passed the tree will work in a "compact" mode; in this mode it automatically closes sections not relevant to the current one.
  • dynamic -- if true is passed the tree will use the "dynamic initialization" technique which greatly improves generation time. Some functionality is not available in this mode until all the tree was generated. In "dynamic" mode the tree is initially collapsed and levels are generated "on the fly" as the end user expands them. You can't retrieve nodes by ID (which implies you can't synchronize to certain nodes) until they have been generated.
  • initLevel -- when this is a numeric value, it specifies the maximum "expand level" that the tree will use initially. Therefore, if for instance you specify 1 then the tree will be initially expanded one level. Pass here 0 to have the tree fully collapsed, or leave it false to have the tree fully expanded.
  • defaultIcons -- you can pass here a string. If so, all tree items will get an additional TD element containing that string in the class attribute. This helps you to include custom default icons without specifying them as IMG tags in the tree. See our examples.

Parameters:
el [string or HTMLElement] -- the UL element
config [Object, optional] -- the configuration options
Returns:

Definition at line 63 of file tree.js.

Zapatec Tree all = {}
 

This global variable keeps a "hash table" (that is, a plain JavaScript object) mapping ID-s to references to Zapatec.Tree objects. It's helpful if you want to operate on a tree but you don't want to keep a reference to it. Example:

   // the following makes a tree for the <ul id="tree-id"> element
   var tree = new Zapatec.Tree("tree-id");
   // ... later
   var existing_tree = Zapatec.Tree.all("tree-id");
   // and now we can use \b existing_tree the same as we can use \b tree
   // the following displays \b true
   alert(existing_tree == tree);

So in short, this variable remembers values returned by "new Zapatec.Tree(...)" in case you didn't.

Definition at line 178 of file tree.js.

Zapatec Tree prototype createTree
 

For internal use only.

Function that creates a (sub)tree. This function walks the UL element, computes and assigns CSS class names and creates HTML elements for a subtree. Each time a LI element is encountered, createItem() is called which effectively creates the item. Beware that createItem() might call back this function in order to create the item's subtree. (so createTree and createItem form an indirect recursion).

Parameters:
list [HTMLElement] -- reference to the UL element
parent [HTMLElement] -- reference to the parent element that should hold the (sub)tree
level [integer] -- the level of this (sub)tree in the main tree.
Returns:
id -- the (sub)tree ID; might be automatically generated.

Definition at line 194 of file tree.js.

Zapatec Tree prototype createItem
 

For internal use only.

This function walks through a LI element and creates the HTML elements associated with that tree item. When it encounters an UL element it calls createTree() in order to create the item's subtree. This function may also call item_addIcon() in order to add the +/- buttons or icons present in the item definition as IMG tags, or item_addDefaultIcon() if the tree configuration specifies "defaultIcons" and no IMG tag was present.

Parameters:
li [HTMLElement] -- reference to the LI element
parent [HTMLElement] -- reference to the parent element where the HTML elements should be created
next_li [HTMLLiElement] -- reference to the next LI element, if this is not the last one
level [integer] -- the level of this item in the main tree
atStart [HTMLElement optional] -- reference to the element DIV with a TABLE object that represents the child following the new child added or inserted
Returns:
[HTMLElement] -- a reference to a DIV element holding the HTML elements of the created item

Definition at line 276 of file tree.js.

Zapatec Tree prototype makeNode
 

Call this function to create a html element with the optional element type specified. By default, it is a element.

Parameters:
html [string, optional] -- html of the node; may include
elements; user is responsible for the content of the html
type [string, optional] -- type of the node to be created

Definition at line 397 of file tree.js.

Zapatec Tree prototype insertBefore
 

A new child can be inserted between two nodes/children or before one node /children at the same tree level. Inserting the new child before the first node of a tree is allowed but not allowed after the last node of the tree, i.e. end of the tree, except before the last node.

Parameters:
newChild [HTMLElement] -- New child node to be inserted into the tree
refChild [HTMLElement] -- Reference to the child node which the new child node will be inserted before

Definition at line 561 of file tree.js.

Zapatec Tree prototype removeChild
 

An old child node in the tree can be removed at any level. If the old child node happens to be the root of a subtree, then the entire subtree including child node(s) under the root node will be removed. If the old child node is just an item node without any child node under it as children, then the only node removed is the old child node. Once an item node or a subtree is removed, the node/subtree before and/or after the old node will be joined together, sometimes with tree lines redrawn. The only limitations are that the root node of the top tree is not permitted for removal; the first child node found will be removed if there are more than one tree node with the same id.

Parameters:
oldChild [HTMLElement] -- Old child node to be removed from the tree where it can be an item node without children or a subtree including the old child node as the root.

Definition at line 636 of file tree.js.

Zapatec Tree prototype item_addDefaultIcon
 

For internal use only.

This function adds a TD element having a certain class attribute which helps having a tree containing icons without defining IMG tags for each item. The class name will be "tgb icon className" (where "className" is the specified parameter). Further, in order to customize the icons, one should add some CSS lines like this:

  div.tree-item td.customIcon {
    background: url("themes/img/fs/document2.png") no-repeat 0 50%;
  }
  div.tree-item-expanded td.customIcon {
    background: url("themes/img/fs/folder-open.png") no-repeat 0 50%;
  }
  div.tree-item-collapsed td.customIcon {
    background: url("themes/img/fs/folder.png") no-repeat 0 50%;
  }

As you can see, it's very easy to customize the default icons for a normal tree item (that has no subtrees) or for expanded or collapsed items. For the above example to work, one has to pass { defaultIcons: "customIcon" } in the tree configuration object.

This function does nothing if the className parameter has a false logical value (i.e. is null).

Parameters:
item [HTMLElement] -- reference to the DIV element holding the item
className -- a string containing the additional class name

Definition at line 779 of file tree.js.

Zapatec Tree prototype item_addIcon
 

For internal use only.

This function does different things, depending on whether the img parameter is passed or not. If the img is passed, then this function adds it as an icon for the given item. If not passed, this function creates a "+/-" button for the given item.

Parameters:
item [HTMLElement] -- reference to the DIV holding the item elements
img [HTMLImgElement, optional] -- reference to an IMG element; normally one found in the

Definition at line 801 of file tree.js.

Zapatec Tree prototype itemClicked
  This function gets called from a global event handler when some item was clicked. It selects the item and toggles it if it has a subtree (expands or collapses it).

Parameters:
item_id [string] -- the item ID

Definition at line 838 of file tree.js.

Zapatec Tree prototype toggleItem
 

This function toggles an item if the state parameter is not specified. If state is true then it expands the item, and if state is false then it collapses the item.

Parameters:
item_id [string] -- the item ID
state [boolean, optional] -- the desired item state
expand [boolean, optional] -- should expand/collapse if it is a branch
Returns:
a reference to the item element if found, null otherwise

Definition at line 860 of file tree.js.

Zapatec Tree prototype collapseAll
  Call this function to collapse all items in the tree.

Definition at line 1021 of file tree.js.

Zapatec Tree prototype expandAll
  Call this function to expand all items in the tree.

Definition at line 1030 of file tree.js.

Zapatec Tree prototype toggleAll
  Call this function to toggle all items in the tree.

Definition at line 1039 of file tree.js.

Zapatec Tree prototype sync
  Call this function to synchronize the tree to a given item. This means that all items will be collapsed, except that item and the full path to it.

Parameters:
item_id [string] -- the ID of the item to sync to.

Definition at line 1051 of file tree.js.

Zapatec Tree prototype destroy
  Destroys the tree. Removes all elements. Does not destroy the Zapatec.Tree object itself (actually there's no proper way in JavaScript to do that).

Definition at line 1082 of file tree.js.

Zapatec Tree prototype _getTree
 

For internal use only.

This function is used when "dynamic initialization" is on. It retrieves a reference to a subtree if already created, or creates it if it wasn't yet and dont_call is false (returns null in that case).

Parameters:
tree_id [string] the ID of the subtree
dont_call [boolean] pass true here if you don't want the subtree to be created
Returns:
reference to the tree if it was found or created, null otherwise.

Definition at line 1097 of file tree.js.

Zapatec Tree prototype onItemSelect = function() {}
 

Third party code can override this member in order to add an event handler that gets called each time a tree item is selected. It receives a single string parameter containing the item ID.

Definition at line 1120 of file tree.js.

Zapatec Tree onItemToggle
 

For internal use only.

This is a global event handler that gets called when a tree item is clicked. Don't override! ;-)

Definition at line 1128 of file tree.js.