source: branches/2.2/expressoMail1_2/js/connector.js @ 5151

Revision 5151, 16.7 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #2301 - Corrigido o problema de abrir as mensagens no IE a partir do Home do Expresso

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