1 if(!dojo._hasResource["dojox.grid._grid.focus"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.grid._grid.focus"] = true;
3 dojo.provide("dojox.grid._grid.focus");
6 dojo.declare("dojox.grid.focus", null, {
8 // Controls grid cell focus. Owned by grid and used internally for focusing.
9 // Note: grid cell actually receives keyboard input only when cell is being edited.
10 constructor: function(inGrid){
14 dojo.connect(this.grid.domNode, "onfocus", this, "doFocus");
17 focusClass: "dojoxGrid-cell-focus",
19 initFocusView: function(){
20 this.focusView = this.grid.views.getFirstScrollingView();
22 isFocusCell: function(inCell, inRowIndex){
24 // states if the given cell is focused
30 // true of the given grid cell is focused
31 return (this.cell == inCell) && (this.rowIndex == inRowIndex);
33 isLastFocusCell: function(){
34 return (this.rowIndex == this.grid.rowCount-1) && (this.cell.index == this.grid.layout.cellCount-1);
36 isFirstFocusCell: function(){
37 return (this.rowIndex == 0) && (this.cell.index == 0);
39 isNoFocusCell: function(){
40 return (this.rowIndex < 0) || !this.cell;
42 _focusifyCellNode: function(inBork){
43 var n = this.cell && this.cell.getNode(this.rowIndex);
45 dojo.toggleClass(n, this.focusClass, inBork);
47 this.scrollIntoView();
49 if(!this.grid.edit.isEditing())
50 dojox.grid.fire(n, "focus");
55 scrollIntoView: function() {
61 s = c.view.scrollboxNode,
68 n = c.getNode(this.rowIndex),
69 r = c.view.getRowNode(this.rowIndex),
70 rt = this.grid.scroller.findScrollTop(this.rowIndex);
71 // place cell within horizontal view
72 if(n.offsetLeft + n.offsetWidth > sr.l + sr.w){
73 s.scrollLeft = n.offsetLeft + n.offsetWidth - sr.w;
74 }else if(n.offsetLeft < sr.l){
75 s.scrollLeft = n.offsetLeft;
77 // place cell within vertical view
78 if(rt + r.offsetHeight > sr.t + sr.h){
79 this.grid.setScrollTop(rt + r.offsetHeight - sr.h);
81 this.grid.setScrollTop(rt);
84 styleRow: function(inRow){
87 setFocusIndex: function(inRowIndex, inCellIndex){
89 // focuses the given grid cell
94 this.setFocusCell(this.grid.getCell(inCellIndex), inRowIndex);
96 setFocusCell: function(inCell, inRowIndex){
98 // focuses the given grid cell
103 if(inCell && !this.isFocusCell(inCell, inRowIndex)){
104 this.tabbingOut = false;
105 this.focusGridView();
106 this._focusifyCellNode(false);
108 this.rowIndex = inRowIndex;
109 this._focusifyCellNode(true);
111 // even if this cell isFocusCell, the document focus may need to be rejiggered
112 // call opera on delay to prevent keypress from altering focus
114 setTimeout(dojo.hitch(this.grid, 'onCellFocus', this.cell, this.rowIndex), 1);
116 this.grid.onCellFocus(this.cell, this.rowIndex);
121 // focus next grid cell
122 var row=this.rowIndex, col=this.cell.index+1, cc=this.grid.layout.cellCount-1, rc=this.grid.rowCount-1;
131 this.setFocusIndex(row, col);
133 previous: function(){
135 // focus previous grid cell
136 var row=(this.rowIndex || 0), col=(this.cell.index || 0) - 1;
138 col = this.grid.layout.cellCount-1;
145 this.setFocusIndex(row, col);
147 move: function(inRowDelta, inColDelta) {
149 // focus grid cell based on position relative to current focus
151 // vertical distance from current focus
153 // horizontal distance from current focus
155 rc = this.grid.rowCount-1,
156 cc = this.grid.layout.cellCount-1,
159 row = Math.min(rc, Math.max(0, r+inRowDelta)),
160 col = Math.min(cc, Math.max(0, i+inColDelta));
161 this.setFocusIndex(row, col);
163 this.grid.updateRow(r);
166 previousKey: function(e){
167 if(this.isFirstFocusCell()){
168 this.tabOut(this.grid.domNode);
174 nextKey: function(e) {
175 if(this.isLastFocusCell()){
176 this.tabOut(this.grid.lastFocusNode);
182 tabOut: function(inFocusNode){
183 this.tabbingOut = true;
186 focusGridView: function(){
187 dojox.grid.fire(this.focusView, "focus");
189 focusGrid: function(inSkipFocusCell){
190 this.focusGridView();
191 this._focusifyCellNode(true);
193 doFocus: function(e){
194 // trap focus only for grid dom node
195 if(e && e.target != e.currentTarget){
198 // do not focus for scrolling if grid is about to blur
199 if(!this.tabbingOut && this.isNoFocusCell()){
200 // establish our virtual-focus, if necessary
201 this.setFocusIndex(0, 0);
203 this.tabbingOut = false;