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