Changeset 6813
- Timestamp:
- 05/22/07 23:40:40 (1 year ago)
- Files:
-
- spinoffs/prototype/branches/inheritance/CHANGELOG (modified) (1 diff)
- spinoffs/prototype/branches/inheritance/src/ajax.js (modified) (13 diffs)
- spinoffs/prototype/branches/inheritance/src/base.js (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/prototype/branches/inheritance/CHANGELOG
r6812 r6813 1 * Test commit (having trouble with svn)2 3 1 *SVN* 4 2 spinoffs/prototype/branches/inheritance/src/ajax.js
r6725 r6813 1 var Ajax = { 1 var Ajax = Class.create({ 2 setOptions: function(options) { 3 this.options = Object.extend({ 4 method: 'post', 5 asynchronous: true, 6 contentType: 'application/x-www-form-urlencoded', 7 encoding: 'UTF-8', 8 parameters: '' 9 }, options || {}); 10 11 this.options.method = this.options.method.toLowerCase(); 12 if (typeof this.options.parameters === 'string') 13 this.options.parameters = this.options.parameters.toQueryParams(); 14 } 15 }); 16 17 Object.extend(Ajax, { 2 18 getTransport: function() { 3 19 return Try.these( 4 function() { return new XMLHttpRequest()},5 function() { return new ActiveXObject('Msxml2.XMLHTTP')},6 function() { return new ActiveXObject('Microsoft.XMLHTTP')}20 function() { return new XMLHttpRequest(); }, 21 function() { return new ActiveXObject('Msxml2.XMLHTTP'); }, 22 function() { return new ActiveXObject('Microsoft.XMLHTTP'); } 7 23 ) || false; 8 24 }, 9 25 10 26 activeRequestCount: 0 11 } 27 }); 12 28 13 29 Ajax.Responders = { 14 30 responders: [], 15 16 31 _each: function(iterator) { 17 32 this.responders._each(iterator); 18 33 }, 19 34 20 35 register: function(responder) { 21 36 if (!this.include(responder)) … … 28 43 29 44 dispatch: function(callback, request, transport, json) { 30 this.each( function(responder) {31 if (typeof responder[callback] == 'function') {45 this.each( function(responder) { 46 if (typeof responder[callback] === 'function') { 32 47 try { 33 48 responder[callback].apply(responder, [request, transport, json]); 34 } catch (e) {}49 } catch(e) {} 35 50 } 36 51 }); … … 43 58 onCreate: function() { 44 59 Ajax.activeRequestCount++; 45 }, 60 }, 61 46 62 onComplete: function() { 47 63 Ajax.activeRequestCount--; … … 49 65 }); 50 66 51 Ajax.Base = function() {}; 52 Ajax.Base.prototype = { 53 setOptions: function(options) { 54 this.options = { 55 method: 'post', 56 asynchronous: true, 57 contentType: 'application/x-www-form-urlencoded', 58 encoding: 'UTF-8', 59 parameters: '' 60 } 61 Object.extend(this.options, options || {}); 62 63 this.options.method = this.options.method.toLowerCase(); 64 if (typeof this.options.parameters == 'string') 65 this.options.parameters = this.options.parameters.toQueryParams(); 66 } 67 } 68 69 Ajax.Request = Class.create(); 70 Ajax.Request.Events = 71 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; 72 73 Ajax.Request.prototype = Object.extend(new Ajax.Base(), { 67 Ajax.Request = Class.extend(Ajax, { 74 68 _complete: false, 75 76 69 initialize: function(url, options) { 70 this.setOptions(options); 71 77 72 this.transport = Ajax.getTransport(); 78 this.setOptions(options);79 73 this.request(url); 80 74 }, 81 75 82 76 request: function(url) { 83 77 this.url = url; 84 78 this.method = this.options.method; 85 79 var params = Object.clone(this.options.parameters); 86 87 if (!['get', 'post'].include(this.method)) { 80 if (!$w('get post').include(this.method)) { 88 81 // simulate other verbs over post 89 82 params['_method'] = this.method; 90 83 this.method = 'post'; 91 84 } 92 93 85 this.parameters = params; 94 95 86 if (params = Hash.toQueryString(params)) { 96 // when GET, append parameters to URL 97 if (this.method == 'get') 87 if (this.method === 'get') 98 88 this.url += (this.url.include('?') ? '&' : '?') + params; 99 else if ( /Konqueror|Safari|KHTML/.test(navigator.userAgent))89 else if (Prototype.Browser.Webkit) 100 90 params += '&_='; 101 91 } 102 103 try { 104 if (this.options.onCreate) this.options.onCreate(this.transport); 92 93 try { 94 if (this.options.onCreate) 95 this.options.onCreate(this.transport); 105 96 Ajax.Responders.dispatch('onCreate', this, this.transport); 106 107 this.transport.open(this.method.toUpperCase(), this.url, 108 this.options.asynchronous); 109 110 if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); 111 97 98 this.transport.open(this.method.toUpperCase(), this.url, 99 this.options.asynchronous); 100 101 if (this.options.asynchronous) 102 this.respondToReadyState.bind(this, 1).defer(); 103 112 104 this.transport.onreadystatechange = this.onStateChange.bind(this); 113 105 this.setRequestHeaders(); … … 115 107 this.body = this.method == 'post' ? (this.options.postBody || params) : null; 116 108 this.transport.send(this.body); 117 118 /* Force Firefox to handle ready state 4 for synchronous requests */ 109 119 110 if (!this.options.asynchronous && this.transport.overrideMimeType) 120 111 this.onStateChange(); 121 122 } 123 catch (e) { 124 this.dispatchException(e); 125 } 126 }, 127 112 } catch(e) { this.dispatchException(e); } 113 }, 114 128 115 onStateChange: function() { 129 116 var readyState = this.transport.readyState; … … 138 125 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' 139 126 }; 140 141 if (this.method == 'post') {127 128 if (this.method === 'post') { 142 129 headers['Content-type'] = this.options.contentType + 143 (this.options.encoding ? '; charset=' + this.options.encoding : '');144 130 (this.options.encoding ? '; charset=' + this.options.encoding : ''); 131 145 132 /* Force "Connection: close" for older Mozilla browsers to work 146 133 * around a bug where XMLHttpRequest sends an incorrect 147 * Content-length header. See Mozilla Bugzilla #246651. 134 * Content-length header. See Mozilla Bugzilla #246651. 148 135 */ 149 136 if (this.transport.overrideMimeType && 150 (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)151 headers['Connection'] = 'close';137 (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) 138 headers['Connection'] = 'close'; 152 139 } 153 140 154 141 // user-defined headers 155 if (typeof this.options.requestHeaders == 'object') {142 if (typeof this.options.requestHeaders === 'object') { 156 143 var extras = this.options.requestHeaders; 157 158 if (typeof extras.push == 'function') 159 for (var i = 0, length = extras.length; i < length; i += 2) 160 headers[extras[i]] = extras[i+1]; 144 if (typeof extras.push === 'function') 145 for (var i = 0, l = extras.length; i < l; i += 2) 146 headers[extras[i]] = extras[i + 1]; 161 147 else 162 $H(extras).each( function(pair) { headers[pair.key] = pair.value });163 } 164 165 for (var name in headers) 148 $H(extras).each( function(pair) { headers[pair.key] = pair.value }); 149 } 150 151 for (var name in headers) 166 152 this.transport.setRequestHeader(name, headers[name]); 167 153 }, … … 185 171 this.dispatchException(e); 186 172 } 187 173 188 174 var contentType = this.getHeader('Content-type'); 189 175 if (contentType && contentType.strip(). … … 198 184 this.dispatchException(e); 199 185 } 200 186 201 187 if (state == 'Complete') { 202 188 // avoid memory leak in MSIE: clean up … … 204 190 } 205 191 }, 206 192 207 193 getHeader: function(name) { 208 194 try { … … 210 196 } catch (e) { return null } 211 197 }, 212 198 213 199 evalJSON: function() { 214 200 try { … … 217 203 } catch (e) { return null } 218 204 }, 219 205 220 206 evalResponse: function() { 221 207 try { … … 229 215 (this.options.onException || Prototype.emptyFunction)(this, exception); 230 216 Ajax.Responders.dispatch('onException', this, exception); 231 } 232 }); 233 234 Ajax.Updater = Class.create(); 235 236 Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { 217 } 218 }); 219 220 Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 221 'Complete']; 222 223 Ajax.Updater = Class.extend(Ajax.Request, { 237 224 initialize: function(container, url, options) { 238 225 this.container = { 239 226 success: (container.success || container), 240 227 failure: (container.failure || (container.success ? null : container)) 241 } 242 243 this.transport = Ajax.getTransport(); 228 }; 229 244 230 this.setOptions(options); 245 246 var onComplete = this.options.onComplete || Prototype.emptyFunction; 231 232 var onComplete = this.options.onComplete || Prototype.emptyFunction; 247 233 this.options.onComplete = (function(transport, param) { 248 234 this.updateContent(); … … 250 236 }).bind(this); 251 237 252 this. request(url);253 }, 254 238 this.parent(url, options); 239 }, 240 255 241 updateContent: function() { 256 242 var receiver = this.container[this.success() ? 'success' : 'failure']; 257 var response = this.transport.responseText , options = this.options;258 259 if (! options.evalScripts) response = response.stripScripts();243 var response = this.transport.responseText; 244 245 if (!this.options.evalScripts) responde = response.stripScripts(); 260 246 261 247 if (receiver = $(receiver)) { 262 if (options.insertion) { 263 if (typeof options.insertion == 'string') 264 receiver.insert(response, options.insertion); 265 else options.insertion(receiver, response); 266 } 248 if (this.options.insertion) 249 new this.options.insertion(receiver, response); 267 250 else receiver.update(response); 268 251 } 269 252 270 if (this.success()) {253 if (this.success()) 271 254 if (this.onComplete) this.onComplete.bind(this).defer(); 272 } 273 } 274 }); 275 276 Ajax.PeriodicalUpdater = Class.create(); 277 Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { 255 } 256 }); 257 258 Ajax.PeriodicalUpdater = Class.extend(Ajax, { 278 259 initialize: function(container, url, options) { 279 260 this.setOptions(options); 280 261 this.onComplete = this.options.onComplete; 281 282 this.frequency = (this.options.frequency || 2);283 this.decay = (this.options.decay || 1);262 263 this.frequency = this.options.frequency || 2; 264 this.decay = this.options.decay || 1; 284 265 285 266 this.updater = {}; 286 267 this.container = container; 287 268 this.url = url; 288 269 289 270 this.start(); 290 271 }, 291 272 292 273 start: function() { 293 274 this.options.onComplete = this.updateComplete.bind(this); 294 275 this.onTimerEvent(); 295 276 }, 296 277 297 278 stop: function() { 298 279 this.updater.options.onComplete = undefined; 299 clearTimeout(this.timer);280 window.clearTimeout(this.timer); 300 281 (this.onComplete || Prototype.emptyFunction).apply(this, arguments); 301 282 }, 302 283 303 284 updateComplete: function(request) { 304 285 if (this.options.decay) { 305 this.decay = (request.responseText == this.lastText ?306 this.decay * this.options.decay : 1);307 286 this.decay = (request.responseText === this.lastText ? 287 this.decay * this.options.decay : 1); 288 308 289 this.lastText = request.responseText; 309 290 } 310 this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); 311 }, 312 291 this.timer = this.onTimerEvent.bind(this).delay(this.decay * 292 this.frequency); 293 }, 294 313 295 onTimerEvent: function() { 314 296 this.updater = new Ajax.Updater(this.container, this.url, this.options); spinoffs/prototype/branches/inheritance/src/base.js
r6723 r6813 1 // Based on Alex Arnell's inheritance implementation. 1 2 var Class = { 2 create: function() { 3 return function() { 4 this.initialize.apply(this, arguments); 5 } 6 } 7 } 3 extend: function(parent, methods) { 4 if (arguments.length == 1) { 5 methods = parent; parent = null; 6 } 7 var method = function() { 8 if (!Class.extending) this.initialize.apply(this, arguments); 9 }; 10 11 if (typeof parent === 'function') { 12 Class.extending = true; 13 method.prototype = new parent(); 14 delete Class.extending; 15 } 16 17 if (methods) Class.inherit(method.prototype, methods); 18 return method; 19 }, 20 21 inherit: function(destination, source, name) { 22 if (arguments.length === 3) { 23 var ancestor = destination[name], descendant = source[name], 24 method = descendant; 25 26 descendant = function() { 27 var ref = this.parent; this.parent = ancestor; 28 var result = method.apply(this, arguments); 29 if (ref) this.parent = ref; else delete this.parent; 30 return result; 31 }; 32 33 Object.extend(descendant, { 34 valueOf: function() { return method; }, 35 toString: function() { return method.toString(); } 36 }); 37 destination[name] = descendant; 38 } else { 39 for (var property in source) { 40 if (destination[property] && typeof source[property] === 'function') 41 Class.inherit(destination, source, property); 42 else destination[property] = source[property]; 43 } 44 } 45 return destination; 46 }, 47 48 mixin: function(destination, source) { 49 return Object.extend(destination.prototype, source); 50 } 51 }; 52 53 Class.create = function() { 54 return Class.extend.apply(this, arguments); 55 }; 8 56 9 57 var Abstract = new Object(); … … 14 62 } 15 63 return destination; 16 } 64 }; 17 65 18 66 Object.extend(Object, { … … 165 213 /*--------------------------------------------------------------------------*/ 166 214 167 var PeriodicalExecuter = Class.create(); 168 PeriodicalExecuter.prototype = { 215 var PeriodicalExecuter = Class.create({ 169 216 initialize: function(callback, frequency) { 170 217 this.callback = callback; … … 195 242 } 196 243 } 197 } 244 });