forked from webismymind/editablegrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editablegrid_renderers.js
292 lines (239 loc) · 10.1 KB
/
editablegrid_renderers.js
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
/**
* Abstract cell renderer
* @constructor
* @class Base class for all cell renderers
* @param {Object} config
*/
function CellRenderer(config) { this.init(config); }
CellRenderer.prototype.init = function(config)
{
// override default properties with the ones given
for (var p in config) this[p] = config[p];
};
CellRenderer.prototype._render = function(rowIndex, columnIndex, element, value)
{
// remember all the things we need
element.rowIndex = rowIndex;
element.columnIndex = columnIndex;
// remove existing content
while (element.hasChildNodes()) element.removeChild(element.firstChild);
// clear isEditing (in case a currently editeed is being re-rendered by some external call)
element.isEditing = false;
// always apply the number class to numerical cells and column headers
if (this.column.isNumerical()) EditableGrid.prototype.addClassName(element, "number");
// always apply the boolean class to boolean column headers
if (this.column.datatype == 'boolean') EditableGrid.prototype.addClassName(element, "boolean");
// apply a css class corresponding to the column name
EditableGrid.prototype.addClassName(element, "editablegrid-" + this.column.name);
// add a data-title attribute used for responsiveness
element.setAttribute('data-title', this.column.label);
// call the specialized render method
return this.render(element, typeof value == 'string' && this.column.datatype != "html" ? (value === null ? null : htmlspecialchars(value, 'ENT_NOQUOTES').replace(/ /g, ' ')) : value);
};
CellRenderer.prototype.render = function(element, value, escapehtml)
{
var _value = escapehtml ? (typeof value == 'string' && this.column.datatype != "html" ? (value === null ? null : htmlspecialchars(value, 'ENT_NOQUOTES').replace(/ /g, ' ')) : value) : value;
element.innerHTML = _value ? _value : "";
};
CellRenderer.prototype.getDisplayValue = function(rowIndex, value)
{
return value;
};
/**
* Enum cell renderer
* @constructor
* @class Class to render a cell with enum values
*/
function EnumCellRenderer(config) { this.init(config); }
EnumCellRenderer.prototype = new CellRenderer();
EnumCellRenderer.prototype.getLabel = function(rowIndex, value)
{
var label = null;
if (typeof value != 'undefined') {
value = value ? value : '';
var optionValues = this.column.getOptionValuesForRender(rowIndex);
if (optionValues && value in optionValues) label = optionValues[value];
if (label === null) {
var isNAN = typeof value == 'number' && isNaN(value);
label = isNAN ? "" : value;
}
}
return label ? label : '';
};
EnumCellRenderer.prototype.render = function(element, value)
{
var label = this.getLabel(element.rowIndex, value);
element.innerHTML = label ? (this.column.datatype != "html" ? htmlspecialchars(label, 'ENT_NOQUOTES').replace(/\s\s/g, ' ') : label) : '';
};
EnumCellRenderer.prototype.getDisplayValue = function(rowIndex, value)
{
// if the column has enumerated values, sort and filter on the value label
return value === null ? null : this.getLabel(rowIndex, value);
};
/**
* Number cell renderer
* @constructor
* @class Class to render a cell with numerical values
*/
function NumberCellRenderer(config) { this.init(config); }
NumberCellRenderer.prototype = new CellRenderer();
NumberCellRenderer.prototype.render = function(element, value)
{
var column = this.column || {}; // in case somebody calls new NumberCellRenderer().render(..)
var isNAN = value === null || (typeof value == 'number' && isNaN(value));
var displayValue = isNAN ? (column.nansymbol || "") : value;
if (typeof displayValue == 'number') {
if (column.precision !== null) {
// displayValue = displayValue.toFixed(column.precision);
displayValue = number_format(displayValue, column.precision, column.decimal_point, column.thousands_separator);
}
if (column.unit !== null) {
if (column.unit_before_number) displayValue = column.unit + ' ' + displayValue;
else displayValue = displayValue + ' ' + column.unit;
}
}
element.innerHTML = displayValue;
if (isNAN) EditableGrid.prototype.addClassName(element, "nan");
else EditableGrid.prototype.removeClassName(element, "nan");
};
/**
* Checkbox cell renderer
* @constructor
* @class Class to render a cell with an HTML checkbox
*/
function CheckboxCellRenderer(config) { this.init(config); }
CheckboxCellRenderer.prototype = new CellRenderer();
CheckboxCellRenderer.prototype._render = function(rowIndex, columnIndex, element, value)
{
// if a checkbox already exists keep it, otherwise clear current content
if (element.firstChild && (typeof element.firstChild.getAttribute != "function" || element.firstChild.getAttribute("type") != "checkbox"))
while (element.hasChildNodes()) element.removeChild(element.firstChild);
// remember all the things we need
element.rowIndex = rowIndex;
element.columnIndex = columnIndex;
// apply a css class corresponding to the column name
EditableGrid.prototype.addClassName(element, "editablegrid-" + this.column.name);
// add a data-title attribute used for responsiveness
element.setAttribute('data-title', this.column.label);
// call the specialized render method
return this.render(element, value);
};
CheckboxCellRenderer.prototype.render = function(element, value)
{
// convert value to boolean just in case
value = (value && value != 0 && value != "false") ? true : false;
// if check box already created, just update its state
if (element.firstChild) { element.firstChild.checked = value; return; }
// create and initialize checkbox
var htmlInput = document.createElement("input");
htmlInput.setAttribute("type", "checkbox");
// give access to the cell editor and element from the editor field
htmlInput.element = element;
htmlInput.cellrenderer = this;
// this renderer is a little special because it allows direct edition
var cellEditor = new CellEditor();
cellEditor.editablegrid = this.editablegrid;
cellEditor.column = this.column;
htmlInput.onclick = function(event) {
element.rowIndex = this.cellrenderer.editablegrid.getRowIndex(element.parentNode); // in case it has changed due to sorting or remove
element.isEditing = true;
cellEditor.applyEditing(element, htmlInput.checked ? true : false);
};
element.appendChild(htmlInput);
htmlInput.checked = value;
htmlInput.disabled = (!this.column.editable || !this.editablegrid.isEditable(element.rowIndex, element.columnIndex));
EditableGrid.prototype.addClassName(element, "boolean");
};
/**
* Email cell renderer
* @constructor
* @class Class to render a cell with emails
*/
function EmailCellRenderer(config) { this.init(config); }
EmailCellRenderer.prototype = new CellRenderer();
EmailCellRenderer.prototype.render = function(element, value)
{
element.innerHTML = value ? "<a href='mailto:" + value + "'>" + value + "</a>" : "";
};
/**
* Website cell renderer
* @constructor
* @class Class to render a cell with websites
*/
function WebsiteCellRenderer(config) { this.init(config); }
WebsiteCellRenderer.prototype = new CellRenderer();
WebsiteCellRenderer.prototype.render = function(element, value)
{
element.innerHTML = value ? "<a href='" + (value.indexOf("//") == -1 ? "http://" + value : value) + "'>" + value + "</a>" : "";
};
/**
* Date cell renderer
* @constructor
* @class Class to render a cell containing a date
*/
function DateCellRenderer(config) { this.init(config); }
DateCellRenderer.prototype = new CellRenderer;
DateCellRenderer.prototype.render = function(cell, value)
{
var date = this.editablegrid.checkDate(value);
if (typeof date == "object") cell.innerHTML = date.formattedDate;
else cell.innerHTML = value ? value : "";
cell.style.whiteSpace = 'nowrap';
};
/**
* Sort header renderer
* @constructor
* @class Class to add sorting functionalities to headers
*/
function SortHeaderRenderer(columnName, cellRenderer) { this.columnName = columnName; this.cellRenderer = cellRenderer; };
SortHeaderRenderer.prototype = new CellRenderer();
SortHeaderRenderer.prototype.render = function(cell, value)
{
if (!value) { if (this.cellRenderer) this.cellRenderer.render(cell, value); }
else {
// create a link that will sort (alternatively ascending/descending)
var link = document.createElement("a");
cell.appendChild(link);
link.columnName = this.columnName;
link.style.cursor = "pointer";
link.innerHTML = value;
link.editablegrid = this.editablegrid;
link.renderer = this;
link.onclick = function() {
with (this.editablegrid) {
var cols = tHead.rows[0].cells;
var clearPrevious = -1;
var backOnFirstPage = false;
if (sortedColumnName != this.columnName) {
clearPrevious = sortedColumnName;
sortedColumnName = this.columnName;
sortDescending = false;
backOnFirstPage = true;
}
else {
if (!sortDescending) sortDescending = true;
else {
clearPrevious = sortedColumnName;
sortedColumnName = -1;
sortDescending = false;
backOnFirstPage = true;
}
}
// render header for previous sort column (not needed anymore since the grid is now fully refreshed after a sort - cf. possible pagination)
// var j = getColumnIndex(clearPrevious);
// if (j >= 0) columns[j].headerRenderer._render(-1, j, cols[j], columns[j].label);
sort(sortedColumnName, sortDescending, backOnFirstPage);
// render header for new sort column (not needed anymore since the grid is now fully refreshed after a sort - cf. possible pagination)
// var j = getColumnIndex(sortedColumnName);
// if (j >= 0) columns[j].headerRenderer._render(-1, j, cols[j], columns[j].label);
}
};
// add an arrow to indicate if sort is ascending or descending
if (this.editablegrid.sortedColumnName == this.columnName) {
cell.appendChild(document.createTextNode("\u00a0"));
cell.appendChild(this.editablegrid.sortDescending ? this.editablegrid.sortDownElement: this.editablegrid.sortUpElement);
}
// call user renderer if any
if (this.cellRenderer) this.cellRenderer.render(cell, value);
}
};