source: companies/celepar/expressoMail1_2/js/connector.js @ 763

Revision 763, 15.9 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

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