source: Dev/branches/rest-dojo-ui/client/dojox/grid/Selection.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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