]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/grid/_grid/selection.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / grid / _grid / selection.js
1 if(!dojo._hasResource['dojox.grid._grid.selection']){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource['dojox.grid._grid.selection'] = true;
3 dojo.provide('dojox.grid._grid.selection');
4
5 dojo.declare("dojox.grid.selection",
6         null,
7         {
8         // summary:
9         //              Manages row selection for grid. Owned by grid and used internally 
10         //              for selection. Override to implement custom selection.
11
12         constructor: function(inGrid){
13                 this.grid = inGrid;
14                 this.selected = [];
15         },
16         
17         multiSelect: true,
18         selected: null,
19         updating: 0,
20         selectedIndex: -1,
21
22         onCanSelect: function(inIndex){
23                 return this.grid.onCanSelect(inIndex);
24         },
25
26         onCanDeselect: function(inIndex){
27                 return this.grid.onCanDeselect(inIndex);
28         },
29
30         onSelected: function(inIndex){
31                 return this.grid.onSelected(inIndex);
32         },
33
34         onDeselected: function(inIndex){
35                 return this.grid.onDeselected(inIndex);
36         },
37
38         //onSetSelected: function(inIndex, inSelect) { };
39         onChanging: function(){
40         },
41
42         onChanged: function(){
43                 return this.grid.onSelectionChanged();
44         },
45
46         isSelected: function(inIndex){
47                 return this.selected[inIndex];
48         },
49
50         getFirstSelected: function(){
51                 for(var i=0, l=this.selected.length; i<l; i++){
52                         if(this.selected[i]){
53                                 return i;
54                         }
55                 }
56                 return -1;
57         },
58
59         getNextSelected: function(inPrev){
60                 for(var i=inPrev+1, l=this.selected.length; i<l; i++){
61                         if(this.selected[i]){
62                                 return i;
63                         }
64                 }
65                 return -1;
66         },
67
68         getSelected: function(){
69                 var result = [];
70                 for(var i=0, l=this.selected.length; i<l; i++){
71                         if(this.selected[i]){
72                                 result.push(i);
73                         }
74                 }
75                 return result;
76         },
77
78         getSelectedCount: function(){
79                 var c = 0;
80                 for(var i=0; i<this.selected.length; i++){
81                         if(this.selected[i]){
82                                 c++;
83                         }
84                 }
85                 return c;
86         },
87
88         beginUpdate: function(){
89                 if(this.updating == 0){
90                         this.onChanging();
91                 }
92                 this.updating++;
93         },
94
95         endUpdate: function(){
96                 this.updating--;
97                 if(this.updating == 0){
98                         this.onChanged();
99                 }
100         },
101
102         select: function(inIndex){
103                 this.unselectAll(inIndex);
104                 this.addToSelection(inIndex);
105         },
106
107         addToSelection: function(inIndex){
108                 inIndex = Number(inIndex);
109                 if(this.selected[inIndex]){
110                         this.selectedIndex = inIndex;
111                 }else{
112                         if(this.onCanSelect(inIndex) !== false){
113                                 this.selectedIndex = inIndex;
114                                 this.beginUpdate();
115                                 this.selected[inIndex] = true;
116                                 this.grid.onSelected(inIndex);
117                                 //this.onSelected(inIndex);
118                                 //this.onSetSelected(inIndex, true);
119                                 this.endUpdate();
120                         }
121                 }
122         },
123
124         deselect: function(inIndex){
125                 inIndex = Number(inIndex);
126                 if(this.selectedIndex == inIndex){
127                         this.selectedIndex = -1;
128                 }
129                 if(this.selected[inIndex]){
130                         if(this.onCanDeselect(inIndex) === false){
131                                 return;
132                         }
133                         this.beginUpdate();
134                         delete this.selected[inIndex];
135                         this.grid.onDeselected(inIndex);
136                         //this.onDeselected(inIndex);
137                         //this.onSetSelected(inIndex, false);
138                         this.endUpdate();
139                 }
140         },
141
142         setSelected: function(inIndex, inSelect){
143                 this[(inSelect ? 'addToSelection' : 'deselect')](inIndex);
144         },
145
146         toggleSelect: function(inIndex){
147                 this.setSelected(inIndex, !this.selected[inIndex])
148         },
149
150         insert: function(inIndex){
151                 this.selected.splice(inIndex, 0, false);
152                 if(this.selectedIndex >= inIndex){
153                         this.selectedIndex++;
154                 }
155         },
156
157         remove: function(inIndex){
158                 this.selected.splice(inIndex, 1);
159                 if(this.selectedIndex >= inIndex){
160                         this.selectedIndex--;
161                 }
162         },
163
164         unselectAll: function(inExcept){
165                 for(var i in this.selected){
166                         if((i!=inExcept)&&(this.selected[i]===true)){
167                                 this.deselect(i);
168                         }
169                 }
170         },
171
172         shiftSelect: function(inFrom, inTo){
173                 var s = (inFrom >= 0 ? inFrom : inTo), e = inTo;
174                 if(s > e){
175                         e = s;
176                         s = inTo;
177                 }
178                 for(var i=s; i<=e; i++){
179                         this.addToSelection(i);
180                 }
181         },
182
183         clickSelect: function(inIndex, inCtrlKey, inShiftKey){
184                 this.beginUpdate();
185                 if(!this.multiSelect){
186                         this.select(inIndex);
187                 }else{
188                         var lastSelected = this.selectedIndex;
189                         if(!inCtrlKey){
190                                 this.unselectAll(inIndex);
191                         }
192                         if(inShiftKey){
193                                 this.shiftSelect(lastSelected, inIndex);
194                         }else if(inCtrlKey){
195                                 this.toggleSelect(inIndex);
196                         }else{
197                                 this.addToSelection(inIndex)
198                         }
199                 }
200                 this.endUpdate();
201         },
202
203         clickSelectEvent: function(e){
204                 this.clickSelect(e.rowIndex, e.ctrlKey, e.shiftKey);
205         },
206
207         clear: function(){
208                 this.beginUpdate();
209                 this.unselectAll();
210                 this.endUpdate();
211         }
212
213 });
214
215 }