forked from webismymind/editablegrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditablegrid_renderers.js
More file actions
256 lines (213 loc) · 8.38 KB
/
Copy patheditablegrid_renderers.js
File metadata and controls
256 lines (213 loc) · 8.38 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
/*
* EditableGrid_renderers.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 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);
// always apply the number style to numerical cells and column headers
if (this.column.isNumerical()) EditableGrid.prototype.addClassName(element, "number");
// call the specialized render method
return this.render(element, typeof value == 'string' ? htmlspecialchars(value, 'ENT_NOQUOTES').replace(/\s\s/g, ' ') : value);
};
CellRenderer.prototype.render = function(element, value)
{
element.innerHTML = value ? 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.render = function(element, value)
{
var optionValues = this.column.getOptionValuesForRender(element.rowIndex);
element.innerHTML = (typeof value != 'undefined' ? (value in optionValues ? optionValues[value] : 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 = typeof value == 'number' && isNaN(value);
var displayValue = isNAN ? (column.nansymbol || "") : value;
if (typeof displayValue == 'number') {
if (column.precision !== null) displayValue = displayValue.toFixed(column.precision);
if (column.unit !== null) displayValue += ' ' + column.unit;
}
element.innerHTML = displayValue;
element.style.fontWeight = isNAN ? "normal" : "";
};
/**
* 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;
// 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 = element.parentNode.rowIndex - this.cellrenderer.editablegrid.nbHeaderRows; // 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));
element.className = "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;
};
/**
* 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;
if (sortedColumnName != this.columnName) {
clearPrevious = sortedColumnName;
sortedColumnName = this.columnName;
sortDescending = false;
}
else {
if (!sortDescending) sortDescending = true;
else {
clearPrevious = sortedColumnName;
sortedColumnName = -1;
sortDescending = false;
}
}
// render header for previous sort column
var j = getColumnIndex(clearPrevious);
if (j >= 0) columns[j].headerRenderer._render(-1, j, cols[j], columns[j].label);
sort(sortedColumnName, sortDescending);
// render header for new sort column
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.sortDownImage: this.editablegrid.sortUpImage);
}
// call user renderer if any
if (this.cellRenderer) this.cellRenderer.render(cell, value);
}
};