1/*--------------------------------------------------|
2| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3|---------------------------------------------------|
4| Copyright (c) 2002-2003 Geir Landr�               |
5|                                                   |
6| This script can be used freely as long as all     |
7| copyright messages are intact.                    |
8|                                                   |
9| Updated: 17.04.2003                               |
10|--------------------------------------------------*/
11
12// Node object
13function Node(id, pid, name, io, shc, icon, iconOpen, open) {
14	this.id = id;
15	this.pid = pid;
16	this.name = name;
17	this.io = io;
18	this.shc = shc;
19	this.icon = icon;
20	this.iconOpen = iconOpen;
21	this._is = false;
22	this._ls = false;
23	this._hc = false;
24	this._ai = 0;
25	this._p;
26};
27
28// Tree object
29function dTree(objName,selectedNode) {
30
31	this.icon = {
32		root				: 'base.gif',
33		folder			: 'folder.gif',
34		folderOpen	: 'folderopen.gif',
35		node				: 'page.gif',
36		empty				: 'empty.gif',
37		line				: 'line.gif',
38		join				: 'join.gif',
39		joinBottom	: 'joinbottom.gif',
40		plus				: 'plus.gif',
41		plusBottom	: 'plusbottom.gif',
42		minus				: 'minus.gif',
43		minusBottom	: 'minusbottom.gif'
44	};
45	this.obj = objName;
46	this.aNodes = [];
47	this.aIndent = [];
48	this.root = new Node(-1);
49	this.selectedNode = selectedNode;
50	this.completed = false;
51};
52
53// Adds a new node to the node array
54dTree.prototype.add = function(id, pid, name, io, shc, icon, iconOpen, open) {
55	this.aNodes[this.aNodes.length] = new Node(id, pid, name, io, shc, icon, iconOpen, open);
56};
57
58// Outputs the tree to the page
59dTree.prototype.toString = function() {
60	var str = '<div class="dtree">\n';
61	if (document.getElementById) {
62		str += this.addNode(this.root);
63	} else str += 'Browser not supported.';
64	str += '</div>';
65	this.completed = true;
66	return str;
67};
68
69// Creates the tree structure
70dTree.prototype.addNode = function(pNode) {
71	var str = '';
72	var n=0;
73	for (n; n<this.aNodes.length; n++) {
74		if (this.aNodes[n].pid == pNode.id) {
75			var cn = this.aNodes[n];
76			cn._p = pNode;
77			cn._ai = n;
78			this.setCS(cn);
79
80			if ( cn.id == this.selectedNode ) {
81					cn._is = true;
82					this.selectedNode = n;
83			}
84
85			str += this.node(cn, n);
86			if (cn._ls) break;
87		}
88	}
89	return str;
90};
91
92// Creates the node icon, url and text
93dTree.prototype.node = function(node, nodeId) {
94	var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
95
96		if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : this.icon.folder;
97		if (!node.iconOpen) node.iconOpen =  this.icon.folderOpen ;
98		if (this.root.id == node.pid) {
99			node.icon = this.icon.root;
100			node.iconOpen = this.icon.root;
101		}
102		str += '<img id="i' + this.obj + nodeId + '" src="' + ((node.io == '1') ? node.iconOpen : node.icon) + '" alt="" />';
103
104		if(this.root.id == node.pid){
105		str += '<a id="s' + this.obj + nodeId + '">';
106		}
107		else{
108		str += '<a id="s' + this.obj + nodeId + '" href="javascript: ' + this.obj + '.s(' + nodeId + ');"  class="'+((node.id == this.selectedNode) ? 'nodeSel' : 'node')+'">';
109		}
110	str += node.name;
111
112	str += '</a>';
113	str += '</div>';
114	if (node._hc) {
115		str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node.io == '1' ) ? 'block' : 'none') + ';">';
116		str += this.addNode(node);
117		str += '</div>';
118	}
119	else
120		str += '<div id="d' + this.obj + nodeId + '" class="clip"></div>';
121	this.aIndent.pop();
122	return str;
123};
124function test()
125{
126//	alert("test");
127}
128// Adds the empty and line icons
129dTree.prototype.indent = function(node, nodeId) {
130	var str = '';
131	if (this.root.id != node.pid) {
132		for (var n=0; n<this.aIndent.length; n++)
133			str += '<img src="' + ( (this.aIndent[n] == 1) ? this.icon.line : this.icon.empty ) + '" alt="" />';
134		(node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
135		if (node.shc == '1') {
136			str += '<a href="javascript: ' + this.obj + '.s(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
137
138		 str += ( (node.io == '1') ? ((node._ls ) ? this.icon.minusBottom : this.icon.minus) : ((node._ls ) ? this.icon.plusBottom : this.icon.plus ) );
139			str += '" alt="" /></a>';
140
141		} else str += '<img id="j' + this.obj + nodeId + '" src="' + ((node._ls) ? this.icon.joinBottom : this.icon.join ) + '" alt="" />';
142	}
143	return str;
144};
145
146// Checks if a node has any children and if it is the last sibling
147dTree.prototype.setCS = function(node) {
148	var lastId;
149	for (var n=0; n<this.aNodes.length; n++) {
150		if (this.aNodes[n].pid == node.id) node._hc = true;
151		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
152	}
153	if (lastId==node.id) node._ls = true;
154};
155
156// Highlights the selected node
157dTree.prototype.s = function(id) {
158
159	var cn = this.aNodes[id];
160//.o
161	var Dio=(cn.io == '1')? '0':'1';
162	cn.io = Dio;
163
164	if (this.selectedNode != id)
165		if (this.selectedNode || this.selectedNode==0) {
166			eOld = document.getElementById("s" + this.obj + this.selectedNode);
167			eOld.className = "node";
168		}
169
170		eNew = document.getElementById("s" + this.obj + id);
171		eNew.className = "nodeSel";
172		this.selectedNode = id;
173
174	var form=document.forms[0];
175	var str = eval('node' + id);
176	echo_info = str.split('*');
177	form.nodenumber.value=id;
178	form.currentpath.value=echo_info[3];
179	//form.folder_path.value=echo_info[3];
180	var value=""+echo_info[0]+"*"+echo_info[1]+"*"+cn.io+"*"+echo_info[3]+"*"+echo_info[4];
181	form.nodevalue.value=value;
182
183	if (cn.io == '1'){
184		form.submit_flag.value="browser_open";
185		form.submit();
186	}
187	else{
188		form.submit_flag.value="browser_close";
189		form.submit();
190	}
191}
192// If Push and pop is not implemented by the browser
193if (!Array.prototype.push) {
194	Array.prototype.push = function array_push() {
195		for(var i=0;i<arguments.length;i++)
196			this[this.length]=arguments[i];
197		return this.length;
198	}
199};
200if (!Array.prototype.pop) {
201	Array.prototype.pop = function array_pop() {
202		lastElement = this[this.length-1];
203		this.length = Math.max(this.length-1,0);
204		return lastElement;
205	}
206};
207