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