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

Revision 2949, 13.1 KB checked in by amuller, 14 years ago (diff)

Ticket #1036 - Arrumando compactador de javascript

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