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