/ext-4.0.7/src/ElementLoader.js

https://bitbucket.org/srogerf/javascript · JavaScript · 414 lines · 170 code · 54 blank · 190 comment · 24 complexity · d889cf48aa581b817cebf4f9a8472da5 MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * A class used to load remote content to an Element. Sample usage:
  11. *
  12. * Ext.get('el').load({
  13. * url: 'myPage.php',
  14. * scripts: true,
  15. * params: {
  16. * id: 1
  17. * }
  18. * });
  19. *
  20. * In general this class will not be instanced directly, rather the {@link Ext.Element#load} method
  21. * will be used.
  22. */
  23. Ext.define('Ext.ElementLoader', {
  24. /* Begin Definitions */
  25. mixins: {
  26. observable: 'Ext.util.Observable'
  27. },
  28. uses: [
  29. 'Ext.data.Connection',
  30. 'Ext.Ajax'
  31. ],
  32. statics: {
  33. Renderer: {
  34. Html: function(loader, response, active){
  35. loader.getTarget().update(response.responseText, active.scripts === true);
  36. return true;
  37. }
  38. }
  39. },
  40. /* End Definitions */
  41. /**
  42. * @cfg {String} url
  43. * The url to retrieve the content from.
  44. */
  45. url: null,
  46. /**
  47. * @cfg {Object} params
  48. * Any params to be attached to the Ajax request. These parameters will
  49. * be overridden by any params in the load options.
  50. */
  51. params: null,
  52. /**
  53. * @cfg {Object} baseParams Params that will be attached to every request. These parameters
  54. * will not be overridden by any params in the load options.
  55. */
  56. baseParams: null,
  57. /**
  58. * @cfg {Boolean/Object} autoLoad
  59. * True to have the loader make a request as soon as it is created.
  60. * This argument can also be a set of options that will be passed to {@link #load} is called.
  61. */
  62. autoLoad: false,
  63. /**
  64. * @cfg {HTMLElement/Ext.Element/String} target
  65. * The target element for the loader. It can be the DOM element, the id or an {@link Ext.Element}.
  66. */
  67. target: null,
  68. /**
  69. * @cfg {Boolean/String} loadMask
  70. * True or a string to show when the element is loading.
  71. */
  72. loadMask: false,
  73. /**
  74. * @cfg {Object} ajaxOptions
  75. * Any additional options to be passed to the request, for example timeout or headers.
  76. */
  77. ajaxOptions: null,
  78. /**
  79. * @cfg {Boolean} scripts
  80. * True to parse any inline script tags in the response.
  81. */
  82. scripts: false,
  83. /**
  84. * @cfg {Function} success
  85. * A function to be called when a load request is successful.
  86. * Will be called with the following config parameters:
  87. *
  88. * - this - The ElementLoader instance.
  89. * - response - The response object.
  90. * - options - Ajax options.
  91. */
  92. /**
  93. * @cfg {Function} failure A function to be called when a load request fails.
  94. * Will be called with the following config parameters:
  95. *
  96. * - this - The ElementLoader instance.
  97. * - response - The response object.
  98. * - options - Ajax options.
  99. */
  100. /**
  101. * @cfg {Function} callback A function to be called when a load request finishes.
  102. * Will be called with the following config parameters:
  103. *
  104. * - this - The ElementLoader instance.
  105. * - success - True if successful request.
  106. * - response - The response object.
  107. * - options - Ajax options.
  108. */
  109. /**
  110. * @cfg {Object} scope
  111. * The scope to execute the {@link #success} and {@link #failure} functions in.
  112. */
  113. /**
  114. * @cfg {Function} renderer
  115. * A custom function to render the content to the element. The passed parameters are:
  116. *
  117. * - The loader
  118. * - The response
  119. * - The active request
  120. */
  121. isLoader: true,
  122. constructor: function(config) {
  123. var me = this,
  124. autoLoad;
  125. config = config || {};
  126. Ext.apply(me, config);
  127. me.setTarget(me.target);
  128. me.addEvents(
  129. /**
  130. * @event beforeload
  131. * Fires before a load request is made to the server.
  132. * Returning false from an event listener can prevent the load
  133. * from occurring.
  134. * @param {Ext.ElementLoader} this
  135. * @param {Object} options The options passed to the request
  136. */
  137. 'beforeload',
  138. /**
  139. * @event exception
  140. * Fires after an unsuccessful load.
  141. * @param {Ext.ElementLoader} this
  142. * @param {Object} response The response from the server
  143. * @param {Object} options The options passed to the request
  144. */
  145. 'exception',
  146. /**
  147. * @event load
  148. * Fires after a successful load.
  149. * @param {Ext.ElementLoader} this
  150. * @param {Object} response The response from the server
  151. * @param {Object} options The options passed to the request
  152. */
  153. 'load'
  154. );
  155. // don't pass config because we have already applied it.
  156. me.mixins.observable.constructor.call(me);
  157. if (me.autoLoad) {
  158. autoLoad = me.autoLoad;
  159. if (autoLoad === true) {
  160. autoLoad = {};
  161. }
  162. me.load(autoLoad);
  163. }
  164. },
  165. /**
  166. * Sets an {@link Ext.Element} as the target of this loader.
  167. * Note that if the target is changed, any active requests will be aborted.
  168. * @param {String/HTMLElement/Ext.Element} target The element or its ID.
  169. */
  170. setTarget: function(target){
  171. var me = this;
  172. target = Ext.get(target);
  173. if (me.target && me.target != target) {
  174. me.abort();
  175. }
  176. me.target = target;
  177. },
  178. /**
  179. * Returns the target of this loader.
  180. * @return {Ext.Component} The target or null if none exists.
  181. */
  182. getTarget: function(){
  183. return this.target || null;
  184. },
  185. /**
  186. * Aborts the active load request
  187. */
  188. abort: function(){
  189. var active = this.active;
  190. if (active !== undefined) {
  191. Ext.Ajax.abort(active.request);
  192. if (active.mask) {
  193. this.removeMask();
  194. }
  195. delete this.active;
  196. }
  197. },
  198. /**
  199. * Removes the mask on the target
  200. * @private
  201. */
  202. removeMask: function(){
  203. this.target.unmask();
  204. },
  205. /**
  206. * Adds the mask on the target
  207. * @private
  208. * @param {Boolean/Object} mask The mask configuration
  209. */
  210. addMask: function(mask){
  211. this.target.mask(mask === true ? null : mask);
  212. },
  213. /**
  214. * Loads new data from the server.
  215. * @param {Object} options The options for the request. They can be any configuration option that can be specified for
  216. * the class, with the exception of the target option. Note that any options passed to the method will override any
  217. * class defaults.
  218. */
  219. load: function(options) {
  220. //<debug>
  221. if (!this.target) {
  222. Ext.Error.raise('A valid target is required when loading content');
  223. }
  224. //</debug>
  225. options = Ext.apply({}, options);
  226. var me = this,
  227. target = me.target,
  228. mask = Ext.isDefined(options.loadMask) ? options.loadMask : me.loadMask,
  229. params = Ext.apply({}, options.params),
  230. ajaxOptions = Ext.apply({}, options.ajaxOptions),
  231. callback = options.callback || me.callback,
  232. scope = options.scope || me.scope || me,
  233. request;
  234. Ext.applyIf(ajaxOptions, me.ajaxOptions);
  235. Ext.applyIf(options, ajaxOptions);
  236. Ext.applyIf(params, me.params);
  237. Ext.apply(params, me.baseParams);
  238. Ext.applyIf(options, {
  239. url: me.url
  240. });
  241. //<debug>
  242. if (!options.url) {
  243. Ext.Error.raise('You must specify the URL from which content should be loaded');
  244. }
  245. //</debug>
  246. Ext.apply(options, {
  247. scope: me,
  248. params: params,
  249. callback: me.onComplete
  250. });
  251. if (me.fireEvent('beforeload', me, options) === false) {
  252. return;
  253. }
  254. if (mask) {
  255. me.addMask(mask);
  256. }
  257. request = Ext.Ajax.request(options);
  258. me.active = {
  259. request: request,
  260. options: options,
  261. mask: mask,
  262. scope: scope,
  263. callback: callback,
  264. success: options.success || me.success,
  265. failure: options.failure || me.failure,
  266. renderer: options.renderer || me.renderer,
  267. scripts: Ext.isDefined(options.scripts) ? options.scripts : me.scripts
  268. };
  269. me.setOptions(me.active, options);
  270. },
  271. /**
  272. * Sets any additional options on the active request
  273. * @private
  274. * @param {Object} active The active request
  275. * @param {Object} options The initial options
  276. */
  277. setOptions: Ext.emptyFn,
  278. /**
  279. * Parses the response after the request completes
  280. * @private
  281. * @param {Object} options Ajax options
  282. * @param {Boolean} success Success status of the request
  283. * @param {Object} response The response object
  284. */
  285. onComplete: function(options, success, response) {
  286. var me = this,
  287. active = me.active,
  288. scope = active.scope,
  289. renderer = me.getRenderer(active.renderer);
  290. if (success) {
  291. success = renderer.call(me, me, response, active);
  292. }
  293. if (success) {
  294. Ext.callback(active.success, scope, [me, response, options]);
  295. me.fireEvent('load', me, response, options);
  296. } else {
  297. Ext.callback(active.failure, scope, [me, response, options]);
  298. me.fireEvent('exception', me, response, options);
  299. }
  300. Ext.callback(active.callback, scope, [me, success, response, options]);
  301. if (active.mask) {
  302. me.removeMask();
  303. }
  304. delete me.active;
  305. },
  306. /**
  307. * Gets the renderer to use
  308. * @private
  309. * @param {String/Function} renderer The renderer to use
  310. * @return {Function} A rendering function to use.
  311. */
  312. getRenderer: function(renderer){
  313. if (Ext.isFunction(renderer)) {
  314. return renderer;
  315. }
  316. return this.statics().Renderer.Html;
  317. },
  318. /**
  319. * Automatically refreshes the content over a specified period.
  320. * @param {Number} interval The interval to refresh in ms.
  321. * @param {Object} options (optional) The options to pass to the load method. See {@link #load}
  322. */
  323. startAutoRefresh: function(interval, options){
  324. var me = this;
  325. me.stopAutoRefresh();
  326. me.autoRefresh = setInterval(function(){
  327. me.load(options);
  328. }, interval);
  329. },
  330. /**
  331. * Clears any auto refresh. See {@link #startAutoRefresh}.
  332. */
  333. stopAutoRefresh: function(){
  334. clearInterval(this.autoRefresh);
  335. delete this.autoRefresh;
  336. },
  337. /**
  338. * Checks whether the loader is automatically refreshing. See {@link #startAutoRefresh}.
  339. * @return {Boolean} True if the loader is automatically refreshing
  340. */
  341. isAutoRefreshing: function(){
  342. return Ext.isDefined(this.autoRefresh);
  343. },
  344. /**
  345. * Destroys the loader. Any active requests will be aborted.
  346. */
  347. destroy: function(){
  348. var me = this;
  349. me.stopAutoRefresh();
  350. delete me.target;
  351. me.abort();
  352. me.clearListeners();
  353. }
  354. });