source: trunk/expressoMail1_2/js/connector.js @ 135

Revision 135, 13.6 KB checked in by niltonneto, 16 years ago (diff)

Correções de bug. Vide ocorrências no Trac para a versão 1.2201

  • 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;
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                       
271                        return data;
272                }
273       
274                return f(str);
275        }
276
277        //Create XMLHTTP object Method
278        cConnector.prototype.createXMLHTTP = function ()
279        {       
280                try
281                {
282                        this.oxmlhttp = new XMLHttpRequest();
283                        this.oxmlhttp.overrideMimeType('text/xml');
284                }
285                catch (e)
286                {
287                        try
288                        {
289                                this.oxmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
290                        }
291                        catch (e1)
292                        {
293                                try
294                                {
295                                        this.oxmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
296                                }
297                                catch (e2)
298                                {
299                                        this.oxmlhttp = null;
300                                }
301                        }
302                }
303       
304        }
305       
306        // Request Constructor Connector       
307        cConnector.prototype.newRequest = function (id, target, method, handler, data)
308        {                               
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(debug_controller) {
343                                                                        document.getElementById("debug_controller").innerHTML += oxmlhttp.responseText;
344                                                                        document.getElementById("debug_controller").innerHTML += "<br>-------------------------------------------------------------------------------------<br>";
345                                                                }                                                                       
346                                                                handler(data);
347                                                                delete _thisObject.requests[id];                                                               
348                                                                _thisObject.requests[id] = null;
349                                                        }
350
351                                                        break;
352
353                                                case 404:
354                                                       
355                                                        alert('Page Not Found!');
356                                                        break;
357
358                                                default:                                                                                               
359                                        }
360                                }
361                        }
362                        catch (e)
363                        {                       
364                                _thisObject.hideProgressBar();
365                                if(debug_controller)
366                                        alert(e+"\n"+oxmlhttp.responseText);
367                                // View Exception in Javascript Console
368                                throw(e);
369                        }
370                                               
371                }
372
373                try
374                {
375                       
376                        if (method == '' || method == 'GET')
377                        {                                                               
378                                oxmlhttp.open("GET",target,true);
379                                if (typeof(handler) == 'function')
380                                {       
381                                        oxmlhttp.onreadystatechange =  sub_handler;                                     
382                                        oxmlhttp.send(null);                                   
383                                }               
384                               
385                        }
386                        else if (method == 'POST')
387                        {
388                                oxmlhttp.open("POST",target, true);
389                                oxmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
390                                if (typeof(handler) == 'function')
391                                {
392                                        oxmlhttp.onreadystatechange = sub_handler;
393                                        oxmlhttp.send(data);
394                                }                               
395                               
396                        }
397                }
398                catch(e)
399                {       
400                        _thisObject.hideProgressBar();
401                        if(debug_controller)
402                                alert(e);
403                        // View Exception in Javascript Console 
404                        throw(e);
405                }
406                                               
407                return true;
408        }
409        // Cancel Request Connector
410        cConnector.prototype.cancelRequest = function (){
411                if (!this.requests[this.tid]){
412                        return false;
413                }
414                this.oxmlhttp.onreadystatechange = null;
415                this.requests[this.tid].abort();
416                this.hideProgressBar();
417        }
418//------------------------------------  END: Functions for Connector HTTPRequest  -------------------------------------------------//
419
420//      ----------------------------------- BEGIN: Functions for build Bar Progress ---------------------------------------------------------//
421        cConnector.prototype.hideProgressBar = function ()
422        {
423                var div = document.getElementById('divProgressBar');
424                div.style.visibility = 'hidden';
425                this.isVisibleBar = false;
426        }
427       
428        cConnector.prototype.showProgressBar = function(){
429                var div = document.getElementById('divProgressBar');
430                div.style.visibility = 'visible';                       
431
432                this.isVisibleBar = true;
433        }
434
435        cConnector.prototype.loadAllScripts = function(scripts) {       
436                for(var i = 0; i < scripts.length; i++){
437                        this.loadScript(scripts[i]);
438                }
439        }
440
441        cConnector.prototype.loadScript = function(scriptPath)  {
442        if (document.getElementById('uploadscript_'+scriptPath)) {
443                return;
444        }
445                var head = document.getElementsByTagName("head")[0];
446                var script = document.createElement("SCRIPT");
447                script.id = 'uploadscript_'+scriptPath;
448                script.type = 'text/javascript';
449               
450                if(is_ie) {
451               
452                        this.oxmlhttp.open("GET", "js/"+scriptPath+".js?"+this.updateVersion, false);
453            this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
454                        this.oxmlhttp.send(null);
455                        if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||  this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
456                                throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
457                        script.text = this.oxmlhttp.responseText;                               
458                }
459                else {
460                        script.src =  "js/"+scriptPath+".js?"+this.updateVersion;
461                }
462
463                head.appendChild(script);
464                return;
465        }
466//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
467        // Default Controller File
468        var DEFAULT_URL = 'controller.php?action=';
469        // connector object
470        var connector = new cConnector();
471        var _onmouseup = document.onmouseup;
472        var isExecuteForm = false;
473        var id = null;
474
475        //      Function executes AJAX
476        //      cExecute (url, handler, params)
477        //      url: 'module.class.method'
478        //  handle: function handle() receive response.
479        //  params: parameters for POST method
480        //      form: form element (for upload files)   
481        function cExecute(url, handler, params, form) {
482                if(isExecuteForm){
483                        isExecuteForm = false;
484                        document.onmouseup = _onmouseup;
485                }
486                if(form) {
487                        cExecuteForm(url, form);
488                        return;
489                }
490               
491                url = DEFAULT_URL + url;
492                       
493                if(params)               
494                        method = "POST";
495                               
496                 else
497                        method = "GET";
498                         
499                 id = url;
500                connector.newRequest(id, url, method, handler, params);
501        }
502       
503        // This function executes submit values to Controller (POST)
504        // The return is void.
505        //      cExecuteForm (url, form)
506        //      url: 'module.class.method'
507        //      form: form element (for upload files)   
508        function cExecuteForm(url, form, handler){
509                connector.buildBar();
510                isExecuteForm = true;           
511                document.onmouseup = alertBut;
512
513                connector.showProgressBar();
514                if(! (divUpload = document.getElementById('divUpload'))) {
515                        divUpload               = document.createElement('DIV');               
516                        divUpload.id    = 'divUpload';
517                        document.body.appendChild(divUpload);
518                }
519
520                if(! (el = document.getElementById('_action'))) {                       
521                        el                      = document.createElement('input');
522                        el.type = 'hidden';
523                        el.id           = '_action';   
524                        el.name = '_action';
525                        form.appendChild(el);
526                }
527
528                if(countFiles) {                       
529                        el                      = document.createElement('input');
530                        el.type = 'hidden';     
531                        el.name = 'countFiles';
532                        el.value        = countFiles;
533                        form.appendChild(el);                                           
534                }               
535
536                form._action.value = url;
537                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>";
538                form.action ="controller.php";
539                form.target ="uploadFile";             
540                form.submit();
541        }       
542       
543       
544        function alertBut(e) {
545                if(!e)
546                        e = window.event;
547
548            if(_onmouseup)
549                        _onmouseup(e);
550
551                if(isExecuteForm) {
552                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
553                        connector.hideProgressBar();
554                        isExecuteForm = false;
555                        delete connector.requests[id];                                                         
556                                connector.requests[id] = null;
557                        stop();                                         
558                        return;
559                }
560                else
561                        return false;
562            }
563        }       
Note: See TracBrowser for help on using the repository browser.