source: branches/2.2.0.1/expressoMail1_2/js/connector.js @ 4062

Revision 4062, 17.3 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1594 - Permitir redimensionamento das colunas na lista emails

  • 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)  {
462        if (document.getElementById('uploadscript_'+scriptPath)) {
463                return;
464        }
465                var head = document.getElementsByTagName("head")[0];
466                var script = document.createElement("SCRIPT");
467                script.id = 'uploadscript_'+scriptPath;
468                script.type = 'text/javascript';
469                if(is_ie) {
470                        this.oxmlhttp.open("GET", "js/"+scriptPath+".js?"+this.updateVersion, false);
471                        this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
472                        this.oxmlhttp.send(null);
473                        if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||  this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
474                                throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
475                        script.text = this.oxmlhttp.responseText;
476                }
477                else {
478                        script.src =  "js/"+scriptPath+".js?"+this.updateVersion;
479                }
480
481                head.appendChild(script);
482                return;
483        }
484//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
485        // Default Controller File
486        var DEFAULT_URL = 'controller.php?action=';
487        // connector object
488        var connector = new cConnector();
489        var _onmouseup = document.onmouseup;
490        var isExecuteForm = false;
491        var id = null;
492
493        cConnector.prototype.queryConnectorCache = function(url,handler){
494                if (this.connectorCache.valid[url])
495                {
496                        handler(this.connectorCache.result[url]);
497                        return true;
498                }
499                else
500                        return false;
501        }
502        cConnector.prototype.purgeCache= function(){
503                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
504                        return false;
505                var i;
506                for (i=0; i<= this.expurgatedCache.length; i++)
507                {
508                        this.connectorCache.valid[this.expurgatedCache[i]] = false;
509                        try {
510                        delete this.connectorCache.result[this.expurgatedCache[i]];
511                        }
512                        catch (e) { };
513                }
514        }
515        cConnector.prototype.addToCache = function(id,data){
516                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
517                        return false;
518                var func = id.substr(id.lastIndexOf('.')+1);
519                if (func.indexOf('&') > 0)
520                        func = func.substr(0,func.indexOf('&'));
521                switch (func){
522                        // functions that enters in cache
523                        case 'get_info_msg':
524                                data.cacheHit = true;
525                        case 'get_preferences':
526                        case 'getSharedUsersFrom':
527                        case 'get_organizations':
528                        case 'get_catalogs':
529                        case 'get_dropdown_contacts':
530                        case 'get_cc_contacts':
531                        case 'get_cc_groups':
532                        case 'getUsersByEmail':
533                                this.connectorCache.valid[id] = true;
534                                this.connectorCache.result[id] = data;
535                                break;
536                        // function that needs expire
537                        case 'get_range_msgs2':
538                        case 'quicksearch':
539                        case 'get_folders_list':
540                        case 'search_msg':
541                        case 'search_for':
542                                this.connectorCache.valid[id] = true;
543                                this.connectorCache.result[id] = data;
544                                var i = this.expurgatedCache.length;
545                                this.expurgatedCache[i+1] = id;
546                                break;
547                        //functions that expires the cache
548                        case 'move_messages':
549                        case 'delete_msgs':
550                        case 'getReturnExecuteForm':
551                        case 'set_messages_flag':
552                        case 'empty_folder':
553                                this.purgeCache();
554                        default: // no cache
555                                break;
556                }
557        }
558
559        //      Function executes AJAX
560        //      cExecute (url, handler, params)
561        //      url: 'module.class.method'
562        //  handle: function handle() receive response.
563        //  params: parameters for POST method
564        //      form: form element (for upload files)   
565        function cExecute(url, handler, params, form) {
566                if(isExecuteForm){
567                        isExecuteForm = false;
568                        document.onmouseup = _onmouseup;
569                }
570                if(form) {
571                        cExecuteForm(url, form);
572                        return;
573                }
574
575                url = DEFAULT_URL + url;
576
577                if (connector.queryConnectorCache(params?url+"&"+params:url,handler))
578                        return;
579
580                if(params)
581                        method = "POST";
582                else
583                        method = "GET";
584
585                id = url;
586                connector.newRequest(id, url, method, handler, params);
587        }
588
589// This function executes submit values to Controller (POST)
590        // The return is void.
591        //      cExecuteForm (url, form)
592        //      url: 'module.class.method'
593        //      form: form element (for upload files)   
594        function cExecuteForm(url, form, handler,id){
595                connector.buildBar();
596                isExecuteForm = true;
597               
598                document.onmouseup = alertBut;
599               
600                connector.showProgressBar();
601                if(! (divUpload = document.getElementById('divUpload'))) {
602                        divUpload               = document.createElement('DIV');               
603                        divUpload.id    = 'divUpload';
604                        document.body.appendChild(divUpload);
605                }
606
607                if(! (el = document.getElementById('_action'))) {                       
608                        el                      = document.createElement('input');
609                        el.type = 'hidden';
610                        el.id           = '_action';   
611                        el.name = '_action';
612                        form.appendChild(el);
613                }
614
615                var divFiles = Element("divFiles_"+id);         
616                if (divFiles && divFiles.firstChild) {
617                        el                      = document.createElement('input');
618                        el.type = 'hidden';     
619                        el.name = 'countFiles';
620                        var countDivFiles = 0;
621                        try{
622                                countDivFiles = parseInt(divFiles.lastChild.firstChild.id.split('_')[2]) + 1; // The id of last file
623                                }
624                        catch (e) { var countDivFiles = 0; };
625                        el.value        = countDivFiles ;
626                        form.appendChild(el);                                           
627                }               
628
629                form._action.value = url;
630                // Connector Bug fixing: Encapsulating returned handler function
631                handlerExecuteForm = handler;
632                var form_handler = function (data){
633                        handlerExecuteForm(data);
634                        document.getElementById('uploadFile').parentNode.removeChild(document.getElementById('uploadFile'));
635                        handlerExecuteForm = null;
636                }
637                if (is_ie)
638                        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>";
639                else{
640                        var iframe = document.createElement('iframe');
641                        iframe.name="uploadFile";
642                        iframe.id="uploadFile";
643                        iframe.onload = function(){cExecute('$this.functions.getReturnExecuteForm', form_handler);}
644                        iframe.style.display = (debug_controller ? "" : "none");
645                        iframe.style.width = (debug_controller ? 400 : 0) + "px";
646                        iframe.style.height = (debug_controller ? 400 : 0) + "px";
647                        divUpload.appendChild(iframe);
648                }
649
650                form.action ="controller.php";
651                form.target ="uploadFile";             
652                form.submit();
653                // Connector Bug fixing: Exclude '_action' element after cExecuteForm execution
654                if(el && el.id == '_action'){
655                        el.parentNode.removeChild(el);
656                }
657        }       
658       
659       
660        function alertBut(e) {
661                if(!e)
662                        e = window.event;
663
664            if(_onmouseup)
665                        _onmouseup(e);
666
667                if(isExecuteForm) {
668                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
669                        connector.hideProgressBar();
670                        isExecuteForm = false;
671                        delete connector.requests[id];                                                         
672                                connector.requests[id] = null;
673                        stop();                                         
674                        return;
675                }
676                else
677                        return false;
678            }
679        }       
Note: See TracBrowser for help on using the repository browser.