93222bea |
1 | /*--------------------------------------------------| |
2 | |
3 | | dTree 2.05 | www.destroydrop.com/javascript/tree/ | |
4 | |
5 | |---------------------------------------------------| |
6 | |
7 | | Copyright (c) 2002-2003 Geir Landr? | |
8 | |
9 | | | |
10 | |
11 | | This script can be used freely as long as all | |
12 | |
13 | | copyright messages are intact. | |
14 | |
15 | | | |
16 | |
17 | | Updated: 17.04.2003 | |
18 | |
19 | |--------------------------------------------------*/ |
20 | |
21 | |
22 | |
23 | // Node object |
24 | |
25 | function Node(id, pid, name, url, title, target, icon, iconOpen, open) { |
26 | |
27 | this.id = id; |
28 | |
29 | this.pid = pid; |
30 | |
31 | this.name = name; |
32 | |
33 | this.url = url; |
34 | |
35 | this.title = title; |
36 | |
37 | this.target = target; |
38 | |
39 | this.icon = icon; |
40 | |
41 | this.iconOpen = iconOpen; |
42 | |
43 | this._io = open || false; |
44 | |
45 | this._is = false; |
46 | |
47 | this._ls = false; |
48 | |
49 | this._hc = false; |
50 | |
51 | this._ai = 0; |
52 | |
53 | this._p; |
54 | |
55 | }; |
56 | |
57 | |
58 | |
59 | // Tree object |
60 | // imagePath parameter added by SquirrelMail Team |
61 | function dTree(objName, imagePath) { |
62 | |
63 | this.config = { |
64 | |
65 | target : null, |
66 | |
67 | folderLinks : true, |
68 | |
69 | useSelection : true, |
70 | |
71 | useCookies : true, |
72 | |
73 | useLines : true, |
74 | |
75 | useIcons : true, |
76 | |
77 | useStatusText : false, |
78 | |
79 | closeSameLevel : false, |
80 | |
81 | inOrder : false |
82 | |
83 | } |
84 | |
85 | this.icon = { |
86 | |
94432dfa |
87 | root : imagePath+'/base.png', |
93222bea |
88 | |
94432dfa |
89 | folder : imagePath+'/folder.png', |
93222bea |
90 | |
94432dfa |
91 | folderOpen : imagePath+'/folderopen.png', |
93222bea |
92 | |
94432dfa |
93 | node : imagePath+'/page.png', |
93222bea |
94 | |
94432dfa |
95 | empty : imagePath+'/empty.png', |
93222bea |
96 | |
94432dfa |
97 | line : imagePath+'/line.png', |
93222bea |
98 | |
94432dfa |
99 | join : imagePath+'/join.png', |
93222bea |
100 | |
94432dfa |
101 | joinBottom : imagePath+'/joinbottom.png', |
93222bea |
102 | |
94432dfa |
103 | plus : imagePath+'/plus.png', |
93222bea |
104 | |
94432dfa |
105 | plusBottom : imagePath+'/plusbottom.png', |
93222bea |
106 | |
94432dfa |
107 | minus : imagePath+'/minus.png', |
93222bea |
108 | |
94432dfa |
109 | minusBottom : imagePath+'/minusbottom.png', |
93222bea |
110 | |
94432dfa |
111 | nlPlus : imagePath+'/nolines_plus.png', |
93222bea |
112 | |
94432dfa |
113 | nlMinus : imagePath+'/nolines_minus.png' |
93222bea |
114 | |
115 | }; |
116 | |
117 | this.obj = objName; |
118 | |
119 | this.aNodes = []; |
120 | |
121 | this.aIndent = []; |
122 | |
123 | this.root = new Node(-1); |
124 | |
125 | this.selectedNode = null; |
126 | |
127 | this.selectedFound = false; |
128 | |
129 | this.completed = false; |
130 | |
131 | this.imagePath = imagePath; |
132 | |
133 | }; |
134 | |
135 | |
136 | |
137 | // Adds a new node to the node array |
138 | |
139 | dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) { |
140 | |
141 | this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open); |
142 | |
143 | }; |
144 | |
145 | |
146 | |
147 | // Open/close all nodes |
148 | |
149 | dTree.prototype.openAll = function() { |
150 | |
151 | this.oAll(true); |
152 | |
153 | }; |
154 | |
155 | dTree.prototype.closeAll = function() { |
156 | |
157 | this.oAll(false); |
158 | |
159 | }; |
160 | |
161 | |
162 | |
163 | // Outputs the tree to the page |
164 | |
165 | dTree.prototype.toString = function() { |
166 | |
167 | var str = '<div class="dtree">\n'; |
168 | |
169 | if (document.getElementById) { |
170 | |
171 | if (this.config.useCookies) this.selectedNode = this.getSelected(); |
172 | |
173 | str += this.addNode(this.root); |
174 | |
175 | } else str += 'Browser not supported.'; |
176 | |
177 | str += '</div>'; |
178 | |
179 | if (!this.selectedFound) this.selectedNode = null; |
180 | |
181 | this.completed = true; |
182 | |
183 | return str; |
184 | |
185 | }; |
186 | |
187 | |
188 | |
189 | // Creates the tree structure |
190 | |
191 | dTree.prototype.addNode = function(pNode) { |
192 | |
193 | var str = ''; |
194 | |
195 | var n=0; |
196 | |
197 | if (this.config.inOrder) n = pNode._ai; |
198 | |
199 | for (n; n<this.aNodes.length; n++) { |
200 | |
201 | if (this.aNodes[n].pid == pNode.id) { |
202 | |
203 | var cn = this.aNodes[n]; |
204 | |
205 | cn._p = pNode; |
206 | |
207 | cn._ai = n; |
208 | |
209 | this.setCS(cn); |
210 | |
211 | if (!cn.target && this.config.target) cn.target = this.config.target; |
212 | |
213 | if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id); |
214 | |
215 | if (!this.config.folderLinks && cn._hc) cn.url = null; |
216 | |
217 | if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) { |
218 | |
219 | cn._is = true; |
220 | |
221 | this.selectedNode = n; |
222 | |
223 | this.selectedFound = true; |
224 | |
225 | } |
226 | |
227 | str += this.node(cn, n); |
228 | |
229 | if (cn._ls) break; |
230 | |
231 | } |
232 | |
233 | } |
234 | |
235 | return str; |
236 | |
237 | }; |
238 | |
239 | |
240 | |
241 | // Creates the node icon, url and text |
242 | |
243 | dTree.prototype.node = function(node, nodeId) { |
244 | |
245 | var str = '<div class="dTreeNode">' + this.indent(node, nodeId); |
246 | |
247 | if (this.config.useIcons) { |
248 | |
249 | if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node); |
250 | |
251 | if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node; |
252 | |
253 | if (this.root.id == node.pid) { |
254 | |
255 | node.icon = this.icon.root; |
256 | |
257 | node.iconOpen = this.icon.root; |
258 | |
259 | } |
260 | |
261 | str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />'; |
262 | |
263 | } |
264 | |
265 | if (node.url) { |
266 | |
267 | str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"'; |
268 | |
269 | if (node.title) str += ' title="' + node.title + '"'; |
270 | |
271 | if (node.target) str += ' target="' + node.target + '"'; |
272 | |
273 | if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" '; |
274 | |
275 | if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc)) |
276 | |
277 | str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"'; |
278 | |
279 | str += '>'; |
280 | |
281 | } |
282 | |
283 | else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id) |
284 | |
285 | str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">'; |
286 | |
287 | str += node.name; |
288 | |
289 | if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>'; |
290 | |
291 | str += '</div>'; |
292 | |
293 | if (node._hc) { |
294 | |
295 | str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">'; |
296 | |
297 | str += this.addNode(node); |
298 | |
299 | str += '</div>'; |
300 | |
301 | } |
302 | |
303 | this.aIndent.pop(); |
304 | |
305 | return str; |
306 | |
307 | }; |
308 | |
309 | |
310 | |
311 | // Adds the empty and line icons |
312 | |
313 | dTree.prototype.indent = function(node, nodeId) { |
314 | |
315 | var str = ''; |
316 | |
317 | if (this.root.id != node.pid) { |
318 | |
319 | for (var n=0; n<this.aIndent.length; n++) |
320 | |
321 | str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />'; |
322 | |
323 | (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1); |
324 | |
325 | if (node._hc) { |
326 | |
327 | str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="'; |
328 | |
329 | if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus; |
330 | |
331 | 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 ) ); |
332 | |
333 | str += '" alt="" /></a>'; |
334 | |
335 | } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />'; |
336 | |
337 | } |
338 | |
339 | return str; |
340 | |
341 | }; |
342 | |
343 | |
344 | |
345 | // Checks if a node has any children and if it is the last sibling |
346 | |
347 | dTree.prototype.setCS = function(node) { |
348 | |
349 | var lastId; |
350 | |
351 | for (var n=0; n<this.aNodes.length; n++) { |
352 | |
353 | if (this.aNodes[n].pid == node.id) node._hc = true; |
354 | |
355 | if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id; |
356 | |
357 | } |
358 | |
359 | if (lastId==node.id) node._ls = true; |
360 | |
361 | }; |
362 | |
363 | |
364 | |
365 | // Returns the selected node |
366 | |
367 | dTree.prototype.getSelected = function() { |
368 | |
369 | var sn = this.getCookie('cs' + this.obj); |
370 | |
371 | return (sn) ? sn : null; |
372 | |
373 | }; |
374 | |
375 | |
376 | |
377 | // Highlights the selected node |
378 | |
379 | dTree.prototype.s = function(id) { |
380 | |
381 | if (!this.config.useSelection) return; |
382 | |
383 | var cn = this.aNodes[id]; |
384 | |
385 | if (cn._hc && !this.config.folderLinks) return; |
386 | |
387 | if (this.selectedNode != id) { |
388 | |
389 | if (this.selectedNode || this.selectedNode==0) { |
390 | |
391 | eOld = document.getElementById("s" + this.obj + this.selectedNode); |
392 | |
393 | eOld.className = "node"; |
394 | |
395 | } |
396 | |
397 | eNew = document.getElementById("s" + this.obj + id); |
398 | |
399 | eNew.className = "nodeSel"; |
400 | |
401 | this.selectedNode = id; |
402 | |
403 | if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id); |
404 | |
405 | } |
406 | |
407 | }; |
408 | |
409 | |
410 | |
411 | // Toggle Open or close |
412 | |
413 | dTree.prototype.o = function(id) { |
414 | |
415 | var cn = this.aNodes[id]; |
416 | |
417 | this.nodeStatus(!cn._io, id, cn._ls); |
418 | |
419 | cn._io = !cn._io; |
420 | |
421 | if (this.config.closeSameLevel) this.closeLevel(cn); |
422 | |
423 | if (this.config.useCookies) this.updateCookie(); |
424 | |
425 | }; |
426 | |
427 | |
428 | |
429 | // Open or close all nodes |
430 | |
431 | dTree.prototype.oAll = function(status) { |
432 | |
433 | for (var n=0; n<this.aNodes.length; n++) { |
434 | |
435 | if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) { |
436 | |
437 | this.nodeStatus(status, n, this.aNodes[n]._ls) |
438 | |
439 | this.aNodes[n]._io = status; |
440 | |
441 | } |
442 | |
443 | } |
444 | |
445 | if (this.config.useCookies) this.updateCookie(); |
446 | |
447 | }; |
448 | |
449 | |
450 | |
451 | // Opens the tree to a specific node |
452 | |
453 | dTree.prototype.openTo = function(nId, bSelect, bFirst) { |
454 | |
455 | if (!bFirst) { |
456 | |
457 | for (var n=0; n<this.aNodes.length; n++) { |
458 | |
459 | if (this.aNodes[n].id == nId) { |
460 | |
461 | nId=n; |
462 | |
463 | break; |
464 | |
465 | } |
466 | |
467 | } |
468 | |
469 | } |
470 | |
471 | var cn=this.aNodes[nId]; |
472 | |
473 | if (cn.pid==this.root.id || !cn._p) return; |
474 | |
475 | cn._io = true; |
476 | |
477 | cn._is = bSelect; |
478 | |
479 | if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls); |
480 | |
481 | if (this.completed && bSelect) this.s(cn._ai); |
482 | |
483 | else if (bSelect) this._sn=cn._ai; |
484 | |
485 | this.openTo(cn._p._ai, false, true); |
486 | |
487 | }; |
488 | |
489 | |
490 | |
491 | // Closes all nodes on the same level as certain node |
492 | |
493 | dTree.prototype.closeLevel = function(node) { |
494 | |
495 | for (var n=0; n<this.aNodes.length; n++) { |
496 | |
497 | if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) { |
498 | |
499 | this.nodeStatus(false, n, this.aNodes[n]._ls); |
500 | |
501 | this.aNodes[n]._io = false; |
502 | |
503 | this.closeAllChildren(this.aNodes[n]); |
504 | |
505 | } |
506 | |
507 | } |
508 | |
509 | } |
510 | |
511 | |
512 | |
513 | // Closes all children of a node |
514 | |
515 | dTree.prototype.closeAllChildren = function(node) { |
516 | |
517 | for (var n=0; n<this.aNodes.length; n++) { |
518 | |
519 | if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) { |
520 | |
521 | if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls); |
522 | |
523 | this.aNodes[n]._io = false; |
524 | |
525 | this.closeAllChildren(this.aNodes[n]); |
526 | |
527 | } |
528 | |
529 | } |
530 | |
531 | } |
532 | |
533 | |
534 | |
535 | // Change the status of a node(open or closed) |
536 | |
537 | dTree.prototype.nodeStatus = function(status, id, bottom) { |
538 | |
539 | eDiv = document.getElementById('d' + this.obj + id); |
540 | |
541 | eJoin = document.getElementById('j' + this.obj + id); |
542 | |
543 | if (this.config.useIcons) { |
544 | |
545 | eIcon = document.getElementById('i' + this.obj + id); |
546 | |
547 | eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon; |
548 | |
549 | } |
550 | |
551 | eJoin.src = (this.config.useLines)? |
552 | |
553 | ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)): |
554 | |
555 | ((status)?this.icon.nlMinus:this.icon.nlPlus); |
556 | |
557 | eDiv.style.display = (status) ? 'block': 'none'; |
558 | |
559 | }; |
560 | |
561 | |
562 | |
563 | |
564 | |
565 | // [Cookie] Clears a cookie |
566 | |
567 | dTree.prototype.clearCookie = function() { |
568 | |
569 | var now = new Date(); |
570 | |
571 | var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24); |
572 | |
573 | this.setCookie('co'+this.obj, 'cookieValue', yesterday); |
574 | |
575 | this.setCookie('cs'+this.obj, 'cookieValue', yesterday); |
576 | |
577 | }; |
578 | |
579 | |
580 | |
581 | // [Cookie] Sets value in a cookie |
582 | |
583 | dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) { |
584 | |
585 | document.cookie = |
586 | |
587 | escape(cookieName) + '=' + escape(cookieValue) |
588 | |
589 | + (expires ? '; expires=' + expires.toGMTString() : '') |
590 | |
591 | + (path ? '; path=' + path : '') |
592 | |
593 | + (domain ? '; domain=' + domain : '') |
594 | |
595 | + (secure ? '; secure' : ''); |
596 | |
597 | }; |
598 | |
599 | |
600 | |
601 | // [Cookie] Gets a value from a cookie |
602 | |
603 | dTree.prototype.getCookie = function(cookieName) { |
604 | |
605 | var cookieValue = ''; |
606 | |
607 | var posName = document.cookie.indexOf(escape(cookieName) + '='); |
608 | |
609 | if (posName != -1) { |
610 | |
611 | var posValue = posName + (escape(cookieName) + '=').length; |
612 | |
613 | var endPos = document.cookie.indexOf(';', posValue); |
614 | |
615 | if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos)); |
616 | |
617 | else cookieValue = unescape(document.cookie.substring(posValue)); |
618 | |
619 | } |
620 | |
621 | return (cookieValue); |
622 | |
623 | }; |
624 | |
625 | |
626 | |
627 | // [Cookie] Returns ids of open nodes as a string |
628 | |
629 | dTree.prototype.updateCookie = function() { |
630 | |
631 | var str = ''; |
632 | |
633 | for (var n=0; n<this.aNodes.length; n++) { |
634 | |
635 | if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) { |
636 | |
637 | if (str) str += '.'; |
638 | |
639 | str += this.aNodes[n].id; |
640 | |
641 | } |
642 | |
643 | } |
644 | |
645 | this.setCookie('co' + this.obj, str); |
646 | |
647 | }; |
648 | |
649 | |
650 | |
651 | // [Cookie] Checks if a node id is in a cookie |
652 | |
653 | dTree.prototype.isOpen = function(id) { |
654 | |
655 | var aOpen = this.getCookie('co' + this.obj).split('.'); |
656 | |
657 | for (var n=0; n<aOpen.length; n++) |
658 | |
659 | if (aOpen[n] == id) return true; |
660 | |
661 | return false; |
662 | |
663 | }; |
664 | |
665 | |
666 | |
667 | // If Push and pop is not implemented by the browser |
668 | |
669 | if (!Array.prototype.push) { |
670 | |
671 | Array.prototype.push = function array_push() { |
672 | |
673 | for(var i=0;i<arguments.length;i++) |
674 | |
675 | this[this.length]=arguments[i]; |
676 | |
677 | return this.length; |
678 | |
679 | } |
680 | |
681 | }; |
682 | |
683 | if (!Array.prototype.pop) { |
684 | |
685 | Array.prototype.pop = function array_pop() { |
686 | |
687 | lastElement = this[this.length-1]; |
688 | |
689 | this.length = Math.max(this.length-1,0); |
690 | |
691 | return lastElement; |
692 | |
693 | } |
694 | |
695 | }; |