[ Index ] |
|
Code source de vtiger CRM 5.0.2 |
1 var Ajax = { 2 getTransport: function() { 3 return Try.these( 4 function() {return new ActiveXObject('Msxml2.XMLHTTP')}, 5 function() {return new ActiveXObject('Microsoft.XMLHTTP')}, 6 function() {return new XMLHttpRequest()} 7 ) || false; 8 }, 9 10 activeRequestCount: 0 11 } 12 13 Ajax.Responders = { 14 responders: [], 15 16 _each: function(iterator) { 17 this.responders._each(iterator); 18 }, 19 20 register: function(responderToAdd) { 21 if (!this.include(responderToAdd)) 22 this.responders.push(responderToAdd); 23 }, 24 25 unregister: function(responderToRemove) { 26 this.responders = this.responders.without(responderToRemove); 27 }, 28 29 dispatch: function(callback, request, transport, json) { 30 this.each(function(responder) { 31 if (responder[callback] && typeof responder[callback] == 'function') { 32 try { 33 responder[callback].apply(responder, [request, transport, json]); 34 } catch (e) {} 35 } 36 }); 37 } 38 }; 39 40 Object.extend(Ajax.Responders, Enumerable); 41 42 Ajax.Responders.register({ 43 onCreate: function() { 44 Ajax.activeRequestCount++; 45 }, 46 47 onComplete: function() { 48 Ajax.activeRequestCount--; 49 } 50 }); 51 52 Ajax.Base = function() {}; 53 Ajax.Base.prototype = { 54 setOptions: function(options) { 55 this.options = { 56 method: 'post', 57 asynchronous: true, 58 parameters: '' 59 } 60 Object.extend(this.options, options || {}); 61 }, 62 63 responseIsSuccess: function() { 64 return this.transport.status == undefined 65 || this.transport.status == 0 66 || (this.transport.status >= 200 && this.transport.status < 300); 67 }, 68 69 responseIsFailure: function() { 70 return !this.responseIsSuccess(); 71 } 72 } 73 74 Ajax.Request = Class.create(); 75 Ajax.Request.Events = 76 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; 77 78 Ajax.Request.prototype = Object.extend(new Ajax.Base(), { 79 initialize: function(url, options) { 80 this.transport = Ajax.getTransport(); 81 this.setOptions(options); 82 this.request(url); 83 }, 84 85 request: function(url) { 86 var parameters = this.options.parameters || ''; 87 if (parameters.length > 0) parameters += '&_='; 88 89 try { 90 this.url = url; 91 if (this.options.method == 'get' && parameters.length > 0) 92 this.url += (this.url.match(/\?/) ? '&' : '?') + parameters; 93 94 Ajax.Responders.dispatch('onCreate', this, this.transport); 95 96 this.transport.open(this.options.method, this.url, 97 this.options.asynchronous); 98 99 if (this.options.asynchronous) { 100 this.transport.onreadystatechange = this.onStateChange.bind(this); 101 setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10); 102 } 103 104 this.setRequestHeaders(); 105 106 var body = this.options.postBody ? this.options.postBody : parameters; 107 this.transport.send(this.options.method == 'post' ? body : null); 108 109 } catch (e) { 110 this.dispatchException(e); 111 } 112 }, 113 114 setRequestHeaders: function() { 115 var requestHeaders = 116 ['X-Requested-With', 'XMLHttpRequest', 117 'X-Prototype-Version', Prototype.Version]; 118 119 if (this.options.method == 'post') { 120 requestHeaders.push('Content-type', 121 'application/x-www-form-urlencoded'); 122 123 /* Force "Connection: close" for Mozilla browsers to work around 124 * a bug where XMLHttpReqeuest sends an incorrect Content-length 125 * header. See Mozilla Bugzilla #246651. 126 */ 127 if (this.transport.overrideMimeType) 128 requestHeaders.push('Connection', 'close'); 129 } 130 131 if (this.options.requestHeaders) 132 requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); 133 134 for (var i = 0; i < requestHeaders.length; i += 2) 135 this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); 136 }, 137 138 onStateChange: function() { 139 var readyState = this.transport.readyState; 140 if (readyState != 1) 141 this.respondToReadyState(this.transport.readyState); 142 }, 143 144 header: function(name) { 145 try { 146 return this.transport.getResponseHeader(name); 147 } catch (e) {} 148 }, 149 150 evalJSON: function() { 151 try { 152 return eval(this.header('X-JSON')); 153 } catch (e) {} 154 }, 155 156 evalResponse: function() { 157 try { 158 return eval(this.transport.responseText); 159 } catch (e) { 160 this.dispatchException(e); 161 } 162 }, 163 164 respondToReadyState: function(readyState) { 165 var event = Ajax.Request.Events[readyState]; 166 var transport = this.transport, json = this.evalJSON(); 167 168 if (event == 'Complete') { 169 try { 170 (this.options['on' + this.transport.status] 171 || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] 172 || Prototype.emptyFunction)(transport, json); 173 } catch (e) { 174 this.dispatchException(e); 175 } 176 177 if ((this.header('Content-type') || '').match(/^text\/javascript/i)) 178 this.evalResponse(); 179 } 180 181 try { 182 (this.options['on' + event] || Prototype.emptyFunction)(transport, json); 183 Ajax.Responders.dispatch('on' + event, this, transport, json); 184 } catch (e) { 185 this.dispatchException(e); 186 } 187 188 /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ 189 if (event == 'Complete') 190 this.transport.onreadystatechange = Prototype.emptyFunction; 191 }, 192 193 dispatchException: function(exception) { 194 (this.options.onException || Prototype.emptyFunction)(this, exception); 195 Ajax.Responders.dispatch('onException', this, exception); 196 } 197 }); 198 199 Ajax.Updater = Class.create(); 200 201 Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { 202 initialize: function(container, url, options) { 203 this.containers = { 204 success: container.success ? $(container.success) : $(container), 205 failure: container.failure ? $(container.failure) : 206 (container.success ? null : $(container)) 207 } 208 209 this.transport = Ajax.getTransport(); 210 this.setOptions(options); 211 212 var onComplete = this.options.onComplete || Prototype.emptyFunction; 213 this.options.onComplete = (function(transport, object) { 214 this.updateContent(); 215 onComplete(transport, object); 216 }).bind(this); 217 218 this.request(url); 219 }, 220 221 updateContent: function() { 222 var receiver = this.responseIsSuccess() ? 223 this.containers.success : this.containers.failure; 224 var response = this.transport.responseText; 225 226 if (!this.options.evalScripts) 227 response = response.stripScripts(); 228 229 if (receiver) { 230 if (this.options.insertion) { 231 new this.options.insertion(receiver, response); 232 } else { 233 Element.update(receiver, response); 234 } 235 } 236 237 if (this.responseIsSuccess()) { 238 if (this.onComplete) 239 setTimeout(this.onComplete.bind(this), 10); 240 } 241 } 242 }); 243 244 Ajax.PeriodicalUpdater = Class.create(); 245 Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { 246 initialize: function(container, url, options) { 247 this.setOptions(options); 248 this.onComplete = this.options.onComplete; 249 250 this.frequency = (this.options.frequency || 2); 251 this.decay = (this.options.decay || 1); 252 253 this.updater = {}; 254 this.container = container; 255 this.url = url; 256 257 this.start(); 258 }, 259 260 start: function() { 261 this.options.onComplete = this.updateComplete.bind(this); 262 this.onTimerEvent(); 263 }, 264 265 stop: function() { 266 this.updater.onComplete = undefined; 267 clearTimeout(this.timer); 268 (this.onComplete || Prototype.emptyFunction).apply(this, arguments); 269 }, 270 271 updateComplete: function(request) { 272 if (this.options.decay) { 273 this.decay = (request.responseText == this.lastText ? 274 this.decay * this.options.decay : 1); 275 276 this.lastText = request.responseText; 277 } 278 this.timer = setTimeout(this.onTimerEvent.bind(this), 279 this.decay * this.frequency * 1000); 280 }, 281 282 onTimerEvent: function() { 283 this.updater = new Ajax.Updater(this.container, this.url, this.options); 284 } 285 });
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 10:22:19 2007 | par Balluche grâce à PHPXref 0.7 |