GridKeyNav.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @class Ext.ux.GridKeyNav
  3. * Simple plugin to implement basic keyboard navigation for Ext 2.x grids that have a
  4. * paging toolbar (required). Adds: Page Down/Page Up/Right Arrow/Left Arrow/Home/End,
  5. * also adds functionality to the selection model to see if Down is pressed at the last record
  6. * of a page or Up is pressed at the first record of the page and attempts to move a page
  7. * in the proper direction.
  8. */
  9. Ext.ux.GridKeyNav = function() {}
  10. Ext.ux.GridKeyNav.prototype = {
  11. /**
  12. * @cfg {Boolean} bottomBar
  13. * Look for the paging toolbar within the grids bbar, set to true to pull from tbar (defaults to true)
  14. */
  15. bottomBar: true,
  16. /**
  17. * @cfg {Ext.PagingToolbar} toolbar
  18. * Manually pass in a toolbar instead of pulling from bbar/tbar of the grid (in case it is rendered elsewhere)
  19. */
  20. toolbar: null,
  21. init: function(grid) {
  22. this.grid = grid;
  23. this.grid.on({
  24. render: this.onRender,
  25. destroy: this.onDestroy,
  26. scope: this
  27. });
  28. },
  29. onRender : function() {
  30. this.nav = new Ext.KeyNav(this.grid.getGridEl(),{
  31. left: this.pagingNav.createDelegate(this,['prev']),
  32. right: this.pagingNav.createDelegate(this,['next']),
  33. pageDown: this.pagingNav.createDelegate(this,['next']),
  34. pageUp: this.pagingNav.createDelegate(this, ['prev']),
  35. home: this.pagingNav.createDelegate(this,['first']),
  36. end: this.pagingNav.createDelegate(this,['last'])
  37. });
  38. this.pt = this.toolbar||((this.bottomBar)?this.grid.getBottomToolbar():this.grid.getTopToolbar());
  39. this.sm = this.grid.getSelectionModel();
  40. this.sm.selectNext = this.sm.selectNext.createInterceptor(this.beforeNext, this);
  41. this.sm.selectPrevious = this.sm.selectPrevious.createInterceptor(this.beforePrevious, this);
  42. },
  43. onDestroy: function() {
  44. this.nav.disable();
  45. delete this.nav;
  46. },
  47. pagingNav: function(page) {
  48. if (!this.pt[page].disabled) {
  49. this.pt.onClick(this.pt[page]);
  50. if (page === 'last') {
  51. this.forceSelectLastRow();
  52. }
  53. } else {
  54. // Is all the data on a single page? If so let home/end work
  55. if (this.pt.pageSize >= this.grid.getStore().data.length) {
  56. if (page == 'first') {
  57. this.sm.selectFirstRow();
  58. } else if (page == 'last') {
  59. this.sm.selectLastRow();
  60. }
  61. }
  62. }
  63. },
  64. beforeNext: function() {
  65. if (!this.sm.hasNext()) {
  66. this.pagingNav('next');
  67. return false;
  68. }
  69. },
  70. beforePrevious: function() {
  71. if (!this.sm.hasPrevious()) {
  72. this.pagingNav('prev');
  73. this.forceSelectLastRow();
  74. return false;
  75. }
  76. },
  77. forceSelectLastRow: function() {
  78. this.grid.getStore().on('load', function() {
  79. this.sm.selectLastRow();
  80. }, this, {single: true});
  81. }
  82. }