source: trunk/jabberit_messenger/js/connector.js @ 382

Revision 382, 7.3 KB checked in by niltonneto, 16 years ago (diff)

Inclusão do módulo JabberIt? Messenger, novo módulo de mensagens
instantâneas do Expresso. É uma customização do projeto original
Jeti, e foi desenvolvido em Java.

  • Property svn:executable set to *
Line 
1(function()
2{
3        var _THREADS = [];
4        var _ie;
5
6        function _config(pObj, pEvent, pHandler)
7        {
8                if ( typeof pObj == 'object' )
9                {
10                        if ( pEvent.substring(0, 2) == 'on' )
11                                pEvent = pEvent.substring(2, pEvent.length);
12
13                        if ( pObj.addEventListener )
14                                pObj.addEventListener(pEvent, pHandler, false);
15                        else if ( pObj.attachEvent )
16                                pObj.attachEvent('on' + pEvent, pHandler);
17                }
18        }
19
20        // xhr = XMLHttpRequest
21        function _xhr()
22        {
23                try
24                {
25                        return new XMLHttpRequest();
26                }
27                catch (_e)
28                {
29                        _ie = true;
30                        try
31                        {
32                                return new ActiveXObject('Msxml2.XMLHTTP');
33                        }
34                        catch (_e1)
35                        {
36                                try
37                                {
38                                        return new ActiveXObject('Microsoft.XMLHTTP');
39                                }
40                                catch (_e2)
41                                {
42                                        return false;
43                                }
44                        }
45                }
46        }
47
48        function _HANDLER()
49        {
50                var _ID = arguments[0];
51
52                if  ( _THREADS[_ID] )
53                {
54                        if ( _ie && _THREADS[_ID]._XHR.readyState != 4 )
55                                return false;
56
57                        switch ( _THREADS[_ID]._XHR.readyState )
58                        {
59                                case 3 :
60                                        if ( _THREADS[_ID]._HANDLER.stream )
61                                        {
62                                                var _data = _THREADS[_ID]._XHR.responseText.substr(_THREADS[_ID]._index).replace(/^ +| +$/g, '');
63                                                //alert(_data);
64                                                _THREADS[_ID]._rtlen = _THREADS[_ID]._XHR.responseText.length;
65
66                                                if ( _THREADS[_ID]._index < _THREADS[_ID]._rtlen && _data.length )
67                                                        try
68                                                        {
69                                                                _THREADS[_ID]._HANDLER.stream(_data);
70                                                        }
71                                                        catch(_e)
72                                                        {
73                                                                //alert("#stream\n\n" + _e + "\n\n" + _e.description);
74                                                        }
75
76                                                if ( _THREADS[_ID] )
77                                                        _THREADS[_ID]._index = _THREADS[_ID]._rtlen;
78                                        }
79                                break;
80                                case 4 :
81                                        try
82                                        {
83                                                switch ( _THREADS[_ID]._XHR.status )
84                                                {
85                                                        case 200:
86                                                                var _data = ( _THREADS[_ID]._MODE == 'XML' ) ?
87                                                                        _THREADS[_ID]._XHR.responseXML :
88                                                                        _THREADS[_ID]._XHR.responseText;
89
90                                                                if ( _ie && _THREADS[_ID]._HANDLER.stream )
91                                                                        _THREADS[_ID]._HANDLER.stream(_data);
92
93                                                                var _request = ( _THREADS[_ID]._HANDLER.request ) ?
94                                                                        _THREADS[_ID]._HANDLER.request : false;
95
96                                                                delete _THREADS[_ID];
97
98                                                                if ( _request )
99                                                                        try
100                                                                        {
101                                                                                _request(_data);
102                                                                        }
103                                                                        catch(_e)
104                                                                        {
105                                                                                //alert("#request\n\n" + _e + "\n\n" + _e.description);
106                                                                        }
107
108                                                        break; // [case : status 200]
109                                                        case 404:
110                                                                delete _THREADS[_ID];
111                                                                alert('Page Not Found!');
112                                                        break; // [case : status 404]
113                                                        default:
114                                                                delete _THREADS[_ID];
115                                                }
116                                        }
117                                        catch(e)
118                                        {
119                                        }
120                                break;
121                                default :
122                        }
123                }
124        }
125
126        function _execute()
127        {
128                var _ID = arguments[0];
129                var _ACTION = 'act=' + _ID;
130                var _TARGET = this._PATH;
131                var _SEND = null;
132
133                if ( _TARGET != '' && _TARGET.lastIndexOf('/') != (_TARGET.length - 1) )
134                        _TARGET += '/';
135
136                _TARGET += ( this._CONTROLLER ) ?
137                        this._CONTROLLER  : 'controller.php';
138
139                if ( _THREADS[_ID]._METHOD == 'GET' )
140                        _TARGET += '?' + _ACTION;
141
142                _THREADS[_ID]._XHR.open(_THREADS[_ID]._METHOD, _TARGET, true);
143
144                if ( _THREADS[_ID]._METHOD == 'POST' )
145                {
146                        _THREADS[_ID]._XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
147                        _THREADS[_ID]._XHR.setRequestHeader('Cache-Control',  'no-store, no-cache, must-revalidate');
148                        _THREADS[_ID]._XHR.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0');
149                        _THREADS[_ID]._XHR.setRequestHeader('Pragma', 'no-cache');
150                        _SEND = _ACTION + '&' + arguments[1];
151                }
152
153                _THREADS[_ID]._XHR.onreadystatechange = function(){_HANDLER(_ID);};
154                _THREADS[_ID]._XHR.send(_SEND);
155        }
156
157        function usage()
158        {
159                return ""+
160                        "Description:\n"+
161                        "\t<obj>.go(string access, [mixed handler[, mixed post]])\n\n"+
162                        "Parameters:\n"+
163                        "\taccess : assinatura de acesso à camada de controle.\n"+
164                        "\thandler : uma função a ser executada no fim da requisição\n"+
165                        "\t\tou um objeto que poderá conter dois índices sendo\n"+
166                        "\t\tque ambos deverão ser uma função que será executada\n"+
167                        "\t\tconforme o status do objeto xhr, sendo que na posição\n"+
168                        "\t\t'stream' será a função a ser executada a cada iteração\n"+
169                        "\t\tdo objeto xhr e na posição 'request' será a função\n"+
170                        "\t\ta ser executada no fim da requisição.\n"+
171                        "\tpost : se especificado deverá ser uma query string ou um\n"+
172                        "\tXML bem formatado.\n\n";
173        }
174
175        // @PARAM arguments[0] string :
176        //              assinatura de acesso à camada de controle
177        //
178        // @PARAM arguments[1] object :
179        //              OBS : neste caso a conexão assumirá que se trata de uma stream
180        //              objeto contendo dois duas funções, sendo,
181        //              no índice stream deverá conter uma função que será execultada
182        //              a cada mudança de status do objeto xhr
183        //      or
184        // @PARAM arguments[1] function : funcão a ser executada no retorno da
185        //              requisição
186        //              OBS : neste caso a conexão assumirá que se trata de uma
187        //              requisição função que será execultada no final da requisição
188        //
189        // @PARAM arguments[2] string :
190        //              este parâmetro define se a conexão é via GET ou POST
191        //              caso o parâmetro não esteja presente a conexão será execultada
192        //              via GET, por outro lado, caso ele exista deverá ser uma query
193        //              string válida ou um xml bem formatado
194        //
195        function go()
196        {
197                var _argv = arguments;
198                var _argc = _argv.length;
199                var _ID = _argv[0];
200                var _POST;
201                if ( _argc < 1 || _argc > 3 )
202                        return {'error' : "#0\n\n" + usage()};
203
204                if ( typeof _ID != 'string' )
205                        return {'error' : "#1\n\n" + usage()};
206
207                //if ( _THREADS[_ID] )
208                //      return {'error' : '#2 - there is a equal request running'};
209
210                _THREADS[_ID] = {
211                        '_HANDLER'      : {},
212                        '_METHOD'       : ( _argv[2] ) ? 'POST' : 'GET',  // [GET | POST]
213                        '_MODE'         : null, // [XML | TEXT]
214                        '_TYPE'         : null, // [4 for request | 3 for stream]
215                        '_XHR'          : null  // [4 for request | 3 for stream]
216                };
217
218                if ( _argv[2] )
219                        _POST = _argv[2];
220
221                if ( _argv[1] )
222                        switch ( typeof _argv[1] )
223                        {
224                                case 'function' :
225                                        _THREADS[_ID]._HANDLER = {'request' : _argv[1]};
226                                break;
227                                case 'object' :
228                                        for ( var i in _argv[1] )
229                                                if ( i != 'stream' && i != 'request' )
230                                                {
231                                                        delete _THREADS[_ID];
232                                                        return {'error' : "#3\n\n" + usage()};
233                                                }
234                                                else if ( i == 'stream' )
235                                                {
236                                                        _THREADS[_ID]._index = 0;
237                                                        _THREADS[_ID]._rtlen = 0;
238                                                }
239                                                _THREADS[_ID]._HANDLER = _argv[1];
240                                break;
241                                case 'string' :
242                                        if ( _argc == 2 )
243                                        {
244                                                _THREADS[_ID]._METHOD = 'POST';
245                                                _POST = _argv[1];
246                                        }
247                                break;
248                                default :
249                                        //delete _THREADS[_ID];
250                                        //return {'error' : "#4\n\n" + usage()};
251                        }
252
253                if ( !(_THREADS[_ID]._XHR = _xhr()) )
254                        return {'error' : "#4 it cannot make a xhr object"};
255
256                ( _THREADS[_ID]._METHOD == 'GET' ) ?
257                        _execute.call(this, _ID) : _execute.call(this, _ID, _POST);
258                return {'success' : "your thread is running and the response "+
259                                                        "will be manipulated by the handler"};
260        }
261
262        function _abort()
263        {
264                for ( var _ID in _THREADS )
265                {
266                        // @TODO
267                        // try/catch for unknown error of IE.
268                        // Check, store and retrieve the try/catch.
269                        try
270                        {
271                                if ( _THREADS[_ID] && _THREADS[_ID]._XHR && _THREADS[_ID]._XHR.abort )
272                                        _THREADS[_ID]._XHR.abort();
273
274                                delete _THREADS[_ID];
275                        }
276                        catch(e){}
277                }
278        }
279
280        function Connector()
281        {
282                var _argv = arguments;
283                this._PATH = ( _argv.length > 0 ) ?
284                        _argv[0] : '';
285                this._CONTROLLER = ( _argv.length == 2 ) ?
286                        _argv[1] : false;
287        }
288
289        Connector.prototype.go = go;
290        Connector.prototype.abort = _abort;
291        window.JITConnector = Connector;
292
293        _config(window, 'onbeforeunload', _abort);
294}
295)();
Note: See TracBrowser for help on using the repository browser.