27 |
jpm |
1 |
/*
|
|
|
2 |
* Ext JS Library 2.0.2
|
|
|
3 |
* Copyright(c) 2006-2008, Ext JS, LLC.
|
|
|
4 |
* licensing@extjs.com
|
|
|
5 |
*
|
|
|
6 |
* http://extjs.com/license
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @class Ext.grid.EditorGridPanel
|
|
|
11 |
* @extends Ext.grid.GridPanel
|
|
|
12 |
* Class for creating and editable grid.
|
|
|
13 |
|
|
|
14 |
* @constructor
|
|
|
15 |
* @param {Object} config The config object
|
|
|
16 |
*/
|
|
|
17 |
Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, {
|
|
|
18 |
/**
|
|
|
19 |
* @cfg {Number} clicksToEdit
|
|
|
20 |
* The number of clicks on a cell required to display the cell's editor (defaults to 2)
|
|
|
21 |
*/
|
|
|
22 |
clicksToEdit: 2,
|
|
|
23 |
|
|
|
24 |
// private
|
|
|
25 |
isEditor : true,
|
|
|
26 |
// private
|
|
|
27 |
detectEdit: false,
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* @cfg {Boolean} autoEncode
|
|
|
31 |
* True to automatically HTML encode and decode values pre and post edit (defaults to false)
|
|
|
32 |
*/
|
|
|
33 |
autoEncode : false,
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @cfg {Boolean} trackMouseOver @hide
|
|
|
37 |
*/
|
|
|
38 |
// private
|
|
|
39 |
trackMouseOver: false, // causes very odd FF errors
|
|
|
40 |
|
|
|
41 |
// private
|
|
|
42 |
initComponent : function(){
|
|
|
43 |
Ext.grid.EditorGridPanel.superclass.initComponent.call(this);
|
|
|
44 |
|
|
|
45 |
if(!this.selModel){
|
|
|
46 |
this.selModel = new Ext.grid.CellSelectionModel();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
this.activeEditor = null;
|
|
|
50 |
|
|
|
51 |
this.addEvents(
|
|
|
52 |
/**
|
|
|
53 |
* @event beforeedit
|
|
|
54 |
* Fires before cell editing is triggered. The edit event object has the following properties <br />
|
|
|
55 |
* <ul style="padding:5px;padding-left:16px;">
|
|
|
56 |
* <li>grid - This grid</li>
|
|
|
57 |
* <li>record - The record being edited</li>
|
|
|
58 |
* <li>field - The field name being edited</li>
|
|
|
59 |
* <li>value - The value for the field being edited.</li>
|
|
|
60 |
* <li>row - The grid row index</li>
|
|
|
61 |
* <li>column - The grid column index</li>
|
|
|
62 |
* <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
|
|
|
63 |
* </ul>
|
|
|
64 |
* @param {Object} e An edit event (see above for description)
|
|
|
65 |
*/
|
|
|
66 |
"beforeedit",
|
|
|
67 |
/**
|
|
|
68 |
* @event afteredit
|
|
|
69 |
* Fires after a cell is edited. <br />
|
|
|
70 |
* <ul style="padding:5px;padding-left:16px;">
|
|
|
71 |
* <li>grid - This grid</li>
|
|
|
72 |
* <li>record - The record being edited</li>
|
|
|
73 |
* <li>field - The field name being edited</li>
|
|
|
74 |
* <li>value - The value being set</li>
|
|
|
75 |
* <li>originalValue - The original value for the field, before the edit.</li>
|
|
|
76 |
* <li>row - The grid row index</li>
|
|
|
77 |
* <li>column - The grid column index</li>
|
|
|
78 |
* </ul>
|
|
|
79 |
* @param {Object} e An edit event (see above for description)
|
|
|
80 |
*/
|
|
|
81 |
"afteredit",
|
|
|
82 |
/**
|
|
|
83 |
* @event validateedit
|
|
|
84 |
* Fires after a cell is edited, but before the value is set in the record. Return false
|
|
|
85 |
* to cancel the change. The edit event object has the following properties <br />
|
|
|
86 |
* <ul style="padding:5px;padding-left:16px;">
|
|
|
87 |
* <li>grid - This grid</li>
|
|
|
88 |
* <li>record - The record being edited</li>
|
|
|
89 |
* <li>field - The field name being edited</li>
|
|
|
90 |
* <li>value - The value being set</li>
|
|
|
91 |
* <li>originalValue - The original value for the field, before the edit.</li>
|
|
|
92 |
* <li>row - The grid row index</li>
|
|
|
93 |
* <li>column - The grid column index</li>
|
|
|
94 |
* <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
|
|
|
95 |
* </ul>
|
|
|
96 |
* @param {Object} e An edit event (see above for description)
|
|
|
97 |
*/
|
|
|
98 |
"validateedit"
|
|
|
99 |
);
|
|
|
100 |
},
|
|
|
101 |
|
|
|
102 |
// private
|
|
|
103 |
initEvents : function(){
|
|
|
104 |
Ext.grid.EditorGridPanel.superclass.initEvents.call(this);
|
|
|
105 |
|
|
|
106 |
this.on("bodyscroll", this.stopEditing, this, [true]);
|
|
|
107 |
|
|
|
108 |
if(this.clicksToEdit == 1){
|
|
|
109 |
this.on("cellclick", this.onCellDblClick, this);
|
|
|
110 |
}else {
|
|
|
111 |
if(this.clicksToEdit == 'auto' && this.view.mainBody){
|
|
|
112 |
this.view.mainBody.on("mousedown", this.onAutoEditClick, this);
|
|
|
113 |
}
|
|
|
114 |
this.on("celldblclick", this.onCellDblClick, this);
|
|
|
115 |
}
|
|
|
116 |
this.getGridEl().addClass("xedit-grid");
|
|
|
117 |
},
|
|
|
118 |
|
|
|
119 |
// private
|
|
|
120 |
onCellDblClick : function(g, row, col){
|
|
|
121 |
this.startEditing(row, col);
|
|
|
122 |
},
|
|
|
123 |
|
|
|
124 |
// private
|
|
|
125 |
onAutoEditClick : function(e, t){
|
|
|
126 |
if(e.button !== 0){
|
|
|
127 |
return;
|
|
|
128 |
}
|
|
|
129 |
var row = this.view.findRowIndex(t);
|
|
|
130 |
var col = this.view.findCellIndex(t);
|
|
|
131 |
if(row !== false && col !== false){
|
|
|
132 |
this.stopEditing();
|
|
|
133 |
if(this.selModel.getSelectedCell){ // cell sm
|
|
|
134 |
var sc = this.selModel.getSelectedCell();
|
|
|
135 |
if(sc && sc.cell[0] === row && sc.cell[1] === col){
|
|
|
136 |
this.startEditing(row, col);
|
|
|
137 |
}
|
|
|
138 |
}else{
|
|
|
139 |
if(this.selModel.isSelected(row)){
|
|
|
140 |
this.startEditing(row, col);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
},
|
|
|
145 |
|
|
|
146 |
// private
|
|
|
147 |
onEditComplete : function(ed, value, startValue){
|
|
|
148 |
this.editing = false;
|
|
|
149 |
this.activeEditor = null;
|
|
|
150 |
ed.un("specialkey", this.selModel.onEditorKey, this.selModel);
|
|
|
151 |
var r = ed.record;
|
|
|
152 |
var field = this.colModel.getDataIndex(ed.col);
|
|
|
153 |
value = this.postEditValue(value, startValue, r, field);
|
|
|
154 |
if(String(value) !== String(startValue)){
|
|
|
155 |
var e = {
|
|
|
156 |
grid: this,
|
|
|
157 |
record: r,
|
|
|
158 |
field: field,
|
|
|
159 |
originalValue: startValue,
|
|
|
160 |
value: value,
|
|
|
161 |
row: ed.row,
|
|
|
162 |
column: ed.col,
|
|
|
163 |
cancel:false
|
|
|
164 |
};
|
|
|
165 |
if(this.fireEvent("validateedit", e) !== false && !e.cancel){
|
|
|
166 |
r.set(field, e.value);
|
|
|
167 |
delete e.cancel;
|
|
|
168 |
this.fireEvent("afteredit", e);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
this.view.focusCell(ed.row, ed.col);
|
|
|
172 |
},
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Starts editing the specified for the specified row/column
|
|
|
176 |
* @param {Number} rowIndex
|
|
|
177 |
* @param {Number} colIndex
|
|
|
178 |
*/
|
|
|
179 |
startEditing : function(row, col){
|
|
|
180 |
this.stopEditing();
|
|
|
181 |
if(this.colModel.isCellEditable(col, row)){
|
|
|
182 |
this.view.ensureVisible(row, col, true);
|
|
|
183 |
var r = this.store.getAt(row);
|
|
|
184 |
var field = this.colModel.getDataIndex(col);
|
|
|
185 |
var e = {
|
|
|
186 |
grid: this,
|
|
|
187 |
record: r,
|
|
|
188 |
field: field,
|
|
|
189 |
value: r.data[field],
|
|
|
190 |
row: row,
|
|
|
191 |
column: col,
|
|
|
192 |
cancel:false
|
|
|
193 |
};
|
|
|
194 |
if(this.fireEvent("beforeedit", e) !== false && !e.cancel){
|
|
|
195 |
this.editing = true;
|
|
|
196 |
var ed = this.colModel.getCellEditor(col, row);
|
|
|
197 |
if(!ed.rendered){
|
|
|
198 |
ed.render(this.view.getEditorParent(ed));
|
|
|
199 |
}
|
|
|
200 |
(function(){ // complex but required for focus issues in safari, ie and opera
|
|
|
201 |
ed.row = row;
|
|
|
202 |
ed.col = col;
|
|
|
203 |
ed.record = r;
|
|
|
204 |
ed.on("complete", this.onEditComplete, this, {single: true});
|
|
|
205 |
ed.on("specialkey", this.selModel.onEditorKey, this.selModel);
|
|
|
206 |
this.activeEditor = ed;
|
|
|
207 |
var v = this.preEditValue(r, field);
|
|
|
208 |
ed.startEdit(this.view.getCell(row, col), v);
|
|
|
209 |
}).defer(50, this);
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
},
|
|
|
213 |
|
|
|
214 |
preEditValue : function(r, field){
|
|
|
215 |
return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlDecode(r.data[field]) : r.data[field];
|
|
|
216 |
},
|
|
|
217 |
|
|
|
218 |
postEditValue : function(value, originalValue, r, field){
|
|
|
219 |
return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
|
|
|
220 |
},
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Stops any active editing
|
|
|
224 |
* @param {Boolean} cancel (optional) True to cancel any changes
|
|
|
225 |
*/
|
|
|
226 |
stopEditing : function(cancel){
|
|
|
227 |
if(this.activeEditor){
|
|
|
228 |
this.activeEditor[cancel === true ? 'cancelEdit' : 'completeEdit']();
|
|
|
229 |
}
|
|
|
230 |
this.activeEditor = null;
|
|
|
231 |
}
|
|
|
232 |
});
|
|
|
233 |
Ext.reg('editorgrid', Ext.grid.EditorGridPanel);
|