]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/dtree.js
169e990a24b71b23d5c89939556c2e83cdf9954b
[scalable-opengroupware.org] / UI / WebServerResources / 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 // Node object
13 function Node(id, pid, name, isParent, url, dataname, datatype, title, target,
14               icon, iconOpen, open) {
15   this.isParent = isParent;
16   this.id = id;
17   this.pid = pid;
18   this.name = name;
19   this.url = url; 
20   this.title = title;
21   this.target = target;
22   this.icon = icon;
23   this.iconOpen = iconOpen;
24   this.dataname = dataname;
25   this.datatype = datatype;
26   this._io = open || false;
27   this._is = false;
28   this._ls = false;
29   this._hc = false;
30   this._ai = 0;
31   this._p;
32 };
33
34 // Tree object
35 function dTree(objName) {
36   this.config = {
37     target : null,
38     hideRoot : false,
39     folderLinks : true,
40     useSelection : true,
41     useCookies : false,
42     useLines : true,
43     useIcons : true,
44     useStatusText : false,
45     closeSameLevel : false,
46     inOrder : false
47   }
48   this.icon = {
49     root : 'img/base.gif',
50     folder : 'img/folder.gif',
51     folderOpen : 'img/folderopen.gif',
52     node : 'img/page.gif',
53     empty : 'img/empty.gif',
54     line : 'img/line.gif',
55     join : 'img/join.gif',
56     joinBottom : 'img/joinbottom.gif',
57     plus : 'img/plus.gif',
58     plusBottom : 'img/plusbottom.gif',
59     minus : 'img/minus.gif',
60     minusBottom : 'img/minusbottom.gif',
61     nlPlus : 'img/nolines_plus.gif',
62     nlMinus : 'img/nolines_minus.gif'
63   };
64   this.obj = objName;
65   this.aNodes = [];
66   this.aIndent = [];
67   this.root = new Node(-1);
68   this.selectedNode = null;
69   this.selectedFound = false;
70   this.completed = false;
71 };
72
73 // Adds a new node to the node array
74 dTree.prototype.add = function(id, pid, name, isParent, url, datatype,
75                                title, target, icon, iconOpen, open) {
76   this.aNodes[this.aNodes.length] = new Node(id, pid, name, isParent, url,
77                                              datatype, title, target, icon,
78                                              iconOpen, open);
79 };
80
81 // Open/close all nodes
82 dTree.prototype.openAll = function() {
83   this.oAll(true);
84 };
85 dTree.prototype.closeAll = function() {
86   this.oAll(false);
87 };
88
89 // Outputs the tree to the page
90 dTree.prototype.toString = function() {
91   var str = '<div class="dtree" id="' + this.obj + '">\n';
92   if (document.getElementById) {
93     if (this.config.useCookies)
94       this.selectedNode = this.getSelected();
95     str += this.addNode(this.root);
96   } else str += 'Browser not supported.';
97   str += '</div>';
98   if (!this.selectedFound) this.selectedNode = null;
99   this.completed = true;
100   return str;
101 };
102
103 // Creates the tree structure
104 dTree.prototype.addNode = function(pNode) {
105   var str = '';
106   var n=0;
107   if (this.config.inOrder) n = pNode._ai;
108   for (n; n<this.aNodes.length; n++) {
109     if (this.aNodes[n].pid == pNode.id) {
110       var cn = this.aNodes[n];
111       cn._p = pNode;
112       cn._ai = n;
113       this.setCS(cn);
114       if (!cn.target && this.config.target) cn.target = this.config.target;
115       if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
116       if (!this.config.folderLinks && cn._hc) cn.url = null;
117       if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
118  cn._is = true;
119  this.selectedNode = n;
120  this.selectedFound = true;
121       }
122       str += this.node(cn, n);
123       if (cn._ls) break;
124     }
125   }
126   return str;
127 };
128
129 // Creates the node icon, url and text
130 dTree.prototype.node = function(node, nodeId) {
131   var str = '';
132
133   if (this.root.id != node.pid || !this.config.hideRoot) {
134     str += '<div class="dTreeNode"';
135     if (node.datatype) str += ' datatype="' + node.datatype + '"';
136     if (node.dataname) str += ' dataname="' + node.dataname + '"';
137     str += '>' + this.indent(node, nodeId);
138     if (node.url) {
139       str += '<a id="s' + this.obj + nodeId + '" class="node" href="' + node.url + '"';
140       if (node.title) str += ' title="' + node.title + '"';
141       if (node.target) str += ' target="' + node.target + '"';
142       if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
143       if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
144         str += ' onclick="' + this.obj + '.s(' + nodeId + ');"';
145       str += '>';
146     }
147     else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
148       str += '<a href="#" onclick="' + this.obj + '.o(' + nodeId + ');" class="node">';
149     if (this.config.useIcons) {
150       if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
151       if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
152       if (this.root.id == node.pid) {
153         node.icon = this.icon.root;
154         node.iconOpen = this.icon.root;
155       }
156       str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
157     }
158     str += '<span class="nodeName';
159     if (!node.isParent)
160       str += ' leaf';
161     str += '">' + node.name + '</span>';
162     if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
163     str += '</div>';
164   }
165   if (node._hc) {
166     str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
167     str += this.addNode(node);
168     str += '</div>';
169   }
170   this.aIndent.pop();
171   return str;
172 };
173
174 // Adds the empty and line icons
175 dTree.prototype.indent = function(node, nodeId) {
176   var str = '';
177   if (this.root.id != node.pid)
178   {
179     for (var n=0; n<this.aIndent.length; n++)
180       str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
181     (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
182     if (node._hc)
183       {
184         str += '<a href="#" onclick="return ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
185         if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
186         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 ) );
187         str += '" alt="" /></a>';
188       }
189     else
190       str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
191   }
192   return str;
193 };
194
195 // Checks if a node has any children and if it is the last sibling
196 dTree.prototype.setCS = function(node) {
197   var lastId;
198   for (var n=0; n<this.aNodes.length; n++) {
199     if (this.aNodes[n].pid == node.id) node._hc = true;
200     if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
201   }
202   if (lastId==node.id) node._ls = true;
203 };
204
205 // Returns the selected node
206 dTree.prototype.getSelected = function() {
207   var sn = this.getCookie('cs' + this.obj);
208   return (sn) ? sn : null;
209 };
210
211 // Highlights the selected node
212 dTree.prototype.s = function(id) {
213   if (!this.config.useSelection) return;
214   var cn = this.aNodes[id];
215   if (cn._hc && !this.config.folderLinks) return;
216   if (this.selectedNode != id) {
217     if (this.selectedNode || this.selectedNode==0) {
218       eOld = document.getElementById("s" + this.obj + this.selectedNode);
219       eOld.deselect();
220     }
221     eNew = document.getElementById("s" + this.obj + id);
222     eNew.select();
223     this.selectedNode = id;
224     if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
225   }
226 };
227
228 // Toggle Open or close
229 dTree.prototype.o = function(id) {
230   var cn = this.aNodes[id];
231   this.nodeStatus(!cn._io, id, cn._ls);
232   cn._io = !cn._io;
233   if (this.config.closeSameLevel) this.closeLevel(cn);
234   if (this.config.useCookies) this.updateCookie();
235
236   return false;
237 };
238
239 // Open or close all nodes
240 dTree.prototype.oAll = function(status) {
241   for (var n=0; n<this.aNodes.length; n++) {
242     if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
243       this.nodeStatus(status, n, this.aNodes[n]._ls)
244       this.aNodes[n]._io = status;
245     }
246   }
247   if (this.config.useCookies) this.updateCookie();
248 };
249
250 // Opens the tree to a specific node
251 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
252   if (!bFirst) {
253     for (var n=0; n<this.aNodes.length; n++) {
254       if (this.aNodes[n].id == nId) {
255         nId=n;
256         break;
257       }
258     }
259   }
260   var cn=this.aNodes[nId];
261   if (cn.pid==this.root.id || !cn._p) return;
262   cn._io = true;
263   cn._is = bSelect;
264   if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
265   if (this.completed && bSelect) this.s(cn._ai);
266   else if (bSelect) this._sn=cn._ai;
267   this.openTo(cn._p._ai, false, true);
268 };
269
270 // Closes all nodes on the same level as certain node
271 dTree.prototype.closeLevel = function(node) {
272   for (var n=0; n<this.aNodes.length; n++) {
273     if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
274       this.nodeStatus(false, n, this.aNodes[n]._ls);
275       this.aNodes[n]._io = false;
276       this.closeAllChildren(this.aNodes[n]);
277     }
278   }
279 }
280
281 // Closes all children of a node
282 dTree.prototype.closeAllChildren = function(node) {
283   for (var n=0; n<this.aNodes.length; n++) {
284     if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
285       if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
286       this.aNodes[n]._io = false;
287       this.closeAllChildren(this.aNodes[n]); 
288     }
289   }
290 }
291
292 // Change the status of a node(open or closed)
293 dTree.prototype.nodeStatus = function(status, id, bottom) {
294   eDiv = document.getElementById('d' + this.obj + id);
295   eJoin = document.getElementById('j' + this.obj + id);
296   if (this.config.useIcons) {
297     eIcon = document.getElementById('i' + this.obj + id);
298     eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
299   }
300   eJoin.src = (this.config.useLines)?
301   ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
302   ((status)?this.icon.nlMinus:this.icon.nlPlus);
303   eDiv.style.display = (status) ? 'block': 'none';
304 };
305
306
307 // [Cookie] Clears a cookie
308 dTree.prototype.clearCookie = function() {
309   var now = new Date();
310   var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
311   this.setCookie('co'+this.obj, 'cookieValue', yesterday);
312   this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
313 };
314
315 // [Cookie] Sets value in a cookie
316 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
317   document.cookie =
318   escape(cookieName) + '=' + escape(cookieValue)
319   + (expires ? '; expires=' + expires.toGMTString() : '')
320   + (path ? '; path=' + path : '')
321   + (domain ? '; domain=' + domain : '')
322   + (secure ? '; secure' : '');
323 };
324
325 // [Cookie] Gets a value from a cookie
326 dTree.prototype.getCookie = function(cookieName) {
327   var cookieValue = '';
328   var posName = document.cookie.indexOf(escape(cookieName) + '=');
329   if (posName != -1) {
330     var posValue = posName + (escape(cookieName) + '=').length;
331     var endPos = document.cookie.indexOf(';', posValue);
332     if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
333     else cookieValue = unescape(document.cookie.substring(posValue));
334   }
335   return (cookieValue);
336 };
337
338 // [Cookie] Returns ids of open nodes as a string
339 dTree.prototype.updateCookie = function() {
340   var str = '';
341   for (var n=0; n<this.aNodes.length; n++) {
342     if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
343       if (str) str += '.';
344       str += this.aNodes[n].id;
345     }
346   }
347   this.setCookie('co' + this.obj, str);
348 };
349
350 // [Cookie] Checks if a node id is in a cookie
351 dTree.prototype.isOpen = function(id) {
352   var aOpen = this.getCookie('co' + this.obj).split('.');
353   for (var n=0; n<aOpen.length; n++)
354   if (aOpen[n] == id) return true;
355   return false;
356 };
357
358 // If Push and pop is not implemented by the browser
359 if (!Array.prototype.push) {
360   Array.prototype.push = function array_push() {
361     for(var i=0;i<arguments.length;i++)
362     this[this.length]=arguments[i];
363     return this.length;
364   }
365 };
366
367 if (!Array.prototype.pop) {
368   Array.prototype.pop = function array_pop() {
369     lastElement = this[this.length-1];
370     this.length = Math.max(this.length-1,0);
371     return lastElement;
372   }
373 };
374