source: trunk/phpgwapi/js/expressoAjax/expressoAjax.js @ 2676

Revision 2676, 13.6 KB checked in by rafaelraymundo, 14 years ago (diff)

Ticket #490 - Reestabelecido o funcionamento do arquivamento local

Line 
1var debug_controller =false;
2var files = new Array();
3var progressBar;
4var handlerExecuteForm = null;
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.hold_session = false;
37                this.oxmlhttp = null;
38                this.isVisibleBar = false;
39                this.tid = 0;
40                this.progressBar = null;
41                this.oldX = 0;
42                this.oldY = 0;
43                this.updateVersion = "";
44                this.cacheRequest = null;
45                this.connectorCache = {
46                                'valid' : [],
47                                'result' : []
48                };
49                this.expurgatedCache = new Array(); // Data to purge from cache
50
51        };
52        cConnector.prototype.verify_session = function(data) {
53                if(data && data.nosession){
54                        // If hold session is setted, dont reload the page ...
55                        if(expresso.connector.hold_session) {
56                                if(typeof(write_msg) == "function" && typeof(get_lang) == "function")
57                                        write_msg(get_lang("your session could not be verified."));
58                                else
59                                        alert("your session could not be verified.");
60                        }
61                        else
62                                window.location.reload();
63                        return false;
64                }
65                else
66                        return true;
67        };
68        cConnector.prototype.buildBar = function()
69                {
70                        var div = document.getElementById('divProgressBar');
71
72                        if(! div) {
73                                div = document.createElement("DIV");
74                                div.style.visibility    = "hidden";
75                                div.style.width = "103px";
76                                div.id = 'divProgressBar';
77                                div.align = "center";
78                                var loadingTitle = document.getElementById('txt_loading');
79                                div.innerHTML = '&nbsp;&nbsp;<font face="Verdana" size="2" color="WHITE">'+loadingTitle.value+'...</font>&nbsp;';
80                                div.style.background = "#cc4444";
81                                div.style.position = 'fixed';
82                                div.style.top = '0px';
83                                div.style.right = '0px';
84                                document.body.appendChild(div);
85
86                                if(is_ie) {
87                                        var elem = document.all[div.id];
88                                        elem.style.position="absolute";
89                                        var root = document.body;
90                                        var posX = elem.offsetLeft-root.scrollLeft;
91                                        var posY = elem.offsetTop-root.scrollTop;
92                                        root.onscroll = function() {
93                                                elem.style.right = '0px';
94                                                elem.style.top = (posY + root.scrollTop) + "px";
95                                        };
96                                }
97
98                                if(debug_controller) {
99                                        div = document.createElement("DIV");
100                                        div.style.width = "800px";
101                                        div.style.height= "400px";
102                                        div.id = "debug_controller";
103                                        div.align='right';
104                                        document.body.appendChild(div);
105                                }
106                        }
107        };
108//------------------------------------ BEGIN: Functions for Connector HTTPRequest  -------------------------------------------------//
109        // Serialize Data Method
110        cConnector.prototype.serialize = function(data)
111        {       var _thisObject = this;
112                var f = function(data)
113                {
114                        var str_data;
115
116                        if (data == null ||
117                                (typeof(data) == 'string' && data == ''))
118                        {
119                                str_data = 'N;';
120                        }
121
122                        else switch(typeof(data))
123                        {
124                                case 'object':
125                                        var arrayCount = 0;
126
127                                        str_data = '';
128
129                                        for (i in data)
130                                        {
131                                                if (i == 'length')
132                                                {
133                                                        continue;
134                                                }
135
136                                                arrayCount++;
137                                                switch (typeof(i))
138                                                {
139                                                        case 'number':
140                                                                str_data += 'i:' + i + ';' + f(data[i]);
141                                                                break;
142
143                                                        case 'string':
144                                                                str_data += 's:' + i.length + ':"' + i + '";' + f(data[i]);
145                                                                break;
146
147                                                        default:
148                                                                showMessage(Element('cc_msg_err_serialize_data_unknown').value);
149                                                                break;
150                                                };
151                                        }
152
153                                        if (!arrayCount)
154                                        {
155                                                str_data = 'N;';
156                                        }
157                                        else
158                                        {
159                                                str_data = 'a:' + arrayCount + ':{' + str_data + '}';
160                                        }
161
162                                        break;
163
164                                case 'string':
165                                        str_data = 's:' + data.length + ':"' + data + '";';
166                                        break;
167
168                                case 'number':
169                                        str_data = 'i:' + data + ';';
170                                        break;
171
172                                case 'boolean':
173                                        str_data = 'b:' + (data ? '1' : '0') + ';';
174                                        break;
175
176//                              default:
177//                                      //showMessage(Element('cc_msg_err_serialize_data_unknown').value);
178//                                      //return null;
179                                       
180                        };
181
182                        return str_data;
183                };
184
185                return f(data);
186        };
187
188
189        //Unserialize Data Method
190        cConnector.prototype.unserialize = function(str)
191        {
192
193                _thisObject = this;
194                var matchB = function (str, iniPos)
195                {
196                        var nOpen, nClose = iniPos;
197                        do
198                        {
199                                nOpen = str.indexOf('{', nClose+1);
200                                nClose = str.indexOf('}', nClose+1);
201
202                                if (nOpen == -1)
203                                {
204                                        return nClose;
205                                }
206                                if (nOpen < nClose )
207                                {
208                                        nClose = matchB(str, nOpen);
209                                }
210                        } while (nOpen < nClose);
211
212                        return nClose;
213                };
214
215                var f = function (str)
216                {
217                        switch (str.toString().charAt(0))
218                        {
219                                case 'a':
220
221                                        var data = new Array();
222                                        var n = parseInt( str.substring( str.indexOf(':')+1, str.indexOf(':',2) ) );
223                                        var arrayContent = str.substring(str.indexOf('{')+1, str.lastIndexOf('}'));
224
225                                        for (var i = 0; i < n; i++)
226                                        {
227                                                var pos = 0;
228
229                                                /* Process Index */
230                                                var indexStr = arrayContent.substr(pos, arrayContent.indexOf(';')+1);
231                                                var index = f(indexStr);
232                                                pos = arrayContent.indexOf(';', pos)+1;
233
234                                                /* Process Content */
235                                                var part = null;
236                                                switch (arrayContent.charAt(pos))
237                                                {
238                                                        case 'a':
239                                                                var pos_ = matchB(arrayContent, arrayContent.indexOf('{', pos))+1;
240                                                                part = arrayContent.substring(pos, pos_);
241                                                                pos = pos_;
242                                                                data[index] = f(part);
243                                                                break;
244
245                                                        case 's':
246                                                                var pval = arrayContent.indexOf(':', pos+2);
247                                                                var val  = parseInt(arrayContent.substring(pos+2, pval));
248                                                                pos = pval + val + 4;
249                                                                data[index] = arrayContent.substr(pval+2, val);
250                                                                break;
251
252                                                        default:
253                                                                part = arrayContent.substring(pos, arrayContent.indexOf(';', pos)+1);
254                                                                pos = arrayContent.indexOf(';', pos)+1;
255                                                                data[index] = f(part);
256                                                                break;
257                                                }
258                                                arrayContent = arrayContent.substr(pos);
259                                        }
260                                        break;
261
262                                case 's':
263                                        var pos = str.indexOf(':', 2);
264                                        var val = parseInt(str.substring(2,pos));
265                                        var data = str.substr(pos+2, val);
266                                        str = str.substr(pos + 4 + val);
267                                        break;
268
269                                case 'i':
270                                case 'd':
271                                        var pos = str.indexOf(';');
272                                        var data = parseInt(str.substring(2,pos));
273                                        str = str.substr(pos + 1);
274                                        break;
275
276                                case 'N':
277                                        var data = null;
278                                        str = str.substr(str.indexOf(';') + 1);
279                                        break;
280
281                                case 'b':
282                                        var data = str.charAt(2) == '1' ? true : false;
283                                        break;
284                        };
285                        return data;
286                };
287
288                return f(str);
289        };
290
291        // Request Constructor Connector
292        cConnector.prototype.newRequest = function (id, target, method, handler, data)
293        {
294                _connector.go( {
295                        "access" : target,
296                        "handler" : function( data )
297                        {
298                                handler( expresso.connector.unserialize( data ) );
299                        }
300                } );
301        };
302        // Cancel Request Connector
303        cConnector.prototype.cancelRequest = function (){
304                return false;
305        };
306//------------------------------------  END: Functions for Connector HTTPRequest  -------------------------------------------------//
307
308//      ----------------------------------- BEGIN: Functions for build Bar Progress ---------------------------------------------------------//
309        cConnector.prototype.hideProgressBar = function ()
310        {
311                var div = document.getElementById('divProgressBar');
312                div.style.visibility = 'hidden';
313                this.isVisibleBar = false;
314        };
315
316        cConnector.prototype.showProgressBar = function(){
317                var div = document.getElementById('divProgressBar');
318                div.style.visibility = 'visible';
319
320                this.isVisibleBar = true;
321        };
322
323        cConnector.prototype.loadAllScripts = function(scripts) {
324                for(var i = 0; i < scripts.length; i++){
325                        this.loadScript(scripts[i]);
326                }
327        };
328
329        cConnector.prototype.loadScript = function(scriptPath)  {
330        if (document.getElementById('uploadscript_'+scriptPath)) {
331                return;
332        }
333                var head = document.getElementsByTagName("head")[0];
334                var script = document.createElement("SCRIPT");
335                script.id = 'uploadscript_'+scriptPath;
336                script.type = 'text/javascript';
337
338                if(is_ie) {
339
340                        this.oxmlhttp.open("GET", URL_SERVER + "expressoMail1_2/js/"+scriptPath+".js?"+this.updateVersion, false);
341                        this.oxmlhttp.setRequestHeader('Content-Type','text/plain');
342                        this.oxmlhttp.send(null);
343                        if(this.oxmlhttp.status != 0 && this.oxmlhttp.status != 200 ||  this.oxmlhttp.status == 0 && this.oxmlhttp.responseText.length == 0)
344                                throw new Error("Error " + this.oxmlhttp.status + "("+this.oxmlhttp.statusText+") when loading script file '"+scriptPath+"'");
345                        script.text = this.oxmlhttp.responseText;
346                }
347                else {
348                        script.src =  URL_SERVER + "expressoMail1_2/js/"+scriptPath+".js?"+this.updateVersion;
349                }
350
351                head.appendChild(script);
352                return;
353        };
354//------------------------------------  END: Functions for Progress Bar  -------------------------------------------------//
355        // Default Controller File
356        var URL_DEFAULT = URL_SERVER + "controller.php?menuaction=";
357        // connector object
358        var _onmouseup = document.onmouseup;
359        var isExecuteForm = false;
360        var id = null;
361
362        cConnector.prototype.queryConnectorCache = function(url,handler){
363                if (this.connectorCache.valid[url])
364                {
365                        handler(this.connectorCache.result[url]);
366                        return true;
367                }
368                else
369                        return false;
370        };
371        cConnector.prototype.purgeCache= function(){
372                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
373                        return false;
374                var i;
375                for (i=0; i<= this.expurgatedCache.length; i++)
376                {
377                        this.connectorCache.valid[this.expurgatedCache[i]] = false;
378                        try {
379                        delete this.connectorCache.result[this.expurgatedCache[i]];
380                        }
381                        catch (e) { };
382                }
383        };
384        cConnector.prototype.cacheNextRequest = function(expiration){
385                if (typeof(expiration) == 'undefined')
386                        expiration=0;
387                this.cacheRequest=expiration;
388        };
389
390        cConnector.prototype.addToCache = function(id,data){
391                if (typeof(preferences) == "undefined" || preferences.use_cache != 'True')
392                        return false;
393                var func = id.substr(id.lastIndexOf('.')+1);
394                if (func.indexOf('&') > 0)
395                        func = func.substr(0,func.indexOf('&'));
396                switch (this.cacheRequest){
397                        // functions that enters in cache and never expires
398                        case 0:
399                                data.cacheHit = true;
400                                this.connectorCache.valid[id] = true;
401                                this.connectorCache.result[id] = data;
402                                break;
403                        case 1:
404                        // function that needs expire
405                                this.connectorCache.valid[id] = true;
406                                this.connectorCache.result[id] = data;
407                                var i = this.expurgatedCache.length;
408                                this.expurgatedCache[i+1] = id;
409                                break;
410                        default:
411                        // no cache
412                                break;
413                }
414        };
415
416        //      Function executes AJAX
417        //      cExecute (url, handler, params)
418        //      url: 'module.class.method'
419        //  handle: function handle() receive response.
420        //  params: parameters for POST method
421        //      form: form element (for upload files)
422        function cExecute(url, handler, params, form) {
423                if(isExecuteForm){
424                        isExecuteForm = false;
425                        document.onmouseup = _onmouseup;
426                }
427                if(form) {
428                        cExecuteForm(url, form);
429                        return;
430                }
431                //url = URL_DEFAULT + url;
432
433                if (expresso.connector.queryConnectorCache(params?url+"&"+params:url,handler))
434                        return;
435
436                if(params)
437                        method = "POST";
438                else
439                        method = "GET";
440
441                id = url;
442                expresso.connector.newRequest(id, url, method, handler, params);
443        };
444
445// This function executes submit values to Controller (POST)
446        // The return is void.
447        //      cExecuteForm (url, form)
448        //      url: 'module.class.method'
449        //      form: form element (for upload files)
450        function cExecuteForm(url, form, handler,id){
451                expresso.connector.buildBar();
452                isExecuteForm = true;
453
454                document.onmouseup = alertBut;
455
456                expresso.connector.showProgressBar();
457                if(! (divUpload = document.getElementById('divUpload'))) {
458                        divUpload               = document.createElement('DIV');
459                        divUpload.id    = 'divUpload';
460                        document.body.appendChild(divUpload);
461                }
462
463                if(! (el = document.getElementById('_action'))) {
464                        el                      = document.createElement('input');
465                        el.type = 'hidden';
466                        el.id           = '_action';
467                        el.name = '_action';
468                        form.appendChild(el);
469                }
470
471                var divFiles = document.getElementById("divFiles_"+id);
472                if (divFiles && divFiles.firstChild) {
473                        el                      = document.createElement('input');
474                        el.type = 'hidden';
475                        el.name = 'countFiles';
476                        var countDivFiles = 0;
477                        try{
478                                countDivFiles = parseInt(divFiles.lastChild.firstChild.id.split('_')[2]) + 1; // The id of last file
479                                }
480                        catch (e) { var countDivFiles = 0; };
481                        el.value        = countDivFiles ;
482                        form.appendChild(el);
483                }
484
485                form._action.value = url;
486                // Connector Bug fixing: Encapsulating returned handler function
487                handlerExecuteForm = handler;
488                var form_handler = function (data){
489                        handlerExecuteForm(data);
490                        handlerExecuteForm = null;
491                };
492                divUpload.innerHTML= "<iframe onload=\"cExecute('expressoMail1_2.functions.getReturnExecuteForm',"+form_handler+");\"  style='display:"+(debug_controller ? "" : "none")+";width:"+(debug_controller ? 400 : 0)+";height:"+(debug_controller ? 400 : 0)+";' name='uploadFile'></iframe>";
493                form.action = URL_DEFAULT + url;
494                form.target ="uploadFile";
495                form.submit();
496                // Connector Bug fixing: Exclude '_action' element after cExecuteForm execution
497                if(el && el.id == '_action'){
498                        el.parentNode.removeChild(el);
499                }
500        };
501
502
503        function alertBut(e) {
504                if(!e)
505                        e = window.event;
506
507            if(_onmouseup)
508                        _onmouseup(e);
509
510                if(isExecuteForm) {
511                if(confirm(get_lang("There's an action processing. Do you want abort it?"))) {
512                        expresso.connector.hideProgressBar();
513                        isExecuteForm = false;
514                        delete expresso.connector.requests[id];
515                                expresso.connector.requests[id] = null;
516                        stop();
517                        return;
518                }
519                else
520                        return false;
521            }
522        };
523
524function expressoAjax(){
525        this.connector = new cConnector();
526
527};
528expressoAjax.prototype.require = function (module){
529        expresso.connector.loadScript(module);
530};
531var expresso = new expressoAjax();
Note: See TracBrowser for help on using the repository browser.