source: trunk/phpgwapi/js/dftree/dftree.js @ 346

Revision 346, 15.6 KB checked in by niltonneto, 16 years ago (diff)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1/* Dynamic Folder Tree
2 * Generates DHTML tree dynamically (on the fly).
3 * License: GNU LGPL
4 *
5 * Copyright (c) 2004, Vinicius Cubas Brand, Raphael Derosso Pereira
6 * {viniciuscb,raphaelpereira} at users.sourceforge.net
7 * All rights reserved.
8 *
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24/*
25 * Chanded by Joao Alfredo Knopik Junior
26 * joao.alfredo at gmail.com
27 */
28
29// NODE
30//Usage: a = new dNode({id:2, caption:'tree root', url:'http://www.w3.org'});
31function dNode(arrayProps) {
32        //mandatory fields
33        this.id;          //node id
34        this.caption;     //node caption
35
36        //optional fields
37        this.url;         //url to open
38        this.target;      //target to open url
39        this.onClick;     //javascript to execute onclick
40        this.onOpen;      //javascript to execute when a node of the tree opens
41        this.onClose;     //javascript to execute when a node of the tree closes
42        this.onFirstOpen; //javascript to execute only on the first open
43        this.iconClosed;  //img.src of closed icon
44        this.iconOpen;    //img.src of open icon
45        this.runJS = true;       //(bool) if true, runs the on* props defined above
46        this.plusSign = true;    //(bool) if the plus sign will appear or not
47        this.captionClass = 'l'; //(string) the class for this node's caption
48
49
50        //The parameters below are private
51        this._opened = false; //already opened
52        this._io = false; //is opened
53
54        this._children = []; //references to children
55        this._parent; //pointer to parent
56        this._myTree; //pointer to myTree
57        this._drawn = false;
58
59        for (var i in arrayProps)
60        {
61                if (i.charAt(0) != '_')
62                {
63                        eval('this.'+i+' = arrayProps[\''+i+'\'];');
64                }
65        }
66}
67
68//changes node state from open to closed, and vice-versa
69dNode.prototype.changeState = function()
70{
71        if (this._io)
72        {
73                this.close();
74        }
75        else
76        {
77                this.open();
78        }
79
80        //cons = COokie of Node Status
81        //setCookie("cons"+this.id,this._io);
82}
83
84dNode.prototype.open = function () {
85        if (!this._io)
86        {
87                if (!this._opened && this.runJS && this.onFirstOpen != null)
88                {
89                        eval(this.onFirstOpen);
90                }
91                else if (this.runJS && this.onOpen != null)
92                {
93                        eval(this.onOpen);
94                }
95
96                this._debug = true;
97                this._opened = true;
98                this._io = true;
99                this._refresh();
100        }
101}
102
103
104dNode.prototype.close = function() {
105        if (this._io)
106        {
107                if (this.runJS && this.onClose != null)
108                {
109                        eval(this.onClose);
110                }
111                this._io = false;
112                this._refresh();
113        }
114}
115
116//alter node label and other properties
117dNode.prototype.alter = function(arrayProps)
118{
119        for (var i in arrayProps)
120        {
121                if (i != 'id' && i.charAt(0) != '_')
122                {
123                        eval('this.'+i+' = arrayProps[\''+i+'\'];');
124                }
125        }
126}
127
128//css and dhtml refresh part
129dNode.prototype._refresh = function() {
130        var nodeDiv      = getObjectById("n"+this.id+this._myTree.name);
131        var plusSpan     = getObjectById("p"+this.id+this._myTree.name);
132        var captionSpan  = getObjectById("l"+this.id+this._myTree.name);
133        var childrenDiv  = getObjectById("ch"+this.id+this._myTree.name);
134
135        if (nodeDiv != null)
136        {
137                //Handling open and close: checks this._io and changes class as needed
138                if (!this._io) //just closed
139                {
140                        childrenDiv.className = "closed";
141                }
142                else //just opened
143                {
144                        //prevents IE undesired behaviour when displaying empty DIVs
145/*                      if (this._children.length > 0)
146                        {*/
147                                childrenDiv.className = "opened";
148                //      }
149                }
150
151                plusSpan.innerHTML = this._properPlus();
152
153                captionSpan.innerHTML = this.caption;
154        }
155
156//alter onLoad, etc
157
158}
159
160//gets the proper plus for this moment
161dNode.prototype._properPlus = function()
162{
163        if (!this._io)
164        {
165                if (this._myTree.useIcons)
166                {
167                        return (this.plusSign)?imageHTML(this._myTree.icons.plus):"";
168                }
169                else
170                {
171                        return (this.plusSign)?"+":" ";
172                }
173        }
174        else
175        {
176                if (this._myTree.useIcons)
177                {
178                        return (this.plusSign)?imageHTML(this._myTree.icons.minus):"";
179                }
180                else
181                {
182                        return (this.plusSign)?"-":" ";
183                }
184        }
185}
186
187//changes node to selected style class. Perform further actions.
188dNode.prototype._select = function()
189{
190        var captionSpan;
191
192        if (this._myTree._selected)
193        {
194                this._myTree._selected._unselect();
195        }
196        this._myTree._selected = this;
197       
198        captionSpan  = getObjectById("l"+this.id+this._myTree.name);
199       
200        if(document.getElementById("div_tree") != null){
201                if(ttree.FOLDER != ""){
202                        ttree.FOLDER = "";
203                }
204                ttree.FOLDER = this.id;
205        }
206       
207        if(document.getElementById("div_folders_search") != null){
208                if(EsearchE.name_box_search != ""){
209                        EsearchE.name_box_search = "";
210                }
211                EsearchE.name_box_search = this.id;
212        }
213
214        //changes class to selected link
215        if (captionSpan)
216        {
217                captionSpan.className = 'sl';
218        }
219
220}
221
222//changes node background color.
223dNode.prototype._onMouseOver = function()
224{
225        var captionSpan;
226        if ((this.id != 'root') && (this.id != 'user')){
227                captionSpan = getObjectById("l"+this.id+this._myTree.name);
228                captionSpan.style.backgroundColor = 'white';
229                captionSpan.style.border = '1px solid black';
230        }
231}
232//changes node background color.
233dNode.prototype._onMouseOut = function()
234{
235        var captionSpan;
236        if ((this.id != 'root') && (this.id != 'user')){
237                captionSpan = getObjectById("l"+this.id+this._myTree.name);
238                captionSpan.style.backgroundColor = '';
239                captionSpan.style.border = '1px solid #f7f7f7';
240        }
241}
242
243//changes node to unselected style class. Perform further actions.
244dNode.prototype._unselect = function()
245{
246        var captionSpan  = getObjectById("l"+this.id+this._myTree.name);
247
248        this._myTree._lastSelected = this._myTree._selected;
249        this._myTree._selected = null;
250
251        //changes class to selected link
252        if (captionSpan)
253        {
254                captionSpan.className = this.captionClass;
255        }
256}
257
258
259//state can be open or closed
260//warning: if drawed node is not child or root, bugs will happen
261dNode.prototype._draw = function()
262{
263        var str;
264        var _this = this;
265        var divN, divCH, spanP, spanL, parentChildrenDiv;
266        var myClass = (this._io)? "opened" : "closed";
267        var myPlus = this._properPlus();
268        var append = true;
269        var captionOnClickEvent = null;
270//      var cook;
271
272        var plusEventHandler = function(){
273                _this.changeState();
274        }
275
276        var captionEventHandler = function(){
277                eval(captionOnClickEvent);
278        }
279
280/*      if (this.myTree.followCookies)
281        {
282                this._io = getCookie("cons"+this.id);
283        }*/
284
285        //FIXME put this in a separate function, as this will be necessary in
286        //various parts
287
288        if (this.onClick) //FIXME when onclick && url
289        {
290                captionEventHandler = function () { _this._select(); typeof(_this.onClick) == 'function' ? _this.onClick() : eval(_this.onClick); };
291        }
292        else if (this.url && this.target)
293        {
294                captionEventHandler = function () { _this._select(); window.open(_this.url,_this.target); };
295        }
296        else if (this.url)
297        {
298                captionEventHandler = function () { _this._select(); window.location=_this.url; };
299        }
300        else //Nao tem onClick
301        {
302                if ((this.id != 'root') && (this.id != 'user')) //e nao seja raiz(root) ou pastas compartilhadas(user).
303                        captionEventHandler = function () { _this._select();};
304        }
305       
306        //The div of this node
307        divN = document.createElement('div');
308        divN.id = 'n'+this.id+this._myTree.name;
309        //divN.className = 'son';
310        divN.className = (!this._parent) ? 'root' : 'son';
311        //divN.style.border = '1px solid black';
312       
313        //The span that holds the plus/minus sign
314        spanP = document.createElement('span');
315        spanP.id = 'p'+this.id+this._myTree.name;
316        spanP.className = 'plus';
317        spanP.onclick = plusEventHandler;
318        spanP.innerHTML = myPlus;
319        //spanP.style.border = '1px solid green';
320
321        //The span that holds the label/caption
322        spanL = document.createElement('span');
323        spanL.id = 'l'+this.id+this._myTree.name;
324        spanL.className = this.captionClass;
325        spanL.onclick = captionEventHandler;
326        spanL.onmouseover = function () { _this._onMouseOver(); };
327        spanL.onmouseout = function () { _this._onMouseOut(); };
328        spanL.style.border = '1px solid #f7f7f7';
329        spanL.innerHTML = this.caption;
330        //spanL.style.border = '1px solid red';
331
332        //The div that holds the children
333        divCH = document.createElement('div');
334        divCH.id = 'ch'+this.id+this._myTree.name;
335        divCH.className = myClass;
336        //divCH.style.border = '1px solid blue';
337       
338//      div.innerHTML = str;
339        divN.appendChild(spanP);
340        divN.appendChild(spanL);
341        divN.appendChild(divCH);
342       
343
344        if (this._parent != null)
345        {
346                parentChildrenDiv = getObjectById("ch"+this._parent.id+this._myTree.name);
347        }
348        else //is root
349        {
350                parentChildrenDiv = getObjectById("dftree_"+this._myTree.name);
351        }
352       
353        if (parentChildrenDiv)
354        {
355                parentChildrenDiv.appendChild(divN);
356        }
357}
358
359// TREE
360//Usage: t = new dFTree({name:t, caption:'tree root', url:'http://www.w3.org'});
361function dFTree(arrayProps) {
362        //mandatory fields
363        this.name;      //the value of this must be the name of the object
364
365        //optional fields
366        this.is_dynamic = true;   //tree is dynamic, i.e. updated on the fly
367        this.followCookies = true;//use previous state (o/c) of nodes
368        this.useIcons = false;     //use icons or not
369       
370
371        //arrayProps[icondir]: Icons Directory
372        iconPath = (arrayProps['icondir'] != null)? arrayProps['icondir'] : '';
373
374        this.icons = {
375                root        : iconPath+'/foldertree_base.gif',
376                folder      : iconPath+'/foldertree_folder.gif',
377                folderOpen  : iconPath+'/foldertree_folderopen.gif',
378                node        : iconPath+'/foldertree_folder.gif',
379                empty       : iconPath+'/foldertree_empty.gif',
380                line        : iconPath+'/foldertree_line.gif',
381                join        : iconPath+'/foldertree_join.gif',
382                joinBottom  : iconPath+'/foldertree_joinbottom.gif',
383                plus        : iconPath+'/foldertree_plus.gif',
384                plusBottom  : iconPath+'/foldertree_plusbottom.gif',
385                minus       : iconPath+'/foldertree_minus.gif',
386                minusBottom : iconPath+'/foldertree_minusbottom.gif',
387                nlPlus      : iconPath+'/foldertree_nolines_plus.gif',
388                nlMinus     : iconPath+'/foldertree_nolines_minus.gif'
389        };
390
391        //private
392        this._root = false; //reference to root node
393        this._aNodes = [];
394        this._lastSelected; //The last selected node
395        this._selected; //The actual selected node
396        this._folderPr = [];
397
398        for (var i in arrayProps)
399        {
400                if (i.charAt(0) != '_')
401                {
402                        eval('this.'+i+' = arrayProps[\''+i+'\'];');
403                }
404        }
405}
406
407dFTree.prototype.draw = function(dest_element) {
408        var main_div;
409       
410        if (!getObjectById("dftree_"+this.name) && dest_element)
411        {
412                main_div = document.createElement('div');
413                main_div.id = 'dftree_'+this.name;
414                dest_element.appendChild(main_div);
415                this._drawn = true;
416        }
417
418        if (this._root != false)
419        {
420                this._root._draw();
421                this._drawBranch(this._root._children);
422        }
423
424}
425
426//Transforms tree in HTML code
427dFTree.prototype.toString = function() {
428        var str = '';
429
430        if (!getObjectById("dftree_"+this.name))
431        {
432                str = '<div id="dftree_'+this.name+'"></div>';
433        }
434        return str;
435
436/*      if (this.root != false)
437        {
438                this.root._draw();
439                this._drawBranch(this.root.children);
440        }*/
441}
442
443//Recursive function, draws children
444dFTree.prototype._drawBranch = function(childrenArray) {
445        var a=0;
446        for (a;a<childrenArray.length;a++)
447        {
448                childrenArray[a]._draw();
449                this._drawBranch(childrenArray[a]._children);
450        }
451}
452
453//add into a position
454dFTree.prototype.add = function(node,pid) {
455        var auxPos;
456        var addNode = false;
457
458        if (typeof (auxPos = this._searchNode(node.id)) != "number")
459        {
460                // if parent exists, add node as its child
461                if (typeof (auxPos = this._searchNode(pid)) == "number")
462                {
463                        node._parent = this._aNodes[auxPos];
464                        this._aNodes[auxPos]._children[this._aNodes[auxPos]._children.length] = node;
465                        addNode = true;
466                }
467                else //if parent cannot be found and there is a tree root, ignores node
468                {
469                        if (!this._root)
470                        {
471                                this._root = node;
472                                addNode = true;
473                        }
474                }
475                if (addNode)
476                {
477                        this._aNodes[this._aNodes.length] = node;
478                        node._myTree = this;
479                        if (this.is_dynamic && this._drawn)
480                        {
481                                node._draw();
482                        }
483                }
484                else
485                {
486                        this._folderPr[this._folderPr.length] = node.id ;
487                }
488               
489        }
490        else {
491                var arrayProps = new Array();
492
493                arrayProps['id'] = node.id;
494                arrayProps['caption'] = node.caption;
495
496                arrayProps['url'] = node.url;
497                arrayProps['target'] = node.target;
498                arrayProps['onClick'] = node.onClick;
499                arrayProps['onOpen'] = node.onOpen;
500                arrayProps['onClose'] = node.onClose;
501                arrayProps['onFirstOpen'] = node.onFirstOpen;
502                arrayProps['iconClosed'] = node.iconClosed;
503                arrayProps['iconOpen'] = node.iconOpen;
504                arrayProps['runJS'] = node.runJS;
505                arrayProps['plusSign'] = node.plusSign;
506                arrayProps['captionClass'] = node.captionClass;
507
508                delete node;
509
510                this.alter(arrayProps);
511        }
512
513}
514
515//arrayProps: same properties of Node
516dFTree.prototype.alter = function(arrayProps) {
517        this.getNodeById(arrayProps['id']).alter(arrayProps);
518}
519
520dFTree.prototype.getNodeById = function(nodeid) {
521        return this._aNodes[this._searchNode(nodeid)];
522}
523
524dFTree.prototype.openTo = function(nodeid)
525{
526        var node = this.getNodeById(nodeid);
527
528        if (node && node._parent)
529        {
530                node._parent.open();
531                this.openTo(node._parent.id);
532        }
533}
534
535//Searches for a node in the node array, returning the position of the array 4it
536dFTree.prototype._searchNode = function(id) {
537        var a=0;
538        for (a;a<this._aNodes.length;a++)
539        {
540                if (this._aNodes[a].id == id)
541                {
542                        return a;
543                }
544        }
545        return false;
546}
547// By jakjr, retorna um array com os ids de todas as pastas (nodes)
548dFTree.prototype.getNodesList = function(imapDelimiter) {
549        var a=0;
550        var nodes=[];
551        for (a;a<this._aNodes.length;a++)
552        {
553                var node = this.getNodeById(this._aNodes[a].id);
554                nodes[a]=[];
555                nodes[a].id = node.id;
556                nodes[a].parent = node._parent ? node._parent.id : 'root';
557                var tmp = node.id.split(imapDelimiter);
558                var tmp_caption = node.caption.split("<");
559
560                if (tmp[tmp.length-1] == 'INBOX')
561                        nodes[a].caption = get_lang('Inbox');
562                else if (tmp[tmp.length-1] == 'user')
563                        nodes[a].caption = get_lang("Shared Folders");
564                else
565                        nodes[a].caption = tmp_caption[0];
566                //nodes[a].caption = tmp[tmp.length-1] == 'INBOX' ? get_lang('Inbox') : tmp[tmp.length-1];
567                nodes[a].plusSign = node.plusSign;
568        }
569        return nodes;
570}
571
572//Auxiliar functions
573//For multi-browser compatibility
574function getObjectById(name)
575{   
576    if (document.getElementById)
577    {
578        return document.getElementById(name);
579    }
580    else if (document.all)
581    {
582        return document.all[name];
583    }
584    else if (document.layers)
585    {
586        return document.layers[name];
587    }
588    return false;
589}
590
591// [Cookie] Clears a cookie
592function clearCookie(cookieName) {
593        var now = new Date();
594        var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
595        this.setCookie(cookieName, 'cookieValue', yesterday);
596        this.setCookie(cookieName, 'cookieValue', yesterday);
597};
598
599// [Cookie] Sets value in a cookie
600function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
601        document.cookie =
602                escape(cookieName) + '=' + escape(cookieValue)
603                + (expires ? '; expires=' + expires.toGMTString() : '')
604                + (path ? '; path=' + path : '')
605                + (domain ? '; domain=' + domain : '')
606                + (secure ? '; secure' : '');
607};
608
609// [Cookie] Gets a value from a cookie
610function getCookie(cookieName) {
611        var cookieValue = '';
612        var posName = document.cookie.indexOf(escape(cookieName) + '=');
613        if (posName != -1) {
614                var posValue = posName + (escape(cookieName) + '=').length;
615                var endPos = document.cookie.indexOf(';', posValue);
616                if (endPos != -1)
617                {
618                        cookieValue = unescape(document.cookie.substring(posValue, endPos));
619                }
620                else
621                {
622                        cookieValue = unescape(document.cookie.substring(posValue));
623                }
624        }
625        return (cookieValue);
626};
627
628
629function imageHTML(src,attributes) {
630        if (attributes != null)
631        {
632                attributes = '';
633        }
634        return "<img "+attributes+" src=\""+src+"\">";
635}
Note: See TracBrowser for help on using the repository browser.