2007-01-18 22:24:27 +00:00
|
|
|
var Ajax = {
|
|
|
|
getTransport: function() {
|
|
|
|
return Try.these(
|
|
|
|
function() {return new XMLHttpRequest()},
|
|
|
|
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
|
|
|
|
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
|
|
|
|
) || false;
|
|
|
|
},
|
|
|
|
|
|
|
|
activeRequestCount: 0
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
Ajax.Responders = {
|
|
|
|
responders: [],
|
|
|
|
|
|
|
|
_each: function(iterator) {
|
|
|
|
this.responders._each(iterator);
|
|
|
|
},
|
|
|
|
|
|
|
|
register: function(responder) {
|
|
|
|
if (!this.include(responder))
|
|
|
|
this.responders.push(responder);
|
|
|
|
},
|
|
|
|
|
|
|
|
unregister: function(responder) {
|
|
|
|
this.responders = this.responders.without(responder);
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatch: function(callback, request, transport, json) {
|
|
|
|
this.each(function(responder) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(responder[callback])) {
|
2007-01-18 22:24:27 +00:00
|
|
|
try {
|
|
|
|
responder[callback].apply(responder, [request, transport, json]);
|
2007-08-08 23:19:45 +00:00
|
|
|
} catch (e) { }
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.extend(Ajax.Responders, Enumerable);
|
|
|
|
|
|
|
|
Ajax.Responders.register({
|
2007-08-18 23:21:29 +00:00
|
|
|
onCreate: function() { Ajax.activeRequestCount++ },
|
|
|
|
onComplete: function() { Ajax.activeRequestCount-- }
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
Ajax.Base = Class.create({
|
|
|
|
initialize: function(options) {
|
2007-01-18 22:24:27 +00:00
|
|
|
this.options = {
|
|
|
|
method: 'post',
|
|
|
|
asynchronous: true,
|
|
|
|
contentType: 'application/x-www-form-urlencoded',
|
|
|
|
encoding: 'UTF-8',
|
2007-08-04 04:40:22 +00:00
|
|
|
parameters: '',
|
|
|
|
evalJSON: true,
|
|
|
|
evalJS: true
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-08-08 23:19:45 +00:00
|
|
|
Object.extend(this.options, options || { });
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
this.options.method = this.options.method.toLowerCase();
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isString(this.options.parameters))
|
2007-01-18 22:24:27 +00:00
|
|
|
this.options.parameters = this.options.parameters.toQueryParams();
|
|
|
|
}
|
2007-08-18 23:21:29 +00:00
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
Ajax.Request = Class.create(Ajax.Base, {
|
2007-01-18 22:24:27 +00:00
|
|
|
_complete: false,
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
initialize: function($super, url, options) {
|
|
|
|
$super(options);
|
2007-01-18 22:24:27 +00:00
|
|
|
this.transport = Ajax.getTransport();
|
|
|
|
this.request(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
request: function(url) {
|
|
|
|
this.url = url;
|
|
|
|
this.method = this.options.method;
|
2007-01-28 07:30:04 +00:00
|
|
|
var params = Object.clone(this.options.parameters);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
if (!['get', 'post'].include(this.method)) {
|
|
|
|
// simulate other verbs over post
|
|
|
|
params['_method'] = this.method;
|
|
|
|
this.method = 'post';
|
|
|
|
}
|
|
|
|
|
2007-01-28 07:30:04 +00:00
|
|
|
this.parameters = params;
|
|
|
|
|
2007-10-13 10:55:52 +00:00
|
|
|
if (params = Object.toQueryString(params)) {
|
2007-01-28 07:30:04 +00:00
|
|
|
// when GET, append parameters to URL
|
2007-01-28 07:34:43 +00:00
|
|
|
if (this.method == 'get')
|
2007-01-28 07:30:04 +00:00
|
|
|
this.url += (this.url.include('?') ? '&' : '?') + params;
|
|
|
|
else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
|
|
|
|
params += '&_=';
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
try {
|
2007-08-04 04:40:22 +00:00
|
|
|
var response = new Ajax.Response(this);
|
|
|
|
if (this.options.onCreate) this.options.onCreate(response);
|
|
|
|
Ajax.Responders.dispatch('onCreate', this, response);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
this.transport.open(this.method.toUpperCase(), this.url,
|
|
|
|
this.options.asynchronous);
|
|
|
|
|
2007-04-29 05:37:07 +00:00
|
|
|
if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
this.transport.onreadystatechange = this.onStateChange.bind(this);
|
|
|
|
this.setRequestHeaders();
|
|
|
|
|
2007-01-28 07:30:04 +00:00
|
|
|
this.body = this.method == 'post' ? (this.options.postBody || params) : null;
|
|
|
|
this.transport.send(this.body);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
/* Force Firefox to handle ready state 4 for synchronous requests */
|
|
|
|
if (!this.options.asynchronous && this.transport.overrideMimeType)
|
|
|
|
this.onStateChange();
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
this.dispatchException(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onStateChange: function() {
|
|
|
|
var readyState = this.transport.readyState;
|
|
|
|
if (readyState > 1 && !((readyState == 4) && this._complete))
|
|
|
|
this.respondToReadyState(this.transport.readyState);
|
|
|
|
},
|
|
|
|
|
|
|
|
setRequestHeaders: function() {
|
|
|
|
var headers = {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'X-Prototype-Version': Prototype.Version,
|
|
|
|
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.method == 'post') {
|
|
|
|
headers['Content-type'] = this.options.contentType +
|
|
|
|
(this.options.encoding ? '; charset=' + this.options.encoding : '');
|
|
|
|
|
|
|
|
/* Force "Connection: close" for older Mozilla browsers to work
|
|
|
|
* around a bug where XMLHttpRequest sends an incorrect
|
|
|
|
* Content-length header. See Mozilla Bugzilla #246651.
|
|
|
|
*/
|
|
|
|
if (this.transport.overrideMimeType &&
|
|
|
|
(navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
|
|
|
|
headers['Connection'] = 'close';
|
|
|
|
}
|
|
|
|
|
|
|
|
// user-defined headers
|
|
|
|
if (typeof this.options.requestHeaders == 'object') {
|
|
|
|
var extras = this.options.requestHeaders;
|
|
|
|
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(extras.push))
|
2007-01-18 22:24:27 +00:00
|
|
|
for (var i = 0, length = extras.length; i < length; i += 2)
|
|
|
|
headers[extras[i]] = extras[i+1];
|
|
|
|
else
|
|
|
|
$H(extras).each(function(pair) { headers[pair.key] = pair.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var name in headers)
|
|
|
|
this.transport.setRequestHeader(name, headers[name]);
|
|
|
|
},
|
|
|
|
|
|
|
|
success: function() {
|
2007-08-04 04:40:22 +00:00
|
|
|
var status = this.getStatus();
|
|
|
|
return !status || (status >= 200 && status < 300);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-08-04 04:40:22 +00:00
|
|
|
|
|
|
|
getStatus: function() {
|
|
|
|
try {
|
|
|
|
return this.transport.status || 0;
|
|
|
|
} catch (e) { return 0 }
|
|
|
|
},
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
respondToReadyState: function(readyState) {
|
2007-08-04 04:40:22 +00:00
|
|
|
var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
if (state == 'Complete') {
|
|
|
|
try {
|
|
|
|
this._complete = true;
|
2007-08-04 04:40:22 +00:00
|
|
|
(this.options['on' + response.status]
|
2007-01-18 22:24:27 +00:00
|
|
|
|| this.options['on' + (this.success() ? 'Success' : 'Failure')]
|
2007-08-04 04:40:22 +00:00
|
|
|
|| Prototype.emptyFunction)(response, response.headerJSON);
|
2007-01-18 22:24:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.dispatchException(e);
|
|
|
|
}
|
|
|
|
|
2007-08-04 04:40:22 +00:00
|
|
|
var contentType = response.getHeader('Content-type');
|
|
|
|
if (this.options.evalJS == 'force'
|
|
|
|
|| (this.options.evalJS && contentType
|
|
|
|
&& contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
|
|
|
|
this.evalResponse();
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2007-08-04 04:40:22 +00:00
|
|
|
(this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
|
|
|
|
Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
|
2007-01-18 22:24:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.dispatchException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == 'Complete') {
|
|
|
|
// avoid memory leak in MSIE: clean up
|
|
|
|
this.transport.onreadystatechange = Prototype.emptyFunction;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getHeader: function(name) {
|
|
|
|
try {
|
|
|
|
return this.transport.getResponseHeader(name);
|
|
|
|
} catch (e) { return null }
|
|
|
|
},
|
|
|
|
|
|
|
|
evalResponse: function() {
|
|
|
|
try {
|
2007-04-24 03:31:14 +00:00
|
|
|
return eval((this.transport.responseText || '').unfilterJSON());
|
2007-01-18 22:24:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.dispatchException(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatchException: function(exception) {
|
|
|
|
(this.options.onException || Prototype.emptyFunction)(this, exception);
|
|
|
|
Ajax.Responders.dispatch('onException', this, exception);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
Ajax.Request.Events =
|
|
|
|
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
|
|
|
|
|
|
|
|
Ajax.Response = Class.create({
|
2007-08-04 04:40:22 +00:00
|
|
|
initialize: function(request){
|
|
|
|
this.request = request;
|
|
|
|
var transport = this.transport = request.transport,
|
|
|
|
readyState = this.readyState = transport.readyState;
|
|
|
|
|
|
|
|
if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
|
|
|
|
this.status = this.getStatus();
|
|
|
|
this.statusText = this.getStatusText();
|
|
|
|
this.responseText = String.interpret(transport.responseText);
|
2007-10-13 11:37:26 +00:00
|
|
|
this.headerJSON = this._getHeaderJSON();
|
2007-08-04 04:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(readyState == 4) {
|
|
|
|
var xml = transport.responseXML;
|
2007-11-15 21:53:15 +00:00
|
|
|
this.responseXML = Object.isUndefined(xml) ? null : xml;
|
2007-10-13 11:37:26 +00:00
|
|
|
this.responseJSON = this._getResponseJSON();
|
2007-08-04 04:40:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
status: 0,
|
|
|
|
statusText: '',
|
|
|
|
|
|
|
|
getStatus: Ajax.Request.prototype.getStatus,
|
|
|
|
|
|
|
|
getStatusText: function() {
|
|
|
|
try {
|
|
|
|
return this.transport.statusText || '';
|
|
|
|
} catch (e) { return '' }
|
|
|
|
},
|
|
|
|
|
|
|
|
getHeader: Ajax.Request.prototype.getHeader,
|
|
|
|
|
|
|
|
getAllHeaders: function() {
|
|
|
|
try {
|
|
|
|
return this.getAllResponseHeaders();
|
|
|
|
} catch (e) { return null }
|
|
|
|
},
|
|
|
|
|
|
|
|
getResponseHeader: function(name) {
|
|
|
|
return this.transport.getResponseHeader(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllResponseHeaders: function() {
|
|
|
|
return this.transport.getAllResponseHeaders();
|
|
|
|
},
|
|
|
|
|
2007-10-13 11:37:26 +00:00
|
|
|
_getHeaderJSON: function() {
|
2007-08-04 04:40:22 +00:00
|
|
|
var json = this.getHeader('X-JSON');
|
2007-10-22 11:20:21 +00:00
|
|
|
if (!json) return null;
|
|
|
|
json = decodeURIComponent(escape(json));
|
2007-08-04 04:40:22 +00:00
|
|
|
try {
|
2007-10-22 11:20:21 +00:00
|
|
|
return json.evalJSON(this.request.options.sanitizeJSON);
|
2007-08-04 04:40:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.request.dispatchException(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-10-13 11:37:26 +00:00
|
|
|
_getResponseJSON: function() {
|
2007-08-04 04:40:22 +00:00
|
|
|
var options = this.request.options;
|
2007-10-22 12:04:06 +00:00
|
|
|
if (!options.evalJSON || (options.evalJSON != 'force' &&
|
2007-11-15 19:37:26 +00:00
|
|
|
!(this.getHeader('Content-type') || '').include('application/json')) ||
|
|
|
|
this.responseText.blank())
|
|
|
|
return null;
|
2007-08-04 04:40:22 +00:00
|
|
|
try {
|
2007-11-15 14:02:25 +00:00
|
|
|
return this.responseText.evalJSON(options.sanitizeJSON);
|
2007-08-04 04:40:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.request.dispatchException(e);
|
|
|
|
}
|
|
|
|
}
|
2007-08-18 23:21:29 +00:00
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
Ajax.Updater = Class.create(Ajax.Request, {
|
|
|
|
initialize: function($super, container, url, options) {
|
2007-01-18 22:24:27 +00:00
|
|
|
this.container = {
|
|
|
|
success: (container.success || container),
|
|
|
|
failure: (container.failure || (container.success ? null : container))
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-11-15 23:14:36 +00:00
|
|
|
options = Object.clone(options);
|
2007-08-18 23:21:29 +00:00
|
|
|
var onComplete = options.onComplete;
|
2007-11-15 23:14:36 +00:00
|
|
|
options.onComplete = (function(response, json) {
|
2007-08-04 04:40:22 +00:00
|
|
|
this.updateContent(response.responseText);
|
2007-11-15 23:14:36 +00:00
|
|
|
if (Object.isFunction(onComplete)) onComplete(response, json);
|
2007-01-18 22:24:27 +00:00
|
|
|
}).bind(this);
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
$super(url, options);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
2007-08-04 04:40:22 +00:00
|
|
|
updateContent: function(responseText) {
|
|
|
|
var receiver = this.container[this.success() ? 'success' : 'failure'],
|
|
|
|
options = this.options;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-04 04:40:22 +00:00
|
|
|
if (!options.evalScripts) responseText = responseText.stripScripts();
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
if (receiver = $(receiver)) {
|
Merge -r6634:HEAD from ../branches/dom.
* Make Element#update and Element#insert work for SELECT tags in IE and Opera. [Tobie Langel]
* Make Element#insert and Element#update better handle TABLE related elements in IE and Opera. Closes #7776, #8040, #7550, #7776, #7938. [Tobie Langel]
* Make Element#readAttribute('title') work in Opera. [Tobie Langel]
* Make Element#replace work with form elements in Firefox and Safari. Closes #8010, #7989. [dsl239, Tobie Langel]
* Add Element#wrap which wraps the element inside a new one. Closes #5732. [P. Vande, Tobie Langel]
* Make Element into a constructor: new Element(tagName, attributes). Add Element#writeAttribute which accepts a hash of attributes or a name/value pair. Closes #7476. [Mislav Marohnić, haraldmartin, Tobie Langel]
* Insertion overhaul: Add Element.insert(content[, position = 'Bottom']). Deprecate Insertion (kept for backwards compatibility). Make Ajax.Updater option.insertion accept both Insertion.Top or the now preferred 'Top'. Closes #7907. [Tobie Langel]
2007-05-12 05:01:56 +00:00
|
|
|
if (options.insertion) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isString(options.insertion)) {
|
|
|
|
var insertion = { }; insertion[options.insertion] = responseText;
|
2007-06-02 16:32:43 +00:00
|
|
|
receiver.insert(insertion);
|
|
|
|
}
|
2007-08-04 04:40:22 +00:00
|
|
|
else options.insertion(receiver, responseText);
|
Merge -r6634:HEAD from ../branches/dom.
* Make Element#update and Element#insert work for SELECT tags in IE and Opera. [Tobie Langel]
* Make Element#insert and Element#update better handle TABLE related elements in IE and Opera. Closes #7776, #8040, #7550, #7776, #7938. [Tobie Langel]
* Make Element#readAttribute('title') work in Opera. [Tobie Langel]
* Make Element#replace work with form elements in Firefox and Safari. Closes #8010, #7989. [dsl239, Tobie Langel]
* Add Element#wrap which wraps the element inside a new one. Closes #5732. [P. Vande, Tobie Langel]
* Make Element into a constructor: new Element(tagName, attributes). Add Element#writeAttribute which accepts a hash of attributes or a name/value pair. Closes #7476. [Mislav Marohnić, haraldmartin, Tobie Langel]
* Insertion overhaul: Add Element.insert(content[, position = 'Bottom']). Deprecate Insertion (kept for backwards compatibility). Make Ajax.Updater option.insertion accept both Insertion.Top or the now preferred 'Top'. Closes #7907. [Tobie Langel]
2007-05-12 05:01:56 +00:00
|
|
|
}
|
2007-08-04 04:40:22 +00:00
|
|
|
else receiver.update(responseText);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
Ajax.PeriodicalUpdater = Class.create(Ajax.Base, {
|
|
|
|
initialize: function($super, container, url, options) {
|
|
|
|
$super(options);
|
2007-01-18 22:24:27 +00:00
|
|
|
this.onComplete = this.options.onComplete;
|
|
|
|
|
|
|
|
this.frequency = (this.options.frequency || 2);
|
|
|
|
this.decay = (this.options.decay || 1);
|
|
|
|
|
2007-08-08 23:19:45 +00:00
|
|
|
this.updater = { };
|
2007-01-18 22:24:27 +00:00
|
|
|
this.container = container;
|
|
|
|
this.url = url;
|
|
|
|
|
|
|
|
this.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
this.options.onComplete = this.updateComplete.bind(this);
|
|
|
|
this.onTimerEvent();
|
|
|
|
},
|
|
|
|
|
|
|
|
stop: function() {
|
|
|
|
this.updater.options.onComplete = undefined;
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
(this.onComplete || Prototype.emptyFunction).apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2007-08-21 16:18:07 +00:00
|
|
|
updateComplete: function(response) {
|
2007-01-18 22:24:27 +00:00
|
|
|
if (this.options.decay) {
|
2007-08-21 16:18:07 +00:00
|
|
|
this.decay = (response.responseText == this.lastText ?
|
2007-01-18 22:24:27 +00:00
|
|
|
this.decay * this.options.decay : 1);
|
|
|
|
|
2007-08-21 16:18:07 +00:00
|
|
|
this.lastText = response.responseText;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-04-29 05:37:07 +00:00
|
|
|
this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onTimerEvent: function() {
|
|
|
|
this.updater = new Ajax.Updater(this.container, this.url, this.options);
|
|
|
|
}
|
|
|
|
});
|