Core.js

Description

Provides common utility functions and the Class object used internally by the library.

Also provides the TreeUtil object for manipulating JSON tree structures

Some of the Basic utility functions and the Class system are based in the MooTools Framework http://mootools.net.  Copyright © 2006-2009 Valerio Proietti, http://mad4milk.net/.  MIT license http://mootools.net/license.txt.

Author

Nicolas Garcia Belmonte

Copyright

Copyright 2008-2009 by Nicolas Garcia Belmonte.

Homepage

http://thejit.org

Version

1.1.3

License

BSD License

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the organization nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY Nicolas Garcia Belmonte ``AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL Nicolas Garcia Belmonte BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Summary
Core.jsProvides common utility functions and the Class object used internally by the library.
TreeUtilSome common JSON tree manipulation methods.
Functions
pruneClears all tree nodes having depth greater than maxLevel.
getParentReturns the parent node of the node having id as id.
getSubtreeReturns the subtree that matches the given id.
getLeavesReturns the leaves of the tree.
eachLevelIterates on tree nodes with relative depth less or equal than a specified level.
eachA tree iterator.
loadSubtreesAppends subtrees to leaves by requesting new subtrees with the request method.

TreeUtil

Some common JSON tree manipulation methods.

Summary
Functions
pruneClears all tree nodes having depth greater than maxLevel.
getParentReturns the parent node of the node having id as id.
getSubtreeReturns the subtree that matches the given id.
getLeavesReturns the leaves of the tree.
eachLevelIterates on tree nodes with relative depth less or equal than a specified level.
eachA tree iterator.
loadSubtreesAppends subtrees to leaves by requesting new subtrees with the request method.

Functions

prune

prune: function(tree,
maxLevel)

Clears all tree nodes having depth greater than maxLevel.

Parameters

treeA JSON tree object.  For more information please see Loader.loadJSON.
maxLevelAn integer specifying the maximum level allowed for this tree.  All nodes having depth greater than max level will be deleted.

getParent

getParent: function(tree,
id)

Returns the parent node of the node having id as id.

Parameters

treeA JSON tree object.  See also Loader.loadJSON.
idThe id of the child node whose parent will be returned.

Returns

A tree JSON node if any, or false otherwise.

getSubtree

getSubtree: function(tree,
id)

Returns the subtree that matches the given id.

Parameters

treeA JSON tree object.  See also Loader.loadJSON.
idA node unique identifier.

Returns

A subtree having a root node matching the given id.  Returns null if no subtree matching the id is found.

getLeaves

getLeaves: function (node,
maxLevel)

Returns the leaves of the tree.

Parameters

nodeA JSON tree node.  See also Loader.loadJSON.
maxLeveloptional A subtree’s max level.

Returns

An array having objects with two properties.

  • The node property contains the leaf node.
  • The level property specifies the depth of the node.

eachLevel

eachLevel: function(tree,
initLevel,
toLevel,
action)

Iterates on tree nodes with relative depth less or equal than a specified level.

Parameters

treeA JSON tree or subtree.  See also Loader.loadJSON.
initLevelAn integer specifying the initial relative level.  Usually zero.
toLevelAn integer specifying a top level.  This method will iterate only through nodes with depth less than or equal this number.
actionA function that receives a node and an integer specifying the actual level of the node.

Example

TreeUtil.eachLevel(tree, 0, 3, function(node, depth) {
   alert(node.name + ' ' + depth);
});

each

each: function(tree,
action)

A tree iterator.

Parameters

treeA JSON tree or subtree.  See also Loader.loadJSON.
actionA function that receives a node.

Example

TreeUtil.each(tree, function(node) {
  alert(node.name);
});

loadSubtrees

loadSubtrees: function(tree,
controller)

Appends subtrees to leaves by requesting new subtrees with the request method.

Parameters

treeA JSON tree node.  Loader.loadJSON.
controllerAn object that implements a request method.

Example

TreeUtil.loadSubtrees(leafNode, {
  request: function(nodeId, level, onComplete) {
    //Pseudo-code to make an ajax request for a new subtree
    // that has as root id _nodeId_ and depth _level_ ...
    Ajax.request({
      'url': 'http://subtreerequesturl/',

      onSuccess: function(json) {
        onComplete.onComplete(nodeId, json);
      }
    });
  }
});
prune: function(tree,
maxLevel)
Clears all tree nodes having depth greater than maxLevel.
getParent: function(tree,
id)
Returns the parent node of the node having id as id.
getSubtree: function(tree,
id)
Returns the subtree that matches the given id.
getLeaves: function (node,
maxLevel)
Returns the leaves of the tree.
eachLevel: function(tree,
initLevel,
toLevel,
action)
Iterates on tree nodes with relative depth less or equal than a specified level.
each: function(tree,
action)
A tree iterator.
loadSubtrees: function(tree,
controller)
Appends subtrees to leaves by requesting new subtrees with the request method.
Some common JSON tree manipulation methods.
loadJSON: function(json,
i)
Loads a JSON structure to the visualization.
Close