You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-grid-CellSelectionModel-method-constructor'><span id='Ext-grid-CellSelectionModel'>/**
  19. </span></span> * @class Ext.grid.CellSelectionModel
  20. * @extends Ext.grid.AbstractSelectionModel
  21. * This class provides the basic implementation for &lt;i&gt;single&lt;/i&gt; &lt;b&gt;cell&lt;/b&gt; selection in a grid.
  22. * The object stored as the selection contains the following properties:
  23. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  24. * &lt;li&gt;&lt;b&gt;cell&lt;/b&gt; : see {@link #getSelectedCell}
  25. * &lt;li&gt;&lt;b&gt;record&lt;/b&gt; : Ext.data.record The {@link Ext.data.Record Record}
  26. * which provides the data for the row containing the selection&lt;/li&gt;
  27. * &lt;/ul&gt;&lt;/div&gt;
  28. * @constructor
  29. * @param {Object} config The object containing the configuration of this model.
  30. */
  31. Ext.grid.CellSelectionModel = Ext.extend(Ext.grid.AbstractSelectionModel, {
  32. constructor : function(config){
  33. Ext.apply(this, config);
  34. this.selection = null;
  35. this.addEvents(
  36. <span id='Ext-grid-CellSelectionModel-event-beforecellselect'> /**
  37. </span> * @event beforecellselect
  38. * Fires before a cell is selected, return false to cancel the selection.
  39. * @param {SelectionModel} this
  40. * @param {Number} rowIndex The selected row index
  41. * @param {Number} colIndex The selected cell index
  42. */
  43. &quot;beforecellselect&quot;,
  44. <span id='Ext-grid-CellSelectionModel-event-cellselect'> /**
  45. </span> * @event cellselect
  46. * Fires when a cell is selected.
  47. * @param {SelectionModel} this
  48. * @param {Number} rowIndex The selected row index
  49. * @param {Number} colIndex The selected cell index
  50. */
  51. &quot;cellselect&quot;,
  52. <span id='Ext-grid-CellSelectionModel-event-selectionchange'> /**
  53. </span> * @event selectionchange
  54. * Fires when the active selection changes.
  55. * @param {SelectionModel} this
  56. * @param {Object} selection null for no selection or an object with two properties
  57. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  58. * &lt;li&gt;&lt;b&gt;cell&lt;/b&gt; : see {@link #getSelectedCell}
  59. * &lt;li&gt;&lt;b&gt;record&lt;/b&gt; : Ext.data.record&lt;p class=&quot;sub-desc&quot;&gt;The {@link Ext.data.Record Record}
  60. * which provides the data for the row containing the selection&lt;/p&gt;&lt;/li&gt;
  61. * &lt;/ul&gt;&lt;/div&gt;
  62. */
  63. &quot;selectionchange&quot;
  64. );
  65. Ext.grid.CellSelectionModel.superclass.constructor.call(this);
  66. },
  67. <span id='global-method-initEvents'> /** @ignore */
  68. </span> initEvents : function(){
  69. this.grid.on('cellmousedown', this.handleMouseDown, this);
  70. this.grid.on(Ext.EventManager.getKeyEvent(), this.handleKeyDown, this);
  71. this.grid.getView().on({
  72. scope: this,
  73. refresh: this.onViewChange,
  74. rowupdated: this.onRowUpdated,
  75. beforerowremoved: this.clearSelections,
  76. beforerowsinserted: this.clearSelections
  77. });
  78. if(this.grid.isEditor){
  79. this.grid.on('beforeedit', this.beforeEdit, this);
  80. }
  81. },
  82. <span id='Ext-grid-CellSelectionModel-method-beforeEdit'> //private
  83. </span> beforeEdit : function(e){
  84. this.select(e.row, e.column, false, true, e.record);
  85. },
  86. <span id='Ext-grid-CellSelectionModel-method-onRowUpdated'> //private
  87. </span> onRowUpdated : function(v, index, r){
  88. if(this.selection &amp;&amp; this.selection.record == r){
  89. v.onCellSelect(index, this.selection.cell[1]);
  90. }
  91. },
  92. <span id='Ext-grid-CellSelectionModel-method-onViewChange'> //private
  93. </span> onViewChange : function(){
  94. this.clearSelections(true);
  95. },
  96. <span id='Ext-grid-CellSelectionModel-method-getSelectedCell'> /**
  97. </span> * Returns an array containing the row and column indexes of the currently selected cell
  98. * (e.g., [0, 0]), or null if none selected. The array has elements:
  99. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  100. * &lt;li&gt;&lt;b&gt;rowIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The index of the selected row&lt;/p&gt;&lt;/li&gt;
  101. * &lt;li&gt;&lt;b&gt;cellIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The index of the selected cell.
  102. * Due to possible column reordering, the cellIndex should &lt;b&gt;not&lt;/b&gt; be used as an
  103. * index into the Record's data. Instead, use the cellIndex to determine the &lt;i&gt;name&lt;/i&gt;
  104. * of the selected cell and use the field name to retrieve the data value from the record:&lt;pre&gt;&lt;code&gt;
  105. // get name
  106. var fieldName = grid.getColumnModel().getDataIndex(cellIndex);
  107. // get data value based on name
  108. var data = record.get(fieldName);
  109. * &lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;&lt;/li&gt;
  110. * &lt;/ul&gt;&lt;/div&gt;
  111. * @return {Array} An array containing the row and column indexes of the selected cell, or null if none selected.
  112. */
  113. getSelectedCell : function(){
  114. return this.selection ? this.selection.cell : null;
  115. },
  116. <span id='Ext-grid-CellSelectionModel-method-clearSelections'> /**
  117. </span> * If anything is selected, clears all selections and fires the selectionchange event.
  118. * @param {Boolean} preventNotify &lt;tt&gt;true&lt;/tt&gt; to prevent the gridview from
  119. * being notified about the change.
  120. */
  121. clearSelections : function(preventNotify){
  122. var s = this.selection;
  123. if(s){
  124. if(preventNotify !== true){
  125. this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
  126. }
  127. this.selection = null;
  128. this.fireEvent(&quot;selectionchange&quot;, this, null);
  129. }
  130. },
  131. <span id='Ext-grid-CellSelectionModel-method-hasSelection'> /**
  132. </span> * Returns &lt;tt&gt;true&lt;/tt&gt; if there is a selection.
  133. * @return {Boolean}
  134. */
  135. hasSelection : function(){
  136. return this.selection ? true : false;
  137. },
  138. <span id='global-method-handleMouseDown'> /** @ignore */
  139. </span> handleMouseDown : function(g, row, cell, e){
  140. if(e.button !== 0 || this.isLocked()){
  141. return;
  142. }
  143. this.select(row, cell);
  144. },
  145. <span id='Ext-grid-CellSelectionModel-method-select'> /**
  146. </span> * Selects a cell. Before selecting a cell, fires the
  147. * {@link #beforecellselect} event. If this check is satisfied the cell
  148. * will be selected and followed up by firing the {@link #cellselect} and
  149. * {@link #selectionchange} events.
  150. * @param {Number} rowIndex The index of the row to select
  151. * @param {Number} colIndex The index of the column to select
  152. * @param {Boolean} preventViewNotify (optional) Specify &lt;tt&gt;true&lt;/tt&gt; to
  153. * prevent notifying the view (disables updating the selected appearance)
  154. * @param {Boolean} preventFocus (optional) Whether to prevent the cell at
  155. * the specified rowIndex / colIndex from being focused.
  156. * @param {Ext.data.Record} r (optional) The record to select
  157. */
  158. select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
  159. if(this.fireEvent(&quot;beforecellselect&quot;, this, rowIndex, colIndex) !== false){
  160. this.clearSelections();
  161. r = r || this.grid.store.getAt(rowIndex);
  162. this.selection = {
  163. record : r,
  164. cell : [rowIndex, colIndex]
  165. };
  166. if(!preventViewNotify){
  167. var v = this.grid.getView();
  168. v.onCellSelect(rowIndex, colIndex);
  169. if(preventFocus !== true){
  170. v.focusCell(rowIndex, colIndex);
  171. }
  172. }
  173. this.fireEvent(&quot;cellselect&quot;, this, rowIndex, colIndex);
  174. this.fireEvent(&quot;selectionchange&quot;, this, this.selection);
  175. }
  176. },
  177. <span id='Ext-grid-CellSelectionModel-method-isSelectable'> //private
  178. </span> isSelectable : function(rowIndex, colIndex, cm){
  179. return !cm.isHidden(colIndex);
  180. },
  181. <span id='Ext-grid-CellSelectionModel-method-onEditorKey'> // private
  182. </span> onEditorKey: function(field, e){
  183. if(e.getKey() == e.TAB){
  184. this.handleKeyDown(e);
  185. }
  186. },
  187. <span id='global-method-handleKeyDown'> /** @ignore */
  188. </span> handleKeyDown : function(e){
  189. if(!e.isNavKeyPress()){
  190. return;
  191. }
  192. var k = e.getKey(),
  193. g = this.grid,
  194. s = this.selection,
  195. sm = this,
  196. walk = function(row, col, step){
  197. return g.walkCells(
  198. row,
  199. col,
  200. step,
  201. g.isEditor &amp;&amp; g.editing ? sm.acceptsNav : sm.isSelectable, // *** handle tabbing while editorgrid is in edit mode
  202. sm
  203. );
  204. },
  205. cell, newCell, r, c, ae;
  206. switch(k){
  207. case e.ESC:
  208. case e.PAGE_UP:
  209. case e.PAGE_DOWN:
  210. // do nothing
  211. break;
  212. default:
  213. // *** call e.stopEvent() only for non ESC, PAGE UP/DOWN KEYS
  214. e.stopEvent();
  215. break;
  216. }
  217. if(!s){
  218. cell = walk(0, 0, 1); // *** use private walk() function defined above
  219. if(cell){
  220. this.select(cell[0], cell[1]);
  221. }
  222. return;
  223. }
  224. cell = s.cell; // currently selected cell
  225. r = cell[0]; // current row
  226. c = cell[1]; // current column
  227. switch(k){
  228. case e.TAB:
  229. if(e.shiftKey){
  230. newCell = walk(r, c - 1, -1);
  231. }else{
  232. newCell = walk(r, c + 1, 1);
  233. }
  234. break;
  235. case e.DOWN:
  236. newCell = walk(r + 1, c, 1);
  237. break;
  238. case e.UP:
  239. newCell = walk(r - 1, c, -1);
  240. break;
  241. case e.RIGHT:
  242. newCell = walk(r, c + 1, 1);
  243. break;
  244. case e.LEFT:
  245. newCell = walk(r, c - 1, -1);
  246. break;
  247. case e.ENTER:
  248. if (g.isEditor &amp;&amp; !g.editing) {
  249. g.startEditing(r, c);
  250. return;
  251. }
  252. break;
  253. }
  254. if(newCell){
  255. // *** reassign r &amp; c variables to newly-selected cell's row and column
  256. r = newCell[0];
  257. c = newCell[1];
  258. this.select(r, c); // *** highlight newly-selected cell and update selection
  259. if(g.isEditor &amp;&amp; g.editing){ // *** handle tabbing while editorgrid is in edit mode
  260. ae = g.activeEditor;
  261. if(ae &amp;&amp; ae.field.triggerBlur){
  262. // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
  263. ae.field.triggerBlur();
  264. }
  265. g.startEditing(r, c);
  266. }
  267. }
  268. },
  269. <span id='Ext-grid-CellSelectionModel-method-acceptsNav'> acceptsNav : function(row, col, cm){
  270. </span> return !cm.isHidden(col) &amp;&amp; cm.isCellEditable(col, row);
  271. }
  272. });</pre>
  273. </body>
  274. </html>