]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/grid/_grid/edit.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / grid / _grid / edit.js
1 if(!dojo._hasResource["dojox.grid._grid.edit"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.grid._grid.edit"] = true;
3 dojo.provide("dojox.grid._grid.edit");
4
5 dojo.declare("dojox.grid.edit", null, {
6         // summary:
7         //              Controls grid cell editing process. Owned by grid and used internally for editing.
8         constructor: function(inGrid){
9                 // inGrid: dojox.Grid
10                 //              The dojox.Grid this editor should be attached to
11                 this.grid = inGrid;
12                 this.connections = [];
13                 if(dojo.isIE){
14                         this.connections.push(dojo.connect(document.body, "onfocus", dojo.hitch(this, "_boomerangFocus")));
15                 }
16         },
17         
18         info: {},
19
20         destroy: function(){
21                 dojo.forEach(this.connections,dojo.disconnect);
22         },
23
24         cellFocus: function(inCell, inRowIndex){
25                 // summary:
26                 //              Invoke editing when cell is focused
27                 // inCell: cell object
28                 //              Grid cell object
29                 // inRowIndex: Integer
30                 //              Grid row index
31                 if(this.grid.singleClickEdit || this.isEditRow(inRowIndex)){
32                         // if same row or quick editing, edit
33                         this.setEditCell(inCell, inRowIndex);
34                 }else{
35                         // otherwise, apply any pending row edits
36                         this.apply();
37                 }
38                 // if dynamic or static editing...
39                 if(this.isEditing() || (inCell && (inCell.editor||0).alwaysOn)){
40                         // let the editor focus itself as needed
41                         this._focusEditor(inCell, inRowIndex);
42                 }
43         },
44
45         rowClick: function(e){
46                 if(this.isEditing() && !this.isEditRow(e.rowIndex)){
47                         this.apply();
48                 }
49         },
50
51         styleRow: function(inRow){
52                 if(inRow.index == this.info.rowIndex){
53                         inRow.customClasses += ' dojoxGrid-row-editing';
54                 }
55         },
56
57         dispatchEvent: function(e){
58                 var c = e.cell, ed = c && c.editor;
59                 return ed && ed.dispatchEvent(e.dispatch, e);
60         },
61
62         // Editing
63         isEditing: function(){
64                 // summary:
65                 //              Indicates editing state of the grid.
66                 // returns: Boolean
67                 //              True if grid is actively editing
68                 return this.info.rowIndex !== undefined;
69         },
70
71         isEditCell: function(inRowIndex, inCellIndex){
72                 // summary:
73                 //              Indicates if the given cell is being edited.
74                 // inRowIndex: Integer
75                 //              Grid row index
76                 // inCellIndex: Integer
77                 //              Grid cell index
78                 // returns: Boolean
79                 //              True if given cell is being edited
80                 return (this.info.rowIndex === inRowIndex) && (this.info.cell.index == inCellIndex);
81         },
82
83         isEditRow: function(inRowIndex){
84                 // summary:
85                 //              Indicates if the given row is being edited.
86                 // inRowIndex: Integer
87                 //              Grid row index
88                 // returns: Boolean
89                 //              True if given row is being edited
90                 return this.info.rowIndex === inRowIndex;
91         },
92
93         setEditCell: function(inCell, inRowIndex){
94                 // summary:
95                 //              Set the given cell to be edited
96                 // inRowIndex: Integer
97                 //              Grid row index
98                 // inCell: Object
99                 //              Grid cell object
100                 if(!this.isEditCell(inRowIndex, inCell.index) && this.grid.canEdit(inCell, inRowIndex)){
101                         this.start(inCell, inRowIndex, this.isEditRow(inRowIndex) || inCell.editor);
102                 }
103         },
104
105         _focusEditor: function(inCell, inRowIndex){
106                 dojox.grid.fire(inCell.editor, "focus", [inRowIndex]);
107         },
108
109         focusEditor: function(){
110                 if(this.isEditing()){
111                         this._focusEditor(this.info.cell, this.info.rowIndex);
112                 }
113         },
114
115         // implement fix for focus boomerang effect on IE
116         _boomerangWindow: 500,
117         _shouldCatchBoomerang: function(){
118                 return this._catchBoomerang > new Date().getTime();
119         },
120         _boomerangFocus: function(){
121                 //console.log("_boomerangFocus");
122                 if(this._shouldCatchBoomerang()){
123                         // make sure we don't utterly lose focus
124                         this.grid.focus.focusGrid();
125                         // let the editor focus itself as needed
126                         this.focusEditor();
127                         // only catch once
128                         this._catchBoomerang = 0;
129                 }
130         },
131         _doCatchBoomerang: function(){
132                 // give ourselves a few ms to boomerang IE focus effects
133                 if(dojo.isIE){this._catchBoomerang = new Date().getTime() + this._boomerangWindow;}
134         },
135         // end boomerang fix API
136
137         start: function(inCell, inRowIndex, inEditing){
138                 this.grid.beginUpdate();
139                 this.editorApply();
140                 if(this.isEditing() && !this.isEditRow(inRowIndex)){
141                         this.applyRowEdit();
142                         this.grid.updateRow(inRowIndex);
143                 }
144                 if(inEditing){
145                         this.info = { cell: inCell, rowIndex: inRowIndex };
146                         this.grid.doStartEdit(inCell, inRowIndex); 
147                         this.grid.updateRow(inRowIndex);
148                 }else{
149                         this.info = {};
150                 }
151                 this.grid.endUpdate();
152                 // make sure we don't utterly lose focus
153                 this.grid.focus.focusGrid();
154                 // let the editor focus itself as needed
155                 this._focusEditor(inCell, inRowIndex);
156                 // give ourselves a few ms to boomerang IE focus effects
157                 this._doCatchBoomerang();
158         },
159
160         _editorDo: function(inMethod){
161                 var c = this.info.cell
162                 //c && c.editor && c.editor[inMethod](c, this.info.rowIndex);
163                 c && c.editor && c.editor[inMethod](this.info.rowIndex);
164         },
165
166         editorApply: function(){
167                 this._editorDo("apply");
168         },
169
170         editorCancel: function(){
171                 this._editorDo("cancel");
172         },
173
174         applyCellEdit: function(inValue, inCell, inRowIndex){
175                 if(this.grid.canEdit(inCell, inRowIndex)){
176                         this.grid.doApplyCellEdit(inValue, inRowIndex, inCell.fieldIndex);
177                 }
178         },
179
180         applyRowEdit: function(){
181                 this.grid.doApplyEdit(this.info.rowIndex);
182         },
183
184         apply: function(){
185                 // summary:
186                 //              Apply a grid edit
187                 if(this.isEditing()){
188                         this.grid.beginUpdate();
189                         this.editorApply();
190                         this.applyRowEdit();
191                         this.info = {};
192                         this.grid.endUpdate();
193                         this.grid.focus.focusGrid();
194                         this._doCatchBoomerang();
195                 }
196         },
197
198         cancel: function(){
199                 // summary:
200                 //              Cancel a grid edit
201                 if(this.isEditing()){
202                         this.grid.beginUpdate();
203                         this.editorCancel();
204                         this.info = {};
205                         this.grid.endUpdate();
206                         this.grid.focus.focusGrid();
207                         this._doCatchBoomerang();
208                 }
209         },
210
211         save: function(inRowIndex, inView){
212                 // summary:
213                 //              Save the grid editing state
214                 // inRowIndex: Integer
215                 //              Grid row index
216                 // inView: Object
217                 //              Grid view
218                 var c = this.info.cell;
219                 if(this.isEditRow(inRowIndex) && (!inView || c.view==inView) && c.editor){
220                         c.editor.save(c, this.info.rowIndex);
221                 }
222         },
223
224         restore: function(inView, inRowIndex){
225                 // summary:
226                 //              Restores the grid editing state
227                 // inRowIndex: Integer
228                 //              Grid row index
229                 // inView: Object
230                 //              Grid view
231                 var c = this.info.cell;
232                 if(this.isEditRow(inRowIndex) && c.view == inView && c.editor){
233                         c.editor.restore(c, this.info.rowIndex);
234                 }
235         }
236 });
237
238 }