source: Dev/branches/rest-dojo-ui/client/dojox/grid/_EditManager.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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