/ext-4.0.7/examples/ux/grid/filter/ListFilter.js

https://bitbucket.org/srogerf/javascript · JavaScript · 192 lines · 33 code · 10 blank · 149 comment · 0 complexity · d45d9d0c7f7ebed74747a5cc21b7ab29 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. * @class Ext.ux.grid.filter.ListFilter
  11. * @extends Ext.ux.grid.filter.Filter
  12. * <p>List filters are able to be preloaded/backed by an Ext.data.Store to load
  13. * their options the first time they are shown. ListFilter utilizes the
  14. * {@link Ext.ux.grid.menu.ListMenu} component.</p>
  15. * <p>Although not shown here, this class accepts all configuration options
  16. * for {@link Ext.ux.grid.menu.ListMenu}.</p>
  17. *
  18. * <p><b><u>Example Usage:</u></b></p>
  19. * <pre><code>
  20. var filters = Ext.create('Ext.ux.grid.GridFilters', {
  21. ...
  22. filters: [{
  23. type: 'list',
  24. dataIndex: 'size',
  25. phpMode: true,
  26. // options will be used as data to implicitly creates an ArrayStore
  27. options: ['extra small', 'small', 'medium', 'large', 'extra large']
  28. }]
  29. });
  30. * </code></pre>
  31. *
  32. */
  33. Ext.define('Ext.ux.grid.filter.ListFilter', {
  34. extend: 'Ext.ux.grid.filter.Filter',
  35. alias: 'gridfilter.list',
  36. /**
  37. * @cfg {Array} options
  38. * <p><code>data</code> to be used to implicitly create a data store
  39. * to back this list when the data source is <b>local</b>. If the
  40. * data for the list is remote, use the <code>{@link #store}</code>
  41. * config instead.</p>
  42. * <br><p>Each item within the provided array may be in one of the
  43. * following formats:</p>
  44. * <div class="mdetail-params"><ul>
  45. * <li><b>Array</b> :
  46. * <pre><code>
  47. options: [
  48. [11, 'extra small'],
  49. [18, 'small'],
  50. [22, 'medium'],
  51. [35, 'large'],
  52. [44, 'extra large']
  53. ]
  54. * </code></pre>
  55. * </li>
  56. * <li><b>Object</b> :
  57. * <pre><code>
  58. labelField: 'name', // override default of 'text'
  59. options: [
  60. {id: 11, name:'extra small'},
  61. {id: 18, name:'small'},
  62. {id: 22, name:'medium'},
  63. {id: 35, name:'large'},
  64. {id: 44, name:'extra large'}
  65. ]
  66. * </code></pre>
  67. * </li>
  68. * <li><b>String</b> :
  69. * <pre><code>
  70. * options: ['extra small', 'small', 'medium', 'large', 'extra large']
  71. * </code></pre>
  72. * </li>
  73. */
  74. /**
  75. * @cfg {Boolean} phpMode
  76. * <p>Adjust the format of this filter. Defaults to false.</p>
  77. * <br><p>When GridFilters <code>@cfg encode = false</code> (default):</p>
  78. * <pre><code>
  79. // phpMode == false (default):
  80. filter[0][data][type] list
  81. filter[0][data][value] value1
  82. filter[0][data][value] value2
  83. filter[0][field] prod
  84. // phpMode == true:
  85. filter[0][data][type] list
  86. filter[0][data][value] value1, value2
  87. filter[0][field] prod
  88. * </code></pre>
  89. * When GridFilters <code>@cfg encode = true</code>:
  90. * <pre><code>
  91. // phpMode == false (default):
  92. filter : [{"type":"list","value":["small","medium"],"field":"size"}]
  93. // phpMode == true:
  94. filter : [{"type":"list","value":"small,medium","field":"size"}]
  95. * </code></pre>
  96. */
  97. phpMode : false,
  98. /**
  99. * @cfg {Ext.data.Store} store
  100. * The {@link Ext.data.Store} this list should use as its data source
  101. * when the data source is <b>remote</b>. If the data for the list
  102. * is local, use the <code>{@link #options}</code> config instead.
  103. */
  104. /**
  105. * @private
  106. * Template method that is to initialize the filter.
  107. * @param {Object} config
  108. */
  109. init : function (config) {
  110. this.dt = Ext.create('Ext.util.DelayedTask', this.fireUpdate, this);
  111. },
  112. /**
  113. * @private @override
  114. * Creates the Menu for this filter.
  115. * @param {Object} config Filter configuration
  116. * @return {Ext.menu.Menu}
  117. */
  118. createMenu: function(config) {
  119. var menu = Ext.create('Ext.ux.grid.menu.ListMenu', config);
  120. menu.on('checkchange', this.onCheckChange, this);
  121. return menu;
  122. },
  123. /**
  124. * @private
  125. * Template method that is to get and return the value of the filter.
  126. * @return {String} The value of this filter
  127. */
  128. getValue : function () {
  129. return this.menu.getSelected();
  130. },
  131. /**
  132. * @private
  133. * Template method that is to set the value of the filter.
  134. * @param {Object} value The value to set the filter
  135. */
  136. setValue : function (value) {
  137. this.menu.setSelected(value);
  138. this.fireEvent('update', this);
  139. },
  140. /**
  141. * @private
  142. * Template method that is to return <tt>true</tt> if the filter
  143. * has enough configuration information to be activated.
  144. * @return {Boolean}
  145. */
  146. isActivatable : function () {
  147. return this.getValue().length > 0;
  148. },
  149. /**
  150. * @private
  151. * Template method that is to get and return serialized filter data for
  152. * transmission to the server.
  153. * @return {Object/Array} An object or collection of objects containing
  154. * key value pairs representing the current configuration of the filter.
  155. */
  156. getSerialArgs : function () {
  157. return {type: 'list', value: this.phpMode ? this.getValue().join(',') : this.getValue()};
  158. },
  159. /** @private */
  160. onCheckChange : function(){
  161. this.dt.delay(this.updateBuffer);
  162. },
  163. /**
  164. * Template method that is to validate the provided Ext.data.Record
  165. * against the filters configuration.
  166. * @param {Ext.data.Record} record The record to validate
  167. * @return {Boolean} true if the record is valid within the bounds
  168. * of the filter, false otherwise.
  169. */
  170. validateRecord : function (record) {
  171. var valuesArray = this.getValue();
  172. return Ext.Array.indexOf(valuesArray, record.get(this.dataIndex)) > -1;
  173. }
  174. });