cleaning and moving a lot of files.
[feisty_meow.git] / webby / javascript / dtree / dtree.js
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 /*
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).
16  */
17
18 // Node object
19 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
20   this.id = id;
21   this.pid = pid;
22   this.name = name;
23   this.url = url;
24   this.title = title;
25   this.target = target;
26   this.icon = icon;
27   this.iconOpen = iconOpen;
28   this._io = open || false;
29   this._is = false;
30   this._ls = false;
31   this._hc = false;
32   this._ai = 0;
33   this._p;
34 };
35
36 // Tree object
37 function dTree(objName) {
38   this.config = {
39     target          : null,
40     folderLinks      : true,
41     useSelection    : true,
42     useCookies      : true,
43     useLines        : true,
44     useIcons        : true,
45     useStatusText    : false,
46     closeSameLevel  : false,
47     inOrder          : false
48   }
49   this.icon = {
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'
64   };
65   this.obj = objName;
66   this.aNodes = [];
67   this.aIndent = [];
68   this.root = new Node(-1);
69   this.selectedNode = null;
70   this.selectedFound = false;
71   this.completed = false;
72 };
73
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);
77 };
78
79 // Open/close all nodes
80 dTree.prototype.openAll = function() {
81   this.oAll(true);
82 };
83 dTree.prototype.closeAll = function() {
84   this.oAll(false);
85 };
86
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.';
95   str += '</div>';
96   if (!this.selectedFound) this.selectedNode = null;
97   this.completed = true;
98   return str;
99 }
100
101 //postponed node is no longer right.  we need to postpone different stuff.
102 function postponed_node(node_stored, partial_string)
103 {
104   this.node_stored = node_stored;
105   this.partial_string = partial_string;
106 }
107
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.
111
112 function open_tab(url)
113 {
114 //  var browser = document.getElementById("content");
115 //  var tab = browser.addTab(url);
116   var wingo = window.open(url);
117   wingo.focus();
118 }
119
120 function postpone_node(node_to_store, partial_string)
121 {
122   if (postpone_count >= MAX_DEPTH) {
123 //uhhhh
124 //complain
125     return;
126   }
127
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;
132   }
133   postpone_count++;
134
135   waiting_nodes[0] = postponed_node(node_to_store, partial_string);
136 }
137
138 function pull_next_node()
139 {
140   if (postpone_count == 0) {
141 //nothing left in list.
142     return '';
143   }
144
145   var str = waiting_nodes[0].partial_string;
146   var pNode = waiting_nodes[0].node_stored
147
148   var i = 1;
149   for (i; i < postpone_count; i++) {
150     waiting_nodes[i - 1] = waiting_nodes[i];
151   }
152   postpone_count--;
153
154   setTimer('pull_next_node()', 0);
155 }
156
157 // Creates the tree structure
158 dTree.prototype.addNode = function(pNode) {
159   var str = '';
160   var n=0;
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];
165       cn._p = pNode;
166       cn._ai = n;
167       this.setCS(cn);
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) {
172           cn._is = true;
173           this.selectedNode = n;
174           this.selectedFound = true;
175       }
176       str += this.node(cn, n);
177       if (cn._ls) break;
178     }
179   }
180   return str;
181 };
182
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;
192     }
193     str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
194   }
195   if (node.url) {
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 + '\')" ';
199
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 + ');"';
205     str += '>';
206   }
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">';
209   str += node.name;
210   if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
211   str += '</div>';
212   if (node._hc) {
213
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);
216     str += '</div>';
217
218   }
219   this.aIndent.pop();
220   return str;
221 };
222
223 // Adds the empty and line icons
224 dTree.prototype.indent = function(node, nodeId) {
225   var str = '';
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);
230     if (node._hc) {
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="" />';
236   }
237   return str;
238 };
239
240 // Checks if a node has any children and if it is the last sibling
241 dTree.prototype.setCS = function(node) {
242   var lastId;
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;
246   }
247   if (lastId==node.id) node._ls = true;
248 };
249
250 // Returns the selected node
251 dTree.prototype.getSelected = function() {
252   var sn = this.getCookie('cs' + this.obj);
253   return (sn) ? sn : null;
254 };
255
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";
265     }
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);
270   }
271 };
272
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);
277   cn._io = !cn._io;
278   if (this.config.closeSameLevel) this.closeLevel(cn);
279   if (this.config.useCookies) this.updateCookie();
280 };
281
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;
288     }
289   }
290   if (this.config.useCookies) this.updateCookie();
291 };
292
293 // Opens the tree to a specific node
294 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
295   if (!bFirst) {
296     for (var n=0; n<this.aNodes.length; n++) {
297       if (this.aNodes[n].id == nId) {
298         nId=n;
299         break;
300       }
301     }
302   }
303   var cn=this.aNodes[nId];
304   if (cn.pid==this.root.id || !cn._p) return;
305   cn._io = true;
306   cn._is = bSelect;
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);
311 };
312
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]);
320     }
321   }
322 }
323
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]);    
331     }
332   }
333 }
334
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;
342   }
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';
347 };
348
349
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);
356 };
357
358 // [Cookie] Sets value in a cookie
359 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
360   document.cookie =
361     escape(cookieName) + '=' + escape(cookieValue)
362     + (expires ? '; expires=' + expires.toGMTString() : '')
363     + (path ? '; path=' + path : '')
364     + (domain ? '; domain=' + domain : '')
365     + (secure ? '; secure' : '');
366 };
367
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) + '=');
372   if (posName != -1) {
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));
377   }
378   return (cookieValue);
379 };
380
381 // [Cookie] Returns ids of open nodes as a string
382 dTree.prototype.updateCookie = function() {
383   var str = '';
384   for (var n=0; n<this.aNodes.length; n++) {
385     if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
386       if (str) str += '.';
387       str += this.aNodes[n].id;
388     }
389   }
390   this.setCookie('co' + this.obj, str);
391 };
392
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;
398   return false;
399 };
400
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];
406     return this.length;
407   }
408 };
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);
413     return lastElement;
414   }
415 };