source: sandbox/expressoMail1_2/MailArchiver/2.2/phpgwapi/js/dftree/dftree.js @ 4668

Revision 4668, 16.7 KB checked in by fernando-alberto, 13 years ago (diff)

Ticket #1269 - Desenvolvimento da nova solucao de arquivamento local MailArchiver?, nome da tree local

  • 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        //window.alert('em dNode.prototype.open\n\nname = ' + this.id + '\nopened = ' + this._opened + '\nis opened = ' + this._io + '\ncaptionClass = ' + this.captionClass + '\nmyTree = ' + this._myTree.name);
86        if((expresso_mail_archive) && (expresso_mail_archive.enabled) )expresso_mail_archive.drawdata.treeName = this._myTree.name;
87        if (!this._io)
88        {
89                if (!this._opened && this.runJS && this.onFirstOpen != null)
90                {
91                        alert('onfirstopen');
92                        eval(this.onFirstOpen);
93                }
94                else if (this.runJS && this.onOpen != null)
95                {
96                        alert('onOpen');
97                        eval(this.onOpen);
98                }
99                else if((this.id.substr(0,5) == 'local') && (!this._opened) && (!this._io) && (expresso_mail_archive.enabled)){
100                    if (this.id != 'local_root')
101                        expresso_mail_archive.getFoldersList(this.id.substr(6));
102                    else
103                        expresso_mail_archive.getFoldersList("");
104                }
105               
106                this._debug = true;
107                this._opened = true;
108                this._io = true;
109                this._refresh();
110        }
111}
112
113
114dNode.prototype.close = function() {
115        if (this._io)
116        {
117                if (this.runJS && this.onClose != null)
118                {
119                        eval(this.onClose);
120                }
121                this._io = false;
122                this._refresh();
123        }
124}
125
126//alter node label and other properties
127dNode.prototype.alter = function(arrayProps)
128{
129        for (var i in arrayProps)
130        {
131                if (i != 'id' && i.charAt(0) != '_')
132                {
133                        eval('this.'+i+' = arrayProps[\''+i+'\'];');
134                }
135        }
136}
137
138//css and dhtml refresh part
139dNode.prototype._refresh = function() {
140        var nodeDiv      = getObjectById("n"+this.id+this._myTree.name);
141        var plusSpan     = getObjectById("p"+this.id+this._myTree.name);
142        var captionSpan  = getObjectById("l"+this.id+this._myTree.name);
143        var childrenDiv  = getObjectById("ch"+this.id+this._myTree.name);
144
145        if (nodeDiv != null)
146        {
147                //Handling open and close: checks this._io and changes class as needed
148                if (!this._io) //just closed
149                {
150                        childrenDiv.className = "closed";
151                }
152                else //just opened
153                {
154                        //prevents IE undesired behaviour when displaying empty DIVs
155/*                      if (this._children.length > 0)
156                        {*/
157                                childrenDiv.className = "opened";
158                //      }
159                }
160
161                plusSpan.innerHTML = this._properPlus();
162
163                captionSpan.innerHTML = this.caption;
164        }
165
166//alter onLoad, etc
167
168}
169
170//gets the proper plus for this moment
171dNode.prototype._properPlus = function()
172{
173       
174        if (!this._io)
175        {
176                if (this._myTree.useIcons)
177                {
178                        return (this.plusSign)?imageHTML(this._myTree.icons.plus):"";
179                }
180                else
181                {
182                        return (this.plusSign)?"+":" ";
183                }
184        }
185        else
186        {
187                if (this._myTree.useIcons)
188                {
189                        return (this.plusSign)?imageHTML(this._myTree.icons.minus):"";
190                }
191                else
192                {
193                        return (this.plusSign)?"-":" ";
194                }
195        }
196}
197
198//changes node to selected style class. Perform further actions.
199dNode.prototype._select = function()
200{
201        var captionSpan;
202
203        if (this._myTree._selected)
204        {
205                this._myTree._selected._unselect();
206        }
207        this._myTree._selected = this;
208       
209        captionSpan  = getObjectById("l"+this.id+this._myTree.name);
210       
211        if(document.getElementById("div_tree") != null){
212                if(ttree.FOLDER != ""){
213                        ttree.FOLDER = "";
214                }
215                ttree.FOLDER = this.id;
216        }
217       
218        if(document.getElementById("div_folders_search") != null){
219                if(EsearchE.name_box_search != ""){
220                        EsearchE.name_box_search = "";
221                }
222                EsearchE.name_box_search = this.id;
223        }
224
225        //changes class to selected link
226        if (captionSpan)
227        {
228                captionSpan.className = 'sl';
229        }
230
231}
232
233//changes node background color.
234dNode.prototype._onMouseOver = function()
235{
236        var captionSpan;
237        if ((this.id != 'root') && (this.id != 'user') && (this.id !='local_root')){
238                captionSpan = getObjectById("l"+this.id+this._myTree.name);
239                captionSpan.style.backgroundColor = 'white';
240                captionSpan.style.border = '1px solid black';
241                captionSpan.style.paddingTop = '0px';
242                captionSpan.style.paddingBottom = '0px';
243        }
244}
245//changes node background color.
246dNode.prototype._onMouseOut = function()
247{
248        var captionSpan;
249        if ((this.id != 'root') && (this.id != 'user') && (this.id !='local_root')){
250                captionSpan = getObjectById("l"+this.id+this._myTree.name);
251                captionSpan.style.backgroundColor = '';
252                captionSpan.style.border = '0px';
253                captionSpan.style.paddingTop = '1px';
254                captionSpan.style.paddingBottom = '1px';
255        }
256}
257
258//changes node to unselected style class. Perform further actions.
259dNode.prototype._unselect = function()
260{
261        var captionSpan  = getObjectById("l"+this.id+this._myTree.name);
262
263        this._myTree._lastSelected = this._myTree._selected;
264        this._myTree._selected = null;
265
266        //changes class to selected link
267        if (captionSpan)
268        {
269                captionSpan.className = this.captionClass;
270        }
271}
272
273
274//state can be open or closed
275//warning: if drawed node is not child or root, bugs will happen
276dNode.prototype._draw = function()
277{
278        var str;
279        var _this = this;
280        var divN, divCH, spanP, spanL, parentChildrenDiv;
281        var myClass = (this._io)? "opened" : "closed";
282        var myPlus = this._properPlus();
283        var append = true;
284        var captionOnClickEvent = null;
285//      var cook;
286
287        var plusEventHandler = function(){
288                _this.changeState();
289        }
290
291        var captionEventHandler = function(){
292                eval(captionOnClickEvent);
293        }
294
295/*      if (this.myTree.followCookies)
296        {
297                this._io = getCookie("cons"+this.id);
298        }*/
299
300        //FIXME put this in a separate function, as this will be necessary in
301        //various parts
302       
303        //window.alert('this.onclick = ' + _this.onClick);
304
305        if (this.onClick) //FIXME when onclick && url
306        {
307                captionEventHandler = function () { _this._select(); typeof(_this.onClick) == 'function' ? _this.onClick() : eval(_this.onClick); };
308        }
309        else if (this.url && this.target)
310        {
311                captionEventHandler = function () { _this._select(); window.open(_this.url,_this.target); };
312        }
313        else if (this.url)
314        {
315                captionEventHandler = function () { _this._select(); window.location=_this.url; };
316        }
317        else //Nao tem onClick
318        {
319                if ((this.id != 'root') && (this.id != 'user') && (this.id !='local_root')) //e nao seja raiz(root) ou pastas compartilhadas(user).
320                        captionEventHandler = function () { _this._select();};
321        }
322       
323        //The div of this node
324        divN = document.createElement('div');
325        divN.id = 'n'+this.id+this._myTree.name;
326        //divN.className = 'son';
327        divN.className = (!this._parent) ? 'root' : 'son';
328        //divN.style.border = '1px solid black';
329       
330        //The span that holds the plus/minus sign
331        spanP = document.createElement('span');
332        spanP.id = 'p'+this.id+this._myTree.name;
333        spanP.className = 'plus';
334        spanP.onclick = plusEventHandler;
335        spanP.innerHTML = myPlus;
336        //spanP.style.border = '1px solid green';
337
338        //The span that holds the label/caption
339        spanL = document.createElement('span');
340        spanL.id = 'l'+this.id+this._myTree.name;
341        spanL.className = this.captionClass;
342        spanL.onclick = captionEventHandler;
343        spanL.onmouseover = function () { _this._onMouseOver(); };
344        spanL.onmouseout = function () { _this._onMouseOut(); };
345//      spanL.style.border = '1px solid #f7f7f7';
346        spanL.innerHTML = this.caption;
347        //spanL.style.border = '1px solid red';
348
349        //The div that holds the children
350        divCH = document.createElement('div');
351        divCH.id = 'ch'+this.id+this._myTree.name;
352        divCH.className = myClass;
353        //divCH.style.border = '1px solid blue';
354       
355//      div.innerHTML = str;
356        divN.appendChild(spanP);
357        divN.appendChild(spanL);
358        divN.appendChild(divCH);
359       
360       
361        if (this._parent != null)
362        {
363        parentChildrenDiv = getObjectById("ch"+this._parent.id+this._myTree.name);
364        }
365        else //is root
366        {
367        parentChildrenDiv = getObjectById("dftree_"+this._myTree.name);
368        }
369       
370        if (parentChildrenDiv)
371        {
372        parentChildrenDiv.appendChild(divN);
373        }
374}
375
376// TREE
377//Usage: t = new dFTree({name:t, caption:'tree root', url:'http://www.w3.org'});
378function dFTree(arrayProps) {
379        //mandatory fields
380        this.name;      //the value of this must be the name of the object
381
382        //optional fields
383        this.is_dynamic = true;   //tree is dynamic, i.e. updated on the fly
384        this.followCookies = true;//use previous state (o/c) of nodes
385        this.useIcons = false;     //use icons or not
386       
387
388        //arrayProps[icondir]: Icons Directory
389        iconPath = (arrayProps['icondir'] != null)? arrayProps['icondir'] : '';
390
391        this.icons = {
392                root        : iconPath+'/foldertree_base.gif',
393                folder      : iconPath+'/foldertree_folder.gif',
394                folderOpen  : iconPath+'/foldertree_folderopen.gif',
395                node        : iconPath+'/foldertree_folder.gif',
396                empty       : iconPath+'/foldertree_empty.gif',
397                line        : iconPath+'/foldertree_line.gif',
398                join        : iconPath+'/foldertree_join.gif',
399                joinBottom  : iconPath+'/foldertree_joinbottom.gif',
400                plus        : iconPath+'/foldertree_plus.gif',
401                plusBottom  : iconPath+'/foldertree_plusbottom.gif',
402                minus       : iconPath+'/foldertree_minus.gif',
403                minusBottom : iconPath+'/foldertree_minusbottom.gif',
404                nlPlus      : iconPath+'/foldertree_nolines_plus.gif',
405                nlMinus     : iconPath+'/foldertree_nolines_minus.gif'
406        };
407
408        //private
409        this._root = false; //reference to root node
410        this._aNodes = [];
411        this._lastSelected; //The last selected node
412        this._selected; //The actual selected node
413        this._folderPr = [];
414
415        for (var i in arrayProps)
416        {
417                if (i.charAt(0) != '_')
418                {
419                        eval('this.'+i+' = arrayProps[\''+i+'\'];');
420                }
421        }
422}
423
424dFTree.prototype.draw = function(dest_element) {
425        var main_div;
426       
427        if (!getObjectById("dftree_"+this.name) && dest_element)
428        {
429                main_div = document.createElement('div');
430                main_div.id = 'dftree_'+this.name;
431                dest_element.appendChild(main_div);
432                this._drawn = true;
433        }
434
435        if (this._root != false)
436        {
437                this._root._draw();
438                this._drawBranch(this._root._children);
439        }
440
441}
442
443//Transforms tree in HTML code
444dFTree.prototype.toString = function() {
445        var str = '';
446
447        if (!getObjectById("dftree_"+this.name))
448        {
449                str = '<div id="dftree_'+this.name+'"></div>';
450        }
451        return str;
452
453/*      if (this.root != false)
454        {
455                this.root._draw();
456                this._drawBranch(this.root.children);
457        }*/
458}
459
460//Recursive function, draws children
461dFTree.prototype._drawBranch = function(childrenArray) {
462        var a=0;
463        for (a;a<childrenArray.length;a++)
464        {
465                childrenArray[a]._draw();
466                this._drawBranch(childrenArray[a]._children);
467        }
468}
469
470//add into a position
471dFTree.prototype.add = function(node,pid) {
472        var auxPos;
473        var addNode = false;
474        if (typeof (auxPos = this._searchNode(node.id)) != "number")
475        {
476                // if parent exists, add node as its child
477                if (typeof (auxPos = this._searchNode(pid)) == "number")
478                {
479                        node._parent = this._aNodes[auxPos];
480                        this._aNodes[auxPos]._children[this._aNodes[auxPos]._children.length] = node;
481                        addNode = true;
482                }
483                else //if parent cannot be found and there is a tree root, ignores node
484                {
485                        if (!this._root)
486                        {
487                                this._root = node;
488                                addNode = true;
489                        }
490                }
491                if (addNode)
492                {
493                        this._aNodes[this._aNodes.length] = node;
494                        node._myTree = this;
495                        if (this.is_dynamic && this._drawn)
496                        {
497                                node._draw();
498                        }
499                }
500                else
501                {
502                        this._folderPr[this._folderPr.length] = node.id ;
503                }
504               
505        }
506        else {
507                var arrayProps = new Array();
508
509                arrayProps['id'] = node.id;
510                arrayProps['caption'] = node.caption;
511
512                arrayProps['url'] = node.url;
513                arrayProps['target'] = node.target;
514                arrayProps['onClick'] = node.onClick;
515                arrayProps['onOpen'] = node.onOpen;
516                arrayProps['onClose'] = node.onClose;
517                arrayProps['onFirstOpen'] = node.onFirstOpen;
518                arrayProps['iconClosed'] = node.iconClosed;
519                arrayProps['iconOpen'] = node.iconOpen;
520                arrayProps['runJS'] = node.runJS;
521                arrayProps['plusSign'] = node.plusSign;
522                arrayProps['captionClass'] = node.captionClass;
523
524                delete node;
525
526                this.alter(arrayProps);
527        }
528
529}
530
531//arrayProps: same properties of Node
532dFTree.prototype.alter = function(arrayProps) {
533        this.getNodeById(arrayProps['id']).alter(arrayProps);
534}
535
536dFTree.prototype.getNodeById = function(nodeid) {
537        return this._aNodes[this._searchNode(nodeid)];
538}
539
540dFTree.prototype.openTo = function(nodeid)
541{
542        var node = this.getNodeById(nodeid);
543
544        if (node && node._parent)
545        {
546                node._parent.open();
547                this.openTo(node._parent.id);
548        }
549}
550
551//Searches for a node in the node array, returning the position of the array 4it
552dFTree.prototype._searchNode = function(id) {
553        var a=0;
554        for (a;a<this._aNodes.length;a++)
555        {
556                if (this._aNodes[a].id == id)
557                {
558                        return a;
559                }
560        }
561        return false;
562}
563// By jakjr, retorna um array com os ids de todas as pastas (nodes)
564dFTree.prototype.getNodesList = function(imapDelimiter) {
565        var a=0;
566        var nodes=[];
567        for (a;a<this._aNodes.length;a++)
568        {
569                var node = this.getNodeById(this._aNodes[a].id);
570                nodes[a]=[];
571                nodes[a].id = node.id;
572                nodes[a].parent = node._parent ? node._parent.id : 'root';             
573                var tmp = node.id.split(imapDelimiter);
574                var tmp_caption = node.caption.split("<");
575
576                if (node.id == 'INBOX')
577                        nodes[a].caption = get_lang('Inbox');
578                else if (node.id == 'user')
579                        nodes[a].caption = get_lang("Shared Folders");
580                else
581                        nodes[a].caption = tmp_caption[0];
582                //nodes[a].caption = tmp[tmp.length-1] == 'INBOX' ? get_lang('Inbox') : tmp[tmp.length-1];
583                nodes[a].plusSign = node.plusSign;
584        }
585        return nodes;
586}
587
588//Auxiliar functions
589//For multi-browser compatibility
590function getObjectById(name)
591{   
592    if (document.getElementById)
593    {
594        return document.getElementById(name);
595    }
596    else if (document.all)
597    {
598        return document.all[name];
599    }
600    else if (document.layers)
601    {
602        return document.layers[name];
603    }
604    return false;
605}
606
607// [Cookie] Clears a cookie
608function clearCookie(cookieName) {
609        var now = new Date();
610        var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
611        this.setCookie(cookieName, 'cookieValue', yesterday);
612        this.setCookie(cookieName, 'cookieValue', yesterday);
613};
614
615// [Cookie] Sets value in a cookie
616function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
617        document.cookie =
618                escape(cookieName) + '=' + escape(cookieValue)
619                + (expires ? '; expires=' + expires.toGMTString() : '')
620                + (path ? '; path=' + path : '')
621                + (domain ? '; domain=' + domain : '')
622                + (secure ? '; secure' : '');
623};
624
625// [Cookie] Gets a value from a cookie
626function getCookie(cookieName) {
627        var cookieValue = '';
628        var posName = document.cookie.indexOf(escape(cookieName) + '=');
629        if (posName != -1) {
630                var posValue = posName + (escape(cookieName) + '=').length;
631                var endPos = document.cookie.indexOf(';', posValue);
632                if (endPos != -1)
633                {
634                        cookieValue = unescape(document.cookie.substring(posValue, endPos));
635                }
636                else
637                {
638                        cookieValue = unescape(document.cookie.substring(posValue));
639                }
640        }
641        return (cookieValue);
642};
643
644
645function imageHTML(src,attributes) {
646        if (attributes != null)
647        {
648                attributes = '';
649        }
650        return "<img "+attributes+" src=\""+src+"\">";
651}
Note: See TracBrowser for help on using the repository browser.