]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/grid/_grid/focus.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / grid / _grid / focus.js
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                         if (inBork){
47                                 this.scrollIntoView();
48                                 try{
49                                         if(!this.grid.edit.isEditing())
50                                                 dojox.grid.fire(n, "focus");
51                                 }catch(e){}
52                         }
53                 }
54         },
55         scrollIntoView: function() {
56                 if(!this.cell){
57                         return;
58                 }
59                 var 
60                         c = this.cell,
61                         s = c.view.scrollboxNode,
62                         sr = {
63                                 w: s.clientWidth,
64                                 l: s.scrollLeft,
65                                 t: s.scrollTop,
66                                 h: s.clientHeight
67                         },
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;
76                 }
77                 // place cell within vertical view
78                 if(rt + r.offsetHeight > sr.t + sr.h){
79                         this.grid.setScrollTop(rt + r.offsetHeight - sr.h);
80                 }else if(rt < sr.t){
81                         this.grid.setScrollTop(rt);
82                 }
83         },
84         styleRow: function(inRow){
85                 return;
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.focusGridView();
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         focusGridView: function(){
187                 dojox.grid.fire(this.focusView, "focus");
188         },
189         focusGrid: function(inSkipFocusCell){
190                 this.focusGridView();
191                 this._focusifyCellNode(true);
192         },
193         doFocus: function(e){
194                 // trap focus only for grid dom node
195                 if(e && e.target != e.currentTarget){
196                         return;
197                 }
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);
202                 }
203                 this.tabbingOut = false;
204         }
205 });
206
207 }