| 2150 |
mathias |
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");
|
|
|
4 |
|
|
|
5 |
// focus management
|
|
|
6 |
dojo.declare("dojox.grid.focus", null, {
|
|
|
7 |
// summary:
|
|
|
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){
|
|
|
11 |
this.grid = inGrid;
|
|
|
12 |
this.cell = null;
|
|
|
13 |
this.rowIndex = -1;
|
|
|
14 |
dojo.connect(this.grid.domNode, "onfocus", this, "doFocus");
|
|
|
15 |
},
|
|
|
16 |
tabbingOut: false,
|
|
|
17 |
focusClass: "dojoxGrid-cell-focus",
|
|
|
18 |
focusView: null,
|
|
|
19 |
initFocusView: function(){
|
|
|
20 |
this.focusView = this.grid.views.getFirstScrollingView();
|
|
|
21 |
},
|
|
|
22 |
isFocusCell: function(inCell, inRowIndex){
|
|
|
23 |
// summary:
|
|
|
24 |
// states if the given cell is focused
|
|
|
25 |
// inCell: object
|
|
|
26 |
// grid cell object
|
|
|
27 |
// inRowIndex: int
|
|
|
28 |
// grid row index
|
|
|
29 |
// returns:
|
|
|
30 |
// true of the given grid cell is focused
|
|
|
31 |
return (this.cell == inCell) && (this.rowIndex == inRowIndex);
|
|
|
32 |
},
|
|
|
33 |
isLastFocusCell: function(){
|
|
|
34 |
return (this.rowIndex == this.grid.rowCount-1) && (this.cell.index == this.grid.layout.cellCount-1);
|
|
|
35 |
},
|
|
|
36 |
isFirstFocusCell: function(){
|
|
|
37 |
return (this.rowIndex == 0) && (this.cell.index == 0);
|
|
|
38 |
},
|
|
|
39 |
isNoFocusCell: function(){
|
|
|
40 |
return (this.rowIndex < 0) || !this.cell;
|
|
|
41 |
},
|
|
|
42 |
_focusifyCellNode: function(inBork){
|
|
|
43 |
var n = this.cell && this.cell.getNode(this.rowIndex);
|
|
|
44 |
if(n){
|
|
|
45 |
dojo.toggleClass(n, this.focusClass, inBork);
|
|
|
46 |
this.scrollIntoView();
|
|
|
47 |
try{
|
|
|
48 |
if(!this.grid.edit.isEditing())
|
|
|
49 |
dojox.grid.fire(n, "focus");
|
|
|
50 |
}catch(e){}
|
|
|
51 |
}
|
|
|
52 |
},
|
|
|
53 |
scrollIntoView: function() {
|
|
|
54 |
if(!this.cell){
|
|
|
55 |
return;
|
|
|
56 |
}
|
|
|
57 |
var
|
|
|
58 |
c = this.cell,
|
|
|
59 |
s = c.view.scrollboxNode,
|
|
|
60 |
sr = {
|
|
|
61 |
w: s.clientWidth,
|
|
|
62 |
l: s.scrollLeft,
|
|
|
63 |
t: s.scrollTop,
|
|
|
64 |
h: s.clientHeight
|
|
|
65 |
},
|
|
|
66 |
n = c.getNode(this.rowIndex),
|
|
|
67 |
r = c.view.getRowNode(this.rowIndex),
|
|
|
68 |
rt = this.grid.scroller.findScrollTop(this.rowIndex);
|
|
|
69 |
// place cell within horizontal view
|
|
|
70 |
if(n.offsetLeft + n.offsetWidth > sr.l + sr.w){
|
|
|
71 |
s.scrollLeft = n.offsetLeft + n.offsetWidth - sr.w;
|
|
|
72 |
}else if(n.offsetLeft < sr.l){
|
|
|
73 |
s.scrollLeft = n.offsetLeft;
|
|
|
74 |
}
|
|
|
75 |
// place cell within vertical view
|
|
|
76 |
if(rt + r.offsetHeight > sr.t + sr.h){
|
|
|
77 |
this.grid.setScrollTop(rt + r.offsetHeight - sr.h);
|
|
|
78 |
}else if(rt < sr.t){
|
|
|
79 |
this.grid.setScrollTop(rt);
|
|
|
80 |
}
|
|
|
81 |
},
|
|
|
82 |
styleRow: function(inRow){
|
|
|
83 |
if(inRow.index == this.rowIndex){
|
|
|
84 |
this._focusifyCellNode(true);
|
|
|
85 |
}
|
|
|
86 |
},
|
|
|
87 |
setFocusIndex: function(inRowIndex, inCellIndex){
|
|
|
88 |
// summary:
|
|
|
89 |
// focuses the given grid cell
|
|
|
90 |
// inRowIndex: int
|
|
|
91 |
// grid row index
|
|
|
92 |
// inCellIndex: int
|
|
|
93 |
// grid cell index
|
|
|
94 |
this.setFocusCell(this.grid.getCell(inCellIndex), inRowIndex);
|
|
|
95 |
},
|
|
|
96 |
setFocusCell: function(inCell, inRowIndex){
|
|
|
97 |
// summary:
|
|
|
98 |
// focuses the given grid cell
|
|
|
99 |
// inCell: object
|
|
|
100 |
// grid cell object
|
|
|
101 |
// inRowIndex: int
|
|
|
102 |
// grid row index
|
|
|
103 |
if(inCell && !this.isFocusCell(inCell, inRowIndex)){
|
|
|
104 |
this.tabbingOut = false;
|
|
|
105 |
this.focusGrid();
|
|
|
106 |
this._focusifyCellNode(false);
|
|
|
107 |
this.cell = inCell;
|
|
|
108 |
this.rowIndex = inRowIndex;
|
|
|
109 |
this._focusifyCellNode(true);
|
|
|
110 |
}
|
|
|
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
|
|
|
113 |
if(dojo.isOpera){
|
|
|
114 |
setTimeout(dojo.hitch(this.grid, 'onCellFocus', this.cell, this.rowIndex), 1);
|
|
|
115 |
}else{
|
|
|
116 |
this.grid.onCellFocus(this.cell, this.rowIndex);
|
|
|
117 |
}
|
|
|
118 |
},
|
|
|
119 |
next: function(){
|
|
|
120 |
// summary:
|
|
|
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;
|
|
|
123 |
if(col > cc){
|
|
|
124 |
col = 0;
|
|
|
125 |
row++;
|
|
|
126 |
}
|
|
|
127 |
if(row > rc){
|
|
|
128 |
col = cc;
|
|
|
129 |
row = rc;
|
|
|
130 |
}
|
|
|
131 |
this.setFocusIndex(row, col);
|
|
|
132 |
},
|
|
|
133 |
previous: function(){
|
|
|
134 |
// summary:
|
|
|
135 |
// focus previous grid cell
|
|
|
136 |
var row=(this.rowIndex || 0), col=(this.cell.index || 0) - 1;
|
|
|
137 |
if(col < 0){
|
|
|
138 |
col = this.grid.layout.cellCount-1;
|
|
|
139 |
row--;
|
|
|
140 |
}
|
|
|
141 |
if(row < 0){
|
|
|
142 |
row = 0;
|
|
|
143 |
col = 0;
|
|
|
144 |
}
|
|
|
145 |
this.setFocusIndex(row, col);
|
|
|
146 |
},
|
|
|
147 |
move: function(inRowDelta, inColDelta) {
|
|
|
148 |
// summary:
|
|
|
149 |
// focus grid cell based on position relative to current focus
|
|
|
150 |
// inRowDelta: int
|
|
|
151 |
// vertical distance from current focus
|
|
|
152 |
// inColDelta: int
|
|
|
153 |
// horizontal distance from current focus
|
|
|
154 |
var
|
|
|
155 |
rc = this.grid.rowCount-1,
|
|
|
156 |
cc = this.grid.layout.cellCount-1,
|
|
|
157 |
r = this.rowIndex,
|
|
|
158 |
i = this.cell.index,
|
|
|
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);
|
|
|
162 |
if(inRowDelta){
|
|
|
163 |
this.grid.updateRow(r);
|
|
|
164 |
}
|
|
|
165 |
},
|
|
|
166 |
previousKey: function(e){
|
|
|
167 |
if(this.isFirstFocusCell()){
|
|
|
168 |
this.tabOut(this.grid.domNode);
|
|
|
169 |
}else{
|
|
|
170 |
dojo.stopEvent(e);
|
|
|
171 |
this.previous();
|
|
|
172 |
}
|
|
|
173 |
},
|
|
|
174 |
nextKey: function(e) {
|
|
|
175 |
if(this.isLastFocusCell()){
|
|
|
176 |
this.tabOut(this.grid.lastFocusNode);
|
|
|
177 |
}else{
|
|
|
178 |
dojo.stopEvent(e);
|
|
|
179 |
this.next();
|
|
|
180 |
}
|
|
|
181 |
},
|
|
|
182 |
tabOut: function(inFocusNode){
|
|
|
183 |
this.tabbingOut = true;
|
|
|
184 |
inFocusNode.focus();
|
|
|
185 |
},
|
|
|
186 |
focusGrid: function(){
|
|
|
187 |
dojox.grid.fire(this.focusView, "focus");
|
|
|
188 |
this._focusifyCellNode(true);
|
|
|
189 |
},
|
|
|
190 |
doFocus: function(e){
|
|
|
191 |
// trap focus only for grid dom node
|
|
|
192 |
if(e && e.target != e.currentTarget){
|
|
|
193 |
return;
|
|
|
194 |
}
|
|
|
195 |
// do not focus for scrolling if grid is about to blur
|
|
|
196 |
if(!this.tabbingOut && this.isNoFocusCell()){
|
|
|
197 |
// establish our virtual-focus, if necessary
|
|
|
198 |
this.setFocusIndex(0, 0);
|
|
|
199 |
}
|
|
|
200 |
this.tabbingOut = false;
|
|
|
201 |
}
|
|
|
202 |
});
|
|
|
203 |
|
|
|
204 |
}
|