CheckboxModel.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-selection-CheckboxModel'>/**
  19. </span> * A selection model that renders a column of checkboxes that can be toggled to
  20. * select or deselect rows. The default mode for this selection model is MULTI.
  21. *
  22. * The selection model will inject a header for the checkboxes in the first view
  23. * and according to the 'injectCheckbox' configuration.
  24. */
  25. Ext.define('Ext.selection.CheckboxModel', {
  26. alias: 'selection.checkboxmodel',
  27. extend: 'Ext.selection.RowModel',
  28. <span id='Ext-selection-CheckboxModel-cfg-mode'> /**
  29. </span> * @cfg {String} mode
  30. * Modes of selection.
  31. * Valid values are SINGLE, SIMPLE, and MULTI.
  32. */
  33. mode: 'MULTI',
  34. <span id='Ext-selection-CheckboxModel-cfg-injectCheckbox'> /**
  35. </span> * @cfg {Number/String} [injectCheckbox=0]
  36. * The index at which to insert the checkbox column.
  37. * Supported values are a numeric index, and the strings 'first' and 'last'.
  38. */
  39. injectCheckbox: 0,
  40. <span id='Ext-selection-CheckboxModel-cfg-checkOnly'> /**
  41. </span> * @cfg {Boolean} checkOnly
  42. * True if rows can only be selected by clicking on the checkbox column.
  43. */
  44. checkOnly: false,
  45. <span id='Ext-selection-CheckboxModel-cfg-showHeaderCheckbox'> /**
  46. </span> * @cfg {Boolean} showHeaderCheckbox
  47. * Configure as `false` to not display the header checkbox at the top of the column.
  48. */
  49. showHeaderCheckbox: true,
  50. headerWidth: 24,
  51. // private
  52. checkerOnCls: Ext.baseCSSPrefix + 'grid-hd-checker-on',
  53. // private
  54. refreshOnRemove: true,
  55. beforeViewRender: function(view) {
  56. var me = this;
  57. me.callParent(arguments);
  58. // if we have a locked header, only hook up to the first
  59. if (!me.hasLockedHeader() || view.headerCt.lockedCt) {
  60. if (me.showHeaderCheckbox !== false) {
  61. view.headerCt.on('headerclick', me.onHeaderClick, me);
  62. }
  63. me.addCheckbox(view, true);
  64. me.mon(view.ownerCt, 'reconfigure', me.onReconfigure, me);
  65. }
  66. },
  67. bindComponent: function(view) {
  68. var me = this;
  69. me.sortable = false;
  70. me.callParent(arguments);
  71. },
  72. hasLockedHeader: function(){
  73. var views = this.views,
  74. vLen = views.length,
  75. v;
  76. for (v = 0; v &lt; vLen; v++) {
  77. if (views[v].headerCt.lockedCt) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. },
  83. <span id='Ext-selection-CheckboxModel-method-addCheckbox'> /**
  84. </span> * Add the header checkbox to the header row
  85. * @private
  86. * @param {Boolean} initial True if we're binding for the first time.
  87. */
  88. addCheckbox: function(view, initial){
  89. var me = this,
  90. checkbox = me.injectCheckbox,
  91. headerCt = view.headerCt;
  92. // Preserve behaviour of false, but not clear why that would ever be done.
  93. if (checkbox !== false) {
  94. if (checkbox == 'first') {
  95. checkbox = 0;
  96. } else if (checkbox == 'last') {
  97. checkbox = headerCt.getColumnCount();
  98. }
  99. Ext.suspendLayouts();
  100. headerCt.add(checkbox, me.getHeaderConfig());
  101. Ext.resumeLayouts();
  102. }
  103. if (initial !== true) {
  104. view.refresh();
  105. }
  106. },
  107. <span id='Ext-selection-CheckboxModel-method-onReconfigure'> /**
  108. </span> * Handles the grid's reconfigure event. Adds the checkbox header if the columns have been reconfigured.
  109. * @private
  110. * @param {Ext.panel.Table} grid
  111. * @param {Ext.data.Store} store
  112. * @param {Object[]} columns
  113. */
  114. onReconfigure: function(grid, store, columns) {
  115. if(columns) {
  116. this.addCheckbox(this.views[0]);
  117. }
  118. },
  119. <span id='Ext-selection-CheckboxModel-method-toggleUiHeader'> /**
  120. </span> * Toggle the ui header between checked and unchecked state.
  121. * @param {Boolean} isChecked
  122. * @private
  123. */
  124. toggleUiHeader: function(isChecked) {
  125. var view = this.views[0],
  126. headerCt = view.headerCt,
  127. checkHd = headerCt.child('gridcolumn[isCheckerHd]');
  128. if (checkHd) {
  129. if (isChecked) {
  130. checkHd.el.addCls(this.checkerOnCls);
  131. } else {
  132. checkHd.el.removeCls(this.checkerOnCls);
  133. }
  134. }
  135. },
  136. <span id='Ext-selection-CheckboxModel-method-onHeaderClick'> /**
  137. </span> * Toggle between selecting all and deselecting all when clicking on
  138. * a checkbox header.
  139. */
  140. onHeaderClick: function(headerCt, header, e) {
  141. if (header.isCheckerHd) {
  142. e.stopEvent();
  143. var me = this,
  144. isChecked = header.el.hasCls(Ext.baseCSSPrefix + 'grid-hd-checker-on');
  145. // Prevent focus changes on the view, since we're selecting/deselecting all records
  146. me.preventFocus = true;
  147. if (isChecked) {
  148. me.deselectAll();
  149. } else {
  150. me.selectAll();
  151. }
  152. delete me.preventFocus;
  153. }
  154. },
  155. <span id='Ext-selection-CheckboxModel-method-getHeaderConfig'> /**
  156. </span> * Retrieve a configuration to be used in a HeaderContainer.
  157. * This should be used when injectCheckbox is set to false.
  158. */
  159. getHeaderConfig: function() {
  160. var me = this,
  161. showCheck = me.showHeaderCheckbox !== false;
  162. return {
  163. isCheckerHd: showCheck,
  164. text : '&amp;#160;',
  165. width: me.headerWidth,
  166. sortable: false,
  167. draggable: false,
  168. resizable: false,
  169. hideable: false,
  170. menuDisabled: true,
  171. dataIndex: '',
  172. cls: showCheck ? Ext.baseCSSPrefix + 'column-header-checkbox ' : '',
  173. renderer: Ext.Function.bind(me.renderer, me),
  174. editRenderer: me.editRenderer || me.renderEmpty,
  175. locked: me.hasLockedHeader()
  176. };
  177. },
  178. renderEmpty: function(){
  179. return '&amp;#160;';
  180. },
  181. <span id='Ext-selection-CheckboxModel-method-renderer'> /**
  182. </span> * Generates the HTML to be rendered in the injected checkbox column for each row.
  183. * Creates the standard checkbox markup by default; can be overridden to provide custom rendering.
  184. * See {@link Ext.grid.column.Column#renderer} for description of allowed parameters.
  185. */
  186. renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
  187. var baseCSSPrefix = Ext.baseCSSPrefix;
  188. metaData.tdCls = baseCSSPrefix + 'grid-cell-special ' + baseCSSPrefix + 'grid-cell-row-checker';
  189. return '&lt;div class=&quot;' + baseCSSPrefix + 'grid-row-checker&quot;&gt;&amp;#160;&lt;/div&gt;';
  190. },
  191. // override
  192. onRowMouseDown: function(view, record, item, index, e) {
  193. view.el.focus();
  194. var me = this,
  195. checker = e.getTarget('.' + Ext.baseCSSPrefix + 'grid-row-checker'),
  196. mode;
  197. if (!me.allowRightMouseSelection(e)) {
  198. return;
  199. }
  200. // checkOnly set, but we didn't click on a checker.
  201. if (me.checkOnly &amp;&amp; !checker) {
  202. return;
  203. }
  204. if (checker) {
  205. mode = me.getSelectionMode();
  206. // dont change the mode if its single otherwise
  207. // we would get multiple selection
  208. if (mode !== 'SINGLE') {
  209. me.setSelectionMode('SIMPLE');
  210. }
  211. me.selectWithEvent(record, e);
  212. me.setSelectionMode(mode);
  213. } else {
  214. me.selectWithEvent(record, e);
  215. }
  216. },
  217. <span id='Ext-selection-CheckboxModel-method-onSelectChange'> /**
  218. </span> * Synchronize header checker value as selection changes.
  219. * @private
  220. */
  221. onSelectChange: function() {
  222. var me = this;
  223. me.callParent(arguments);
  224. me.updateHeaderState();
  225. },
  226. <span id='Ext-selection-CheckboxModel-method-onStoreLoad'> /**
  227. </span> * @private
  228. */
  229. onStoreLoad: function() {
  230. var me = this;
  231. me.callParent(arguments);
  232. me.updateHeaderState();
  233. },
  234. <span id='Ext-selection-CheckboxModel-method-updateHeaderState'> /**
  235. </span> * @private
  236. */
  237. updateHeaderState: function() {
  238. // check to see if all records are selected
  239. var hdSelectStatus = this.selected.getCount() === this.store.getCount();
  240. this.toggleUiHeader(hdSelectStatus);
  241. }
  242. });
  243. </pre>
  244. </body>
  245. </html>