1 /*--------------------------------------------------|
2 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3 |---------------------------------------------------|
4 | Copyright (c) 2002-2003 Geir Landrö |
6 | This script can be used freely as long as all |
7 | copyright messages are intact. |
9 | Updated: 17.04.2003 |
10 |--------------------------------------------------*/
13 * Modified by Chris Koeritz for http://gruntose.com (c) copyright 5/2005
14 * All changes donated back into public domain following same license
15 * as author (Geir Landroe).
19 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
27 this.iconOpen = iconOpen;
28 this._io = open || false;
37 function dTree(objName) {
45 useStatusText : false,
46 closeSameLevel : false,
50 root : 'dtree_img/base.gif',
51 folder : 'dtree_img/folder.gif',
52 folderOpen : 'dtree_img/folderopen.gif',
53 node : 'dtree_img/page.gif',
54 empty : 'dtree_img/empty.gif',
55 line : 'dtree_img/line.gif',
56 join : 'dtree_img/join.gif',
57 joinBottom : 'dtree_img/joinbottom.gif',
58 plus : 'dtree_img/plus.gif',
59 plusBottom : 'dtree_img/plusbottom.gif',
60 minus : 'dtree_img/minus.gif',
61 minusBottom : 'dtree_img/minusbottom.gif',
62 nlPlus : 'dtree_img/nolines_plus.gif',
63 nlMinus : 'dtree_img/nolines_minus.gif'
68 this.root = new Node(-1);
69 this.selectedNode = null;
70 this.selectedFound = false;
71 this.completed = false;
74 // Adds a new node to the node array
75 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
76 this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
79 // Open/close all nodes
80 dTree.prototype.openAll = function() {
83 dTree.prototype.closeAll = function() {
87 // Outputs the tree to the page
88 dTree.prototype.toString = function() {
89 var str = ''; // reset in case multiple trees are used.
90 str += '<div class="dtree">\n';
91 if (document.getElementById) {
92 if (this.config.useCookies) this.selectedNode = this.getSelected();
93 str += this.addNode(this.root);
94 } else str += 'Browser not supported.';
96 if (!this.selectedFound) this.selectedNode = null;
97 this.completed = true;
101 //postponed node is no longer right. we need to postpone different stuff.
102 function postponed_node(node_stored, partial_string)
104 this.node_stored = node_stored;
105 this.partial_string = partial_string;
108 const MAX_DEPTH = 1000; // maximum number of nodes postponed at once.
109 var waiting_nodes = Array(MAX_DEPTH); // the list of postponed nodes.
110 var postpone_count = 0; // how many nodes are stored in the table.
112 function open_tab(url)
114 // var browser = document.getElementById("content");
115 // var tab = browser.addTab(url);
116 var wingo = window.open(url);
120 function postpone_node(node_to_store, partial_string)
122 if (postpone_count >= MAX_DEPTH) {
128 // move out the other nodes that are waiting.
129 var i = postpone_count - 1;
130 for (i; i >= 0; i--) {
131 waiting_nodes[i + 1] = waiting_nodes[i].partial_string;
135 waiting_nodes[0] = postponed_node(node_to_store, partial_string);
138 function pull_next_node()
140 if (postpone_count == 0) {
141 //nothing left in list.
145 var str = waiting_nodes[0].partial_string;
146 var pNode = waiting_nodes[0].node_stored
149 for (i; i < postpone_count; i++) {
150 waiting_nodes[i - 1] = waiting_nodes[i];
154 setTimer('pull_next_node()', 0);
157 // Creates the tree structure
158 dTree.prototype.addNode = function(pNode) {
161 if (this.config.inOrder) n = pNode._ai;
162 for (n; n<this.aNodes.length; n++) {
163 if (this.aNodes[n].pid == pNode.id) {
164 var cn = this.aNodes[n];
168 if (!cn.target && this.config.target) cn.target = this.config.target;
169 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
170 if (!this.config.folderLinks && cn._hc) cn.url = null;
171 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
173 this.selectedNode = n;
174 this.selectedFound = true;
176 str += this.node(cn, n);
183 // Creates the node icon, url and text
184 dTree.prototype.node = function(node, nodeId) {
185 var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
186 if (this.config.useIcons) {
187 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
188 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
189 if (this.root.id == node.pid) {
190 node.icon = this.icon.root;
191 node.iconOpen = this.icon.root;
193 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
196 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="" '
197 + 'onclick="open_tab(\''
198 + node.url + '\')" ';
200 if (node.title) str += ' title="' + node.title + '"';
201 if (node.target) str += ' target="' + node.target + '"';
202 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
203 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
204 str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
207 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
208 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
210 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
214 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
215 str += this.addNode(node);
223 // Adds the empty and line icons
224 dTree.prototype.indent = function(node, nodeId) {
226 if (this.root.id != node.pid) {
227 for (var n=0; n<this.aIndent.length; n++)
228 str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
229 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
231 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
232 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
233 else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
234 str += '" alt="" /></a>';
235 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
240 // Checks if a node has any children and if it is the last sibling
241 dTree.prototype.setCS = function(node) {
243 for (var n=0; n<this.aNodes.length; n++) {
244 if (this.aNodes[n].pid == node.id) node._hc = true;
245 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
247 if (lastId==node.id) node._ls = true;
250 // Returns the selected node
251 dTree.prototype.getSelected = function() {
252 var sn = this.getCookie('cs' + this.obj);
253 return (sn) ? sn : null;
256 // Highlights the selected node
257 dTree.prototype.s = function(id) {
258 if (!this.config.useSelection) return;
259 var cn = this.aNodes[id];
260 if (cn._hc && !this.config.folderLinks) return;
261 if (this.selectedNode != id) {
262 if (this.selectedNode || this.selectedNode==0) {
263 eOld = document.getElementById("s" + this.obj + this.selectedNode);
264 eOld.className = "node";
266 eNew = document.getElementById("s" + this.obj + id);
267 eNew.className = "nodeSel";
268 this.selectedNode = id;
269 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
273 // Toggle Open or close
274 dTree.prototype.o = function(id) {
275 var cn = this.aNodes[id];
276 this.nodeStatus(!cn._io, id, cn._ls);
278 if (this.config.closeSameLevel) this.closeLevel(cn);
279 if (this.config.useCookies) this.updateCookie();
282 // Open or close all nodes
283 dTree.prototype.oAll = function(status) {
284 for (var n=0; n<this.aNodes.length; n++) {
285 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
286 this.nodeStatus(status, n, this.aNodes[n]._ls)
287 this.aNodes[n]._io = status;
290 if (this.config.useCookies) this.updateCookie();
293 // Opens the tree to a specific node
294 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
296 for (var n=0; n<this.aNodes.length; n++) {
297 if (this.aNodes[n].id == nId) {
303 var cn=this.aNodes[nId];
304 if (cn.pid==this.root.id || !cn._p) return;
307 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
308 if (this.completed && bSelect) this.s(cn._ai);
309 else if (bSelect) this._sn=cn._ai;
310 this.openTo(cn._p._ai, false, true);
313 // Closes all nodes on the same level as certain node
314 dTree.prototype.closeLevel = function(node) {
315 for (var n=0; n<this.aNodes.length; n++) {
316 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
317 this.nodeStatus(false, n, this.aNodes[n]._ls);
318 this.aNodes[n]._io = false;
319 this.closeAllChildren(this.aNodes[n]);
324 // Closes all children of a node
325 dTree.prototype.closeAllChildren = function(node) {
326 for (var n=0; n<this.aNodes.length; n++) {
327 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
328 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
329 this.aNodes[n]._io = false;
330 this.closeAllChildren(this.aNodes[n]);
335 // Change the status of a node (open or closed)
336 dTree.prototype.nodeStatus = function(status, id, bottom) {
337 eDiv = document.getElementById('d' + this.obj + id);
338 eJoin = document.getElementById('j' + this.obj + id);
339 if (this.config.useIcons) {
340 eIcon = document.getElementById('i' + this.obj + id);
341 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
343 eJoin.src = (this.config.useLines)?
344 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
345 ((status)?this.icon.nlMinus:this.icon.nlPlus);
346 eDiv.style.display = (status) ? 'block': 'none';
350 // [Cookie] Clears a cookie
351 dTree.prototype.clearCookie = function() {
352 var now = new Date();
353 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
354 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
355 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
358 // [Cookie] Sets value in a cookie
359 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
361 escape(cookieName) + '=' + escape(cookieValue)
362 + (expires ? '; expires=' + expires.toGMTString() : '')
363 + (path ? '; path=' + path : '')
364 + (domain ? '; domain=' + domain : '')
365 + (secure ? '; secure' : '');
368 // [Cookie] Gets a value from a cookie
369 dTree.prototype.getCookie = function(cookieName) {
370 var cookieValue = '';
371 var posName = document.cookie.indexOf(escape(cookieName) + '=');
373 var posValue = posName + (escape(cookieName) + '=').length;
374 var endPos = document.cookie.indexOf(';', posValue);
375 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
376 else cookieValue = unescape(document.cookie.substring(posValue));
378 return (cookieValue);
381 // [Cookie] Returns ids of open nodes as a string
382 dTree.prototype.updateCookie = function() {
384 for (var n=0; n<this.aNodes.length; n++) {
385 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
387 str += this.aNodes[n].id;
390 this.setCookie('co' + this.obj, str);
393 // [Cookie] Checks if a node id is in a cookie
394 dTree.prototype.isOpen = function(id) {
395 var aOpen = this.getCookie('co' + this.obj).split('.');
396 for (var n=0; n<aOpen.length; n++)
397 if (aOpen[n] == id) return true;
401 // If Push and pop is not implemented by the browser
402 if (!Array.prototype.push) {
403 Array.prototype.push = function array_push() {
404 for(var i=0;i<arguments.length;i++)
405 this[this.length]=arguments[i];
409 if (!Array.prototype.pop) {
410 Array.prototype.pop = function array_pop() {
411 lastElement = this[this.length-1];
412 this.length = Math.max(this.length-1,0);