summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/UpdateManager.js
Unidiff
Diffstat (limited to 'frontend/beta/js/YUI-extensions/UpdateManager.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/YUI-extensions/UpdateManager.js484
1 files changed, 484 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/UpdateManager.js b/frontend/beta/js/YUI-extensions/UpdateManager.js
new file mode 100644
index 0000000..c2eb23f
--- a/dev/null
+++ b/frontend/beta/js/YUI-extensions/UpdateManager.js
@@ -0,0 +1,484 @@
1/**
2 * @class YAHOO.ext.UpdateManager
3 * @extends YAHOO.ext.util.Observable
4 * Provides AJAX-style update for Element object using Yahoo
5 * UI library YAHOO.util.Connect functionality.<br><br>
6 * Usage:<br>
7 * <pre><code>
8 * // Get it from a YAHOO.ext.Element object
9 * var el = getEl('foo');
10 * var mgr = el.getUpdateManager();
11 * mgr.update('http://myserver.com/index.php', 'param1=1&amp;param2=2');
12 * ...
13 * mgr.formUpdate('myFormId', 'http://myserver.com/index.php');
14 * <br>
15 * // or directly (returns the same UpdateManager instance)
16 * var mgr = new YAHOO.ext.UpdateManager('myElementId');
17 * mgr.startAutoRefresh(60, 'http://myserver.com/index.php');
18 * mgr.on('update', myFcnNeedsToKnow);
19 * <br>
20 * </code></pre>
21 * @requires YAHOO.ext.Element
22 * @requires YAHOO.util.Dom
23 * @requires YAHOO.util.Event
24 * @requires YAHOO.util.CustomEvent
25 * @requires YAHOO.util.Connect
26 * @constructor
27 * Create new UpdateManager directly.
28 * @param {String/HTMLElement/YAHOO.ext.Element} el The element to update
29 * @param {<i>Boolean</i>} 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).
30 */
31YAHOO.ext.UpdateManager = function(el, forceNew){
32 el = YAHOO.ext.Element.get(el);
33 if(!forceNew && el.updateManager){
34 return el.updateManager;
35 }
36 /**
37 * The Element object
38 * @type YAHOO.ext.Element
39 */
40 this.el = el;
41 /**
42 * Cached url to use for refreshes. Overwritten every time update() is called unless 'discardUrl' param is set to true.
43 * @type String
44 */
45 this.defaultUrl = null;
46 this.beforeUpdate = new YAHOO.util.CustomEvent('UpdateManager.beforeUpdate');
47 this.onUpdate = new YAHOO.util.CustomEvent('UpdateManager.onUpdate');
48 this.onFailure = new YAHOO.util.CustomEvent('UpdateManager.onFailure');
49
50 this.events = {
51 /**
52 * @event beforeupdate
53 * Fired before an update is made, return false from your handler and the update is cancelled.
54 * @param {YAHOO.ext.Element} el
55 * @param {String/Object/Function} url
56 * @param {String/Object} params
57 */
58 'beforeupdate': this.beforeUpdate,
59 /**
60 * @event update
61 * Fired after successful update is made.
62 * @param {YAHOO.ext.Element} el
63 * @param {Object} oResponseObject The YAHOO.util.Connect response Object
64 */
65 'update': this.onUpdate,
66 /**
67 * @event failure
68 * Fired on update failure. Uses fireDirect with signature: (oElement, oResponseObject)
69 * @param {YAHOO.ext.Element} el
70 * @param {Object} oResponseObject The YAHOO.util.Connect response Object
71 */
72 'failure': this.onFailure
73 };
74
75 /**
76 * Blank page URL to use with SSL file uploads (Defaults to YAHOO.ext.UpdateManager.defaults.sslBlankUrl or 'about:blank').
77 * @type String
78 */
79 this.sslBlankUrl = YAHOO.ext.UpdateManager.defaults.sslBlankUrl;
80 /**
81 * Whether to append unique parameter on get request to disable caching (Defaults to YAHOO.ext.UpdateManager.defaults.disableCaching or false).
82 * @type Boolean
83 */
84 this.disableCaching = YAHOO.ext.UpdateManager.defaults.disableCaching;
85 /**
86 * Text for loading indicator (Defaults to YAHOO.ext.UpdateManager.defaults.indicatorText or '&lt;div class="loading-indicator"&gt;Loading...&lt;/div&gt;').
87 * @type String
88 */
89 this.indicatorText = YAHOO.ext.UpdateManager.defaults.indicatorText;
90 /**
91 * Whether to show indicatorText when loading (Defaults to YAHOO.ext.UpdateManager.defaults.showLoadIndicator or true).
92 * @type String
93 */
94 this.showLoadIndicator = YAHOO.ext.UpdateManager.defaults.showLoadIndicator;
95 /**
96 * Timeout for requests or form posts in seconds (Defaults to YAHOO.ext.UpdateManager.defaults.timeout or 30 seconds).
97 * @type Number
98 */
99 this.timeout = YAHOO.ext.UpdateManager.defaults.timeout;
100
101 /**
102 * True to process scripts in the output (Defaults to YAHOO.ext.UpdateManager.defaults.loadScripts (false)).
103 * @type Boolean
104 */
105 this.loadScripts = YAHOO.ext.UpdateManager.defaults.loadScripts;
106
107 /**
108 * YAHOO.util.Connect transaction object of current executing transaction
109 */
110 this.transaction = null;
111
112 /**
113 * @private
114 */
115 this.autoRefreshProcId = null;
116 /**
117 * Delegate for refresh() prebound to 'this', use myUpdater.refreshDelegate.createCallback(arg1, arg2) to bind arguments
118 * @type Function
119 */
120 this.refreshDelegate = this.refresh.createDelegate(this);
121 /**
122 * Delegate for update() prebound to 'this', use myUpdater.updateDelegate.createCallback(arg1, arg2) to bind arguments
123 * @type Function
124 */
125 this.updateDelegate = this.update.createDelegate(this);
126 /**
127 * Delegate for formUpdate() prebound to 'this', use myUpdater.formUpdateDelegate.createCallback(arg1, arg2) to bind arguments
128 * @type Function
129 */
130 this.formUpdateDelegate = this.formUpdate.createDelegate(this);
131 /**
132 * @private
133 */
134 this.successDelegate = this.processSuccess.createDelegate(this);
135 /**
136 * @private
137 */
138 this.failureDelegate = this.processFailure.createDelegate(this);
139
140 /**
141 * The renderer for this UpdateManager. Defaults to {@link YAHOO.ext.UpdateManager.BasicRenderer}.
142 */
143 this.renderer = new YAHOO.ext.UpdateManager.BasicRenderer();
144};
145
146YAHOO.ext.UpdateManager.prototype = {
147 fireEvent : YAHOO.ext.util.Observable.prototype.fireEvent,
148 on : YAHOO.ext.util.Observable.prototype.on,
149 addListener : YAHOO.ext.util.Observable.prototype.addListener,
150 delayedListener : YAHOO.ext.util.Observable.prototype.delayedListener,
151 removeListener : YAHOO.ext.util.Observable.prototype.removeListener,
152 purgeListeners : YAHOO.ext.util.Observable.prototype.purgeListeners,
153 bufferedListener : YAHOO.ext.util.Observable.prototype.bufferedListener,
154 /**
155 * Get the Element this UpdateManager is bound to
156 * @return {YAHOO.ext.Element} The element
157 */
158 getEl : function(){
159 return this.el;
160 },
161
162 /**
163 * Performs an async request, updating this element with the response. If params are specified it uses POST, otherwise it uses GET.
164 * @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:
165<pre><code>
166um.update({<br/>
167 url: 'your-url.php',<br/>
168 params: {param1: 'foo', param2: 'bar'}, // or a URL encoded string<br/>
169 callback: yourFunction,<br/>
170 scope: yourObject, //(optional scope) <br/>
171 discardUrl: false, <br/>
172 nocache: false,<br/>
173 text: 'Loading...',<br/>
174 timeout: 30,<br/>
175 scripts: false<br/>
176});
177</code></pre>
178 * The only required property is url. The optional properties nocache, text and scripts
179 * are shorthand for disableCaching, indicatorText and loadScripts and are used to set their associated property on this UpdateManager instance.
180 * @param {<i>String/Object</i>} params (optional) The parameters to pass as either a url encoded string "param1=1&amp;param2=2" or an object {param1: 1, param2: 2}
181 * @param {<i>Function</i>} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess, oResponse)
182 * @param {<i>Boolean</i>} 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.
183 */
184 update : function(url, params, callback, discardUrl){
185 if(this.beforeUpdate.fireDirect(this.el, url, params) !== false){
186 if(typeof url == 'object'){ // must be config object
187 var cfg = url;
188 url = cfg.url;
189 params = params || cfg.params;
190 callback = callback || cfg.callback;
191 discardUrl = discardUrl || cfg.discardUrl;
192 if(callback && cfg.scope){
193 callback = callback.createDelegate(cfg.scope);
194 }
195 if(typeof cfg.nocache != 'undefined'){this.disableCaching = cfg.nocache};
196 if(typeof cfg.text != 'undefined'){this.indicatorText = '<div class="loading-indicator">'+cfg.text+'</div>'};
197 if(typeof cfg.scripts != 'undefined'){this.loadScripts = cfg.scripts};
198 if(typeof cfg.timeout != 'undefined'){this.timeout = cfg.timeout};
199 }
200 this.showLoading();
201 if(!discardUrl){
202 this.defaultUrl = url;
203 }
204 if(typeof url == 'function'){
205 url = url();
206 }
207 if(typeof params == 'function'){
208 params = params();
209 }
210 if(params && typeof params != 'string'){ // must be object
211 var buf = [];
212 for(var key in params){
213 if(typeof params[key] != 'function'){
214 buf.push(encodeURIComponent(key), '=', encodeURIComponent(params[key]), '&');
215 }
216 }
217 delete buf[buf.length-1];
218 params = buf.join('');
219 }
220 var callback = {
221 success: this.successDelegate,
222 failure: this.failureDelegate,
223 timeout: (this.timeout*1000),
224 argument: {'url': url, 'form': null, 'callback': callback, 'params': params}
225 };
226 var method = params ? 'POST' : 'GET';
227 if(method == 'GET'){
228 url = this.prepareUrl(url);
229 }
230 this.transaction = YAHOO.util.Connect.asyncRequest(method, url, callback, params);
231 }
232 },
233
234 /**
235 * 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.
236 * Uses this.sslBlankUrl for SSL file uploads to prevent IE security warning. See YUI docs for more info.
237 * @param {String/HTMLElement} form The form Id or form element
238 * @param {<i>String</i>} url (optional) The url to pass the form to. If omitted the action attribute on the form will be used.
239 * @param {<i>Boolean</i>} reset (optional) Whether to try to reset the form after the update
240 * @param {<i>Function</i>} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess, oResponse)
241 */
242 formUpdate : function(form, url, reset, callback){
243 if(this.beforeUpdate.fireDirect(this.el, form, url) !== false){
244 formEl = YAHOO.util.Dom.get(form);
245 this.showLoading();
246 if(typeof url == 'function'){
247 url = url();
248 }
249 if(typeof params == 'function'){
250 params = params();
251 }
252 url = url || formEl.action;
253 var callback = {
254 success: this.successDelegate,
255 failure: this.failureDelegate,
256 timeout: (this.timeout*1000),
257 argument: {'url': url, 'form': form, 'callback': callback, 'reset': reset}
258 };
259 var isUpload = false;
260 var enctype = formEl.getAttribute('enctype');
261 if(enctype && enctype.toLowerCase() == 'multipart/form-data'){
262 isUpload = true;
263 }
264 YAHOO.util.Connect.setForm(form, isUpload, this.sslBlankUrl);
265 this.transaction = YAHOO.util.Connect.asyncRequest('POST', url, callback);
266 }
267 },
268
269 /**
270 * Refresh the element with the last used url or defaultUrl. If there is no url, it returns immediately
271 * @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
272 */
273 refresh : function(callback){
274 if(this.defaultUrl == null){
275 return;
276 }
277 this.update(this.defaultUrl, null, callback, true);
278 },
279
280 /**
281 * Set this element to auto refresh.
282 * @param {Number} interval How often to update (in seconds).
283 * @param {<i>String/Function</i>} url (optional) The url for this request or a function to call to get the url (Defaults to the last used url)
284 * @param {<i>String/Object</i>} params (optional) The parameters to pass as either a url encoded string "&param1=1&param2=2" or as an object {param1: 1, param2: 2}
285 * @param {<i>Function</i>} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
286 * @param {<i>Boolean</i>} refreshNow (optional) Whether to execute the refresh now, or wait the interval
287 */
288 startAutoRefresh : function(interval, url, params, callback, refreshNow){
289 if(refreshNow){
290 this.update(url || this.defaultUrl, params, callback, true);
291 }
292 if(this.autoRefreshProcId){
293 clearInterval(this.autoRefreshProcId);
294 }
295 this.autoRefreshProcId = setInterval(this.update.createDelegate(this, [url || this.defaultUrl, params, callback, true]), interval*1000);
296 },
297
298 /**
299 * Stop auto refresh on this element.
300 */
301 stopAutoRefresh : function(){
302 if(this.autoRefreshProcId){
303 clearInterval(this.autoRefreshProcId);
304 }
305 },
306
307 /**
308 * Called to update the element to "Loading" state. Override to perform custom action.
309 */
310 showLoading : function(){
311 if(this.showLoadIndicator){
312 this.el.update(this.indicatorText);
313 }
314 },
315
316 /**
317 * Adds unique parameter to query string if disableCaching = true
318 * @private
319 */
320 prepareUrl : function(url){
321 if(this.disableCaching){
322 var append = '_dc=' + (new Date().getTime());
323 if(url.indexOf('?') !== -1){
324 url += '&' + append;
325 }else{
326 url += '?' + append;
327 }
328 }
329 return url;
330 },
331
332 /**
333 * @private
334 */
335 processSuccess : function(response){
336 this.transaction = null;
337 if(response.argument.form && response.argument.reset){
338 try{ // put in try/catch since some older FF releases had problems with this
339 response.argument.form.reset();
340 }catch(e){}
341 }
342 if(this.loadScripts){
343 this.renderer.render(this.el, response, this,
344 this.updateComplete.createDelegate(this, [response]));
345 }else{
346 this.renderer.render(this.el, response, this);
347 this.updateComplete(response);
348 }
349 },
350
351 updateComplete : function(response){
352 this.fireEvent('update', this.el, response);
353 if(typeof response.argument.callback == 'function'){
354 response.argument.callback(this.el, true, response);
355 }
356 },
357
358 /**
359 * @private
360 */
361 processFailure : function(response){
362 this.transaction = null;
363 this.onFailure.fireDirect(this.el, response);
364 if(typeof response.argument.callback == 'function'){
365 response.argument.callback(this.el, false, response);
366 }
367 },
368
369 /**
370 * Set the content renderer for this UpdateManager. See {@link YAHOO.ext.UpdateManager.BasicRenderer#render} for more details.
371 * @param {Object} renderer The object implementing the render() method
372 */
373 setRenderer : function(renderer){
374 this.renderer = renderer;
375 },
376
377 getRenderer : function(){
378 return this.renderer;
379 },
380
381 /**
382 * Set the defaultUrl used for updates
383 * @param {String/Function} defaultUrl The url or a function to call to get the url
384 */
385 setDefaultUrl : function(defaultUrl){
386 this.defaultUrl = defaultUrl;
387 },
388
389 /**
390 * Aborts the executing transaction
391 */
392 abort : function(){
393 if(this.transaction){
394 YAHOO.util.Connect.abort(this.transaction);
395 }
396 },
397
398 /**
399 * Returns true if an update is in progress
400 * @return {Boolean}
401 */
402 isUpdating : function(){
403 if(this.transaction){
404 return YAHOO.util.Connect.isCallInProgress(this.transaction);
405 }
406 return false;
407 }
408};
409
410/**
411 * @class YAHOO.ext.UpdateManager.defaults
412 * The defaults collection enables customizing the default properties of UpdateManager
413 */
414 YAHOO.ext.UpdateManager.defaults = {
415 /**
416 * Timeout for requests or form posts in seconds (Defaults 30 seconds).
417 * @type Number
418 */
419 timeout : 30,
420
421 /**
422 * True to process scripts by default (Defaults to false).
423 * @type Boolean
424 */
425 loadScripts : false,
426
427 /**
428 * Blank page URL to use with SSL file uploads (Defaults to 'javascript:false').
429 * @type String
430 */
431 sslBlankUrl : (YAHOO.ext.SSL_SECURE_URL || 'javascript:false'),
432 /**
433 * Whether to append unique parameter on get request to disable caching (Defaults to false).
434 * @type Boolean
435 */
436 disableCaching : false,
437 /**
438 * Whether to show indicatorText when loading (Defaults to true).
439 * @type Boolean
440 */
441 showLoadIndicator : true,
442 /**
443 * Text for loading indicator (Defaults to '&lt;div class="loading-indicator"&gt;Loading...&lt;/div&gt;').
444 * @type String
445 */
446 indicatorText : '<div class="loading-indicator">Loading...</div>'
447 };
448
449/**
450 * Static convenience method, Usage:
451 * <pre><code>YAHOO.ext.UpdateManager.updateElement('my-div', 'stuff.php');</code></pre>
452 * @param {String/HTMLElement/YAHOO.ext.Element} el The element to update
453 * @param {String} url The url
454 * @param {<i>String/Object</i>} params (optional) Url encoded param string or an object of name/value pairs
455 * @param {<i>Object</i>} options (optional) A config object with any of the UpdateManager properties you want to set - for example: {disableCaching:true, indicatorText: 'Loading data...'}
456 * @static
457 */
458YAHOO.ext.UpdateManager.updateElement = function(el, url, params, options){
459 var um = getEl(el, true).getUpdateManager();
460 YAHOO.ext.util.Config.apply(um, options);
461 um.update(url, params, options ? options.callback : null);
462};
463// alias for backwards compat
464YAHOO.ext.UpdateManager.update = YAHOO.ext.UpdateManager.updateElement;
465/**
466 * @class YAHOO.ext.UpdateManager.BasicRenderer
467 * Default Content renderer. Updates the elements innerHTML with the responseText.
468 */
469YAHOO.ext.UpdateManager.BasicRenderer = function(){};
470
471YAHOO.ext.UpdateManager.BasicRenderer.prototype = {
472 /**
473 * This is called when the transaction is completed and it's time to update the element - The BasicRenderer
474 * updates the elements innerHTML with the responseText - To perform a custom render (i.e. XML or JSON processing),
475 * create an object with a "render(el, response)" method and pass it to setRenderer on the UpdateManager.
476 * @param {YAHOO.ext.Element} el The element being rendered
477 * @param {Object} response The YUI Connect response object
478 * @param {UpdateManager} updateManager The calling update manager
479 * @param {Function} callback A callback that will need to be called if loadScripts is true on the UpdateManager
480 */
481 render : function(el, response, updateManager, callback){
482 el.update(response.responseText, updateManager.loadScripts, callback);
483 }
484};