forked from webismymind/editablegrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditablegrid_editors.js
More file actions
315 lines (255 loc) · 11.7 KB
/
editablegrid_editors.js
File metadata and controls
315 lines (255 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* EditableGrid_editors.js
*
* Copyright 2010 Webismymind SPRL
*
* This file is part of EditableGrid.
*
* EditableGrid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* EditableGrid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EditableGrid. If not, see http://www.gnu.org/licenses.
*
*/
/**
* Abstract cell editor
* @constructor
* @class Base class for all cell editors
*/
function CellEditor(config) { this.init(config); }
CellEditor.prototype.init = function(config)
{
// override default properties with the ones given
for (var p in config) this[p] = config[p];
};
CellEditor.prototype.edit = function(rowIndex, columnIndex, element, value)
{
// tag element and remember all the things we need to apply/cancel edition
element.isEditing = true;
element.rowIndex = rowIndex;
element.columnIndex = columnIndex;
// call the specialized getEditor method
var editorInput = this.getEditor(element, value);
if (!editorInput) return false;
// give access to the cell editor and element from the editor widget
editorInput.element = element;
editorInput.celleditor = this;
// listen to pressed keys
// - tab does not work with onkeyup (it's too late)
// - on Safari escape does not work with onkeypress
// - with onkeydown everything is fine (but don't forget to return false)
editorInput.onkeydown = function(event) {
event = event || window.event;
// ENTER or TAB: apply value
if (event.keyCode == 13 || event.keyCode == 9) {
this.onblur = null;
this.celleditor.applyEditing(this.element, this.celleditor.getEditorValue(this));
return false;
}
// ESC: cancel editing
if (event.keyCode == 27) {
this.onblur = null;
this.celleditor.cancelEditing(this.element);
return false;
}
};
// and display the resulting editor widget
this.displayEditor(element, editorInput);
// give focus to the created editor
editorInput.focus();
// is simultaneous edition is not allowed, we cancel edition when focus is lost
if (!this.editablegrid.allowSimultaneousEdition) editorInput.onblur = this.editablegrid.saveOnBlur ?
function(event) { this.onblur = null; this.celleditor.applyEditing(this.element, this.celleditor.getEditorValue(this)); } :
function(event) { this.onblur = null; this.celleditor.cancelEditing(this.element); };
};
CellEditor.prototype.getEditor = function(element, value) {
return null;
};
CellEditor.prototype.getEditorValue = function(editorInput) {
return editorInput.value;
};
CellEditor.prototype.formatValue = function(value) {
return value;
};
CellEditor.prototype.displayEditor = function(element, editorInput)
{
// use same font in input as in cell content
editorInput.style.fontFamily = this.editablegrid.getStyle(element, "fontFamily", "font-family");
editorInput.style.fontSize = this.editablegrid.getStyle(element, "fontSize", "font-size");
// static mode: add input field in the table cell
if (this.editablegrid.editmode == "static") {
while (element.hasChildNodes()) element.removeChild(element.firstChild);
element.appendChild(editorInput);
}
// absolute mode: add input field in absolute position over table cell, leaving current content
if (this.editablegrid.editmode == "absolute") {
element.appendChild(editorInput);
editorInput.style.position = "absolute";
// position editor input on the cell with the same padding as the actual cell content
var paddingLeft = parseInt(this.editablegrid.getStyle(element, "paddingLeft", "padding-left"));
var paddingTop = parseInt(this.editablegrid.getStyle(element, "paddingTop", "padding-top"));
if (isNaN(paddingLeft)) paddingLeft = 0; else paddingLeft = Math.max(0, paddingLeft - 3);
if (isNaN(paddingTop)) paddingTop = 0; else paddingTop = Math.max(0, paddingTop - 3);
var offsetScrollX = this.editablegrid.table.parentNode ? parseInt(this.editablegrid.table.parentNode.scrollLeft) : 0;
var offsetScrollY = this.editablegrid.table.parentNode ? parseInt(this.editablegrid.table.parentNode.scrollTop) : 0;
editorInput.style.left = (this.editablegrid.getCellX(element) - offsetScrollX + paddingLeft) + "px";
editorInput.style.top = (this.editablegrid.getCellY(element) - offsetScrollY + paddingTop) + "px";
// if number type: align field and its content to the right
if (this.column.datatype == 'integer' || this.column.datatype == 'double') {
var rightPadding = this.editablegrid.getCellX(element) - offsetScrollX + element.offsetWidth - (parseInt(editorInput.style.left) + editorInput.offsetWidth);
editorInput.style.left = (parseInt(editorInput.style.left) + rightPadding) + "px";
editorInput.style.textAlign = "right";
}
}
// fixed mode: don't show input field in the cell
if (this.editablegrid.editmode == "fixed") {
var editorzone = _$(this.editablegrid.editorzoneid);
while (editorzone.hasChildNodes()) editorzone.removeChild(editorzone.firstChild);
editorzone.appendChild(editorInput);
}
};
CellEditor.prototype._clearEditor = function(element)
{
// untag element
element.isEditing = false;
// clear fixed editor zone if any
if (this.editablegrid.editmode == "fixed") {
var editorzone = _$(this.editablegrid.editorzoneid);
while (editorzone.hasChildNodes()) editorzone.removeChild(editorzone.firstChild);
}
};
CellEditor.prototype.cancelEditing = function(element)
{
with (this) {
// check that the element is still being edited (otherwise onblur will be called on textfields that have been closed when we go to another tab in Firefox)
if (element && element.isEditing) {
// render value before editon
var renderer = this == column.headerEditor ? column.headerRenderer : column.cellRenderer;
renderer._render(element.rowIndex, element.columnIndex, element, editablegrid.getValueAt(element.rowIndex, element.columnIndex));
_clearEditor(element);
}
}
};
CellEditor.prototype.applyEditing = function(element, newValue)
{
with (this) {
// check that the element is still being edited (otherwise onblur will be called on textfields that have been closed when we go to another tab in Firefox)
if (element && element.isEditing) {
// do nothing if the value is rejected by at least one validator
if (!column.isValid(newValue)) return false;
// format the value before applying
var formattedValue = formatValue(newValue);
// update model and render cell (keeping previous value)
var previousValue = editablegrid.setValueAt(element.rowIndex, element.columnIndex, formattedValue);
// if the new value is different than the previous one, let the user handle the model change
var newValue = editablegrid.getValueAt(element.rowIndex, element.columnIndex);
if (!this.editablegrid.isSame(newValue, previousValue)) {
editablegrid.modelChanged(element.rowIndex, element.columnIndex, previousValue, newValue, editablegrid.getRow(element.rowIndex));
}
_clearEditor(element);
}
}
};
/**
* Text cell editor
* @constructor
* @class Class to edit a cell with an HTML text input
*/
function TextCellEditor(size, maxlen, config) { this.fieldSize = size || -1; this.maxLength = maxlen || 255; if (config) this.init(config); };
TextCellEditor.prototype = new CellEditor();
TextCellEditor.prototype.editorValue = function(value) {
return value;
};
TextCellEditor.prototype.updateStyle = function(htmlInput)
{
// change style for invalid values
if (this.column.isValid(this.getEditorValue(htmlInput))) this.editablegrid.removeClassName(htmlInput, this.editablegrid.invalidClassName);
else this.editablegrid.addClassName(htmlInput, this.editablegrid.invalidClassName);
};
TextCellEditor.prototype.getEditor = function(element, value)
{
// create and initialize text field
var htmlInput = document.createElement("input");
htmlInput.setAttribute("type", "text");
if (this.maxLength > 0) htmlInput.setAttribute("maxlength", this.maxLength);
if (this.fieldSize > 0) htmlInput.setAttribute("size", this.fieldSize);
else htmlInput.style.width = this.editablegrid.autoWidth(element) + 'px'; // auto-adapt width to cell, if no length specified
var autoHeight = this.editablegrid.autoHeight(element);
if (this.editablegrid.Browser.Gecko) autoHeight -= 2; // Firefox: input higher then given size in px!
htmlInput.style.height = autoHeight + 'px'; // auto-adapt height to cell
htmlInput.value = this.editorValue(value);
// listen to keyup to check validity and update style of input field
htmlInput.onkeyup = function(event) { this.celleditor.updateStyle(this); };
return htmlInput;
};
TextCellEditor.prototype.displayEditor = function(element, htmlInput)
{
// call base method
CellEditor.prototype.displayEditor.call(this, element, htmlInput);
// update style of input field
this.updateStyle(htmlInput);
// select text
htmlInput.select();
};
/**
* Number cell editor
* @constructor
* @class Class to edit a numeric cell with an HTML text input
*/
function NumberCellEditor(type) { this.type = type; }
NumberCellEditor.prototype = new TextCellEditor(-1, 32);
NumberCellEditor.prototype.editorValue = function(value) {
return isNaN(value) ? "" : value;
};
NumberCellEditor.prototype.formatValue = function(value)
{
return this.type == 'integer' ? parseInt(value) : parseFloat(value);
};
/**
* Select cell editor
* @constructor
* @class Class to edit a cell with an HTML select input
*/
function SelectCellEditor() { this.minWidth = 100; this.minHeight = 22; this.adaptHeight = true; this.adaptWidth = true;}
SelectCellEditor.prototype = new CellEditor();
SelectCellEditor.prototype.getEditor = function(element, value)
{
// create select list
var htmlInput = document.createElement("select");
// auto adapt dimensions to cell, with a min width
if (this.adaptWidth) htmlInput.style.width = Math.max(this.minWidth, this.editablegrid.autoWidth(element)) + 'px';
if (this.adaptHeight) htmlInput.style.height = Math.max(this.minHeight, this.editablegrid.autoHeight(element)) + 'px';
// get column option values for this row
var optionValues = this.column.getOptionValuesForEdit(element.rowIndex);
// add these options, selecting the current one
var index = 0, valueFound = false;
for (var optionValue in optionValues) {
var option = document.createElement('option');
option.text = optionValues[optionValue];
option.value = optionValue;
// add does not work as expected in IE7 (cf. second arg)
try { htmlInput.add(option, null); } catch (e) { htmlInput.add(option); }
if (optionValue == value) { htmlInput.selectedIndex = index; valueFound = true; }
index++;
}
// if the current value is not in the list add it to the front
if (!valueFound) {
var option = document.createElement('option');
option.text = value ? value : "";
option.value = value ? value : "";
// add does not work as expected in IE7 (cf. second arg)
try { htmlInput.add(option, htmlInput.options[0]); } catch (e) { htmlInput.add(option); }
htmlInput.selectedIndex = 0;
}
// when a new value is selected we apply it
htmlInput.onchange = function(event) { this.onblur = null; this.celleditor.applyEditing(this.element, this.value); };
return htmlInput;
};