source: Dev/trunk/src/client/dojox/grid/Selection.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.4 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/array",
4        "dojo/_base/lang",
5        "dojo/dom-attr"
6], function(declare, array, lang, domAttr){
7
8return declare("dojox.grid.Selection", null, {
9        // summary:
10        //              Manages row selection for grid. Owned by grid and used internally
11        //              for selection. Override to implement custom selection.
12
13        constructor: function(inGrid){
14                this.grid = inGrid;
15                this.selected = [];
16
17                this.setMode(inGrid.selectionMode);
18        },
19
20        mode: 'extended',
21
22        selected: null,
23        updating: 0,
24        selectedIndex: -1,
25        rangeStartIndex: -1,
26
27        setMode: function(mode){
28                if(this.selected.length){
29                        this.deselectAll();
30                }
31                if(mode != 'extended' && mode != 'multiple' && mode != 'single' && mode != 'none'){
32                        this.mode = 'extended';
33                }else{
34                        this.mode = mode;
35                }
36        },
37
38        onCanSelect: function(inIndex){
39                return this.grid.onCanSelect(inIndex);
40        },
41
42        onCanDeselect: function(inIndex){
43                return this.grid.onCanDeselect(inIndex);
44        },
45
46        onSelected: function(inIndex){
47        },
48
49        onDeselected: function(inIndex){
50        },
51
52        //onSetSelected: function(inIndex, inSelect) { };
53        onChanging: function(){
54        },
55
56        onChanged: function(){
57        },
58
59        isSelected: function(inIndex){
60                if(this.mode == 'none'){
61                        return false;
62                }
63                return this.selected[inIndex];
64        },
65
66        getFirstSelected: function(){
67                if(!this.selected.length||this.mode == 'none'){ return -1; }
68                for(var i=0, l=this.selected.length; i<l; i++){
69                        if(this.selected[i]){
70                                return i;
71                        }
72                }
73                return -1;
74        },
75
76        getNextSelected: function(inPrev){
77                if(this.mode == 'none'){ return -1; }
78                for(var i=inPrev+1, l=this.selected.length; i<l; i++){
79                        if(this.selected[i]){
80                                return i;
81                        }
82                }
83                return -1;
84        },
85
86        getSelected: function(){
87                var result = [];
88                for(var i=0, l=this.selected.length; i<l; i++){
89                        if(this.selected[i]){
90                                result.push(i);
91                        }
92                }
93                return result;
94        },
95
96        getSelectedCount: function(){
97                var c = 0;
98                for(var i=0; i<this.selected.length; i++){
99                        if(this.selected[i]){
100                                c++;
101                        }
102                }
103                return c;
104        },
105
106        _beginUpdate: function(){
107                if(this.updating === 0){
108                        this.onChanging();
109                }
110                this.updating++;
111        },
112
113        _endUpdate: function(){
114                this.updating--;
115                if(this.updating === 0){
116                        this.onChanged();
117                }
118        },
119
120        select: function(inIndex){
121                if(this.mode == 'none'){ return; }
122                if(this.mode != 'multiple'){
123                        this.deselectAll(inIndex);
124                        this.addToSelection(inIndex);
125                }else{
126                        this.toggleSelect(inIndex);
127                }
128        },
129
130        addToSelection: function(inIndex){
131                if(this.mode == 'none'){ return; }
132                if(lang.isArray(inIndex)){
133                        array.forEach(inIndex, this.addToSelection, this);
134                        return;
135                }
136                inIndex = Number(inIndex);
137                if(this.selected[inIndex]){
138                        this.selectedIndex = inIndex;
139                }else{
140                        if(this.onCanSelect(inIndex) !== false){
141                                this.selectedIndex = inIndex;
142                                var rowNode = this.grid.getRowNode(inIndex);
143                                if(rowNode){
144                                        domAttr.set(rowNode, "aria-selected", "true");
145                                }
146                                this._beginUpdate();
147                                this.selected[inIndex] = true;
148                                //this.grid.onSelected(inIndex);
149                                this.onSelected(inIndex);
150                                //this.onSetSelected(inIndex, true);
151                                this._endUpdate();
152                        }
153                }
154        },
155
156        deselect: function(inIndex){
157                if(this.mode == 'none'){ return; }
158                if(lang.isArray(inIndex)){
159                        array.forEach(inIndex, this.deselect, this);
160                        return;
161                }
162                inIndex = Number(inIndex);
163                if(this.selectedIndex == inIndex){
164                        this.selectedIndex = -1;
165                }
166                if(this.selected[inIndex]){
167                        if(this.onCanDeselect(inIndex) === false){
168                                return;
169                        }
170                        var rowNode = this.grid.getRowNode(inIndex);
171                        if(rowNode){
172                                domAttr.set(rowNode, "aria-selected", "false");
173                        }
174                        this._beginUpdate();
175                        delete this.selected[inIndex];
176                        //this.grid.onDeselected(inIndex);
177                        this.onDeselected(inIndex);
178                        //this.onSetSelected(inIndex, false);
179                        this._endUpdate();
180                }
181        },
182
183        setSelected: function(inIndex, inSelect){
184                this[(inSelect ? 'addToSelection' : 'deselect')](inIndex);
185        },
186
187        toggleSelect: function(inIndex){
188                if(lang.isArray(inIndex)){
189                        array.forEach(inIndex, this.toggleSelect, this);
190                        return;
191                }
192                this.setSelected(inIndex, !this.selected[inIndex]);
193        },
194
195        _range: function(inFrom, inTo, func){
196                var s = (inFrom >= 0 ? inFrom : inTo), e = inTo;
197                if(s > e){
198                        e = s;
199                        s = inTo;
200                }
201                for(var i=s; i<=e; i++){
202                        func(i);
203                }
204        },
205
206        selectRange: function(inFrom, inTo){
207                this._range(inFrom, inTo, lang.hitch(this, "addToSelection"));
208        },
209
210        deselectRange: function(inFrom, inTo){
211                this._range(inFrom, inTo, lang.hitch(this, "deselect"));
212        },
213
214        insert: function(inIndex){
215                this.selected.splice(inIndex, 0, false);
216                if(this.selectedIndex >= inIndex){
217                        this.selectedIndex++;
218                }
219        },
220
221        remove: function(inIndex){
222                this.selected.splice(inIndex, 1);
223                if(this.selectedIndex >= inIndex){
224                        this.selectedIndex--;
225                }
226        },
227
228        deselectAll: function(inExcept){
229                for(var i in this.selected){
230                        if((i!=inExcept)&&(this.selected[i]===true)){
231                                this.deselect(i);
232                        }
233                }
234        },
235
236        clickSelect: function(inIndex, inCtrlKey, inShiftKey){
237                if(this.mode == 'none'){ return; }
238                this._beginUpdate();
239                if(this.mode != 'extended'){
240                        this.select(inIndex);
241                }else{
242                        if(!inShiftKey || this.rangeStartIndex < 0){
243                                this.rangeStartIndex = inIndex;
244                        }
245                        if(!inCtrlKey){
246                                this.deselectAll(inIndex);
247                        }
248                        if(inShiftKey){
249                                this.selectRange(this.rangeStartIndex, inIndex);
250                        }else if(inCtrlKey){
251                                this.toggleSelect(inIndex);
252                        }else{
253                                this.addToSelection(inIndex);
254                        }
255                }
256                this._endUpdate();
257        },
258
259        clickSelectEvent: function(e){
260                this.clickSelect(e.rowIndex, dojo.isCopyKey(e), e.shiftKey);
261        },
262
263        clear: function(){
264                this._beginUpdate();
265                this.deselectAll();
266                this._endUpdate();
267        }
268});
269});
Note: See TracBrowser for help on using the repository browser.