Roo/grid/EditorGrid.js
[roojs1] / Roo / grid / EditorGrid.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.grid.EditorGrid
14  * @extends Roo.grid.Grid
15  * Class for creating and editable grid.
16  * @param {String/HTMLElement/Roo.Element} container The element into which this grid will be rendered - 
17  * The container MUST have some type of size defined for the grid to fill. The container will be 
18  * automatically set to position relative if it isn't already.
19  * @param {Object} dataSource The data model to bind to
20  * @param {Object} colModel The column model with info about this grid's columns
21  */
22 Roo.grid.EditorGrid = function(container, config){
23     Roo.grid.EditorGrid.superclass.constructor.call(this, container, config);
24     this.getGridEl().addClass("xedit-grid");
25
26     if(!this.selModel){
27         this.selModel = new Roo.grid.CellSelectionModel();
28     }
29
30     this.activeEditor = null;
31
32         this.addEvents({
33             /**
34              * @event beforeedit
35              * Fires before cell editing is triggered. The edit event object has the following properties <br />
36              * <ul style="padding:5px;padding-left:16px;">
37              * <li>grid - This grid</li>
38              * <li>record - The record being edited</li>
39              * <li>field - The field name being edited</li>
40              * <li>value - The value for the field being edited.</li>
41              * <li>row - The grid row index</li>
42              * <li>column - The grid column index</li>
43              * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
44              * </ul>
45              * @param {Object} e An edit event (see above for description)
46              */
47             "beforeedit" : true,
48             /**
49              * @event afteredit
50              * Fires after a cell is edited. <br />
51              * <ul style="padding:5px;padding-left:16px;">
52              * <li>grid - This grid</li>
53              * <li>record - The record being edited</li>
54              * <li>field - The field name being edited</li>
55              * <li>value - The value being set</li>
56              * <li>originalValue - The original value for the field, before the edit.</li>
57              * <li>row - The grid row index</li>
58              * <li>column - The grid column index</li>
59              * </ul>
60              * @param {Object} e An edit event (see above for description)
61              */
62             "afteredit" : true,
63             /**
64              * @event validateedit
65              * Fires after a cell is edited, but before the value is set in the record. 
66          * You can use this to modify the value being set in the field, Return false
67              * to cancel the change. The edit event object has the following properties <br />
68              * <ul style="padding:5px;padding-left:16px;">
69          * <li>editor - This editor</li>
70              * <li>grid - This grid</li>
71              * <li>record - The record being edited</li>
72              * <li>field - The field name being edited</li>
73              * <li>value - The value being set</li>
74              * <li>originalValue - The original value for the field, before the edit.</li>
75              * <li>row - The grid row index</li>
76              * <li>column - The grid column index</li>
77              * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
78              * </ul>
79              * @param {Object} e An edit event (see above for description)
80              */
81             "validateedit" : true
82         });
83     this.on("bodyscroll", this.stopEditing,  this);
84     this.on(this.clicksToEdit == 1 ? "cellclick" : "celldblclick", this.onCellDblClick,  this);
85 };
86
87 Roo.extend(Roo.grid.EditorGrid, Roo.grid.Grid, {
88     /**
89      * @cfg {Number} clicksToEdit
90      * The number of clicks on a cell required to display the cell's editor (defaults to 2)
91      */
92     clicksToEdit: 2,
93
94     // private
95     isEditor : true,
96     // private
97     trackMouseOver: false, // causes very odd FF errors
98
99     onCellDblClick : function(g, row, col){
100         this.startEditing(row, col);
101     },
102
103     onEditComplete : function(ed, value, startValue){
104         this.editing = false;
105         this.activeEditor = null;
106         ed.un("specialkey", this.selModel.onEditorKey, this.selModel);
107         var r = ed.record;
108         var field = this.colModel.getDataIndex(ed.col);
109         var e = {
110             grid: this,
111             record: r,
112             field: field,
113             originalValue: startValue,
114             value: value,
115             row: ed.row,
116             column: ed.col,
117             cancel:false,
118             editor: ed
119         };
120         if(String(value) !== String(startValue)){
121             
122             if(this.fireEvent("validateedit", e) !== false && !e.cancel){
123                 r.set(field, e.value);
124                 // if we are dealing with a combo box..
125                 // then we also set the 'name' colum to be the displayField
126                 if (ed.field.displayField && ed.field.name) {
127                     r.set(ed.field.name, ed.field.el.dom.value);
128                 }
129                 
130                 delete e.cancel; //?? why!!!
131                 this.fireEvent("afteredit", e);
132             }
133         } else {
134             this.fireEvent("afteredit", e); // always fire it!
135         }
136         this.view.focusCell(ed.row, ed.col);
137     },
138
139     /**
140      * Starts editing the specified for the specified row/column
141      * @param {Number} rowIndex
142      * @param {Number} colIndex
143      */
144     startEditing : function(row, col){
145         this.stopEditing();
146         if(this.colModel.isCellEditable(col, row)){
147             this.view.ensureVisible(row, col, true);
148             var view = this.view;
149             var r = this.dataSource.getAt(row);
150             var field = this.colModel.getDataIndex(col);
151              var cell = Roo.get(view.getCell(row,col))
152             var e = {
153                 grid: this,
154                 record: r,
155                 field: field,
156                 value: r.data[field],
157                 row: row,
158                 column: col,
159                 cancel:false 
160             };
161             if(this.fireEvent("beforeedit", e) !== false && !e.cancel){
162                 this.editing = true;
163                 var ed = this.colModel.getCellEditor(col, row);
164                 
165                 if (!ed) {
166                     return;
167                 }
168                 if(!ed.rendered){
169                     ed.render(ed.parentEl || document.body);
170                 }
171                 ed.field.reset();
172                
173                 cell.hide();
174                 
175                 (function(){ // complex but required for focus issues in safari, ie and opera
176                     ed.row = row;
177                     ed.col = col;
178                     ed.record = r;
179                     ed.on("complete",   this.onEditComplete,        this,       {single: true});
180                     ed.on("specialkey", this.selModel.onEditorKey,  this.selModel);
181                     this.activeEditor = ed;
182                     var v = r.data[field];
183                     ed.startEdit(this.view.getCell(row, col), v);
184                     // combo's with 'displayField and name set
185                     if (ed.field.displayField && ed.field.name) {
186                         ed.field.el.dom.value = r.data[ed.field.name];
187                     }
188                     
189                     
190                 }).defer(50, this);
191             }
192         }
193     },
194         
195     /**
196      * Stops any active editing
197      */
198     stopEditing : function(){
199         if(this.activeEditor){
200             this.activeEditor.completeEdit();
201         }
202         this.activeEditor = null;
203     }
204 });