/**
* @class YAHOO.ext.UpdateManager
* @extends YAHOO.ext.util.Observable
* Provides AJAX-style update for Element object using Yahoo
* UI library YAHOO.util.Connect functionality.
* Usage:
*
* // Get it from a YAHOO.ext.Element object
* var el = getEl('foo');
* var mgr = el.getUpdateManager();
* mgr.update('http://myserver.com/index.php', 'param1=1¶m2=2');
* ...
* mgr.formUpdate('myFormId', 'http://myserver.com/index.php');
*
* // or directly (returns the same UpdateManager instance)
* var mgr = new YAHOO.ext.UpdateManager('myElementId');
* mgr.startAutoRefresh(60, 'http://myserver.com/index.php');
* mgr.on('update', myFcnNeedsToKnow);
*
*
* @requires YAHOO.ext.Element
* @requires YAHOO.util.Dom
* @requires YAHOO.util.Event
* @requires YAHOO.util.CustomEvent
* @requires YAHOO.util.Connect
* @constructor
* Create new UpdateManager directly.
* @param {String/HTMLElement/YAHOO.ext.Element} el The element to update
* @param {Boolean} forceNew (optional) By default the constructor checks to see if the passed element already has an UpdateManager and if it does it returns the same instance. This will skip that check (useful for extending this class).
*/
YAHOO.ext.UpdateManager = function(el, forceNew){
el = YAHOO.ext.Element.get(el);
if(!forceNew && el.updateManager){
return el.updateManager;
}
/**
* The Element object
* @type YAHOO.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.beforeUpdate = new YAHOO.util.CustomEvent('UpdateManager.beforeUpdate');
this.onUpdate = new YAHOO.util.CustomEvent('UpdateManager.onUpdate');
this.onFailure = new YAHOO.util.CustomEvent('UpdateManager.onFailure');
this.events = {
/**
* @event beforeupdate
* Fired before an update is made, return false from your handler and the update is cancelled.
* @param {YAHOO.ext.Element} el
* @param {String/Object/Function} url
* @param {String/Object} params
*/
'beforeupdate': this.beforeUpdate,
/**
* @event update
* Fired after successful update is made.
* @param {YAHOO.ext.Element} el
* @param {Object} oResponseObject The YAHOO.util.Connect response Object
*/
'update': this.onUpdate,
/**
* @event failure
* Fired on update failure. Uses fireDirect with signature: (oElement, oResponseObject)
* @param {YAHOO.ext.Element} el
* @param {Object} oResponseObject The YAHOO.util.Connect response Object
*/
'failure': this.onFailure
};
/**
* Blank page URL to use with SSL file uploads (Defaults to YAHOO.ext.UpdateManager.defaults.sslBlankUrl or 'about:blank').
* @type String
*/
this.sslBlankUrl = YAHOO.ext.UpdateManager.defaults.sslBlankUrl;
/**
* Whether to append unique parameter on get request to disable caching (Defaults to YAHOO.ext.UpdateManager.defaults.disableCaching or false).
* @type Boolean
*/
this.disableCaching = YAHOO.ext.UpdateManager.defaults.disableCaching;
/**
* Text for loading indicator (Defaults to YAHOO.ext.UpdateManager.defaults.indicatorText or '<div class="loading-indicator">Loading...</div>').
* @type String
*/
this.indicatorText = YAHOO.ext.UpdateManager.defaults.indicatorText;
/**
* Whether to show indicatorText when loading (Defaults to YAHOO.ext.UpdateManager.defaults.showLoadIndicator or true).
* @type String
*/
this.showLoadIndicator = YAHOO.ext.UpdateManager.defaults.showLoadIndicator;
/**
* Timeout for requests or form posts in seconds (Defaults to YAHOO.ext.UpdateManager.defaults.timeout or 30 seconds).
* @type Number
*/
this.timeout = YAHOO.ext.UpdateManager.defaults.timeout;
/**
* True to process scripts in the output (Defaults to YAHOO.ext.UpdateManager.defaults.loadScripts (false)).
* @type Boolean
*/
this.loadScripts = YAHOO.ext.UpdateManager.defaults.loadScripts;
/**
* YAHOO.util.Connect transaction object of current executing transaction
*/
this.transaction = null;
/**
* @private
*/
this.autoRefreshProcId = 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);
/**
* @private
*/
this.successDelegate = this.processSuccess.createDelegate(this);
/**
* @private
*/
this.failureDelegate = this.processFailure.createDelegate(this);
/**
* The renderer for this UpdateManager. Defaults to {@link YAHOO.ext.UpdateManager.BasicRenderer}.
*/
this.renderer = new YAHOO.ext.UpdateManager.BasicRenderer();
};
YAHOO.ext.UpdateManager.prototype = {
fireEvent : YAHOO.ext.util.Observable.prototype.fireEvent,
on : YAHOO.ext.util.Observable.prototype.on,
addListener : YAHOO.ext.util.Observable.prototype.addListener,
delayedListener : YAHOO.ext.util.Observable.prototype.delayedListener,
removeListener : YAHOO.ext.util.Observable.prototype.removeListener,
purgeListeners : YAHOO.ext.util.Observable.prototype.purgeListeners,
bufferedListener : YAHOO.ext.util.Observable.prototype.bufferedListener,
/**
* Get the Element this UpdateManager is bound to
* @return {YAHOO.ext.Element} The element
*/
getEl : function(){
return this.el;
},
/**
* Performs an async request, updating this element with the response. If params are specified it uses POST, otherwise it uses GET.
* @param {Object/String/Function} url The url for this request or a function to call to get the url or a config object containing any of the following options:
um.update({
url: 'your-url.php',
params: {param1: 'foo', param2: 'bar'}, // or a URL encoded string
callback: yourFunction,
scope: yourObject, //(optional scope)
discardUrl: false,
nocache: false,
text: 'Loading...',
timeout: 30,
scripts: false
});
* The only required property is url. The optional properties nocache, text and scripts
* are shorthand for disableCaching, indicatorText and loadScripts and are used to set their associated property on this UpdateManager instance.
* @param {String/Object} params (optional) The parameters to pass as either a url encoded string "param1=1¶m2=2" or an object {param1: 1, param2: 2}
* @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess, oResponse)
* @param {Boolean} discardUrl (optional) By default when you execute an update the defaultUrl is changed to the last used url. If true, it will not store the url.
*/
update : function(url, params, callback, discardUrl){
if(this.beforeUpdate.fireDirect(this.el, url, params) !== false){
if(typeof url == 'object'){ // must be config object
var cfg = url;
url = cfg.url;
params = params || cfg.params;
callback = callback || cfg.callback;
discardUrl = discardUrl || cfg.discardUrl;
if(callback && cfg.scope){
callback = callback.createDelegate(cfg.scope);
}
if(typeof cfg.nocache != 'undefined'){this.disableCaching = cfg.nocache};
if(typeof cfg.text != 'undefined'){this.indicatorText = 'YAHOO.ext.UpdateManager.updateElement('my-div', 'stuff.php');
* @param {String/HTMLElement/YAHOO.ext.Element} 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 UpdateManager properties you want to set - for example: {disableCaching:true, indicatorText: 'Loading data...'}
* @static
*/
YAHOO.ext.UpdateManager.updateElement = function(el, url, params, options){
var um = getEl(el, true).getUpdateManager();
YAHOO.ext.util.Config.apply(um, options);
um.update(url, params, options ? options.callback : null);
};
// alias for backwards compat
YAHOO.ext.UpdateManager.update = YAHOO.ext.UpdateManager.updateElement;
/**
* @class YAHOO.ext.UpdateManager.BasicRenderer
* Default Content renderer. Updates the elements innerHTML with the responseText.
*/
YAHOO.ext.UpdateManager.BasicRenderer = function(){};
YAHOO.ext.UpdateManager.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 UpdateManager.
* @param {YAHOO.ext.Element} el The element being rendered
* @param {Object} response The YUI Connect response object
* @param {UpdateManager} updateManager The calling update manager
* @param {Function} callback A callback that will need to be called if loadScripts is true on the UpdateManager
*/
render : function(el, response, updateManager, callback){
el.update(response.responseText, updateManager.loadScripts, callback);
}
};