Amazing Tree

You can leave comments at the bottom of this page. Amazing Tree is also listed on the projects page

Contents

  1. What is Amazing Tree ?
  2. Demo
  3. Features
  4. Browser Support
  5. Documentation
    1. AT_Node
    2. node.addChildNode / node.removeChildNode
    3. node.isParentOf
    4. node.permission.parent / node.permission.above / node.permission.below
    5. node.render
    6. node.expand / node.collapse
    7. AmazingTree.successfulDragDrop / AmazingTree.unsuccessfulDragDrop
    8. AmazingTree.pointsOfInterest
    9. AmazingTree.baseURL
    10. AmazingTree.enableDragDrop
    11. AmazingTree.scroll.speed / AmazingTree.scroll.boundary
  6. License

What is Amazing Tree ?

Amazing Tree is a javascript A navigation tree that allows you to drag and drop nodes around the tree. It provides a number of features while working within most of the popular browsers. If you like it, you will find the license flexible enough to use it in almost any project.

Demo

Scroll Demo: Demo that shows scrolling when the mouse is at the edge of the frame.

Full Demo: Shows off many different features of the library.

Clone Demo: Shows the clone feature which allows nodes to be cloned on drag and drop instead of moving them to the new location.

Features

  1. callback function on successful and unsuccessful drag and drop
  2. Points of intrest can be defined with callbacks for mouse events
  3. Enable and disable drag drop on the tree
  4. Set scroll speed and boundaries when mouse reaches the edges
  5. Dynamically add and remove nodes
  6. Expand and collapse nodes through function calls
  7. Permission callbacks to allow and disallow certain drag drop situations

Browser Support

Internet explorer 6+

FireFox 1.5+

Opera 9.5+

Safari

Google Chrome

Documentation

AT_Node

An AT_Node represents one link in the tree. It contains the properties and functions related to the node.

new AT_Node({
	text: string,
	url: string,
	target: "_blank",
	icon: string,
	iconOpen: string,
	clone: boolean,
	data: object
})

text is the link text.
url is the address to visit when the link is clicked.
target is added to the anchor tag.
icon is the url of the icon to show when the node is not expanded.
iconOpen is the url of the icon to show when the link is expanded.
If clone is set to true, the node is cloned instead of being moved to the new location on drag and drop.
data is any user defined data associated with the node and is an object.
All the above parameters are optional.

Example:

var node = new AT_Node({
	text: "Link Name",
	url: "http://www.ankur.com",
	target: "_blank",
	icon: "images/icon1.png",
	iconOpen: "images/icon2.png",
	clone: false,
	data: {
		linkID: 1,
		status: "new_node"
	}
});

node.addChildNode / node.removeChildNode

The AT_Node functions addChildNode and removeChildNode add or remove one AT_Node as a child of another AT_Node.

parentNode.addChildNode( childNode );
parentNode.removeChildNode( childNode );

Example:

parentNode = new AT_Node({
	text: "Node 1",
	url: "http://www.ankur.com/"
});

childNode = new AT_Node({
	text: "Node 1.1",
	url: "http://www.ankur.com/"
});

parentNode.addChildNode( childNode );

parentNode.removeChildNode( childNode );

node.isParentOf

The AT_Node function isParentOf return is used to check if one node is a child of another node or not.

parentNode.isParentOf( childNode );

The function call returns true if parentNode is a parent of childNode and false otherwise. You should note that parent is not necessarily the direct parent. The function returns true as long as parentNode is an ancestor of childNode.

Example:

if ( parentNode.isParentOf( childNode ) ) {
	alert(parentNode.text + " is an ancestor of " + childNode.text);
} else {
	alert(parentNode.text + " is not an ancestor of " + childNode.text);
}

node.permission.parent / node.permission.above / node.permission.below

The AT_Node variables permission.parent, permission.above and permission.below are function references that indicate if a particular drag drop of a node is allowed or not.

node.permission.parent = functionName;
node.permission.above = functionName;
node.permission.below = functionName;

The function for each of them should return true if the drag drop is allowed and false if it isn’t. The functions are passed the dragged node and the dropped node (drbopped node will be nothing but node in the above code snippet). You can use these to return true to allow the drag drop operation or return false not to allow it.
node.permission.parent decides if node can become the parent of the dragged node. node.permission.above decides if the dragged node can be dropped above node. node.permission.below decides if the dragged node can be dropped below node.

Example:

node1.permission.parent = function(draggedNode, dropNode) {

	if (draggedNode.text == "Blog") {
		return true;
	}
	return false;

}

According to the above function, if the text of the dragged node is “Blog”, it is allowed to be dropped as a child of node1. Otherwise, it is not allowed. Please note that this function is called only if the drag drop will result in the dragged node becoming a child of node1. If that situation does not arise due to the drag drop, the function is not called.

node.render

The AT_Node function render will construct the tree structure on the page. You need to pass it an element inside which the markup of the tree will be placed.

node.render( element );

node will usually be the root of the tree, though you can use any other node to render part of the tree.

Example:

node.render( getElementById("treeContainer") );

node.expand / node.collapse

The AT_Node functions expand and collapse will expand or collapse a node exactly the same way clicking on + expands the node and – collapses the node. If the node does not have any childrent, calling these functions has no effect.

node.expand();
node.collapse();

AmazingTree.successfulDragDrop / AmazingTree.unsuccessfulDragDrop

The variables AmazingTree.successfulDragDrop and AmazingTree.unsuccessfulDragDrop are function references which are called on mouse up after a drag and drop operation. If the drag drop operation resulted in a node being moved, AmazingTree.successfulDragDrop is called. If no node was moved, AmazingTree.unsuccessfulDragDrop is called.

AmazingTree.successfulDragDrop = functionName;
AmazingTree.unsuccessfulDragDrop = functionName;

Each of the functions are passed two objects – dragged and dropped. They contain the variables node, parent, above and below each of which are AT_Node objects. Some of them might be null if they are not aplicable for the situation.

Example:

AmazingTree.successfulDragDrop = function(dragged, dropped) {
	alert("dragged node text is " + dragged.node.text);
	alert("dropped parent node text is " + dropped.parent.text);
}

AmazingTree.pointsOfInterest

The variable AmazingTree.pointsOfInterest contains objects defining elements for which you would like your functions to be called when certain mouse events fire on them. The mouse events you can watch are mouse over, mouse out and mouse up. These events are called only when a node of the tree is being dragged and the event occurs.

AmazingTree.pointsOfInterest[ index] = {
    element: element,
    mouseOverCallback: functionName,
    mouseOutCallback: functionName,
    mouseUpCallback: functionName
}

Each of the functions are passed the dragged node and the mouse position. The mouse position is an object with variables x and y representing the distances in pixels from the left and top of the window respectively.

Example:

AmazingTree.pointsOfInterest[0] = {
    element: document.getElementById("delete"),
    mouseOverCallback: function(draggedNode, mousePos) {
        document.getElementById("status").innerHTML = "About to delete " + draggedNode.text;
    },
    mouseOutCallback: function(draggedNode, mousePos) {
		document.getElementById("status").innerHTML = "";
    },
    mouseUpCallback: function(draggedNode, mousePos) {
        document.getElementById("status").innerHTML = "deleting " + draggedNode.text;
		// Code to delete node
    }
}

AmazingTree.baseURL

The variable AmazingTree.baseURL is a string which sets the URL to reach the Amazing Tree directory. If this is set incorrectly, the default images of the tree will not load.

AmazingTree.baseURL = url;

Example:

AmazingTree.baseURL = "http://www.ankur.com/amazingTree/";

AmazingTree.enableDragDrop

The variable AmazingTree.enableDragDrop can be set to true or false to enable or disable drag drop on the tree.

AmazingTree.enableDragDrop = boolean;

Example:

AmazingTree.enableDragDrop = true;

AmazingTree.scroll.speed / AmazingTree.scroll.boundary

The variables within AmazingTree.scroll are used to set the scroll speed and scroll boundaries used to scroll when a dragged node reaches the edge of the window. AmazingTree.scroll.speed can be set to a number between 1 and 1000 with 1000 being the fastest and 1 being the slowest scroll speed. Practically, you probably will not notice much difference beyond 200. AmazingTree.scroll.boundary.top, AmazingTree.scroll.boundary.bottom, AmazingTree.scroll.boundary.left and AmazingTree.scroll.boundary.right set the number of pixels from the top, bottom, left and right boundaries when scrolling should begin.

AmazingTree.scroll.speed = number;
AmazingTree.scroll.boundary.top = number;
AmazingTree.scroll.boundary.bottom = number;
AmazingTree.scroll.boundary.left = number;
AmazingTree.scroll.boundary.right = number;

Example:

AmazingTree.scroll.speed = 100;
AmazingTree.scroll.boundary.top = 20;
AmazingTree.scroll.boundary.bottom = 20;
AmazingTree.scroll.boundary.left = 20;
AmazingTree.scroll.boundary.right = 20;

License

Amazing Tree is licensed under the BSD license. This should allow you to use it for any project. If this license is unsuitable for you, please contact me. In most reasonable cases, there won’t be any problem in giving it to you under a slightly different license.

Leave a Reply