source: trunk/expressoMail1_2/js/connector.js @ 4476

Revision 4476, 17.4 KB checked in by airton, 13 years ago (diff)

Ticket #1911 - Parametrização das buscas LDAP no expressoMail

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1/****************************************** Public variables *************************************************/
2var debug_controller =false;
3var files = new Array();
4var progressBar;
5var handlerExecuteForm = null;
6
7  if (document.all)
8        {
9                navigator.userAgent.toLowerCase().indexOf('msie 5') != -1 ? is_ie5 = true : is_ie5 = false;
10                is_ie = true;
11                is_moz1_6 = false;
12                is_mozilla = false;
13                is_webkit = false;
14                is_ns4 = false;
15        }
16        else if (document.getElementById)
17        {
18                navigator.userAgent.toLowerCase().match('mozilla.*rv[:]1\.6.*gecko') ? is_moz1_6 = true : is_moz1_6 = false;
19                navigator.userAgent.toLowerCase().match('applewebkit') ? is_webkit = true : is_webkit = false;
20                is_ie = false;
21                is_ie5 = false;
22                is_mozilla = true;
23                is_ns4 = false;
24        }
25        else if (document.layers)
26        {
27                is_ie = false;
28                is_ie5 = false
29                is_moz1_6 = false;
30                is_mozilla = false;
31                is_webkit = false;
32                is_ns4 = true;
33        }       
34
35/****************************************** Connector Class *************************************************/
36        // Constructor
37        function cConnector()
38        {
39                this.requests = new Array();
40                this.oxmlhttp = null;
41                this.isVisibleBar = false;
42                this.tid = 0;
43                this.progressBar = null;
44                this.oldX = 0;
45                this.oldY = 0;
46                this.updateVersion = "";
47                this.connectorCache = {
48                                'valid' : [],
49                                'result' : []
50                };
51                this.expurgatedCache = new Array(); // Data to purge from cache
52
53        }
54       
55        cConnector.prototype.buildBar = function()
56                {                       
57                        var div = document.getElementById('divProgressBar');
58               
59                        if(! div) {                                                                                             
60                                div = document.createElement("DIV");
61                                div.style.visibility    = "hidden";             
62                                div.style.width = "103px";
63                                div.id = 'divProgressBar';
64                                div.align = "center";
65                                div.innerHTML = '&nbsp;&nbsp;<font face="Verdana" size="2" color="WHITE">'+document.getElementById('txt_loading').value+'...</font>&nbsp;';
66                                div.style.background = "#cc4444";
67                                div.style.position = 'fixed';
68                                div.style.top = '0px';
69                                div.style.right = '0px';
70                                document.body.appendChild(div);                                                                                                                         
71                               
72                                if(is_ie) {
73                                        var elem = document.all[div.id];
74                                        elem.style.position="absolute";
75                                        var root = document.body;
76                                        var posX = elem.offsetLeft-root.scrollLeft;
77                                        var posY = elem.offsetTop-root.scrollTop;
78                                        root.onscroll = function() {
79                                                elem.style.right = '0px';
80                                                elem.style.top = (posY + root.scrollTop) + "px";
81                                        };                                     
82                                }
83                               
84                                if(debug_controller) {
85                                        div = document.createElement("DIV");
86                                        div.style.width = "800px";
87                                        div.style.height= "400px";
88                                        div.id = "debug_controller";
89                                        div.align='right';
90                                        document.body.appendChild(div);                                                                                                                         
91                                }
92                        }                                                               
93        }       
94//------------------------------------ BEGIN: Functions for Connector HTTPRequest  -------------------------------------------------// 
95        // Serialize Data Method
96        cConnector.prototype.serialize = function(data)
97        {       var _thisObject = this;         
98                var f = function(data)
99                {
100                        var str_data;
101       
102                        if (data == null ||
103                                (typeof(data) == 'string' && data == ''))
104                        {
105                                str_data = 'N;';
106                        }
107       
108                        else switch(typeof(data))
109                        {
110                                case 'object':
111                                        var arrayCount = 0;
112       
113                                        str_data = '';
114       
115                                        for (i in data)
116                                        {
117                                                if (i == 'length')
118                                                {
119                                                        continue;
120                                                }
121                                               
122                                                arrayCount++;
123                                                switch (typeof(i))
124                                                {
125                                                        case 'number':
126                                                                str_data += 'i:' + i + ';' + f(data[i]);
127                                                                break;
128       
129                                                        case 'string':
130                                                                str_data += 's:' + i.length + ':"' + i + '";' + f(data[i]);
131                                                                break;
132       
133                                                        default:
134                                                                showMessage(Element('cc_msg_err_serialize_data_unknown').value);
135                                                                break;
136                                                }
137                                        }
138       
139                                        if (!arrayCount)
140                                        {
141                                                str_data = 'N;';       
142                                        }
143                                        else
144                                        {
145                                                str_data = 'a:' + arrayCount + ':{' + str_data + '}';
146                                        }
147                                       
148                                        break;
149                       
150                                case 'string':
151                                        str_data = 's:' + data.length + ':"' + data + '";';
152                                        break;
153                                       
154                                case 'number':
155                                        str_data = 'i:' + data + ';';
156                                        break;
157       
158                                case 'boolean':
159                                        str_data = 'b:' + (data ? '1' : '0') + ';';
160                                        break;
161       
162                                default:
163                                        showMessage(Element('cc_msg_err_serialize_data_unknown').value);
164                                        return null;
165                        }
166
167                        return str_data;
168                }
169       
170                return f(data);
171        }
172
173               
174        //Unserialize Data Method
175        cConnector.prototype.unserialize = function(str)
176        {
177               
178                _thisObject = this;
179                var matchB = function (str, iniPos)
180                {
181                        var nOpen, nClose = iniPos;
182                        do
183                        {
184                                nOpen = str.indexOf('{', nClose+1);
185                                nClose = str.indexOf('}', nClose+1);
186
187                                if (nOpen == -1)
188                                {
189                                        return nClose;
190                                }
191                                if (nOpen < nClose )
192                                {
193                                        nClose = matchB(str, nOpen);
194                                }
195                        } while (nOpen < nClose);
196
197                        return nClose;
198                }
199               
200                var f = function (str)
201                {
202                        switch (str.charAt(0))
203                        {
204                                case 'a':
205                                       
206                                        var data = new Array();
207                                        var n = parseInt( str.substring( str.indexOf(':')+1, str.indexOf(':',2) ) );
208                                        var arrayContent = str.substring(str.indexOf('{')+1, str.lastIndexOf('}'));
209                               
210                                        for (var i = 0; i < n; i++)
211                                        {
212                                                var pos = 0;
213       
214                                                /* Process Index */
215                                                var indexStr = arrayContent.substr(pos, arrayContent.indexOf(';')+1);
216                                                var index = f(indexStr);
217                                                pos = arrayContent.indexOf(';', pos)+1;
218                                               
219                                                /* Process Content */
220                                                var part = null;
221                                                switch (arrayContent.charAt(pos))
222                                                {
223                                                        case 'a':
224                                                                var pos_ = matchB(arrayContent, arrayContent.indexOf('{', pos))+1;
225                                                                part = arrayContent.substring(pos, pos_);
226                                                                pos = pos_;
227                                                                data[index] = f(part);
228                                                                break;
229                                               
230                                                        case 's':
231                                                                var pval = arrayContent.indexOf(':', pos+2);
232                                                                var val  = parseInt(arrayContent.substring(pos+2, pval));
233                                                                pos = pval + val + 4;
234                                                                data[index] = arrayContent.substr(pval+2, val);
235                                                                break;
236       
237                                                        default:
238                                                                part = arrayContent.substring(pos, arrayContent.indexOf(';', pos)+1);
239                                                                pos = arrayContent.indexOf(';', pos)+1;
240                                                                data[index] = f(part);
241                                                                break;
242                                                }
243                                                arrayContent = arrayContent.substr(pos);
244                                        }
245                                        break;
246                                       
247                                case 's':
248                                        var pos = str.indexOf(':', 2);
249                                        var val = parseInt(str.substring(2,pos));
250                                        var data = str.substr(pos+2, val);
251                                        str = str.substr(pos + 4 + val);
252                                        break;
253       
254                                case 'i':
255                                case 'd':
256                                        var pos = str.indexOf(';');
257                                        var data = parseInt(str.substring(2,pos));
258                                        str = str.substr(pos + 1);
259                                        break;
260                               
261                                case 'N':
262                                        var data = null;
263                                        str = str.substr(str.indexOf(';') + 1);
264                                        break;
265       
266                                case 'b':
267                                        var data = str.charAt(2) == '1' ? true : false;
268                                        break;
269                        }
270                        return data;
271                }
272       
273                return f(str);
274        }
275
276        //Create XMLHTTP object Method
277        cConnector.prototype.createXMLHTTP = function ()
278        {       
279                try
280                {
281                        this.oxmlhttp = new XMLHttpRequest();
282                        this.oxmlhttp.overrideMimeType('text/xml');
283                }
284                catch (e)
285                {
286                        try
287                        {
288                                this.oxmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
289                        }
290                        catch (e1)
291                        {
292                                try
293                                {
294                                        this.oxmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
295                                }
296                                catch (e2)
297                                {
298                                        this.oxmlhttp = null;
299                                }
300                        }
301                }
302       
303        }
304       
305        // Request Constructor Connector       
306        cConnector.prototype.newRequest = function (id, target, method, handler, data)
307        {
308               
309                var params = data;
310                this.tid = id;
311               
312                if (this.requests[id]) {
313                        return false;
314                }
315
316                this.createXMLHTTP();
317                var oxmlhttp = this.oxmlhttp;
318                var _thisObject = this;         
319
320                if (! oxmlhttp)         
321                        return false;
322                               
323                this.requests[id] = oxmlhttp;
324                this.buildBar();               
325                this.showProgressBar();
326
327                var sub_handler = function ()
328                {                       
329                        var progressBar = _thisObject.progressBar;
330                       
331                        try
332                        {
333                                if (oxmlhttp.readyState == 4 )
334                                {
335                                        switch (oxmlhttp.status)
336                                        {
337
338                                                case 200:
339                                                        if (typeof(handler) == 'function')
340                                                        {                                                                                                                               
341                                                                _thisObject.hideProgressBar();                                                                                                                         
342                                                                var data = _thisObject.unserialize(oxmlhttp.responseText);
343                                                                if ( typeof data == 'undefined' )
344                                                                        data = oxmlhttp.responseText;
345                                                                // Verify user session
346                                                                if(data && data.nosession){
347                                                                        // If hold session is setted, dont reload the page ...
348                                                                        if(hold_session) {
349                                                                                if(typeof(write_msg) == "function" && typeof(get_lang) == "function")
350                                                                                        write_msg(get_lang("your session could not be verified."));
351                                                                                else
352                                                                                        alert("your session could not be verified.");
353                                                                        }
354                                                                        else
355                                                                                window.location.reload();
356
357                                                                        delete _thisObject.requests[id];
358                                                                        _thisObject.requests[id] = null;
359                                                                        return false;
360                                                                }
361                                                                if(debug_controller) {
362                                                                        document.getElementById("debug_controller").innerHTML += oxmlhttp.responseText;
363                                                                        document.getElementById("debug_controller").innerHTML += "<br>-------------------------------------------------------------------------------------<br>";
364                                                                }
365
366                                                                _thisObject.addToCache(params?id+"&"+params:id,data);
367                                                                delete _thisObject.requests[id];                                                               
368                                                                _thisObject.requests[id] = null;
369                                                                handler(data);
370                                                        }
371
372                                                        break;
373
374                                                case 404:
375                                                       
376                                                        alert(get_lang('Page Not Found!'));
377                                                        break;
378
379                                                default:                                                                                               
380                                        }
381                                }
382                        }
383                        catch (e)
384                        {                       
385                                _thisObject.hideProgressBar();
386                                if(debug_controller)
387                                        alert(e+"\n"+oxmlhttp.responseText);
388                                // View Exception in Javascript Console
389                                throw(e);
390                        }
391                                               
392                }
393
394                try
395                {
396                        if (method == '' || method == 'GET')
397                        {                                                               
398                                oxmlhttp.open("GET",target,true);
399                                if (typeof(handler) == 'function')
400                                {       
401                                        oxmlhttp.onreadystatechange =  sub_handler;                                     
402                                        oxmlhttp.send(null);                                   
403                                }               
404                               
405                        }
406                        else if (method == 'POST')
407                        {
408                                oxmlhttp.open("POST",target, true);
409                                oxmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
410                                if (typeof(handler) == 'function')
411                                {
412                                        oxmlhttp.onreadystatechange = sub_handler;
413                                        oxmlhttp.send(data);
414                                }                               
415                               
416                        }
417                }
418                catch(e)
419                {       
420                        _thisObject.hideProgressBar();
421                        if(debug_controller)
422                                alert(e);
423                        // View Exception in Javascript Console 
424                        throw(e);
425                }
426                                               
427                return true;
428        }
429        // Cancel Request Connector
430        cConnector.prototype.cancelRequest = function (){
431                if (!this.requests[this.tid]){
432                        return false;
433                }
434                this.oxmlhttp.onreadystatechange = null;
435                this.requests[this.tid].abort();
436                this.hideProgressBar();
437        }
438//------------------------------------  END: Functions for Connector HTTPRequest  -------------------------------------------------//
439
440//      ----------------------------------- BEGIN: Functions for build Bar Progress ---------------------------------------------------------//
441        cConnector.prototype.hideProgressBar = function ()
442        {
443                var div = document.getElementById('divProgressBar');
444                div.style.visibility = 'hidden';
445                this.isVisibleBar = false;
446        }
447       
448        cConnector.prototype.showProgressBar = function(){
449                var div = document.getElementById('divProgressBar');
450                div.style.visibility = 'visible';                       
451
452                this.isVisibleBar = true;
453        }
454
455        cConnector.prototype.loadAllScripts = function(scripts) {       
456        for(var i = 0; i < scripts.length; i++){
457                this.loadScript(scripts[i]);
458                }
459        }
460
461        cConnector.prototype.loadScript = function(scriptPath, basePath)
462        {
463            if (document.getElementById('uploadscript_'+scriptPath)) {
464                    return;
465            }
466
467            if( basePath == null )
468                basePath = "js/";
469
470            var path = basePath+scriptPath+".js?"+this.updateVersion;
471
472            var head = document.getElementsByTagName("head")[0];
473            var script = document.createElement("SCRIPT");
474            script.id = 'uploadscript_'+scriptPath;
475            script.type = 'text/javascript';
476            if(is_ie) {
477                    this.oxmlhttp.open("GET", path, false);
478                    this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
479                    this.oxmlhttp.send(null);
480                    if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||      this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
481                            throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
482                    script.text = this.oxmlhttp.responseText;
483            }
484            else {
485                    script.src =  path;
486            }
487
488            head.appendChild(script);
489            return;
490        }
491//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
492        // Default Controller File
493        var DEFAULT_URL = 'controller.php?action=';
494        // connector object
495        var connector = new cConnector();
496        var _onmouseup = document.onmouseup;
497        var isExecuteForm = false;
498        var id = null;
499
500        cConnector.prototype.queryConnectorCache = function(url,handler){
501                if (this.connectorCache.valid[url])
502                {
503                        handler(this.connectorCache.result[url]);
504                        return true;
505                }
506                else
507                        return false;
508        }
509        cConnector.prototype.purgeCache= function(){
510                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
511                        return false;
512                var i;
513                for (i=0; i<= this.expurgatedCache.length; i++)
514                {
515                        this.connectorCache.valid[this.expurgatedCache[i]] = false;
516                        try {
517                        delete this.connectorCache.result[this.expurgatedCache[i]];
518                        }
519                        catch (e) { };
520                }
521        }
522        cConnector.prototype.addToCache = function(id,data){
523                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
524                        return false;
525                var func = id.substr(id.lastIndexOf('.')+1);
526                if (func.indexOf('&') > 0)
527                        func = func.substr(0,func.indexOf('&'));
528                switch (func){
529                        // functions that enters in cache
530                        case 'get_info_msg':
531                                data.cacheHit = true;
532                        case 'get_preferences':
533                        case 'getSharedUsersFrom':
534                        case 'get_organizations':
535                        case 'get_catalogs':
536                        case 'get_dropdown_contacts':
537                        case 'get_cc_contacts':
538                        case 'get_cc_groups':
539                        case 'getUsersByEmail':
540                                this.connectorCache.valid[id] = true;
541                                this.connectorCache.result[id] = data;
542                                break;
543                        // function that needs expire
544                        case 'get_range_msgs2':
545                        case 'quicksearch':
546                        case 'get_folders_list':
547                        case 'search_msg':
548                        case 'search_for':
549                                this.connectorCache.valid[id] = true;
550                                this.connectorCache.result[id] = data;
551                                var i = this.expurgatedCache.length;
552                                this.expurgatedCache[i+1] = id;
553                                break;
554                        //functions that expires the cache
555                        case 'move_messages':
556                        case 'delete_msgs':
557                        case 'getReturnExecuteForm':
558                        case 'set_messages_flag':
559                        case 'empty_folder':
560                                this.purgeCache();
561                        default: // no cache
562                                break;
563                }
564        }
565
566        //      Function executes AJAX
567        //      cExecute (url, handler, params)
568        //      url: 'module.class.method'
569        //  handle: function handle() receive response.
570        //  params: parameters for POST method
571        //      form: form element (for upload files)   
572        function cExecute(url, handler, params, form) {
573                if(isExecuteForm){
574                        isExecuteForm = false;
575                        document.onmouseup = _onmouseup;
576                }
577                if(form) {
578                        cExecuteForm(url, form);
579                        return;
580                }
581
582                url = DEFAULT_URL + url;
583
584                if (connector.queryConnectorCache(params?url+"&"+params:url,handler))
585                        return;
586
587                if(params)
588                        method = "POST";
589                else
590                        method = "GET";
591
592                id = url;
593                connector.newRequest(id, url, method, handler, params);
594        }
595
596// This function executes submit values to Controller (POST)
597        // The return is void.
598        //      cExecuteForm (url, form)
599        //      url: 'module.class.method'
600        //      form: form element (for upload files)   
601        function cExecuteForm(url, form, handler,id){
602                connector.buildBar();
603                isExecuteForm = true;
604               
605                document.onmouseup = alertBut;
606               
607                connector.showProgressBar();
608                if(! (divUpload = document.getElementById('divUpload'))) {
609                        divUpload               = document.createElement('DIV');               
610                        divUpload.id    = 'divUpload';
611                        document.body.appendChild(divUpload);
612                }
613
614                if(! (el = document.getElementById('_action'))) {                       
615                        el                      = document.createElement('input');
616                        el.type = 'hidden';
617                        el.id           = '_action';   
618                        el.name = '_action';
619                        form.appendChild(el);
620                }
621
622                var divFiles = Element("divFiles_"+id);         
623                if (divFiles && divFiles.firstChild) {
624                        el                      = document.createElement('input');
625                        el.type = 'hidden';     
626                        el.name = 'countFiles';
627                        var countDivFiles = 0;
628                        try{
629                                countDivFiles = parseInt(divFiles.lastChild.firstChild.id.split('_')[2]) + 1; // The id of last file
630                                }
631                        catch (e) { var countDivFiles = 0; };
632                        el.value        = countDivFiles ;
633                        form.appendChild(el);                                           
634                }               
635
636                form._action.value = url;
637                // Connector Bug fixing: Encapsulating returned handler function
638                handlerExecuteForm = handler;
639                var form_handler = function (data){
640                        handlerExecuteForm(data);
641                        document.getElementById('uploadFile').parentNode.removeChild(document.getElementById('uploadFile'));
642                        handlerExecuteForm = null;
643                }
644                if (is_ie)
645                        divUpload.innerHTML= "<iframe onload=\"cExecute('$this.functions.getReturnExecuteForm',"+form_handler+");\"  style='display:"+(debug_controller ? "" : "none")+";width:"+(debug_controller ? 400 : 0)+";height:"+(debug_controller ? 400 : 0)+";' name='uploadFile'></iframe>";
646                else{
647                        var iframe = document.createElement('iframe');
648                        iframe.name="uploadFile";
649                        iframe.id="uploadFile";
650                        iframe.onload = function(){cExecute('$this.functions.getReturnExecuteForm', form_handler);}
651                        iframe.style.display = (debug_controller ? "" : "none");
652                        iframe.style.width = (debug_controller ? 400 : 0) + "px";
653                        iframe.style.height = (debug_controller ? 400 : 0) + "px";
654                        divUpload.appendChild(iframe);
655                }
656
657                form.action ="controller.php";
658                form.target ="uploadFile";             
659                form.submit();
660                // Connector Bug fixing: Exclude '_action' element after cExecuteForm execution
661                if(el && el.id == '_action'){
662                        el.parentNode.removeChild(el);
663                }
664        }       
665       
666       
667        function alertBut(e) {
668                if(!e)
669                        e = window.event;
670
671            if(_onmouseup)
672                        _onmouseup(e);
673
674                if(isExecuteForm) {
675                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
676                        connector.hideProgressBar();
677                        isExecuteForm = false;
678                        delete connector.requests[id];                                                         
679                                connector.requests[id] = null;
680                        stop();                                         
681                        return;
682                }
683                else
684                        return false;
685            }
686        }       
Note: See TracBrowser for help on using the repository browser.