<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>NanoTree</title>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!-- nanotree.js -->
<script type="text/javascript" language="JavaScript">
/**
* Original Author of this file: Martin Mouritzen. ([email protected])
*
*
* (Lack of) Documentation:
*
*
* If a finishedLoading method exists, it will be called when the tree is loaded.
* (good to display a div, etc.).
*
*
* You have to set the variable rootNode (as a TreeNode).
*
* You have to set a container element, this is the element in which the tree will be.
*
*
* TODO:
* Save cookies better (only 1 cookie for each tree). Else the page will totally cookieclutter.
*
***********************************************************************
* Configuration variables.
************************************************************************/
// Should the rootNode be displayed.
var showRootNode = true;
// Should the dashed lines between nodes be shown.
var showLines = true;
// Should the nodes be sorted? (You can either specify a number, then it will be sorted by that, else it will
// be sorted alphabetically (by name).
var sortNodes = true;
// This is IMPORTANT... use an unique id for each document you use the tree in. (else they'll get mixed up).
var documentID = window.location.href;
// being read from cookie.
var nodesOpen = new Array();
// RootNode of the tree.
var rootNode;
// Container to display the Tree in.
var container;
// Shows/Hides subnodes on startup
var showAllNodesOnStartup = false;
// Is the roots dragable?
var dragable = false;
/************************************************************************
* The following is just instancevariables.
************************************************************************/
var href = '';
// rootNodeCallBack name (if null, it's not selectable).
var rootNodeCallBack = null;
// selectedNode
var selectedNode = null;
var states = '';
var statearray = new Array();
var treeNodeEdited = null;
var editaborted = false;
var floatDragElement = null;
var colouredElement = null;
var draggedNodeID = null;
var lastDraggedOnNodeID = null;
/**
* The TreeNode Object
* @param id unique id of this treenode
* @param name The title of this node
* @param icon The icon if this node (Can also be an array with 2 elements, the first one will represent the closed state, and the next one the open state)
* @param param A parameter, this can be pretty much anything. (eg. an array with information).
* @param orderNumber an orderNumber If one is given the nodes will be sorted by this (else they'll be sorted alphabetically (If sorting is on).
*/ function TreeNode(id,name,icon,param,orderNumber) { this.id = id; this.childs = new Array(); this.name = (name == null ? 'unset name' : name); this.icon = (icon == null ? '' : icon); this.parent = null; this.handler = null; this.param = (param == null ? '' : param); this.orderNumber = (orderNumber == null ? -1 : orderNumber);
this.openeventlisteners = new Array(); this.editeventlisteners = new Array(); this.moveeventlisteners = new Array(); this.haschilds = false; this.editable = false; this.linestring = '';
if (treeNode.gotHandler()) {
eval(treeNode.getHandler() + '(getTreeNode(' + nodeID + '));');
} else {
standardClick(treeNode);
}
} function refreshNode(treeNode) {
var submenu = document.getElementById('node' + treeNode.getID() + 'sub');
var str = ''; for(var i=0;i<treeNode.getChildCount();i++) {
var parent = treeNode.getParent(); if (!parent) {
treeNode.childs[i].setLineString(treeNode.getLineString() + 'B');
} else { if (parent.childs[parent.childs.length - 1] == treeNode) {
treeNode.childs[i].setLineString(treeNode.getLineString() + 'B');
} else {
treeNode.childs[i].setLineString(treeNode.getLineString() + 'I');
}
}
str += showNode(treeNode.childs[i],i == (treeNode.getChildCount() - 1));
}
var actionimage = document.getElementById('handler' + treeNode.getID()); if (treeNode.getChildCount() == 0) {
// TreeNode haven't got any children, make sure the right image is displayed. if (actionimage.src.indexOf('last') == -1) {
actionimage.src = href + 'nanoImages/' + (showLines ? 't' : 'white') + '.gif';
} else {
actionimage.src = href + 'nanoImages/' + (showLines ? 'lastnode' : 'white') + '.gif';
}
actionimage.onclick = null;
// Close the submenu if (submenu) {
submenu.style.display = 'none';
}
} else {
// We have children, make sure to display the + and - icon. if (actionimage.src.indexOf('plus') != -1) {
// The TreeNode have already got children, and displays them.
} else if (actionimage.src.indexOf('minus') != -1) {
// The TreeNode have already got children, and displays them.
} else { if (actionimage.src.indexOf('last') == -1) {
actionimage.outerHTML = '<img id="handler' + treeNode.getID() + '" src="' + href + 'nanoImages/' + (showLines ? 'plus' : 'plus_nolines') + '.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
} else {
actionimage.outerHTML = '<img id="handler' + treeNode.getID() + '" src="' + href + 'nanoImages/plus_last.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
}
}
}
submenu.innerHTML = str;
} function handleNode(nodeID) {
var treeNode = getTreeNode(nodeID); if (!treeNode.hasChilds()) { // No reason to handle a node without childs. return;
}
var submenu = document.getElementById('node' + nodeID + 'sub');
var iconimageholder = document.getElementById('iconimage' + nodeID);
var actionimage = document.getElementById('handler' + nodeID);
// This will be used if showRootNode is set to false.
var firstChildOfRoot = false; if (actionimage.src.indexOf('_no_root') != -1) {
firstChildOfRoot = true;
}
if (submenu.style.display == 'none') {
writeStates(nodeID,'open');
fireOpenEvent(treeNode);
submenu.style.display = 'block';
if (actionimage.src.indexOf('last') == -1) {
actionimage.src = href + 'nanoImages/' + ((firstChildOfRoot) ? 'plus_no_root' : (showLines ? 'plus' : 'plus_nolines')) + '.gif';
} else {
actionimage.src = href + 'nanoImages/' + ((firstChildOfRoot) ? 'plus_last_no_root' : (showLines ? 'plus_last' : 'plus_nolines')) + '.gif';
}
}
} function fireOpenEvent(treeNode) { if (treeNode.gotOpenEventListeners()) { for(var i=0;i<treeNode.openeventlisteners.length;i++) {
eval(treeNode.openeventlisteners[i] + '(' + treeNode.getID() + ');');
}
}
} function fireEditEvent(treeNode,newVal) { if (treeNode.gotEditEventListeners()) { for(var i=0;i<treeNode.editeventlisteners.length;i++) {
eval(treeNode.editeventlisteners[i] + '(' + treeNode.getID() + ',\'' + escape(newVal) + '\');');
}
}
} function fireMoveEvent(treeNode,draggedNodeID,droppedOnNodeID) { if (treeNode.gotMoveEventListeners()) { for(var i=0;i<treeNode.moveeventlisteners.length;i++) {
eval(treeNode.moveeventlisteners[i] + '(' + draggedNodeID + ',' + droppedOnNodeID + ');');
}
}
} function blurSelection() { if (selectedNode != null) {
var oldNodeTitle = document.getElementById('title' + selectedNode);
oldNodeTitle.className = 'treetitleselectedblured';
}
} function focusSelection() { if (selectedNode != null) {
var oldNodeTitle = document.getElementById('title' + selectedNode);
oldNodeTitle.className = 'treetitleselectedfocused';
}
} function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";",offset); if (endstr == -1) {
endstr = document.cookie.length;
} return unescape(document.cookie.substring(offset,endstr));
} function getCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0; while (i < clen) {
var j = i + alen; if (document.cookie.substring(i, j) == arg) { return getCookieVal(j);
}
i = document.cookie.indexOf(" ", i) + 1; if (i == 0) { break;
}
} return null;
} function setCookie (name, value) {
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
} function expandNode() {
var state = getState(selectedNode); if (state == 'open') {
var currentTreeNode = getTreeNode(selectedNode); if (currentTreeNode.hasChilds()) {
selectNode(currentTreeNode.childs[0].getID());
}
} else {
handleNode(selectedNode);
}
} function subtractNode() {
var state = getState(selectedNode); if (state == 'closed') {
var currentTreeNode = getTreeNode(selectedNode);
var parent = currentTreeNode.getParent(); if (parent != null && parent != rootNode) {
selectNode(parent.getID());
}
} else {
handleNode(selectedNode);
}
} function selectPrevNode() {
var currentTreeNode = getTreeNode(selectedNode); if (currentTreeNode.prevSibling != null) {
var state = getState(currentTreeNode.prevSibling.getID());
if (state == 'open' && currentTreeNode.prevSibling.hasChilds()) {
// We have to find the last open child of the previoussiblings childs.
var current = currentTreeNode.prevSibling.childs[currentTreeNode.prevSibling.childs.length - 1];
var currentstate = 'open'; while (current.hasChilds() && (getState(current.getID()) == 'open')) {
current = current.childs[current.childs.length - 1];
}
selectNode(current.getID());
} else {
selectNode(currentTreeNode.prevSibling.getID());
}
} else { if (currentTreeNode.getParent() != null && currentTreeNode.getParent() != rootNode) {
selectNode(currentTreeNode.getParent().getID());
}
}
} function selectNextNode() {
var currentTreeNode = getTreeNode(selectedNode);
var state = getState(selectedNode); if (state == 'open' && currentTreeNode.hasChilds()) {
selectNode(currentTreeNode.childs[0].getID());
} else { if (currentTreeNode.nextSibling != null) {
selectNode(currentTreeNode.nextSibling.getID());
} else {
// Continue up the tree until we either hit null, or a parent which have a child.
var parent = currentTreeNode; while ((parent = parent.getParent()) != rootNode) { if (parent.nextSibling != null) {
selectNode(parent.nextSibling.getID()); break;
}
}
/*
if (currentTreeNode.getParent().nextSibling != null) {
selectNode(currentTreeNode.getParent().nextSibling.getID());
}
*/
}
}
} function keyDown(event) { if (window.event) {
event = window.event;
} if (event.keyCode == 38) { // Up
selectPrevNode(); return false;
} else if (event.keyCode == 40) { // Down
selectNextNode(); return false;
} else if (event.keyCode == 37) { // left
subtractNode(); return false;
} else if (event.keyCode == 39) { // right
expandNode(); return false;
}
}
document.onkeydown = keyDown;
</script>
<script type="text/javascript" language="JavaScript">
showRootNode = false;
sortNodes = false;
dragable = false;
/**
* Needed to initialize the tree.
* And to call showTree(imagePath); to actually show the tree.
* Alternatively this can be done in a script block at the bottom of the page.
* Though this method is somewhat cleaner.
*/ function init() {
container = document.getElementById('examplediv');
showTree('');
}
/**
* Called when a user clicks on a node.
* @param treeNode the TreeNode object which have been clicked.
*/ function standardClick(treeNode) {
var mytext = document.getElementById('mytext');
var param = treeNode.getParam();
}
var closedGif = 'nanoImages/folder_closed.gif';
var openGif = 'nanoImages/folder_open.gif';
var pageIcon = 'nanoImages/page16x16.gif';
var userIcon = 'nanoImages/user_16x16.gif';
var helpIcon = 'nanoImages/help_16x16.gif';
rootNode = new TreeNode(1,'RootNode');
var node1 = new TreeNode(2,'subpage 1',helpIcon,'<P>This treenode can be renamed (Try to click onnce more on the node).<br>(You have to handle the renaming itself in a function though, since this is implementation specific. ie: Go down in a database and change the titel of a page).<br>A good example would be to open a PHP script to do the renaming, this can for example be done by setting location.href on a hidden iframe, or by setting the src attribute on a script element.</P>');
node1.setEditable(true);
node1.addEditEventListener('nodeEdited');
var node2 = new TreeNode(3,'subpage 2',new Array(closedGif,openGif));
var node2a = new TreeNode(4,'1st subpage to Node 2',new Array(closedGif,openGif));
var node2aa = new TreeNode(5,'subpage to subpage of Node2',new Array(closedGif,openGif));
var node2aaa = new TreeNode(6,'Some child',pageIcon);
var node2aab = new TreeNode(7,'Some child',pageIcon);
var node2aac = new TreeNode(8,'Some child',pageIcon);
node2aa.addChild(node2aaa);
node2aa.addChild(node2aab);
node2aa.addChild(node2aac);
node2a.addChild(node2aa);
var node2b = new TreeNode(9,'2nd subpage to Node 2',pageIcon);
var node2c = new TreeNode(10,'3rd subpageto Node 2',pageIcon);
node2.addChild(node2a);
node2.addChild(node2b);
node2.addChild(node2c);
var node3 = new TreeNode(11,'subpage 3',new Array(closedGif,openGif));
var node3a = new TreeNode(12,'Yet another child',pageIcon);
node3.addChild(node3a);
var node4 = new TreeNode(13,'subpage 4',userIcon);
// rootNode.addChild(node1);
rootNode.addChild(node2);
rootNode.addChild(node3);
// rootNode.addChild(node4);
</script>
<style type="text/css">
div,p,a {
font-family: Verdana,Arial;
font-size: 11px;
}
#exampletable {
width: 100%;
height: 100%;
}
#examplediv {
width: 250px;
height: 100%;
overflow: auto;
}
.explanation {
font-weight: bold;
font-style: italic;
}
</style>
</head>
<body OnLoad="init();">
<table id="exampletable">
<tr>
<td valign="top" style="width: 250px;">
<div id="examplediv"></div>
</td>
<td valign="top">
<div style="background-color:#EEE;border-style:dashed;border-color:#000000;border-width:1px;padding:5px;">
<div style="font-weight: bold;">NanoTree.</div>
<div id="mytext">
<p>NanoTree is a JavaScript tree, published under the <a href="http://www.gnu.org/copyleft/lesser.html">LGPL License</a>, which is developed to work in (at least) Internet Explorer and Mozilla<br>
<p style="font-style:italic;">CopyRight Martin Mouritzen</p>
</div>
</div>
<br>
</td>
</tr>
</table>
</body></html>