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
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (typeof responder[callback] == 'function') {
|
|
|
|
try {
|
|
|
|
responder[callback].apply(responder, [request, transport, json]);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.extend(Ajax.Responders, Enumerable);
|
|
|
|
|
|
|
|
Ajax.Responders.register({
|
|
|
|
onCreate: function() {
|
|
|
|
Ajax.activeRequestCount++;
|
|
|
|
},
|
|
|
|
onComplete: function() {
|
|
|
|
Ajax.activeRequestCount--;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ajax.Base = function() {};
|
|
|
|
Ajax.Base.prototype = {
|
|
|
|
setOptions: function(options) {
|
|
|
|
this.options = {
|
|
|
|
method: 'post',
|
|
|
|
asynchronous: true,
|
|
|
|
contentType: 'application/x-www-form-urlencoded',
|
|
|
|
encoding: 'UTF-8',
|
|
|
|
parameters: ''
|
|
|
|
}
|
|
|
|
Object.extend(this.options, options || {});
|
|
|
|
|
|
|
|
this.options.method = this.options.method.toLowerCase();
|
|
|
|
if (typeof this.options.parameters == 'string')
|
|
|
|
this.options.parameters = this.options.parameters.toQueryParams();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ajax.Request = Class.create();
|
|
|
|
Ajax.Request.Events =
|
|
|
|
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
|
|
|
|
|
|
|
|
Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
|
|
|
|
_complete: false,
|
|
|
|
|
|
|
|
initialize: function(url, options) {
|
|
|
|
this.transport = Ajax.getTransport();
|
|
|
|
this.setOptions(options);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (params = Hash.toQueryString(params)) {
|
|
|
|
// 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-04-24 03:58:34 +00:00
|
|
|
if (this.options.onCreate) this.options.onCreate(this.transport);
|
2007-01-18 22:24:27 +00:00
|
|
|
Ajax.Responders.dispatch('onCreate', this, this.transport);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (typeof extras.push == 'function')
|
|
|
|
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() {
|
|
|
|
return !this.transport.status
|
|
|
|
|| (this.transport.status >= 200 && this.transport.status < 300);
|
|
|
|
},
|
|
|
|
|
|
|
|
respondToReadyState: function(readyState) {
|
|
|
|
var state = Ajax.Request.Events[readyState];
|
|
|
|
var transport = this.transport, json = this.evalJSON();
|
|
|
|
|
|
|
|
if (state == 'Complete') {
|
|
|
|
try {
|
|
|
|
this._complete = true;
|
|
|
|
(this.options['on' + this.transport.status]
|
|
|
|
|| this.options['on' + (this.success() ? 'Success' : 'Failure')]
|
|
|
|
|| Prototype.emptyFunction)(transport, json);
|
|
|
|
} catch (e) {
|
|
|
|
this.dispatchException(e);
|
|
|
|
}
|
|
|
|
|
2007-04-24 03:34:30 +00:00
|
|
|
var contentType = this.getHeader('Content-type');
|
|
|
|
if (contentType && contentType.strip().
|
2007-01-18 22:24:27 +00:00
|
|
|
match(/^(text|application)\/(x-)?(java|ecma)script(;.*)?$/i))
|
|
|
|
this.evalResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
(this.options['on' + state] || Prototype.emptyFunction)(transport, json);
|
|
|
|
Ajax.Responders.dispatch('on' + state, this, transport, json);
|
|
|
|
} 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 }
|
|
|
|
},
|
|
|
|
|
|
|
|
evalJSON: function() {
|
|
|
|
try {
|
|
|
|
var json = this.getHeader('X-JSON');
|
2007-04-24 03:31:14 +00:00
|
|
|
return json ? json.evalJSON() : null;
|
2007-01-18 22:24:27 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ajax.Updater = Class.create();
|
|
|
|
|
|
|
|
Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), {
|
|
|
|
initialize: function(container, url, options) {
|
|
|
|
this.container = {
|
|
|
|
success: (container.success || container),
|
|
|
|
failure: (container.failure || (container.success ? null : container))
|
|
|
|
}
|
|
|
|
|
|
|
|
this.transport = Ajax.getTransport();
|
|
|
|
this.setOptions(options);
|
|
|
|
|
|
|
|
var onComplete = this.options.onComplete || Prototype.emptyFunction;
|
|
|
|
this.options.onComplete = (function(transport, param) {
|
|
|
|
this.updateContent();
|
|
|
|
onComplete(transport, param);
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
this.request(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateContent: function() {
|
|
|
|
var receiver = this.container[this.success() ? 'success' : 'failure'];
|
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
|
|
|
var response = this.transport.responseText, options = this.options;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
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.evalScripts) response = response.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) {
|
|
|
|
if (typeof options.insertion == 'string')
|
2007-06-02 15:08:50 +00:00
|
|
|
receiver.insert( {}[options.insertion] = response );
|
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
|
|
|
else options.insertion(receiver, response);
|
|
|
|
}
|
|
|
|
else receiver.update(response);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.success()) {
|
2007-04-29 05:37:07 +00:00
|
|
|
if (this.onComplete) this.onComplete.bind(this).defer();
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ajax.PeriodicalUpdater = Class.create();
|
|
|
|
Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), {
|
|
|
|
initialize: function(container, url, options) {
|
|
|
|
this.setOptions(options);
|
|
|
|
this.onComplete = this.options.onComplete;
|
|
|
|
|
|
|
|
this.frequency = (this.options.frequency || 2);
|
|
|
|
this.decay = (this.options.decay || 1);
|
|
|
|
|
|
|
|
this.updater = {};
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateComplete: function(request) {
|
|
|
|
if (this.options.decay) {
|
|
|
|
this.decay = (request.responseText == this.lastText ?
|
|
|
|
this.decay * this.options.decay : 1);
|
|
|
|
|
|
|
|
this.lastText = request.responseText;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
});
|