source: trunk/expressoAdmin1_2/js/jscode/connector.js @ 317

Revision 317, 13.5 KB checked in by niltonneto, 16 years ago (diff)

Versionamento feito pelo desenvolvedor (jakjr).

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