source: branches/2.3/expressoMail1_2/js/connector.js @ 6064

Revision 6064, 17.5 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2671 - Mensagem excedendo limite ao enviar com enexos.

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