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

Revision 2, 13.5 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • 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        }
43       
44        cConnector.prototype.buildBar = function()
45                {                       
46                        var div = document.getElementById('divProgressBar');
47               
48                        if(! div) {                                                                                             
49                                div = document.createElement("DIV");
50                                div.style.visibility    = "hidden";             
51                                div.style.width = "103px";
52                                div.id = 'divProgressBar';
53                                div.align = "center";
54                                div.innerHTML = '&nbsp;&nbsp;<font face="Verdana" size="2" color="WHITE">'+document.getElementById('txt_loading').value+'...</font>&nbsp;';
55                                div.style.background = "#cc4444";
56                                div.style.position = 'fixed';
57                                div.style.top = '0px';
58                                div.style.right = '0px';
59                                document.body.appendChild(div);                                                                                                                         
60                               
61                                if(is_ie) {
62                                        var elem = document.all[div.id];
63                                        elem.style.position="absolute";
64                                        var root = document.body;
65                                        var posX = elem.offsetLeft-root.scrollLeft;
66                                        var posY = elem.offsetTop-root.scrollTop;
67                                        root.onscroll = function() {
68                                                elem.style.right = '0px';
69                                                elem.style.top = (posY + root.scrollTop) + "px";
70                                        };                                     
71                                }
72                               
73                                if(debug_controller) {
74                                        div = document.createElement("DIV");
75                                        div.style.width = "800px";
76                                        div.style.height= "400px";
77                                        div.id = "debug_controller";
78                                        div.align='right';
79                                        document.body.appendChild(div);                                                                                                                         
80                                }
81                        }                                                               
82        }       
83//------------------------------------ BEGIN: Functions for Connector HTTPRequest  -------------------------------------------------// 
84        // Serialize Data Method
85        cConnector.prototype.serialize = function(data)
86        {       var _thisObject = this;         
87                var f = function(data)
88                {
89                        var str_data;
90       
91                        if (data == null ||
92                                (typeof(data) == 'string' && data == ''))
93                        {
94                                str_data = 'N;';
95                        }
96       
97                        else switch(typeof(data))
98                        {
99                                case 'object':
100                                        var arrayCount = 0;
101       
102                                        str_data = '';
103       
104                                        for (i in data)
105                                        {
106                                                if (i == 'length')
107                                                {
108                                                        continue;
109                                                }
110                                               
111                                                arrayCount++;
112                                                switch (typeof(i))
113                                                {
114                                                        case 'number':
115                                                                str_data += 'i:' + i + ';' + _thisObject.serialize(data[i]);
116                                                                break;
117       
118                                                        case 'string':
119                                                                str_data += 's:' + i.length + ':"' + i + '";' + _thisObject.serialize(data[i]);
120                                                                break;
121       
122                                                        default:
123                                                                showMessage(Element('cc_msg_err_serialize_data_unknown').value);
124                                                                break;
125                                                }
126                                        }
127       
128                                        if (!arrayCount)
129                                        {
130                                                str_data = 'N;';       
131                                        }
132                                        else
133                                        {
134                                                str_data = 'a:' + arrayCount + ':{' + str_data + '}';
135                                        }
136                                       
137                                        break;
138                       
139                                case 'string':
140                                        str_data = 's:' + data.length + ':"' + data + '";';
141                                        break;
142                                       
143                                case 'number':
144                                        str_data = 'i:' + data + ';';
145                                        break;
146       
147                                case 'boolean':
148                                        str_data = 'b:' + (data ? '1' : '0') + ';';
149                                        break;
150       
151                                default:
152                                        showMessage(Element('cc_msg_err_serialize_data_unknown').value);
153                                        return null;
154                        }
155       
156                        return str_data;
157                }
158       
159                var sdata = f(data);
160                return sdata;
161        }
162        cConnector.prototype.matchBracket = function(strG, iniPosG)
163        {
164                _thisObject = this;
165                var f = function (str, iniPos)
166                {
167                        var nOpen, nClose = iniPos;
168               
169                        do
170                        {
171                                nOpen = str.indexOf('{', nClose+1);
172                                nClose = str.indexOf('}', nClose+1);
173
174                                if (nOpen == -1)
175                                {
176                                        return nClose;
177                                }
178                       
179                                if (nOpen < nClose )
180                                {
181                                        nClose = _thisObject.matchBracket(str, nOpen);
182                                }
183                       
184                        } while (nOpen < nClose);
185
186                        return nClose;
187                }
188
189                return f(strG, iniPosG);
190        }
191       
192               
193        //Unserialize Data Method
194        cConnector.prototype.unserialize = function(str)
195        {
196               
197                _thisObject = this;
198               
199                var f = function (str)
200                {
201                        switch (str.charAt(0))
202                        {
203                                case 'a':
204                                       
205                                        var data = new Array();
206                                        var n = parseInt( str.substring( str.indexOf(':')+1, str.indexOf(':',2) ) );
207                                        var arrayContent = str.substring(str.indexOf('{')+1, str.lastIndexOf('}'));
208                               
209                                        for (var i = 0; i < n; i++)
210                                        {
211                                                var pos = 0;
212       
213                                                /* Process Index */
214                                                var indexStr = arrayContent.substr(pos, arrayContent.indexOf(';')+1);
215                                                var index = _thisObject.unserialize(indexStr);
216                                                pos = arrayContent.indexOf(';', pos)+1;
217                                               
218                                                /* Process Content */
219                                                var part = null;
220                                                switch (arrayContent.charAt(pos))
221                                                {
222                                                        case 'a':
223                                                                var pos_ = _thisObject.matchBracket(arrayContent, arrayContent.indexOf('{', pos))+1;
224                                                                part = arrayContent.substring(pos, pos_);
225                                                                pos = pos_;
226                                                                data[index] = _thisObject.unserialize(part);
227                                                                break;
228                                               
229                                                        case 's':
230                                                                var pval = arrayContent.indexOf(':', pos+2);
231                                                                var val  = parseInt(arrayContent.substring(pos+2, pval));
232                                                                pos = pval + val + 4;
233                                                                data[index] = arrayContent.substr(pval+2, val);
234                                                                break;
235       
236                                                        default:
237                                                                part = arrayContent.substring(pos, arrayContent.indexOf(';', pos)+1);
238                                                                pos = arrayContent.indexOf(';', pos)+1;
239                                                                data[index] = _thisObject.unserialize(part);
240                                                                break;
241                                                }
242                                                arrayContent = arrayContent.substr(pos);
243                                        }
244                                        break;
245                                       
246                                case 's':
247                                        var pos = str.indexOf(':', 2);
248                                        var val = parseInt(str.substring(2,pos));
249                                        var data = str.substr(pos+2, val);
250                                        str = str.substr(pos + 4 + val);
251                                        break;
252       
253                                case 'i':
254                                case 'd':
255                                        var pos = str.indexOf(';');
256                                        var data = parseInt(str.substring(2,pos));
257                                        str = str.substr(pos + 1);
258                                        break;
259                               
260                                case 'N':
261                                        var data = null;
262                                        str = str.substr(str.indexOf(';') + 1);
263                                        break;
264       
265                                case 'b':
266                                        var data = str.charAt(2) == '1' ? true : false;
267                                        break;
268                        }
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                this.tid = id;
309                if (this.requests[id]) {
310                        return false;
311                }
312       
313                this.createXMLHTTP();
314                var oxmlhttp = this.oxmlhttp;
315                var _thisObject = this;         
316               
317                if (! oxmlhttp)         
318                        return false;
319                               
320                this.requests[id] = oxmlhttp;
321                this.buildBar();               
322                this.showProgressBar();
323               
324                var sub_handler = function ()
325                {                       
326                        var progressBar = _thisObject.progressBar;
327                       
328                        try
329                        {
330                                if (oxmlhttp.readyState == 4 )
331                                {
332                                       
333                                        switch (oxmlhttp.status)
334                                        {
335                                               
336                                                case 200:
337                                                        if (typeof(handler) == 'function')
338                                                        {                                                                                                                               
339                                                                _thisObject.hideProgressBar();                                                                                                                         
340                                                                var data = _thisObject.unserialize(oxmlhttp.responseText);
341                                                                if(debug_controller) {
342                                                                        document.getElementById("debug_controller").innerHTML += oxmlhttp.responseText;
343                                                                        document.getElementById("debug_controller").innerHTML += "<br>-------------------------------------------------------------------------------------<br>";
344                                                                }                                                                       
345                                                                handler(data);
346                                                                delete _thisObject.requests[id];                                                               
347                                                                _thisObject.requests[id] = null;
348                                                        }
349
350                                                        break;
351
352                                                case 404:
353                                                       
354                                                        alert('Page Not Found!');
355                                                        break;
356
357                                                default:                                                                                               
358                                        }
359                                }
360                        }
361                        catch (e)
362                        {                       
363                                _thisObject.hideProgressBar();
364                                if(debug_controller)
365                                        alert(e+"\n"+oxmlhttp.responseText);
366                                // View Exception in Javascript Console
367                                throw(e);
368                        }
369                                               
370                }
371
372                try
373                {
374                       
375                        if (method == '' || method == 'GET')
376                        {                                                               
377                                oxmlhttp.open("GET",target,true);
378                                if (typeof(handler) == 'function')
379                                {       
380                                        oxmlhttp.onreadystatechange =  sub_handler;                                     
381                                        oxmlhttp.send(null);                                   
382                                }               
383                               
384                        }
385                        else if (method == 'POST')
386                        {
387                                oxmlhttp.open("POST",target, true);
388                                oxmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
389                                if (typeof(handler) == 'function')
390                                {
391                                        oxmlhttp.onreadystatechange = sub_handler;
392                                        oxmlhttp.send(data);
393                                }                               
394                               
395                        }
396                }
397                catch(e)
398                {       
399                        _thisObject.hideProgressBar();
400                        if(debug_controller)
401                                alert(e);
402                        // View Exception in Javascript Console 
403                        throw(e);
404                }
405                                               
406                return true;
407        }
408        // Cancel Request Connector
409        cConnector.prototype.cancelRequest = function (){
410                if (!this.requests[this.tid]){
411                        return false;
412                }
413                this.oxmlhttp.onreadystatechange = null;
414                this.requests[this.tid].abort();
415                this.hideProgressBar();
416        }
417//------------------------------------  END: Functions for Connector HTTPRequest  -------------------------------------------------//
418
419//      ----------------------------------- BEGIN: Functions for build Bar Progress ---------------------------------------------------------//
420        cConnector.prototype.hideProgressBar = function ()
421        {
422                var div = document.getElementById('divProgressBar');
423                div.style.visibility = 'hidden';
424                this.isVisibleBar = false;
425        }
426       
427        cConnector.prototype.showProgressBar = function(){
428                var div = document.getElementById('divProgressBar');
429                div.style.visibility = 'visible';                       
430
431                this.isVisibleBar = true;
432        }
433
434        cConnector.prototype.loadAllScripts = function(scripts) {       
435                for(var i = 0; i < scripts.length; i++){
436                        this.loadScript(scripts[i]);
437                }
438        }
439
440        cConnector.prototype.loadScript = function(scriptPath)  {
441        if (document.getElementById('uploadscript_'+scriptPath)) {
442                return;
443        }
444                var head = document.getElementsByTagName("head")[0];
445                var script = document.createElement("SCRIPT");
446                script.id = 'uploadscript_'+scriptPath;
447                script.type = 'text/javascript';
448               
449                if(is_ie) {
450               
451                        this.oxmlhttp.open("GET", "js/"+scriptPath+".js", false);
452            this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
453                        this.oxmlhttp.send(null);
454                        if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||  this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
455                                throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
456                        script.text = this.oxmlhttp.responseText;                               
457                }
458                else {
459                        script.src =  "js/"+scriptPath+".js";
460                }
461
462                head.appendChild(script);
463                return;
464        }
465//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
466        // Default Controller File
467        var DEFAULT_URL = 'controller.php?action=';
468        // connector object
469        var connector = new cConnector();
470        var _onmouseup = document.onmouseup;
471        var isExecuteForm = false;
472        var id = null;
473
474        //      Function executes AJAX
475        //      cExecute (url, handler, params)
476        //      url: 'module.class.method'
477        //  handle: function handle() receive response.
478        //  params: parameters for POST method
479        //      form: form element (for upload files)   
480        function cExecute(url, handler, params, form) {
481                if(isExecuteForm){
482                        isExecuteForm = false;
483                        document.onmouseup = _onmouseup;
484                }
485                if(form) {
486                        cExecuteForm(url, form);
487                        return;
488                }
489               
490                url = DEFAULT_URL + url;
491                       
492                if(params)               
493                        method = "POST";
494                               
495                 else
496                        method = "GET";
497                         
498                 id = url;
499                connector.newRequest(id, url, method, handler, params);
500        }
501       
502        // This function executes submit values to Controller (POST)
503        // The return is void.
504        //      cExecuteForm (url, form)
505        //      url: 'module.class.method'
506        //      form: form element (for upload files)   
507        function cExecuteForm(url, form, handler){
508                connector.buildBar();
509                isExecuteForm = true;           
510                document.onmouseup = alertBut;
511
512                connector.showProgressBar();
513                if(! (divUpload = document.getElementById('divUpload'))) {
514                        divUpload               = document.createElement('DIV');               
515                        divUpload.id    = 'divUpload';
516                        document.body.appendChild(divUpload);
517                }
518
519                if(! (el = document.getElementById('_action'))) {                       
520                        el                      = document.createElement('input');
521                        el.type = 'hidden';
522                        el.id           = '_action';   
523                        el.name = '_action';
524                        form.appendChild(el);
525                }
526
527                if(countFiles) {                       
528                        el                      = document.createElement('input');
529                        el.type = 'hidden';     
530                        el.name = 'countFiles';
531                        el.value        = countFiles;
532                        form.appendChild(el);                                           
533                }               
534
535                form._action.value = url;
536                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>";
537                form.action ="controller.php";
538                form.target ="uploadFile";             
539                form.submit();
540        }       
541       
542       
543        function alertBut(e) {
544                if(!e)
545                        e = window.event;
546
547            if(_onmouseup)
548                        _onmouseup(e);
549
550                if(isExecuteForm) {
551                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
552                        connector.hideProgressBar();
553                        isExecuteForm = false;
554                        delete connector.requests[id];                                                         
555                                connector.requests[id] = null;
556                        stop();                                         
557                        return;
558                }
559                else
560                        return false;
561            }
562        }       
Note: See TracBrowser for help on using the repository browser.