root / trunk / web / dojo / dojox / grid / _FocusManager.js @ 9
History | View | Annotate | Download (11.8 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._FocusManager"]){ |
||
9 | dojo._hasResource["dojox.grid._FocusManager"]=true; |
||
10 | dojo.provide("dojox.grid._FocusManager");
|
||
11 | dojo.require("dojox.grid.util");
|
||
12 | dojo.declare("dojox.grid._FocusManager",null,{constructor:function(_1){ |
||
13 | this.grid=_1;
|
||
14 | this.cell=null; |
||
15 | this.rowIndex=-1; |
||
16 | this._connects=[];
|
||
17 | this.headerMenu=this.grid.headerMenu; |
||
18 | this._connects.push(dojo.connect(this.grid.domNode,"onfocus",this,"doFocus")); |
||
19 | this._connects.push(dojo.connect(this.grid.domNode,"onblur",this,"doBlur")); |
||
20 | this._connects.push(dojo.connect(this.grid.domNode,"oncontextmenu",this,"doContextMenu")); |
||
21 | this._connects.push(dojo.connect(this.grid.lastFocusNode,"onfocus",this,"doLastNodeFocus")); |
||
22 | this._connects.push(dojo.connect(this.grid.lastFocusNode,"onblur",this,"doLastNodeBlur")); |
||
23 | this._connects.push(dojo.connect(this.grid,"_onFetchComplete",this,"_delayedCellFocus")); |
||
24 | this._connects.push(dojo.connect(this.grid,"postrender",this,"_delayedHeaderFocus")); |
||
25 | },destroy:function(){ |
||
26 | dojo.forEach(this._connects,dojo.disconnect);
|
||
27 | delete this.grid; |
||
28 | delete this.cell; |
||
29 | },_colHeadNode:null,_colHeadFocusIdx:null,_contextMenuBindNode:null,tabbingOut:false,focusClass:"dojoxGridCellFocus",focusView:null,initFocusView:function(){ |
||
30 | this.focusView=this.grid.views.getFirstScrollingView()||this.focusView; |
||
31 | this._initColumnHeaders();
|
||
32 | },isFocusCell:function(_2,_3){ |
||
33 | return (this.cell==_2)&&(this.rowIndex==_3); |
||
34 | },isLastFocusCell:function(){ |
||
35 | if(this.cell){ |
||
36 | return (this.rowIndex==this.grid.rowCount-1)&&(this.cell.index==this.grid.layout.cellCount-1); |
||
37 | } |
||
38 | return false; |
||
39 | },isFirstFocusCell:function(){ |
||
40 | if(this.cell){ |
||
41 | return (this.rowIndex===0)&&(this.cell.index===0); |
||
42 | } |
||
43 | return false; |
||
44 | },isNoFocusCell:function(){ |
||
45 | return (this.rowIndex<0)||!this.cell; |
||
46 | },isNavHeader:function(){ |
||
47 | return (!!this._colHeadNode); |
||
48 | },getHeaderIndex:function(){ |
||
49 | if(this._colHeadNode){ |
||
50 | return dojo.indexOf(this._findHeaderCells(),this._colHeadNode); |
||
51 | }else{
|
||
52 | return -1; |
||
53 | } |
||
54 | },_focusifyCellNode:function(_4){ |
||
55 | var n=this.cell&&this.cell.getNode(this.rowIndex); |
||
56 | if(n){
|
||
57 | dojo.toggleClass(n,this.focusClass,_4);
|
||
58 | if(_4){
|
||
59 | var sl=this.scrollIntoView(); |
||
60 | try{
|
||
61 | if(!this.grid.edit.isEditing()){ |
||
62 | dojox.grid.util.fire(n,"focus");
|
||
63 | if(sl){
|
||
64 | this.cell.view.scrollboxNode.scrollLeft=sl;
|
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | catch(e){
|
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | },_delayedCellFocus:function(){ |
||
73 | if(this.isNavHeader()||!this.grid._focused){ |
||
74 | return;
|
||
75 | } |
||
76 | var n=this.cell&&this.cell.getNode(this.rowIndex); |
||
77 | if(n){
|
||
78 | try{
|
||
79 | if(!this.grid.edit.isEditing()){ |
||
80 | dojo.toggleClass(n,this.focusClass,true); |
||
81 | this.blurHeader();
|
||
82 | dojox.grid.util.fire(n,"focus");
|
||
83 | } |
||
84 | } |
||
85 | catch(e){
|
||
86 | } |
||
87 | } |
||
88 | },_delayedHeaderFocus:function(){ |
||
89 | if(this.isNavHeader()){ |
||
90 | this.focusHeader();
|
||
91 | this.grid.domNode.focus();
|
||
92 | } |
||
93 | },_initColumnHeaders:function(){ |
||
94 | var _5=this._findHeaderCells(); |
||
95 | for(var i=0;i<_5.length;i++){ |
||
96 | this._connects.push(dojo.connect(_5[i],"onfocus",this,"doColHeaderFocus")); |
||
97 | this._connects.push(dojo.connect(_5[i],"onblur",this,"doColHeaderBlur")); |
||
98 | } |
||
99 | },_findHeaderCells:function(){ |
||
100 | var _6=dojo.query("th",this.grid.viewsHeaderNode); |
||
101 | var _7=[];
|
||
102 | for(var i=0;i<_6.length;i++){ |
||
103 | var _8=_6[i];
|
||
104 | var _9=dojo.hasAttr(_8,"tabIndex"); |
||
105 | var _a=dojo.attr(_8,"tabIndex"); |
||
106 | if(_9&&_a<0){ |
||
107 | _7.push(_8); |
||
108 | } |
||
109 | } |
||
110 | return _7;
|
||
111 | },_setActiveColHeader:function(_b,_c,_d){ |
||
112 | dojo.attr(this.grid.domNode,"aria-activedescendant",_b.id); |
||
113 | if(_d!=null&&_d>=0&&_d!=_c){ |
||
114 | dojo.toggleClass(this._findHeaderCells()[_d],this.focusClass,false); |
||
115 | } |
||
116 | dojo.toggleClass(_b,this.focusClass,true); |
||
117 | this._colHeadNode=_b;
|
||
118 | this._colHeadFocusIdx=_c;
|
||
119 | this._scrollHeader(this._colHeadFocusIdx); |
||
120 | },scrollIntoView:function(){ |
||
121 | var _e=(this.cell?this._scrollInfo(this.cell):null); |
||
122 | if(!_e||!_e.s){
|
||
123 | return null; |
||
124 | } |
||
125 | var rt=this.grid.scroller.findScrollTop(this.rowIndex); |
||
126 | if(_e.n&&_e.sr){
|
||
127 | if(_e.n.offsetLeft+_e.n.offsetWidth>_e.sr.l+_e.sr.w){
|
||
128 | _e.s.scrollLeft=_e.n.offsetLeft+_e.n.offsetWidth-_e.sr.w; |
||
129 | }else{
|
||
130 | if(_e.n.offsetLeft<_e.sr.l){
|
||
131 | _e.s.scrollLeft=_e.n.offsetLeft; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | if(_e.r&&_e.sr){
|
||
136 | if(rt+_e.r.offsetHeight>_e.sr.t+_e.sr.h){
|
||
137 | this.grid.setScrollTop(rt+_e.r.offsetHeight-_e.sr.h);
|
||
138 | }else{
|
||
139 | if(rt<_e.sr.t){
|
||
140 | this.grid.setScrollTop(rt);
|
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | return _e.s.scrollLeft;
|
||
145 | },_scrollInfo:function(_f,_10){ |
||
146 | if(_f){
|
||
147 | var cl=_f,sbn=cl.view.scrollboxNode,_11={w:sbn.clientWidth,l:sbn.scrollLeft,t:sbn.scrollTop,h:sbn.clientHeight},rn=cl.view.getRowNode(this.rowIndex); |
||
148 | return {c:cl,s:sbn,sr:_11,n:(_10?_10:_f.getNode(this.rowIndex)),r:rn}; |
||
149 | } |
||
150 | return null; |
||
151 | },_scrollHeader:function(_12){ |
||
152 | var _13=null; |
||
153 | if(this._colHeadNode){ |
||
154 | var _14=this.grid.getCell(_12); |
||
155 | _13=this._scrollInfo(_14,_14.getNode(0)); |
||
156 | } |
||
157 | if(_13&&_13.s&&_13.sr&&_13.n){
|
||
158 | var _15=_13.sr.l+_13.sr.w;
|
||
159 | if(_13.n.offsetLeft+_13.n.offsetWidth>_15){
|
||
160 | _13.s.scrollLeft=_13.n.offsetLeft+_13.n.offsetWidth-_13.sr.w; |
||
161 | }else{
|
||
162 | if(_13.n.offsetLeft<_13.sr.l){
|
||
163 | _13.s.scrollLeft=_13.n.offsetLeft; |
||
164 | }else{
|
||
165 | if(dojo.isIE<=7&&_14&&_14.view.headerNode){ |
||
166 | _14.view.headerNode.scrollLeft=_13.s.scrollLeft; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | },_isHeaderHidden:function(){ |
||
172 | var _16=this.focusView; |
||
173 | if(!_16){
|
||
174 | for(var i=0,_17;(_17=this.grid.views.views[i]);i++){ |
||
175 | if(_17.headerNode){
|
||
176 | _16=_17; |
||
177 | break;
|
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | return (_16&&dojo.getComputedStyle(_16.headerNode).display=="none"); |
||
182 | },colSizeAdjust:function(e,_18,_19){ |
||
183 | var _1a=this._findHeaderCells(); |
||
184 | var _1b=this.focusView; |
||
185 | if(!_1b){
|
||
186 | for(var i=0,_1c;(_1c=this.grid.views.views[i]);i++){ |
||
187 | if(_1c.header.tableMap.map){
|
||
188 | _1b=_1c; |
||
189 | break;
|
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | var _1d=_1a[_18];
|
||
194 | if(!_1b||(_18==_1a.length-1&&_18===0)){ |
||
195 | return;
|
||
196 | } |
||
197 | _1b.content.baseDecorateEvent(e); |
||
198 | e.cellNode=_1d; |
||
199 | e.cellIndex=_1b.content.getCellNodeIndex(e.cellNode); |
||
200 | e.cell=(e.cellIndex>=0?this.grid.getCell(e.cellIndex):null); |
||
201 | if(_1b.header.canResize(e)){
|
||
202 | var _1e={l:_19}; |
||
203 | var _1f=_1b.header.colResizeSetup(e,false); |
||
204 | _1b.header.doResizeColumn(_1f,null,_1e);
|
||
205 | _1b.update(); |
||
206 | } |
||
207 | },styleRow:function(_20){ |
||
208 | return;
|
||
209 | },setFocusIndex:function(_21,_22){ |
||
210 | this.setFocusCell(this.grid.getCell(_22),_21); |
||
211 | },setFocusCell:function(_23,_24){ |
||
212 | if(_23&&!this.isFocusCell(_23,_24)){ |
||
213 | this.tabbingOut=false; |
||
214 | if(this._colHeadNode){ |
||
215 | this.blurHeader();
|
||
216 | } |
||
217 | this._colHeadNode=this._colHeadFocusIdx=null; |
||
218 | this.focusGridView();
|
||
219 | this._focusifyCellNode(false); |
||
220 | this.cell=_23;
|
||
221 | this.rowIndex=_24;
|
||
222 | this._focusifyCellNode(true); |
||
223 | } |
||
224 | if(dojo.isOpera){
|
||
225 | setTimeout(dojo.hitch(this.grid,"onCellFocus",this.cell,this.rowIndex),1); |
||
226 | }else{
|
||
227 | this.grid.onCellFocus(this.cell,this.rowIndex); |
||
228 | } |
||
229 | },next:function(){ |
||
230 | if(this.cell){ |
||
231 | var row=this.rowIndex,col=this.cell.index+1,cc=this.grid.layout.cellCount-1,rc=this.grid.rowCount-1; |
||
232 | if(col>cc){
|
||
233 | col=0;
|
||
234 | row++; |
||
235 | } |
||
236 | if(row>rc){
|
||
237 | col=cc; |
||
238 | row=rc; |
||
239 | } |
||
240 | if(this.grid.edit.isEditing()){ |
||
241 | var _25=this.grid.getCell(col); |
||
242 | if(!this.isLastFocusCell()&&!_25.editable){ |
||
243 | this.cell=_25;
|
||
244 | this.rowIndex=row;
|
||
245 | this.next();
|
||
246 | return;
|
||
247 | } |
||
248 | } |
||
249 | this.setFocusIndex(row,col);
|
||
250 | } |
||
251 | },previous:function(){ |
||
252 | if(this.cell){ |
||
253 | var row=(this.rowIndex||0),col=(this.cell.index||0)-1; |
||
254 | if(col<0){ |
||
255 | col=this.grid.layout.cellCount-1; |
||
256 | row--; |
||
257 | } |
||
258 | if(row<0){ |
||
259 | row=0;
|
||
260 | col=0;
|
||
261 | } |
||
262 | if(this.grid.edit.isEditing()){ |
||
263 | var _26=this.grid.getCell(col); |
||
264 | if(!this.isFirstFocusCell()&&!_26.editable){ |
||
265 | this.cell=_26;
|
||
266 | this.rowIndex=row;
|
||
267 | this.previous();
|
||
268 | return;
|
||
269 | } |
||
270 | } |
||
271 | this.setFocusIndex(row,col);
|
||
272 | } |
||
273 | },move:function(_27,_28){ |
||
274 | var _29=_28<0?-1:1; |
||
275 | if(this.isNavHeader()){ |
||
276 | var _2a=this._findHeaderCells(); |
||
277 | var _2b=currentIdx=dojo.indexOf(_2a,this._colHeadNode); |
||
278 | currentIdx+=_28; |
||
279 | while(currentIdx>=0&¤tIdx<_2a.length&&_2a[currentIdx].style.display=="none"){ |
||
280 | currentIdx+=_29; |
||
281 | } |
||
282 | if((currentIdx>=0)&&(currentIdx<_2a.length)){ |
||
283 | this._setActiveColHeader(_2a[currentIdx],currentIdx,_2b);
|
||
284 | } |
||
285 | }else{
|
||
286 | if(this.cell){ |
||
287 | var sc=this.grid.scroller,r=this.rowIndex,rc=this.grid.rowCount-1,row=Math.min(rc,Math.max(0,r+_27)); |
||
288 | if(_27){
|
||
289 | if(_27>0){ |
||
290 | if(row>sc.getLastPageRow(sc.page)){
|
||
291 | this.grid.setScrollTop(this.grid.scrollTop+sc.findScrollTop(row)-sc.findScrollTop(r)); |
||
292 | } |
||
293 | }else{
|
||
294 | if(_27<0){ |
||
295 | if(row<=sc.getPageRow(sc.page)){
|
||
296 | this.grid.setScrollTop(this.grid.scrollTop-sc.findScrollTop(r)-sc.findScrollTop(row)); |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | var cc=this.grid.layout.cellCount-1,i=this.cell.index,col=Math.min(cc,Math.max(0,i+_28)); |
||
302 | var _2c=this.grid.getCell(col); |
||
303 | while(col>=0&&col<cc&&_2c&&_2c.hidden===true){ |
||
304 | col+=_29; |
||
305 | _2c=this.grid.getCell(col);
|
||
306 | } |
||
307 | if(!_2c||_2c.hidden===true){ |
||
308 | col=i; |
||
309 | } |
||
310 | this.setFocusIndex(row,col);
|
||
311 | if(_27){
|
||
312 | this.grid.updateRow(r);
|
||
313 | } |
||
314 | } |
||
315 | } |
||
316 | },previousKey:function(e){ |
||
317 | if(this.grid.edit.isEditing()){ |
||
318 | dojo.stopEvent(e); |
||
319 | this.previous();
|
||
320 | }else{
|
||
321 | if(!this.isNavHeader()&&!this._isHeaderHidden()){ |
||
322 | this.grid.domNode.focus();
|
||
323 | dojo.stopEvent(e); |
||
324 | }else{
|
||
325 | this.tabOut(this.grid.domNode); |
||
326 | if(this._colHeadFocusIdx!=null){ |
||
327 | dojo.toggleClass(this._findHeaderCells()[this._colHeadFocusIdx],this.focusClass,false); |
||
328 | this._colHeadFocusIdx=null; |
||
329 | } |
||
330 | this._focusifyCellNode(false); |
||
331 | } |
||
332 | } |
||
333 | },nextKey:function(e){ |
||
334 | var _2d=(this.grid.rowCount===0); |
||
335 | if(e.target===this.grid.domNode&&this._colHeadFocusIdx==null){ |
||
336 | this.focusHeader();
|
||
337 | dojo.stopEvent(e); |
||
338 | }else{
|
||
339 | if(this.isNavHeader()){ |
||
340 | this.blurHeader();
|
||
341 | if(!this.findAndFocusGridCell()){ |
||
342 | this.tabOut(this.grid.lastFocusNode); |
||
343 | } |
||
344 | this._colHeadNode=this._colHeadFocusIdx=null; |
||
345 | }else{
|
||
346 | if(this.grid.edit.isEditing()){ |
||
347 | dojo.stopEvent(e); |
||
348 | this.next();
|
||
349 | }else{
|
||
350 | this.tabOut(this.grid.lastFocusNode); |
||
351 | } |
||
352 | } |
||
353 | } |
||
354 | },tabOut:function(_2e){ |
||
355 | this.tabbingOut=true; |
||
356 | _2e.focus(); |
||
357 | },focusGridView:function(){ |
||
358 | dojox.grid.util.fire(this.focusView,"focus"); |
||
359 | },focusGrid:function(_2f){ |
||
360 | this.focusGridView();
|
||
361 | this._focusifyCellNode(true); |
||
362 | },findAndFocusGridCell:function(){ |
||
363 | var _30=true; |
||
364 | var _31=(this.grid.rowCount===0); |
||
365 | if(this.isNoFocusCell()&&!_31){ |
||
366 | var _32=0; |
||
367 | var _33=this.grid.getCell(_32); |
||
368 | if(_33.hidden){
|
||
369 | _32=this.isNavHeader()?this._colHeadFocusIdx:0; |
||
370 | } |
||
371 | this.setFocusIndex(0,_32); |
||
372 | }else{
|
||
373 | if(this.cell&&!_31){ |
||
374 | if(this.focusView&&!this.focusView.rowNodes[this.rowIndex]){ |
||
375 | this.grid.scrollToRow(this.rowIndex); |
||
376 | } |
||
377 | this.focusGrid();
|
||
378 | }else{
|
||
379 | _30=false;
|
||
380 | } |
||
381 | } |
||
382 | this._colHeadNode=this._colHeadFocusIdx=null; |
||
383 | return _30;
|
||
384 | },focusHeader:function(){ |
||
385 | var _34=this._findHeaderCells(); |
||
386 | var _35=this._colHeadFocusIdx; |
||
387 | if(this._isHeaderHidden()){ |
||
388 | this.findAndFocusGridCell();
|
||
389 | }else{
|
||
390 | if(!this._colHeadFocusIdx){ |
||
391 | if(this.isNoFocusCell()){ |
||
392 | this._colHeadFocusIdx=0; |
||
393 | }else{
|
||
394 | this._colHeadFocusIdx=this.cell.index; |
||
395 | } |
||
396 | } |
||
397 | } |
||
398 | this._colHeadNode=_34[this._colHeadFocusIdx]; |
||
399 | while(this._colHeadNode&&this._colHeadFocusIdx>=0&&this._colHeadFocusIdx<_34.length&&this._colHeadNode.style.display=="none"){ |
||
400 | this._colHeadFocusIdx++;
|
||
401 | this._colHeadNode=_34[this._colHeadFocusIdx]; |
||
402 | } |
||
403 | if(this._colHeadNode&&this._colHeadNode.style.display!="none"){ |
||
404 | if(this.headerMenu&&this._contextMenuBindNode!=this.grid.domNode){ |
||
405 | this.headerMenu.unBindDomNode(this.grid.viewsHeaderNode); |
||
406 | this.headerMenu.bindDomNode(this.grid.domNode); |
||
407 | this._contextMenuBindNode=this.grid.domNode; |
||
408 | } |
||
409 | this._setActiveColHeader(this._colHeadNode,this._colHeadFocusIdx,_35); |
||
410 | this._scrollHeader(this._colHeadFocusIdx); |
||
411 | this._focusifyCellNode(false); |
||
412 | }else{
|
||
413 | this.findAndFocusGridCell();
|
||
414 | } |
||
415 | },blurHeader:function(){ |
||
416 | dojo.removeClass(this._colHeadNode,this.focusClass); |
||
417 | dojo.removeAttr(this.grid.domNode,"aria-activedescendant"); |
||
418 | if(this.headerMenu&&this._contextMenuBindNode==this.grid.domNode){ |
||
419 | var _36=this.grid.viewsHeaderNode; |
||
420 | this.headerMenu.unBindDomNode(this.grid.domNode); |
||
421 | this.headerMenu.bindDomNode(_36);
|
||
422 | this._contextMenuBindNode=_36;
|
||
423 | } |
||
424 | },doFocus:function(e){ |
||
425 | if(e&&e.target!=e.currentTarget){
|
||
426 | dojo.stopEvent(e); |
||
427 | return;
|
||
428 | } |
||
429 | if(!this.tabbingOut){ |
||
430 | this.focusHeader();
|
||
431 | } |
||
432 | this.tabbingOut=false; |
||
433 | dojo.stopEvent(e); |
||
434 | },doBlur:function(e){ |
||
435 | dojo.stopEvent(e); |
||
436 | },doContextMenu:function(e){ |
||
437 | if(!this.headerMenu){ |
||
438 | dojo.stopEvent(e); |
||
439 | } |
||
440 | },doLastNodeFocus:function(e){ |
||
441 | if(this.tabbingOut){ |
||
442 | this._focusifyCellNode(false); |
||
443 | }else{
|
||
444 | if(this.grid.rowCount>0){ |
||
445 | if(this.isNoFocusCell()){ |
||
446 | this.setFocusIndex(0,0); |
||
447 | } |
||
448 | this._focusifyCellNode(true); |
||
449 | }else{
|
||
450 | this.focusHeader();
|
||
451 | } |
||
452 | } |
||
453 | this.tabbingOut=false; |
||
454 | dojo.stopEvent(e); |
||
455 | },doLastNodeBlur:function(e){ |
||
456 | dojo.stopEvent(e); |
||
457 | },doColHeaderFocus:function(e){ |
||
458 | this._setActiveColHeader(e.target,dojo.attr(e.target,"idx"),this._colHeadFocusIdx); |
||
459 | this._scrollHeader(this.getHeaderIndex()); |
||
460 | dojo.stopEvent(e); |
||
461 | },doColHeaderBlur:function(e){ |
||
462 | dojo.toggleClass(e.target,this.focusClass,false); |
||
463 | }}); |
||
464 | } |