/*
* Ext JS Library 2.2.1
* Copyright(c) 2006-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.Updater
* @extends Ext.util.Observable
* Provides AJAX-style update capabilities for Element objects. Updater can be used to {@link #update} an Element once,
* or you can use {@link #startAutoRefresh} to set up an auto-updating Element on a specific interval.
* Usage:
*
* // Get it from a Ext.Element object
* var el = Ext.get("foo");
* var mgr = el.getUpdater();
* mgr.update({
url: "http://myserver.com/index.php",
params: {
param1: "foo",
param2: "bar"
}
* });
* ...
* mgr.formUpdate("myFormId", "http://myserver.com/index.php");
*
* // or directly (returns the same Updater instance)
* var mgr = new Ext.Updater("myElementId");
* mgr.startAutoRefresh(60, "http://myserver.com/index.php");
* mgr.on("update", myFcnNeedsToKnow);
*
* // short handed call directly from the element object
* Ext.get("foo").load({
url: "bar.php",
scripts: true,
params: "param1=foo¶m2=bar",
text: "Loading Foo..."
* });
*
* @constructor
* Create new Updater directly.
* @param {Mixed} el The element to update
* @param {Boolean} forceNew (optional) By default the constructor checks to see if the passed element already
* has an Updater and if it does it returns the same instance. This will skip that check (useful for extending this class).
*/
Ext.Updater = Ext.extend(Ext.util.Observable, {
constructor: function(el, forceNew){
el = Ext.get(el);
if(!forceNew && el.updateManager){
return el.updateManager;
}
/**
* The Element object
* @type Ext.Element
*/
this.el = el;
/**
* Cached url to use for refreshes. Overwritten every time update() is called unless "discardUrl" param is set to true.
* @type String
*/
this.defaultUrl = null;
this.addEvents(
/**
* @event beforeupdate
* Fired before an update is made, return false from your handler and the update is cancelled.
* @param {Ext.Element} el
* @param {String/Object/Function} url
* @param {String/Object} params
*/
"beforeupdate",
/**
* @event update
* Fired after successful update is made.
* @param {Ext.Element} el
* @param {Object} oResponseObject The response Object
*/
"update",
/**
* @event failure
* Fired on update failure.
* @param {Ext.Element} el
* @param {Object} oResponseObject The response Object
*/
"failure"
);
var d = Ext.Updater.defaults;
/**
* Blank page URL to use with SSL file uploads (defaults to {@link Ext.Updater.defaults#sslBlankUrl}).
* @type String
*/
this.sslBlankUrl = d.sslBlankUrl;
/**
* Whether to append unique parameter on get request to disable caching (defaults to {@link Ext.Updater.defaults#disableCaching}).
* @type Boolean
*/
this.disableCaching = d.disableCaching;
/**
* Text for loading indicator (defaults to {@link Ext.Updater.defaults#indicatorText}).
* @type String
*/
this.indicatorText = d.indicatorText;
/**
* Whether to show indicatorText when loading (defaults to {@link Ext.Updater.defaults#showLoadIndicator}).
* @type String
*/
this.showLoadIndicator = d.showLoadIndicator;
/**
* Timeout for requests or form posts in seconds (defaults to {@link Ext.Updater.defaults#timeout}).
* @type Number
*/
this.timeout = d.timeout;
/**
* True to process scripts in the output (defaults to {@link Ext.Updater.defaults#loadScripts}).
* @type Boolean
*/
this.loadScripts = d.loadScripts;
/**
* Transaction object of the current executing transaction, or null if there is no active transaction.
*/
this.transaction = null;
/**
* Delegate for refresh() prebound to "this", use myUpdater.refreshDelegate.createCallback(arg1, arg2) to bind arguments
* @type Function
*/
this.refreshDelegate = this.refresh.createDelegate(this);
/**
* Delegate for update() prebound to "this", use myUpdater.updateDelegate.createCallback(arg1, arg2) to bind arguments
* @type Function
*/
this.updateDelegate = this.update.createDelegate(this);
/**
* Delegate for formUpdate() prebound to "this", use myUpdater.formUpdateDelegate.createCallback(arg1, arg2) to bind arguments
* @type Function
*/
this.formUpdateDelegate = this.formUpdate.createDelegate(this);
if(!this.renderer){
/**
* The renderer for this Updater (defaults to {@link Ext.Updater.BasicRenderer}).
*/
this.renderer = this.getDefaultRenderer();
}
Ext.Updater.superclass.constructor.call(this);
},
/**
* This is an overrideable method which returns a reference to a default
* renderer class if none is specified when creating the Ext.Updater.
* Defaults to {@link Ext.Updater.BasicRenderer}
*/
getDefaultRenderer: function() {
return new Ext.Updater.BasicRenderer();
},
/**
* Get the Element this Updater is bound to
* @return {Ext.Element} The element
*/
getEl : function(){
return this.el;
},
/**
* Performs an asynchronous request, updating this element with the response.
* If params are specified it uses POST, otherwise it uses GET.The URL to request or a function which * returns the URL (defaults to the value of {@link Ext.Ajax#url} if not specified).
The HTTP method to * use. Defaults to POST if the params argument is present, otherwise GET.
The * parameters to pass to the server (defaults to none). These may be specified as a url-encoded * string, or as an object containing properties which represent parameters, * or as a function, which returns such an object.
If true * any <script> tags embedded in the response text will be extracted * and executed (defaults to {@link Ext.Updater.defaults#loadScripts}). If this option is specified, * the callback will be called after the execution of the scripts.
A function to * be called when the response from the server arrives. The following * parameters are passed:
The Element being updated.
True for success, false for failure.
The XMLHttpRequest which processed the update.
The config object passed to the update call.
The scope in which * to execute the callback (The callback's this reference.) If the * params argument is a function, this scope is used for that function also.
By default, the URL of this request becomes * the default URL for this Updater object, and will be subsequently used in {@link #refresh} * calls. To bypass this behavior, pass discardUrl:true (defaults to false).
The number of seconds to wait for a response before * timing out (defaults to {@link Ext.Updater.defaults#timeout}).
The text to use as the innerHTML of the * {@link Ext.Updater.defaults#indicatorText} div (defaults to 'Loading...'). To replace the entire div, not * just the text, override {@link Ext.Updater.defaults#indicatorText} directly.
Only needed for GET * requests, this option causes an extra, auto-generated parameter to be appended to the request * to defeat caching (defaults to {@link Ext.Updater.defaults#disableCaching}).
* For example:
um.update({
url: "your-url.php",
params: {param1: "foo", param2: "bar"}, // or a URL encoded string
callback: yourFunction,
scope: yourObject, //(optional scope)
discardUrl: true,
nocache: true,
text: "Loading...",
timeout: 60,
scripts: false // Save time by avoiding RegExp execution.
});
*/
update : function(url, params, callback, discardUrl){
if(this.fireEvent("beforeupdate", this.el, url, params) !== false){
var cfg, callerScope;
if(typeof url == "object"){ // must be config object
cfg = url;
url = cfg.url;
params = params || cfg.params;
callback = callback || cfg.callback;
discardUrl = discardUrl || cfg.discardUrl;
callerScope = cfg.scope;
if(typeof cfg.nocache != "undefined"){this.disableCaching = cfg.nocache;};
if(typeof cfg.text != "undefined"){this.indicatorText = 'Performs an async form post, updating this element with the response. If the form has the attribute * enctype="multipart/form-data", it assumes it's a file upload. * Uses this.sslBlankUrl for SSL file uploads to prevent IE security warning.
*File uploads are not performed using normal "Ajax" techniques, that is they are not * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the * DOM <form> element temporarily modified to have its * target set to refer * to a dynamically generated, hidden <iframe> which is inserted into the document * but removed after the return data has been gathered.
*Be aware that file upload packets, sent with the content type multipart/form-data * and some server technologies (notably JEE) may require some custom processing in order to * retrieve parameter names and parameter values from the packet content.
* @param {String/HTMLElement} form The form Id or form element * @param {String} url (optional) The url to pass the form to. If omitted the action attribute on the form will be used. * @param {Boolean} reset (optional) Whether to try to reset the form after the update * @param {Function} callback (optional) Callback when transaction is complete. The following * parameters are passed:The Element being updated.
True for success, false for failure.
The XMLHttpRequest which processed the update.
Ext.Updater.updateElement("my-div", "stuff.php");
* @param {Mixed} el The element to update
* @param {String} url The url
* @param {String/Object} params (optional) Url encoded param string or an object of name/value pairs
* @param {Object} options (optional) A config object with any of the Updater properties you want to set - for
* example: {disableCaching:true, indicatorText: "Loading data..."}
* @static
* @deprecated
* @member Ext.Updater
*/
Ext.Updater.updateElement = function(el, url, params, options){
var um = Ext.get(el).getUpdater();
Ext.apply(um, options);
um.update(url, params, options ? options.callback : null);
};
/**
* @class Ext.Updater.BasicRenderer
* Default Content renderer. Updates the elements innerHTML with the responseText.
*/
Ext.Updater.BasicRenderer = function(){};
Ext.Updater.BasicRenderer.prototype = {
/**
* This is called when the transaction is completed and it's time to update the element - The BasicRenderer
* updates the elements innerHTML with the responseText - To perform a custom render (i.e. XML or JSON processing),
* create an object with a "render(el, response)" method and pass it to setRenderer on the Updater.
* @param {Ext.Element} el The element being rendered
* @param {Object} response The XMLHttpRequest object
* @param {Updater} updateManager The calling update manager
* @param {Function} callback A callback that will need to be called if loadScripts is true on the Updater
*/
render : function(el, response, updateManager, callback){
el.update(response.responseText, updateManager.loadScripts, callback);
}
};
Ext.UpdateManager = Ext.Updater;