3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
11
* @extends Ext.util.Observable
12
* Provides AJAX-style update capabilities for Element objects. Updater can be used to {@link #update} an Element once,
13
* or you can use {@link #startAutoRefresh} to set up an auto-updating Element on a specific interval.<br><br>
16
* // Get it from a Ext.Element object
17
* var el = Ext.get("foo");
18
* var mgr = el.getUpdater();
20
url: "http://myserver.com/index.php",
27
* mgr.formUpdate("myFormId", "http://myserver.com/index.php");
29
* // or directly (returns the same Updater instance)
30
* var mgr = new Ext.Updater("myElementId");
31
* mgr.startAutoRefresh(60, "http://myserver.com/index.php");
32
* mgr.on("update", myFcnNeedsToKnow);
34
* // short handed call directly from the element object
35
* Ext.get("foo").load({
38
params: "param1=foo&param2=bar",
39
text: "Loading Foo..."
43
* Create new Updater directly.
44
* @param {Mixed} el The element to update
45
* @param {Boolean} forceNew (optional) By default the constructor checks to see if the passed element already
46
* has an Updater and if it does it returns the same instance. This will skip that check (useful for extending this class).
48
Ext.Updater = Ext.extend(Ext.util.Observable, {
49
constructor: function(el, forceNew){
51
if(!forceNew && el.updateManager){
52
return el.updateManager;
60
* Cached url to use for refreshes. Overwritten every time update() is called unless "discardUrl" param is set to true.
63
this.defaultUrl = null;
68
* Fired before an update is made, return false from your handler and the update is cancelled.
69
* @param {Ext.Element} el
70
* @param {String/Object/Function} url
71
* @param {String/Object} params
76
* Fired after successful update is made.
77
* @param {Ext.Element} el
78
* @param {Object} oResponseObject The response Object
83
* Fired on update failure.
84
* @param {Ext.Element} el
85
* @param {Object} oResponseObject The response Object
89
var d = Ext.Updater.defaults;
91
* Blank page URL to use with SSL file uploads (defaults to {@link Ext.Updater.defaults#sslBlankUrl}).
94
this.sslBlankUrl = d.sslBlankUrl;
96
* Whether to append unique parameter on get request to disable caching (defaults to {@link Ext.Updater.defaults#disableCaching}).
99
this.disableCaching = d.disableCaching;
101
* Text for loading indicator (defaults to {@link Ext.Updater.defaults#indicatorText}).
104
this.indicatorText = d.indicatorText;
106
* Whether to show indicatorText when loading (defaults to {@link Ext.Updater.defaults#showLoadIndicator}).
109
this.showLoadIndicator = d.showLoadIndicator;
111
* Timeout for requests or form posts in seconds (defaults to {@link Ext.Updater.defaults#timeout}).
114
this.timeout = d.timeout;
116
* True to process scripts in the output (defaults to {@link Ext.Updater.defaults#loadScripts}).
119
this.loadScripts = d.loadScripts;
121
* Transaction object of the current executing transaction, or null if there is no active transaction.
123
this.transaction = null;
125
* Delegate for refresh() prebound to "this", use myUpdater.refreshDelegate.createCallback(arg1, arg2) to bind arguments
128
this.refreshDelegate = this.refresh.createDelegate(this);
130
* Delegate for update() prebound to "this", use myUpdater.updateDelegate.createCallback(arg1, arg2) to bind arguments
133
this.updateDelegate = this.update.createDelegate(this);
135
* Delegate for formUpdate() prebound to "this", use myUpdater.formUpdateDelegate.createCallback(arg1, arg2) to bind arguments
138
this.formUpdateDelegate = this.formUpdate.createDelegate(this);
142
* The renderer for this Updater (defaults to {@link Ext.Updater.BasicRenderer}).
144
this.renderer = this.getDefaultRenderer();
146
Ext.Updater.superclass.constructor.call(this);
149
* This is an overrideable method which returns a reference to a default
150
* renderer class if none is specified when creating the Ext.Updater.
151
* Defaults to {@link Ext.Updater.BasicRenderer}
153
getDefaultRenderer: function() {
154
return new Ext.Updater.BasicRenderer();
157
* Get the Element this Updater is bound to
158
* @return {Ext.Element} The element
165
* Performs an <b>asynchronous</b> request, updating this element with the response.
166
* If params are specified it uses POST, otherwise it uses GET.<br><br>
167
* <b>Note:</b> Due to the asynchronous nature of remote server requests, the Element
168
* will not have been fully updated when the function returns. To post-process the returned
169
* data, use the callback option, or an <b><tt>update</tt></b> event handler.
170
* @param {Object} options A config object containing any of the following options:<ul>
171
* <li>url : <b>String/Function</b><p class="sub-desc">The URL to request or a function which
172
* <i>returns</i> the URL (defaults to the value of {@link Ext.Ajax#url} if not specified).</p></li>
173
* <li>method : <b>String</b><p class="sub-desc">The HTTP method to
174
* use. Defaults to POST if the <tt>params</tt> argument is present, otherwise GET.</p></li>
175
* <li>params : <b>String/Object/Function</b><p class="sub-desc">The
176
* parameters to pass to the server (defaults to none). These may be specified as a url-encoded
177
* string, or as an object containing properties which represent parameters,
178
* or as a function, which returns such an object.</p></li>
179
* <li>scripts : <b>Boolean</b><p class="sub-desc">If <tt>true</tt>
180
* any <script> tags embedded in the response text will be extracted
181
* and executed (defaults to {@link Ext.Updater.defaults#loadScripts}). If this option is specified,
182
* the callback will be called <i>after</i> the execution of the scripts.</p></li>
183
* <li>callback : <b>Function</b><p class="sub-desc">A function to
184
* be called when the response from the server arrives. The following
185
* parameters are passed:<ul>
186
* <li><b>el</b> : Ext.Element<p class="sub-desc">The Element being updated.</p></li>
187
* <li><b>success</b> : Boolean<p class="sub-desc">True for success, false for failure.</p></li>
188
* <li><b>response</b> : XMLHttpRequest<p class="sub-desc">The XMLHttpRequest which processed the update.</p></li>
189
* <li><b>options</b> : Object<p class="sub-desc">The config object passed to the update call.</p></li></ul>
191
* <li>scope : <b>Object</b><p class="sub-desc">The scope in which
192
* to execute the callback (The callback's <tt>this</tt> reference.) If the
193
* <tt>params</tt> argument is a function, this scope is used for that function also.</p></li>
194
* <li>discardUrl : <b>Boolean</b><p class="sub-desc">By default, the URL of this request becomes
195
* the default URL for this Updater object, and will be subsequently used in {@link #refresh}
196
* calls. To bypass this behavior, pass <tt>discardUrl:true</tt> (defaults to false).</p></li>
197
* <li>timeout : <b>Number</b><p class="sub-desc">The number of seconds to wait for a response before
198
* timing out (defaults to {@link Ext.Updater.defaults#timeout}).</p></li>
199
* <li>text : <b>String</b><p class="sub-desc">The text to use as the innerHTML of the
200
* {@link Ext.Updater.defaults#indicatorText} div (defaults to 'Loading...'). To replace the entire div, not
201
* just the text, override {@link Ext.Updater.defaults#indicatorText} directly.</p></li>
202
* <li>nocache : <b>Boolean</b><p class="sub-desc">Only needed for GET
203
* requests, this option causes an extra, auto-generated parameter to be appended to the request
204
* to defeat caching (defaults to {@link Ext.Updater.defaults#disableCaching}).</p></li></ul>
210
params: {param1: "foo", param2: "bar"}, // or a URL encoded string
211
callback: yourFunction,
212
scope: yourObject, //(optional scope)
217
scripts: false // Save time by avoiding RegExp execution.
221
update : function(url, params, callback, discardUrl){
222
if(this.fireEvent("beforeupdate", this.el, url, params) !== false){
223
var cfg, callerScope;
224
if(typeof url == "object"){ // must be config object
227
params = params || cfg.params;
228
callback = callback || cfg.callback;
229
discardUrl = discardUrl || cfg.discardUrl;
230
callerScope = cfg.scope;
231
if(typeof cfg.nocache != "undefined"){this.disableCaching = cfg.nocache;};
232
if(typeof cfg.text != "undefined"){this.indicatorText = '<div class="loading-indicator">'+cfg.text+"</div>";};
233
if(typeof cfg.scripts != "undefined"){this.loadScripts = cfg.scripts;};
234
if(typeof cfg.timeout != "undefined"){this.timeout = cfg.timeout;};
239
this.defaultUrl = url;
241
if(typeof url == "function"){
242
url = url.call(this);
245
var o = Ext.apply({}, {
247
params: (typeof params == "function" && callerScope) ? params.createDelegate(callerScope) : params,
248
success: this.processSuccess,
249
failure: this.processFailure,
252
timeout: (this.timeout*1000),
253
disableCaching: this.disableCaching,
258
"callback": callback,
259
"scope": callerScope || window,
264
this.transaction = Ext.Ajax.request(o);
269
* Performs an async form post, updating this element with the response. If the form has the attribute
270
* enctype="multipart/form-data", it assumes it's a file upload.
271
* Uses this.sslBlankUrl for SSL file uploads to prevent IE security warning.
272
* @param {String/HTMLElement} form The form Id or form element
273
* @param {String} url (optional) The url to pass the form to. If omitted the action attribute on the form will be used.
274
* @param {Boolean} reset (optional) Whether to try to reset the form after the update
275
* @param {Function} callback (optional) Callback when transaction is complete. The following
276
* parameters are passed:<ul>
277
* <li><b>el</b> : Ext.Element<p class="sub-desc">The Element being updated.</p></li>
278
* <li><b>success</b> : Boolean<p class="sub-desc">True for success, false for failure.</p></li>
279
* <li><b>response</b> : XMLHttpRequest<p class="sub-desc">The XMLHttpRequest which processed the update.</p></li></ul>
281
formUpdate : function(form, url, reset, callback){
282
if(this.fireEvent("beforeupdate", this.el, form, url) !== false){
283
if(typeof url == "function"){
284
url = url.call(this);
286
form = Ext.getDom(form)
287
this.transaction = Ext.Ajax.request({
290
success: this.processSuccess,
291
failure: this.processFailure,
293
timeout: (this.timeout*1000),
297
"callback": callback,
301
this.showLoading.defer(1, this);
306
* Refresh the element with the last used url or defaultUrl. If there is no url, it returns immediately
307
* @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
309
refresh : function(callback){
310
if(this.defaultUrl == null){
313
this.update(this.defaultUrl, null, callback, true);
317
* Set this element to auto refresh. Can be canceled by calling {@link #stopAutoRefresh}.
318
* @param {Number} interval How often to update (in seconds).
319
* @param {String/Object/Function} url (optional) The url for this request, a config object in the same format
320
* supported by {@link #load}, or a function to call to get the url (defaults to the last used url). Note that while
321
* the url used in a load call can be reused by this method, other load config options will not be reused and must be
322
* sepcified as part of a config object passed as this paramter if needed.
323
* @param {String/Object} params (optional) The parameters to pass as either a url encoded string
324
* "¶m1=1¶m2=2" or as an object {param1: 1, param2: 2}
325
* @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
326
* @param {Boolean} refreshNow (optional) Whether to execute the refresh now, or wait the interval
328
startAutoRefresh : function(interval, url, params, callback, refreshNow){
330
this.update(url || this.defaultUrl, params, callback, true);
332
if(this.autoRefreshProcId){
333
clearInterval(this.autoRefreshProcId);
335
this.autoRefreshProcId = setInterval(this.update.createDelegate(this, [url || this.defaultUrl, params, callback, true]), interval*1000);
339
* Stop auto refresh on this element.
341
stopAutoRefresh : function(){
342
if(this.autoRefreshProcId){
343
clearInterval(this.autoRefreshProcId);
344
delete this.autoRefreshProcId;
349
* Returns true if the Updater is currently set to auto refresh its content (see {@link #startAutoRefresh}), otherwise false.
351
isAutoRefreshing : function(){
352
return this.autoRefreshProcId ? true : false;
356
* Display the element's "loading" state. By default, the element is updated with {@link #indicatorText}. This
357
* method may be overridden to perform a custom action while this Updater is actively updating its contents.
359
showLoading : function(){
360
if(this.showLoadIndicator){
361
this.el.update(this.indicatorText);
366
processSuccess : function(response){
367
this.transaction = null;
368
if(response.argument.form && response.argument.reset){
369
try{ // put in try/catch since some older FF releases had problems with this
370
response.argument.form.reset();
373
if(this.loadScripts){
374
this.renderer.render(this.el, response, this,
375
this.updateComplete.createDelegate(this, [response]));
377
this.renderer.render(this.el, response, this);
378
this.updateComplete(response);
383
updateComplete : function(response){
384
this.fireEvent("update", this.el, response);
385
if(typeof response.argument.callback == "function"){
386
response.argument.callback.call(response.argument.scope, this.el, true, response, response.argument.options);
391
processFailure : function(response){
392
this.transaction = null;
393
this.fireEvent("failure", this.el, response);
394
if(typeof response.argument.callback == "function"){
395
response.argument.callback.call(response.argument.scope, this.el, false, response, response.argument.options);
400
* Sets the content renderer for this Updater. See {@link Ext.Updater.BasicRenderer#render} for more details.
401
* @param {Object} renderer The object implementing the render() method
403
setRenderer : function(renderer){
404
this.renderer = renderer;
408
* Returns the content renderer for this Updater. See {@link Ext.Updater.BasicRenderer#render} for more details.
411
getRenderer : function(){
412
return this.renderer;
416
* Sets the default URL used for updates.
417
* @param {String/Function} defaultUrl The url or a function to call to get the url
419
setDefaultUrl : function(defaultUrl){
420
this.defaultUrl = defaultUrl;
424
* Aborts the currently executing transaction, if any.
427
if(this.transaction){
428
Ext.Ajax.abort(this.transaction);
433
* Returns true if an update is in progress, otherwise false.
436
isUpdating : function(){
437
if(this.transaction){
438
return Ext.Ajax.isLoading(this.transaction);
445
* @class Ext.Updater.defaults
446
* The defaults collection enables customizing the default properties of Updater
448
Ext.Updater.defaults = {
450
* Timeout for requests or form posts in seconds (defaults to 30 seconds).
455
* True to process scripts by default (defaults to false).
460
* Blank page URL to use with SSL file uploads (defaults to {@link Ext#SSL_SECURE_URL} if set, or "javascript:false").
463
sslBlankUrl : (Ext.SSL_SECURE_URL || "javascript:false"),
465
* True to append a unique parameter to GET requests to disable caching (defaults to false).
468
disableCaching : false,
470
* Whether or not to show {@link #indicatorText} during loading (defaults to true).
473
showLoadIndicator : true,
475
* Text for loading indicator (defaults to '<div class="loading-indicator">Loading...</div>').
478
indicatorText : '<div class="loading-indicator">Loading...</div>'
482
* Static convenience method. <b>This method is deprecated in favor of el.load({url:'foo.php', ...})</b>.
484
* <pre><code>Ext.Updater.updateElement("my-div", "stuff.php");</code></pre>
485
* @param {Mixed} el The element to update
486
* @param {String} url The url
487
* @param {String/Object} params (optional) Url encoded param string or an object of name/value pairs
488
* @param {Object} options (optional) A config object with any of the Updater properties you want to set - for
489
* example: {disableCaching:true, indicatorText: "Loading data..."}
492
* @member Ext.Updater
494
Ext.Updater.updateElement = function(el, url, params, options){
495
var um = Ext.get(el).getUpdater();
496
Ext.apply(um, options);
497
um.update(url, params, options ? options.callback : null);
500
* @class Ext.Updater.BasicRenderer
501
* Default Content renderer. Updates the elements innerHTML with the responseText.
503
Ext.Updater.BasicRenderer = function(){};
505
Ext.Updater.BasicRenderer.prototype = {
507
* This is called when the transaction is completed and it's time to update the element - The BasicRenderer
508
* updates the elements innerHTML with the responseText - To perform a custom render (i.e. XML or JSON processing),
509
* create an object with a "render(el, response)" method and pass it to setRenderer on the Updater.
510
* @param {Ext.Element} el The element being rendered
511
* @param {Object} response The XMLHttpRequest object
512
* @param {Updater} updateManager The calling update manager
513
* @param {Function} callback A callback that will need to be called if loadScripts is true on the Updater
515
render : function(el, response, updateManager, callback){
516
el.update(response.responseText, updateManager.loadScripts, callback);
520
Ext.UpdateManager = Ext.Updater;