_SelectionPreserver.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. define("dojox/grid/enhanced/plugins/_SelectionPreserver", [
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dojo/_base/connect",
  5. '../../_SelectionPreserver'
  6. ], function(declare, lang, connect, _SelectionPreserver){
  7. return declare("dojox.grid.enhanced.plugins._SelectionPreserver", _SelectionPreserver, {
  8. // summary:
  9. // Preserve selections across various user actions.
  10. //
  11. // description:
  12. // Extends dojox.grid._SelectionPreserver adding a bit more support to make selection persistence working well
  13. // with various EnhancedGrid features, e.g. filtering, nested sorting, pagination, select all etc.
  14. //
  15. // Precondition - Identifier(id) is required for store, as id is used for differentiating row items.
  16. // Known issue - The preserved selections might be inaccurate if some unloaded rows are previously selected by range(e.g.SHIFT + click)
  17. //
  18. // example:
  19. // | //To turn on this - set 'keepSelection' attribute to true
  20. // | <div dojoType="dojox.grid.EnhancedGrid" keepSelection = true .../>
  21. constructor: function(selection){
  22. var grid = this.grid;
  23. grid.onSelectedById = this.onSelectedById;
  24. this._oldClearData = grid._clearData;
  25. var self = this;
  26. grid._clearData = function(){
  27. self._updateMapping(!grid._noInternalMapping);
  28. self._trustSelection = [];
  29. self._oldClearData.apply(grid, arguments);
  30. };
  31. this._connects.push(
  32. connect.connect(selection, 'selectRange', lang.hitch(this, '_updateMapping', true, true, false)),
  33. connect.connect(selection, 'deselectRange', lang.hitch(this, '_updateMapping', true, false, false)),
  34. connect.connect(selection, 'deselectAll', lang.hitch(this, '_updateMapping', true, false, true))
  35. );
  36. },
  37. destroy: function(){
  38. this.inherited(arguments);
  39. this.grid._clearData = this._oldClearData;
  40. },
  41. reset: function(){
  42. this.inherited(arguments);
  43. this._idMap = [];
  44. this._trustSelection = [];
  45. this._defaultSelected = false;
  46. },
  47. _reSelectById: function(item, index){
  48. // summary:
  49. // Overwritten
  50. var s = this.selection, g = this.grid;
  51. if(item && g._hasIdentity){
  52. var id = g.store.getIdentity(item);
  53. if(this._selectedById[id] === undefined){
  54. if(!this._trustSelection[index]){
  55. s.selected[index] = this._defaultSelected;
  56. }
  57. }else{
  58. s.selected[index] = this._selectedById[id];
  59. }
  60. this._idMap.push(id);
  61. g.onSelectedById(id, index, s.selected[index]);
  62. }
  63. },
  64. _selectById: function(toSelect, inItemOrIndex){
  65. // summary:
  66. // Overwritten
  67. if(!this.inherited(arguments)){
  68. this._trustSelection[inItemOrIndex] = true;
  69. }
  70. },
  71. onSelectedById: function(id, rowIndex, value){},
  72. _updateMapping: function(trustSelection, isSelect, isForAll, from, to){
  73. // summary:
  74. // This function try to keep the selection info updated when range selection is performed.
  75. // 1. Calculate how many unloaded rows are there;
  76. // 2. update _selectedById data if grid.selection._selected can be trusted, so loaded but unselected rows can
  77. // be properly recorded.
  78. var s = this.selection, g = this.grid, flag = 0, unloaded = 0, i, id;
  79. for(i = g.rowCount - 1; i >= 0; --i){
  80. if(!g._by_idx[i]){
  81. ++unloaded;
  82. flag += s.selected[i] ? 1 : -1;
  83. }else{
  84. id = g._by_idx[i].idty;
  85. if(id && (trustSelection || this._selectedById[id] === undefined)){
  86. this._selectedById[id] = !!s.selected[i];
  87. }
  88. }
  89. }
  90. if(unloaded){
  91. this._defaultSelected = flag > 0;
  92. }
  93. if(!isForAll && from !== undefined && to !== undefined){
  94. isForAll = !g.usingPagination && Math.abs(to - from + 1) === g.rowCount;
  95. }
  96. // When deselectAll, make sure every thing is deselected, even if it was selected but not loaded now.
  97. // This occurs only when pagination's "All" is used.
  98. if(isForAll && (!g.usingPagination || g.selectionMode === 'single')){
  99. for(i = this._idMap.length - 1; i >= 0; --i){
  100. this._selectedById[this._idMap[i]] = isSelect;
  101. }
  102. }
  103. }
  104. });
  105. });