_SelectionPreserver.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. define("dojox/grid/_SelectionPreserver", [
  2. "dojo/_base/declare",
  3. "dojo/_base/connect",
  4. "dojo/_base/lang",
  5. "dojo/_base/array"
  6. ], function(declare, connect, lang, array){
  7. return declare("dojox.grid._SelectionPreserver", null, {
  8. // summary:
  9. // Preserve selections across various user actions.
  10. //
  11. // description:
  12. // When this feature is turned on, Grid will try to preserve selections across actions, e.g. sorting, filtering etc.
  13. //
  14. // Precondition - Identifier(id) is required for store since id is the only way for differentiating row items.
  15. // Known issue - The preserved selections might be inaccurate if some unloaded rows are previously selected by range(e.g.SHIFT + click)
  16. //
  17. // example:
  18. // | //To turn on this - please set 'keepSelection' attribute to true
  19. // | <div dojoType="dojox.grid.DataGrid" keepSelection = true .../>
  20. // | <div dojoType="dojox.grid.TreeGrid" keepSelection = true .../>
  21. // | <div dojoType="dojox.grid.LazyTreeGrid" keepSelection = true .../>
  22. constructor: function(selection){
  23. this.selection = selection;
  24. var grid = this.grid = selection.grid;
  25. this.reset();
  26. this._connects = [
  27. connect.connect(grid, '_setStore', this, 'reset'),
  28. connect.connect(grid, '_addItem', this, '_reSelectById'),
  29. connect.connect(selection, 'addToSelection', lang.hitch(this, '_selectById', true)),
  30. connect.connect(selection, 'deselect', lang.hitch(this, '_selectById', false)),
  31. connect.connect(selection, 'deselectAll', this, 'reset')
  32. ];
  33. },
  34. destroy: function(){
  35. this.reset();
  36. array.forEach(this._connects, connect.disconnect);
  37. delete this._connects;
  38. },
  39. reset: function(){
  40. this._selectedById = {};
  41. },
  42. _reSelectById: function(item, index){
  43. // summary:
  44. // When some rows is fetched, determine whether it should be selected.
  45. if(item && this.grid._hasIdentity){
  46. this.selection.selected[index] = this._selectedById[this.grid.store.getIdentity(item)];
  47. }
  48. },
  49. _selectById: function(toSelect, inItemOrIndex){
  50. // summary:
  51. // Record selected rows by ID.
  52. if(this.selection.mode == 'none' || !this.grid._hasIdentity){ return; }
  53. var item = inItemOrIndex, g = this.grid;
  54. if(typeof inItemOrIndex == "number" || typeof inItemOrIndex == "string"){
  55. var entry = g._by_idx[inItemOrIndex];
  56. item = entry && entry.item;
  57. }
  58. if(item){
  59. this._selectedById[g.store.getIdentity(item)] = !!toSelect;
  60. }
  61. return item;
  62. }
  63. });
  64. });