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