root / trunk / web / dojo / dojox / grid / Selection.js @ 12
History | View | Annotate | Download (4.1 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.grid.Selection"]){ |
||
9 | dojo._hasResource["dojox.grid.Selection"]=true; |
||
10 | dojo.provide("dojox.grid.Selection");
|
||
11 | dojo.declare("dojox.grid.Selection",null,{constructor:function(_1){ |
||
12 | this.grid=_1;
|
||
13 | this.selected=[];
|
||
14 | this.setMode(_1.selectionMode);
|
||
15 | },mode:"extended",selected:null,updating:0,selectedIndex:-1,setMode:function(_2){ |
||
16 | if(this.selected.length){ |
||
17 | this.deselectAll();
|
||
18 | } |
||
19 | if(_2!="extended"&&_2!="multiple"&&_2!="single"&&_2!="none"){ |
||
20 | this.mode="extended"; |
||
21 | }else{
|
||
22 | this.mode=_2;
|
||
23 | } |
||
24 | },onCanSelect:function(_3){ |
||
25 | return this.grid.onCanSelect(_3); |
||
26 | },onCanDeselect:function(_4){ |
||
27 | return this.grid.onCanDeselect(_4); |
||
28 | },onSelected:function(_5){ |
||
29 | },onDeselected:function(_6){ |
||
30 | },onChanging:function(){ |
||
31 | },onChanged:function(){ |
||
32 | },isSelected:function(_7){ |
||
33 | if(this.mode=="none"){ |
||
34 | return false; |
||
35 | } |
||
36 | return this.selected[_7]; |
||
37 | },getFirstSelected:function(){ |
||
38 | if(!this.selected.length||this.mode=="none"){ |
||
39 | return -1; |
||
40 | } |
||
41 | for(var i=0,l=this.selected.length;i<l;i++){ |
||
42 | if(this.selected[i]){ |
||
43 | return i;
|
||
44 | } |
||
45 | } |
||
46 | return -1; |
||
47 | },getNextSelected:function(_8){ |
||
48 | if(this.mode=="none"){ |
||
49 | return -1; |
||
50 | } |
||
51 | for(var i=_8+1,l=this.selected.length;i<l;i++){ |
||
52 | if(this.selected[i]){ |
||
53 | return i;
|
||
54 | } |
||
55 | } |
||
56 | return -1; |
||
57 | },getSelected:function(){ |
||
58 | var _9=[];
|
||
59 | for(var i=0,l=this.selected.length;i<l;i++){ |
||
60 | if(this.selected[i]){ |
||
61 | _9.push(i); |
||
62 | } |
||
63 | } |
||
64 | return _9;
|
||
65 | },getSelectedCount:function(){ |
||
66 | var c=0; |
||
67 | for(var i=0;i<this.selected.length;i++){ |
||
68 | if(this.selected[i]){ |
||
69 | c++; |
||
70 | } |
||
71 | } |
||
72 | return c;
|
||
73 | },_beginUpdate:function(){ |
||
74 | if(this.updating===0){ |
||
75 | this.onChanging();
|
||
76 | } |
||
77 | this.updating++;
|
||
78 | },_endUpdate:function(){ |
||
79 | this.updating--;
|
||
80 | if(this.updating===0){ |
||
81 | this.onChanged();
|
||
82 | } |
||
83 | },select:function(_a){ |
||
84 | if(this.mode=="none"){ |
||
85 | return;
|
||
86 | } |
||
87 | if(this.mode!="multiple"){ |
||
88 | this.deselectAll(_a);
|
||
89 | this.addToSelection(_a);
|
||
90 | }else{
|
||
91 | this.toggleSelect(_a);
|
||
92 | } |
||
93 | },addToSelection:function(_b){ |
||
94 | if(this.mode=="none"){ |
||
95 | return;
|
||
96 | } |
||
97 | if(dojo.isArray(_b)){
|
||
98 | dojo.forEach(_b,this.addToSelection,this); |
||
99 | return;
|
||
100 | } |
||
101 | _b=Number(_b); |
||
102 | if(this.selected[_b]){ |
||
103 | this.selectedIndex=_b;
|
||
104 | }else{
|
||
105 | if(this.onCanSelect(_b)!==false){ |
||
106 | this.selectedIndex=_b;
|
||
107 | var _c=this.grid.getRowNode(_b); |
||
108 | if(_c){
|
||
109 | dojo.attr(_c,"aria-selected","true"); |
||
110 | } |
||
111 | this._beginUpdate();
|
||
112 | this.selected[_b]=true; |
||
113 | this.onSelected(_b);
|
||
114 | this._endUpdate();
|
||
115 | } |
||
116 | } |
||
117 | },deselect:function(_d){ |
||
118 | if(this.mode=="none"){ |
||
119 | return;
|
||
120 | } |
||
121 | if(dojo.isArray(_d)){
|
||
122 | dojo.forEach(_d,this.deselect,this); |
||
123 | return;
|
||
124 | } |
||
125 | _d=Number(_d); |
||
126 | if(this.selectedIndex==_d){ |
||
127 | this.selectedIndex=-1; |
||
128 | } |
||
129 | if(this.selected[_d]){ |
||
130 | if(this.onCanDeselect(_d)===false){ |
||
131 | return;
|
||
132 | } |
||
133 | var _e=this.grid.getRowNode(_d); |
||
134 | if(_e){
|
||
135 | dojo.attr(_e,"aria-selected","false"); |
||
136 | } |
||
137 | this._beginUpdate();
|
||
138 | delete this.selected[_d]; |
||
139 | this.onDeselected(_d);
|
||
140 | this._endUpdate();
|
||
141 | } |
||
142 | },setSelected:function(_f,_10){ |
||
143 | this[(_10?"addToSelection":"deselect")](_f); |
||
144 | },toggleSelect:function(_11){ |
||
145 | if(dojo.isArray(_11)){
|
||
146 | dojo.forEach(_11,this.toggleSelect,this); |
||
147 | return;
|
||
148 | } |
||
149 | this.setSelected(_11,!this.selected[_11]); |
||
150 | },_range:function(_12,_13,_14){ |
||
151 | var s=(_12>=0?_12:_13),e=_13; |
||
152 | if(s>e){
|
||
153 | e=s; |
||
154 | s=_13; |
||
155 | } |
||
156 | for(var i=s;i<=e;i++){ |
||
157 | _14(i); |
||
158 | } |
||
159 | },selectRange:function(_15,_16){ |
||
160 | this._range(_15,_16,dojo.hitch(this,"addToSelection")); |
||
161 | },deselectRange:function(_17,_18){ |
||
162 | this._range(_17,_18,dojo.hitch(this,"deselect")); |
||
163 | },insert:function(_19){ |
||
164 | this.selected.splice(_19,0,false); |
||
165 | if(this.selectedIndex>=_19){ |
||
166 | this.selectedIndex++;
|
||
167 | } |
||
168 | },remove:function(_1a){ |
||
169 | this.selected.splice(_1a,1); |
||
170 | if(this.selectedIndex>=_1a){ |
||
171 | this.selectedIndex--;
|
||
172 | } |
||
173 | },deselectAll:function(_1b){ |
||
174 | for(var i in this.selected){ |
||
175 | if((i!=_1b)&&(this.selected[i]===true)){ |
||
176 | this.deselect(i);
|
||
177 | } |
||
178 | } |
||
179 | },clickSelect:function(_1c,_1d,_1e){ |
||
180 | if(this.mode=="none"){ |
||
181 | return;
|
||
182 | } |
||
183 | this._beginUpdate();
|
||
184 | if(this.mode!="extended"){ |
||
185 | this.select(_1c);
|
||
186 | }else{
|
||
187 | var _1f=this.selectedIndex; |
||
188 | if(!_1d){
|
||
189 | this.deselectAll(_1c);
|
||
190 | } |
||
191 | if(_1e){
|
||
192 | this.selectRange(_1f,_1c);
|
||
193 | }else{
|
||
194 | if(_1d){
|
||
195 | this.toggleSelect(_1c);
|
||
196 | }else{
|
||
197 | this.addToSelection(_1c);
|
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | this._endUpdate();
|
||
202 | },clickSelectEvent:function(e){ |
||
203 | this.clickSelect(e.rowIndex,dojo.isCopyKey(e),e.shiftKey);
|
||
204 | },clear:function(){ |
||
205 | this._beginUpdate();
|
||
206 | this.deselectAll();
|
||
207 | this._endUpdate();
|
||
208 | }}); |
||
209 | } |