source: branches/2.4/expressoMail1_2/js/connector.js @ 5210

Revision 5210, 17.5 KB checked in by angelo, 12 years ago (diff)

Ticket #2320 - Sistema esta emitindo mensagem de erro ao salvar uma mensagem

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