Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @class Ext.ContentLoader
  3.  * @extends Ext.util.Observable
  4.  * A class used to load remote content to a component. Sample usage:
  5.  * <pre><code>
  6. new Ext.Component({
  7.     tpl: '{firstName} - {lastName}',
  8.     loader: {
  9.         url: 'myPage.php',
  10.         contentType: 'data',
  11.         params: {
  12.             userId: 1
  13.         }
  14.     }
  15. });
  16.  * </code></pre>
  17.  */
  18. Ext.ContentLoader = Ext.extend(Ext.util.Observable, {
  19.    
  20.     /**
  21.      * @cfg {String} url The url to retrieve the content from. Defaults to <tt>null</tt>.
  22.      */
  23.     url: null,
  24.    
  25.     /**
  26.      * @cfg {Object} params Any params to be attached to the Ajax request. Defaults to <tt>null</tt>.
  27.      */
  28.     params: null,
  29.    
  30.     /**
  31.      * @cfg {Ext.Component} target The target {@link Ext.Component} for the loader. Defaults to <tt>null</tt>.
  32.      */
  33.     target: null,
  34.    
  35.     /**
  36.      * @cfg {Mixed} loadMask True or a {@link Ext.LoadMask} configuration to enable masking during loading. Defaults to <tt>false</tt>.
  37.      */
  38.     loadMask: false,
  39.    
  40.     /**
  41.      * @cfg {Object} ajaxOptions Any additional options to be passed to the request, for example timeout or headers. Defaults to <tt>null</tt>.
  42.      */
  43.     ajaxOptions: null,
  44.    
  45.     /**
  46.      * @cfg {Function} success A function to be called when a load request is successful.
  47.      */
  48.    
  49.     /**
  50.      * @cfg {Function} failure A function to be called when a load request fails.
  51.      */
  52.    
  53.     /**
  54.      * @cfg {Object} scope The scope to execute the {@link #success} and {@link #failure} functions in.
  55.      */
  56.    
  57.     /**
  58.      * @cfg {String} renderer The type of content that is to be loaded into, which can be one of 3 types:
  59.      * <div class="mdetail-params"><ul>
  60.      * <li><b>html</b> : Loads raw html content, see {@link Ext.Component.html}</li>
  61.      * <li><b>data</b> : Loads raw html content, see {@link Ext.Component.data}</li>
  62.      * <li><b>component</b> : Loads child {Ext.Component} instances. This option is only valid when used with a Container.</li>
  63.      * </ul>
  64.      * Defaults to <tt>'html'</tt>
  65.      */
  66.     renderer: 'html',
  67.    
  68.     isLoader: true,
  69.    
  70.     constructor: function(config) {
  71.         config = config || {};
  72.         Ext.apply(this, config);
  73.         this.addEvents('');
  74.         Ext.ContentLoader.superclass.constructor.call(this, config);
  75.     },
  76.    
  77.     /**
  78.      * Set a {Ext.Component} as the target of this loader.
  79.      * @param {Ext.Component} target The component to be the target of this loader.
  80.      */
  81.     setTarget: function(target){
  82.         this.target = target;
  83.     },
  84.    
  85.     /**
  86.      * Aborts the active load request
  87.      */
  88.     abort: function(){
  89.         var active = this.active;
  90.         if (active !== undefined) {
  91.             Ext.Ajax.abort(active.request);
  92.             if (active.mask) {
  93.                 this.target.setLoading(false);
  94.             }
  95.             delete this.active;
  96.         }
  97.     },
  98.    
  99.     /**
  100.      * Load new data from the server.
  101.      * @param {Object} options
  102.      */
  103.     load: function(options) {
  104.         var me = this,
  105.             target = me.target,
  106.             mask = Ext.isDefined(options.loadMask) ? options.loadMask : me.loadMask,
  107.             ajaxOptions = Ext.apply({}, options.ajaxOptions),
  108.             request;
  109.        
  110.         options = Ext.apply({}, options);
  111.        
  112.         Ext.applyIf(ajaxOptions, me.ajaxOptions);
  113.        
  114.         Ext.applyIf(options, ajaxOptions);
  115.         Ext.applyIf(options, {
  116.             url: me.url,
  117.             params: me.params
  118.         });
  119.        
  120.         Ext.apply(options, {
  121.             scope: me,
  122.             callback: me.onComplete
  123.         });
  124.        
  125.         if (mask) {
  126.             me.target.setLoading(mask);
  127.         }
  128.        
  129.         request = Ext.Ajax.request(options);
  130.         this.active = {
  131.             request: request,
  132.             options: options,
  133.             mask: mask,
  134.             success: options.success || me.success,
  135.             failure: options.failure || me.failure,
  136.             callback: options.callback || me.callback,
  137.             scope: options.scope || me.scope,
  138.             renderer: options.renderer || me.renderer
  139.         };
  140.     },
  141.  
  142.     /**
  143.      * Parse the response after the request completes
  144.      * @private
  145.      * @param {Object} options Ajax options
  146.      * @param {Boolean} success Success status of the request
  147.      * @param {Object} response The response object
  148.      */
  149.     onComplete: function(options, success, response) {
  150.         var me = this,
  151.             active = me.active,
  152.             scope = active.scope,
  153.             renderer = this.getRenderer(active.renderer),
  154.             renderOptions = Ext.isObject(active.renderer) ? active.renderer : {};
  155.            
  156.            
  157.         if (success) {
  158.             success = renderer.render(this, response, renderOptions);        
  159.         }
  160.        
  161.         if (success) {
  162.             Ext.callback(active.success, scope, [me, response, options]);
  163.         } else {
  164.             Ext.callback(active.failure, scope, [me, response, options]);
  165.         }
  166.         Ext.callback(active.callback, scope, [me, options, success, response]);
  167.        
  168.         if (active.mask) {
  169.             this.target.setLoading(false);
  170.         }
  171.        
  172.         delete me.active;
  173.     },
  174.    
  175.     getRenderer: function(renderer){
  176.         var renderers = Ext.ContentLoader.Renderer;
  177.        
  178.         if (Ext.isObject(renderer)) {
  179.             renderer = renderer.type;
  180.         }
  181.        
  182.         switch (renderer) {
  183.             case 'component':
  184.                 return renderers.Component;
  185.             case 'data':
  186.                 return renderers.Data;
  187.             default:
  188.                 return renderers.HTML;
  189.         }
  190.     },
  191.    
  192.     /**
  193.      * Destroys the loader
  194.      */
  195.     destroy: function(){
  196.         this.abort();
  197.         this.purgeListeners();
  198.     }
  199. });
  200.  
  201. Ext.ContentLoader.Renderer = {
  202.     HTML: {
  203.         render: function(loader, response, options) {
  204.             loader.target.update(response.responseText);
  205.             return true;
  206.         }
  207.     },
  208.    
  209.     Data: {
  210.         render: function(loader, response, options) {
  211.             var success = true;
  212.             try {
  213.                 loader.update(Ext.decode(response.responseText));
  214.             } catch (e) {
  215.                 success = false;
  216.             }
  217.             return success;
  218.         }
  219.     },
  220.    
  221.     Component: {
  222.         render: function(loader, response, options) {
  223.             var success = true,
  224.                 target = loader.target,
  225.                 items = [];
  226.                
  227.             try {
  228.                 items = Ext.decode(response.responseText);
  229.             } catch (e) {
  230.                 success = false;
  231.             }
  232.            
  233.             if (success) {
  234.                 if (options.removeAll) {
  235.                     target.removeAll();
  236.                 }
  237.                 target.add(items);
  238.                 if (options.autoLayout) {
  239.                     target.doLayout();
  240.                 }
  241.             }
  242.            
  243.             return success;
  244.         }
  245.     }
  246. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement