/hippo/src/main/webapp/ext/src/widgets/grid/RowSelectionModel.js

http://hdbc.googlecode.com/ · JavaScript · 530 lines · 314 code · 39 blank · 177 comment · 85 complexity · 897aea7cff276056ee82780fbedc5b48 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.grid.RowSelectionModel
  9. * @extends Ext.grid.AbstractSelectionModel
  10. * The default SelectionModel used by {@link Ext.grid.GridPanel}.
  11. * It supports multiple selections and keyboard selection/navigation. The objects stored
  12. * as selections and returned by {@link #getSelected}, and {@link #getSelections} are
  13. * the {@link Ext.data.Record Record}s which provide the data for the selected rows.
  14. * @constructor
  15. * @param {Object} config
  16. */
  17. Ext.grid.RowSelectionModel = function(config){
  18. Ext.apply(this, config);
  19. this.selections = new Ext.util.MixedCollection(false, function(o){
  20. return o.id;
  21. });
  22. this.last = false;
  23. this.lastActive = false;
  24. this.addEvents(
  25. /**
  26. * @event selectionchange
  27. * Fires when the selection changes
  28. * @param {SelectionModel} this
  29. */
  30. "selectionchange",
  31. /**
  32. * @event beforerowselect
  33. * Fires before a row is selected, return false to cancel the selection.
  34. * @param {SelectionModel} this
  35. * @param {Number} rowIndex The index to be selected
  36. * @param {Boolean} keepExisting False if other selections will be cleared
  37. * @param {Record} record The record to be selected
  38. */
  39. "beforerowselect",
  40. /**
  41. * @event rowselect
  42. * Fires when a row is selected.
  43. * @param {SelectionModel} this
  44. * @param {Number} rowIndex The selected index
  45. * @param {Ext.data.Record} r The selected record
  46. */
  47. "rowselect",
  48. /**
  49. * @event rowdeselect
  50. * Fires when a row is deselected. To prevent deselection
  51. * {@link Ext.grid.AbstractSelectionModel#lock lock the selections}.
  52. * @param {SelectionModel} this
  53. * @param {Number} rowIndex
  54. * @param {Record} record
  55. */
  56. "rowdeselect"
  57. );
  58. Ext.grid.RowSelectionModel.superclass.constructor.call(this);
  59. };
  60. Ext.extend(Ext.grid.RowSelectionModel, Ext.grid.AbstractSelectionModel, {
  61. /**
  62. * @cfg {Boolean} singleSelect
  63. * <tt>true</tt> to allow selection of only one row at a time (defaults to <tt>false</tt>
  64. * allowing multiple selections)
  65. */
  66. singleSelect : false,
  67. /**
  68. * @cfg {Boolean} moveEditorOnEnter
  69. * <tt>false</tt> to turn off moving the editor to the next row down when the enter key is pressed
  70. * or the next row up when shift + enter keys are pressed.
  71. */
  72. // private
  73. initEvents : function(){
  74. if(!this.grid.enableDragDrop && !this.grid.enableDrag){
  75. this.grid.on("rowmousedown", this.handleMouseDown, this);
  76. }else{ // allow click to work like normal
  77. this.grid.on("rowclick", function(grid, rowIndex, e) {
  78. if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
  79. this.selectRow(rowIndex, false);
  80. grid.view.focusRow(rowIndex);
  81. }
  82. }, this);
  83. }
  84. this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
  85. "up" : function(e){
  86. if(!e.shiftKey || this.singleSelect){
  87. this.selectPrevious(false);
  88. }else if(this.last !== false && this.lastActive !== false){
  89. var last = this.last;
  90. this.selectRange(this.last, this.lastActive-1);
  91. this.grid.getView().focusRow(this.lastActive);
  92. if(last !== false){
  93. this.last = last;
  94. }
  95. }else{
  96. this.selectFirstRow();
  97. }
  98. },
  99. "down" : function(e){
  100. if(!e.shiftKey || this.singleSelect){
  101. this.selectNext(false);
  102. }else if(this.last !== false && this.lastActive !== false){
  103. var last = this.last;
  104. this.selectRange(this.last, this.lastActive+1);
  105. this.grid.getView().focusRow(this.lastActive);
  106. if(last !== false){
  107. this.last = last;
  108. }
  109. }else{
  110. this.selectFirstRow();
  111. }
  112. },
  113. scope: this
  114. });
  115. var view = this.grid.view;
  116. view.on("refresh", this.onRefresh, this);
  117. view.on("rowupdated", this.onRowUpdated, this);
  118. view.on("rowremoved", this.onRemove, this);
  119. },
  120. // private
  121. onRefresh : function(){
  122. var ds = this.grid.store, index;
  123. var s = this.getSelections();
  124. this.clearSelections(true);
  125. for(var i = 0, len = s.length; i < len; i++){
  126. var r = s[i];
  127. if((index = ds.indexOfId(r.id)) != -1){
  128. this.selectRow(index, true);
  129. }
  130. }
  131. if(s.length != this.selections.getCount()){
  132. this.fireEvent("selectionchange", this);
  133. }
  134. },
  135. // private
  136. onRemove : function(v, index, r){
  137. if(this.selections.remove(r) !== false){
  138. this.fireEvent('selectionchange', this);
  139. }
  140. },
  141. // private
  142. onRowUpdated : function(v, index, r){
  143. if(this.isSelected(r)){
  144. v.onRowSelect(index);
  145. }
  146. },
  147. /**
  148. * Select records.
  149. * @param {Array} records The records to select
  150. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
  151. */
  152. selectRecords : function(records, keepExisting){
  153. if(!keepExisting){
  154. this.clearSelections();
  155. }
  156. var ds = this.grid.store;
  157. for(var i = 0, len = records.length; i < len; i++){
  158. this.selectRow(ds.indexOf(records[i]), true);
  159. }
  160. },
  161. /**
  162. * Gets the number of selected rows.
  163. * @return {Number}
  164. */
  165. getCount : function(){
  166. return this.selections.length;
  167. },
  168. /**
  169. * Selects the first row in the grid.
  170. */
  171. selectFirstRow : function(){
  172. this.selectRow(0);
  173. },
  174. /**
  175. * Select the last row.
  176. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
  177. */
  178. selectLastRow : function(keepExisting){
  179. this.selectRow(this.grid.store.getCount() - 1, keepExisting);
  180. },
  181. /**
  182. * Selects the row immediately following the last selected row.
  183. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
  184. * @return {Boolean} <tt>true</tt> if there is a next row, else <tt>false</tt>
  185. */
  186. selectNext : function(keepExisting){
  187. if(this.hasNext()){
  188. this.selectRow(this.last+1, keepExisting);
  189. this.grid.getView().focusRow(this.last);
  190. return true;
  191. }
  192. return false;
  193. },
  194. /**
  195. * Selects the row that precedes the last selected row.
  196. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
  197. * @return {Boolean} <tt>true</tt> if there is a previous row, else <tt>false</tt>
  198. */
  199. selectPrevious : function(keepExisting){
  200. if(this.hasPrevious()){
  201. this.selectRow(this.last-1, keepExisting);
  202. this.grid.getView().focusRow(this.last);
  203. return true;
  204. }
  205. return false;
  206. },
  207. /**
  208. * Returns true if there is a next record to select
  209. * @return {Boolean}
  210. */
  211. hasNext : function(){
  212. return this.last !== false && (this.last+1) < this.grid.store.getCount();
  213. },
  214. /**
  215. * Returns true if there is a previous record to select
  216. * @return {Boolean}
  217. */
  218. hasPrevious : function(){
  219. return !!this.last;
  220. },
  221. /**
  222. * Returns the selected records
  223. * @return {Array} Array of selected records
  224. */
  225. getSelections : function(){
  226. return [].concat(this.selections.items);
  227. },
  228. /**
  229. * Returns the first selected record.
  230. * @return {Record}
  231. */
  232. getSelected : function(){
  233. return this.selections.itemAt(0);
  234. },
  235. /**
  236. * Calls the passed function with each selection. If the function returns
  237. * <tt>false</tt>, iteration is stopped and this function returns
  238. * <tt>false</tt>. Otherwise it returns <tt>true</tt>.
  239. * @param {Function} fn
  240. * @param {Object} scope (optional)
  241. * @return {Boolean} true if all selections were iterated
  242. */
  243. each : function(fn, scope){
  244. var s = this.getSelections();
  245. for(var i = 0, len = s.length; i < len; i++){
  246. if(fn.call(scope || this, s[i], i) === false){
  247. return false;
  248. }
  249. }
  250. return true;
  251. },
  252. /**
  253. * Clears all selections if the selection model
  254. * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
  255. * @param {Boolean} fast (optional) <tt>true</tt> to bypass the
  256. * conditional checks and events described in {@link #deselectRow}.
  257. */
  258. clearSelections : function(fast){
  259. if(this.isLocked()){
  260. return;
  261. }
  262. if(fast !== true){
  263. var ds = this.grid.store;
  264. var s = this.selections;
  265. s.each(function(r){
  266. this.deselectRow(ds.indexOfId(r.id));
  267. }, this);
  268. s.clear();
  269. }else{
  270. this.selections.clear();
  271. }
  272. this.last = false;
  273. },
  274. /**
  275. * Selects all rows if the selection model
  276. * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
  277. */
  278. selectAll : function(){
  279. if(this.isLocked()){
  280. return;
  281. }
  282. this.selections.clear();
  283. for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
  284. this.selectRow(i, true);
  285. }
  286. },
  287. /**
  288. * Returns <tt>true</tt> if there is a selection.
  289. * @return {Boolean}
  290. */
  291. hasSelection : function(){
  292. return this.selections.length > 0;
  293. },
  294. /**
  295. * Returns <tt>true</tt> if the specified row is selected.
  296. * @param {Number/Record} index The record or index of the record to check
  297. * @return {Boolean}
  298. */
  299. isSelected : function(index){
  300. var r = typeof index == "number" ? this.grid.store.getAt(index) : index;
  301. return (r && this.selections.key(r.id) ? true : false);
  302. },
  303. /**
  304. * Returns <tt>true</tt> if the specified record id is selected.
  305. * @param {String} id The id of record to check
  306. * @return {Boolean}
  307. */
  308. isIdSelected : function(id){
  309. return (this.selections.key(id) ? true : false);
  310. },
  311. // private
  312. handleMouseDown : function(g, rowIndex, e){
  313. if(e.button !== 0 || this.isLocked()){
  314. return;
  315. }
  316. var view = this.grid.getView();
  317. if(e.shiftKey && !this.singleSelect && this.last !== false){
  318. var last = this.last;
  319. this.selectRange(last, rowIndex, e.ctrlKey);
  320. this.last = last; // reset the last
  321. view.focusRow(rowIndex);
  322. }else{
  323. var isSelected = this.isSelected(rowIndex);
  324. if(e.ctrlKey && isSelected){
  325. this.deselectRow(rowIndex);
  326. }else if(!isSelected || this.getCount() > 1){
  327. this.selectRow(rowIndex, e.ctrlKey || e.shiftKey);
  328. view.focusRow(rowIndex);
  329. }
  330. }
  331. },
  332. /**
  333. * Selects multiple rows.
  334. * @param {Array} rows Array of the indexes of the row to select
  335. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep
  336. * existing selections (defaults to <tt>false</tt>)
  337. */
  338. selectRows : function(rows, keepExisting){
  339. if(!keepExisting){
  340. this.clearSelections();
  341. }
  342. for(var i = 0, len = rows.length; i < len; i++){
  343. this.selectRow(rows[i], true);
  344. }
  345. },
  346. /**
  347. * Selects a range of rows if the selection model
  348. * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
  349. * All rows in between startRow and endRow are also selected.
  350. * @param {Number} startRow The index of the first row in the range
  351. * @param {Number} endRow The index of the last row in the range
  352. * @param {Boolean} keepExisting (optional) True to retain existing selections
  353. */
  354. selectRange : function(startRow, endRow, keepExisting){
  355. var i;
  356. if(this.isLocked()){
  357. return;
  358. }
  359. if(!keepExisting){
  360. this.clearSelections();
  361. }
  362. if(startRow <= endRow){
  363. for(i = startRow; i <= endRow; i++){
  364. this.selectRow(i, true);
  365. }
  366. }else{
  367. for(i = startRow; i >= endRow; i--){
  368. this.selectRow(i, true);
  369. }
  370. }
  371. },
  372. /**
  373. * Deselects a range of rows if the selection model
  374. * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
  375. * All rows in between startRow and endRow are also deselected.
  376. * @param {Number} startRow The index of the first row in the range
  377. * @param {Number} endRow The index of the last row in the range
  378. */
  379. deselectRange : function(startRow, endRow, preventViewNotify){
  380. if(this.isLocked()){
  381. return;
  382. }
  383. for(var i = startRow; i <= endRow; i++){
  384. this.deselectRow(i, preventViewNotify);
  385. }
  386. },
  387. /**
  388. * Selects a row. Before selecting a row, checks if the selection model
  389. * {@link Ext.grid.AbstractSelectionModel#isLocked is locked} and fires the
  390. * {@link #beforerowselect} event. If these checks are satisfied the row
  391. * will be selected and followed up by firing the {@link #rowselect} and
  392. * {@link #selectionchange} events.
  393. * @param {Number} row The index of the row to select
  394. * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
  395. * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
  396. * prevent notifying the view (disables updating the selected appearance)
  397. */
  398. selectRow : function(index, keepExisting, preventViewNotify){
  399. if(this.isLocked() || (index < 0 || index >= this.grid.store.getCount()) || (keepExisting && this.isSelected(index))){
  400. return;
  401. }
  402. var r = this.grid.store.getAt(index);
  403. if(r && this.fireEvent("beforerowselect", this, index, keepExisting, r) !== false){
  404. if(!keepExisting || this.singleSelect){
  405. this.clearSelections();
  406. }
  407. this.selections.add(r);
  408. this.last = this.lastActive = index;
  409. if(!preventViewNotify){
  410. this.grid.getView().onRowSelect(index);
  411. }
  412. this.fireEvent("rowselect", this, index, r);
  413. this.fireEvent("selectionchange", this);
  414. }
  415. },
  416. /**
  417. * Deselects a row. Before deselecting a row, checks if the selection model
  418. * {@link Ext.grid.AbstractSelectionModel#isLocked is locked}.
  419. * If this check is satisfied the row will be deselected and followed up by
  420. * firing the {@link #rowdeselect} and {@link #selectionchange} events.
  421. * @param {Number} row The index of the row to deselect
  422. * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
  423. * prevent notifying the view (disables updating the selected appearance)
  424. */
  425. deselectRow : function(index, preventViewNotify){
  426. if(this.isLocked()){
  427. return;
  428. }
  429. if(this.last == index){
  430. this.last = false;
  431. }
  432. if(this.lastActive == index){
  433. this.lastActive = false;
  434. }
  435. var r = this.grid.store.getAt(index);
  436. if(r){
  437. this.selections.remove(r);
  438. if(!preventViewNotify){
  439. this.grid.getView().onRowDeselect(index);
  440. }
  441. this.fireEvent("rowdeselect", this, index, r);
  442. this.fireEvent("selectionchange", this);
  443. }
  444. },
  445. // private
  446. restoreLast : function(){
  447. if(this._last){
  448. this.last = this._last;
  449. }
  450. },
  451. // private
  452. acceptsNav : function(row, col, cm){
  453. return !cm.isHidden(col) && cm.isCellEditable(col, row);
  454. },
  455. // private
  456. onEditorKey : function(field, e){
  457. var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
  458. var shift = e.shiftKey;
  459. if(k == e.TAB){
  460. e.stopEvent();
  461. ed.completeEdit();
  462. if(shift){
  463. newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
  464. }else{
  465. newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
  466. }
  467. }else if(k == e.ENTER){
  468. e.stopEvent();
  469. ed.completeEdit();
  470. if(this.moveEditorOnEnter !== false){
  471. if(shift){
  472. newCell = g.walkCells(ed.row - 1, ed.col, -1, this.acceptsNav, this);
  473. }else{
  474. newCell = g.walkCells(ed.row + 1, ed.col, 1, this.acceptsNav, this);
  475. }
  476. }
  477. }else if(k == e.ESC){
  478. ed.cancelEdit();
  479. }
  480. if(newCell){
  481. g.startEditing(newCell[0], newCell[1]);
  482. }
  483. },
  484. destroy: function(){
  485. if(this.rowNav){
  486. this.rowNav.disable();
  487. this.rowNav = null;
  488. }
  489. Ext.grid.RowSelectionModel.superclass.destroy.call(this);
  490. }
  491. });