-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathrhandsontable.js
More file actions
481 lines (395 loc) · 13.7 KB
/
rhandsontable.js
File metadata and controls
481 lines (395 loc) · 13.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
HTMLWidgets.widget({
name: 'rhandsontable',
type: 'output',
params: null,
initialize: function(el, width, height) {
return {
};
},
renderValue: function(el, x, instance) {
// convert json to array
if (x.data.length > 0 && x.data[0].constructor === Array) {
x.data = x.data;
} else {
x.data = toArray(x.data.map(function(d) {
return x.rColnames.map(function(ky) {
return d[ky];
});
}));
}
if (x.isHeatmap === true) {
x.afterLoadData = this.initHeatmap;
x.beforeChangeRender = this.updateHeatmap;
}
if (x.overflow) {
$("#" + el.id).css('overflow', x.overflow);
}
if (x.rowHeaderWidth) {
$("#" + el.id).css('col.rowHeader', x.rowHeaderWidth + 'px');
}
//this.afterRender(x);
this.params = x;
if (instance.hot) { // update existing instance
if (x.debug && x.debug > 0) {
console.log("rhandsontable: update table");
}
instance.hot.params = x;
instance.hot.updateSettings(x);
} else { // create new instance
if (x.debug && x.debug > 0) {
console.log("rhandsontable: new table");
}
instance.hot = new Handsontable(el, x);
this.afterChangeCallback(x);
this.afterCellMetaCallback(x);
this.afterRowAndColChange(x);
if (x.selectCallback) {
this.afterSelectCallback(x);
}
instance.hot.params = x;
instance.hot.updateSettings(x);
var searchField = document.getElementById('searchField');
if (typeof(searchField) != 'undefined' && searchField !== null) {
Handsontable.dom.addEvent(searchField, 'keyup', function (event) {
var search = instance.hot.getPlugin('search');
var queryResult = search.query(this.value);
instance.hot.render();
});
}
}
},
resize: function(el, width, height, instance) {
},
afterRender: function(x) {
x.afterRender = function(isForced) {
var plugin = this.getPlugin('autoColumnSize');
if (plugin.isEnabled() && this.params) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("rhandsontable: resizing column widths");
}
}
var wdths = plugin.widths;
for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
if (this.params.columns && this.params.columns[i].renderer.name != "customRenderer") {
plugin.calculateColumnsWidth(i, 300, true);
}
}
}
};
},
afterChangeCallback: function(x) {
x.afterChange = function(changes, source) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterChange: " + source);
}
if (this.params.debug > 1) {
console.log("afterChange:");
console.log(changes);
}
}
if (HTMLWidgets.shinyMode) {
if (changes && (changes[0][2] !== null || changes[0][3] !== null)) {
if (this.sortIndex && this.sortIndex.length !== 0) {
c = [this.sortIndex[changes[0][0]][0], changes[0].slice(1, 1 + 3)];
} else {
c = changes;
}
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterChange: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterChange", changes: c, source: source },
params: this.params
});
} else if (source == "loadData" && this.params) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterChange: Shiny.onInputChange: " + this.rootElement.id);
}
}
// push input change to shiny so input$hot and output$hot are in sync (see #137)
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterChange", changes: null },
params: this.params
});
}
}
};
x.afterLoadData = function(firstTime) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterLoadData: " + firstTime);
}
}
};
x.afterChangesObserved = function(firstTime) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterChangesObserved");
}
}
};
x.afterInit = function() {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterInit");
}
}
};
},
afterCellMetaCallback: function(x) {
x.afterSetCellMeta = function(r, c, key, val) {
if (HTMLWidgets.shinyMode && key === "comment") {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterSetCellMeta: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id + "_comment", {
data: this.getData(),
comment: { r: r + 1, c: c + 1, key: key, val: val},
params: this.params
});
}
};
},
afterSelectCallback: function(x) {
x.afterSelectionEnd = function(r, c, r2, c2) {
var r_all = [];
var c_all = [];
if (HTMLWidgets.shinyMode) {
if ( r2 < r ) { r2 = [r, r = r2][0]; }
for ( var i=r; i <= r2; i++ ) {
r_all.push( this.toPhysicalRow(i) + 1 );
}
if ( c2 < c ) { c2 = [c, c = c2][0]; }
for ( var ii=c; ii <= c2; ii++ ) {
c_all.push( this.toPhysicalColumn(ii) + 1 );
}
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterSelectionEnd: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id + "_select:rhandsontable.customSelectDeserializer", {
data: this.getData(),
select: { r: r + 1,
c: c + 1,
r2: r2 + 1,
c2: c2 + 1,
rAll: r_all,
cAll: c_all },
params: this.params
});
}
};
},
afterRowAndColChange: function(x) {
x.afterCreateRow = function(ind, ct) {
if (HTMLWidgets.shinyMode) {
if (this.params && this.params.columns) {
for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
this.setDataAtCell(ind, i, this.params.columns[i].default);
}
}
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterCreateRow: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterCreateRow", ind: ind, ct: ct },
params: this.params
});
}
};
x.afterRemoveRow = function(ind, ct) {
if (HTMLWidgets.shinyMode) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterRemoveRow: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterRemoveRow", ind: ind, ct: ct },
params: this.params
});
}
};
x.afterCreateCol = function(ind, ct) {
if (HTMLWidgets.shinyMode) {
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterCreateCol: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterCreateCol", ind: ind, ct: ct },
params: this.params
});
}
};
x.afterRemoveCol = function(ind, ct) {
if (HTMLWidgets.shinyMode)
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterRemoveCol: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterRemoveCol", ind: ind, ct: ct },
params: this.params
});
};
x.afterRowMove = function(ind, ct) {
if (HTMLWidgets.shinyMode)
if (this.params && this.params.debug) {
if (this.params.debug > 0) {
console.log("afterRowMove: Shiny.onInputChange: " + this.rootElement.id);
}
}
Shiny.onInputChange(this.rootElement.id, {
data: this.getData(),
changes: { event: "afterRowMove", ind: ind, ct: ct },
params: this.params
});
};
},
// see http://handsontable.com/demo/heatmaps.html
initHeatmap: function(firstTime, source) {
this.heatmap = [];
for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
this.heatmap[i] = generateHeatmapData.call(this, i);
}
},
updateHeatmap: function(change, source) {
this.heatmap[change[0][1]] = generateHeatmapData.call(this, change[0][1]);
}
});
function generateHeatmapData(colId) {
var values = this.getDataAtCol(colId);
return {
min: Math.min.apply(null, values),
max: Math.max.apply(null, values)
};
}
// https://stackoverflow.com/questions/22477612/converting-array-of-objects-into-array-of-arrays
function toArray(input) {
var result = input.map(function(obj) {
return Object.keys(obj).map(function(key) {
return obj[key];
});
});
return result;
}
// csv logic adapted from https://github.com/juantascon/jquery-handsontable-csv
function csvString(instance, sep, dec) {
var headers = instance.getColHeader();
var csv = headers.join(sep) + "\n";
for (var i = 0; i < instance.countRows(); i++) {
var row = [];
for (var h in headers) {
var col = instance.propToCol(h);
var value = instance.getDataAtRowProp(i, col);
if ( !isNaN(value) ) {
value = value.toString().replace(".", dec);
}
row.push(value);
}
csv += row.join(sep);
csv += "\n";
}
return csv;
}
function customRenderer(instance, TD, row, col, prop, value, cellProperties) {
if (['date', 'handsontable', 'dropdown'].indexOf(cellProperties.type) > -1) {
type = 'autocomplete';
} else {
type = cellProperties.type;
}
Handsontable.renderers.getRenderer(type)(instance, TD, row, col, prop, value, cellProperties);
}
function renderSparkline(instance, td, row, col, prop, value, cellProperties) {
try {
val = JSON.parse(value);
nm = 'sparklines_r' + row + '_c' + col;
td.innerHTML = '<span class=\"' + nm + '\"></span>';
// adjust for cell padding
if (val.options && val.options.type &&
['bar', 'tristate'].indexOf(val.options.type[0]) > -1) {
val.options.barSpacing = 1;
val.options.barWidth = Math.max(1, Math.round((instance.getColWidth(col) - 8 - (val.values.length - 1)) / val.values.length));
} else {
if (!val.options) {
val.options = {};
}
val.options.width = (instance.getColWidth(col) - 8) + "px";
}
$('.' + nm).sparkline(val.values, val.options);
} catch(err) {
td.innerHTML = '';
}
return td;
}
// http://docs.handsontable.com/0.16.1/demo-custom-renderers.html
function strip_tags(input, allowed) {
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
// making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
function safeHtmlRenderer(instance, td, row, col, prop, value, cellProperties) {
var escaped = Handsontable.helper.stringify(value);
if (instance.getSettings().allowedTags) {
tags = instance.getSettings().allowedTags;
} else {
tags = '<em><b><strong><a><big>';
}
escaped = strip_tags(escaped, tags); //be sure you only allow certain HTML tags to avoid XSS threats (you should also remove unwanted HTML attributes)
td.innerHTML = escaped;
return td;
}
// if the table is in an ineractive session, shiny mode,
// then we register the handler for setting data interactively
if (HTMLWidgets.shinyMode) {
Shiny.addCustomMessageHandler(
"handler_setDataAtCell",
function(message) {
var hot = window.HTMLWidgets.find('#' + message.id).hot;
var count = message.size;
// setDataAtCell is overloaded so there's different parameters to send one value
if( count == 1 ) {
hot.setDataAtCell(message.row, message.col, message.val);
return;
}
// if sending 2+ values to the table then we use an array of arrays
var datArr = [];
// when the table is sorted we need to send the values to their visual locations
if ( hot.getPlugin('columnSorting').isSorted() ) {
for ( var i=0; i < count; i++ ) {
datArr[datArr.length] = [hot.toVisualRow(message.row[i]),
hot.toVisualColumn(message.col[i]),
message.val[i]];
}
} else {
// when not sorted we just use the values from R
for ( var i=0; i < count; i++ ) {
datArr[datArr.length] = [message.row[i], message.col[i], message.val[i]];
}
}
hot.setDataAtCell(datArr);
});
}