Debian Codegraph @

inst/htmlwidgets/rhandsontable.js

raw symbols SCIP
1 HTMLWidgets.widget({
2
3 name: 'rhandsontable',
4
5 type: 'output',
6
7 params: null,
8
9 initialize: function(el, width, height) {
10
11 return {
12
13 };
14
15 },
16
17 renderValue: function(el, x, instance) {
18
19 // convert json to array
20 if (x.data.length > 0 && x.data[0].constructor === Array) {
21 x.data = x.data;
22 } else {
23 x.data = toArray(x.data.map(function(d) {
24 return x.rColnames.map(function(ky) {
25 return d[ky];
26 });
27 }));
28 }
29
30 if (x.isHeatmap === true) {
31 x.afterLoadData = this.initHeatmap;
32 x.beforeChangeRender = this.updateHeatmap;
33 }
34
35 if (x.overflow) {
36 $("#" + el.id).css('overflow', x.overflow);
37 }
38
39 if (x.rowHeaderWidth) {
40 $("#" + el.id).css('col.rowHeader', x.rowHeaderWidth + 'px');
41 }
42
43 //this.afterRender(x);
44
45 this.params = x;
46
47 if (instance.hot) { // update existing instance
48 if (x.debug && x.debug > 0) {
49 console.log("rhandsontable: update table");
50 }
51
52 instance.hot.params = x;
53 instance.hot.updateSettings(x);
54 } else { // create new instance
55 if (x.debug && x.debug > 0) {
56 console.log("rhandsontable: new table");
57 }
58
59 instance.hot = new Handsontable(el, x);
60
61 this.afterChangeCallback(x);
62 this.afterCellMetaCallback(x);
63 this.afterRowAndColChange(x);
64
65 if (x.selectCallback) {
66 this.afterSelectCallback(x);
67 }
68
69 instance.hot.params = x;
70 instance.hot.updateSettings(x);
71
72 var searchField = document.getElementById('searchField');
73 if (typeof(searchField) != 'undefined' && searchField !== null) {
74 Handsontable.dom.addEvent(searchField, 'keyup', function (event) {
75 var search = instance.hot.getPlugin('search');
76 var queryResult = search.query(this.value);
77 instance.hot.render();
78 });
79 }
80 }
81 },
82
83 resize: function(el, width, height, instance) {
84
85 },
86
87 afterRender: function(x) {
88 x.afterRender = function(isForced) {
89 var plugin = this.getPlugin('autoColumnSize');
90 if (plugin.isEnabled() && this.params) {
91 if (this.params && this.params.debug) {
92 if (this.params.debug > 0) {
93 console.log("rhandsontable: resizing column widths");
94 }
95 }
96
97 var wdths = plugin.widths;
98 for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
99 if (this.params.columns && this.params.columns[i].renderer.name != "customRenderer") {
100 plugin.calculateColumnsWidth(i, 300, true);
101 }
102 }
103 }
104 };
105 },
106
107 afterChangeCallback: function(x) {
108
109 x.afterChange = function(changes, source) {
110 if (this.params && this.params.debug) {
111 if (this.params.debug > 0) {
112 console.log("afterChange: " + source);
113 }
114 if (this.params.debug > 1) {
115 console.log("afterChange:");
116 console.log(changes);
117 }
118 }
119
120 if (HTMLWidgets.shinyMode) {
121 if (changes && (changes[0][2] !== null || changes[0][3] !== null)) {
122 if (this.sortIndex && this.sortIndex.length !== 0) {
123 c = [this.sortIndex[changes[0][0]][0], changes[0].slice(1, 1 + 3)];
124 } else {
125 c = changes;
126 }
127
128 if (this.params && this.params.debug) {
129 if (this.params.debug > 0) {
130 console.log("afterChange: Shiny.onInputChange: " + this.rootElement.id);
131 }
132 }
133 Shiny.onInputChange(this.rootElement.id, {
134 data: this.getData(),
135 changes: { event: "afterChange", changes: c, source: source },
136 params: this.params
137 });
138 } else if (source == "loadData" && this.params) {
139
140 if (this.params && this.params.debug) {
141 if (this.params.debug > 0) {
142 console.log("afterChange: Shiny.onInputChange: " + this.rootElement.id);
143 }
144 }
145 // push input change to shiny so input$hot and output$hot are in sync (see #137)
146 Shiny.onInputChange(this.rootElement.id, {
147 data: this.getData(),
148 changes: { event: "afterChange", changes: null },
149 params: this.params
150 });
151 }
152 }
153
154 };
155
156 x.afterLoadData = function(firstTime) {
157 if (this.params && this.params.debug) {
158 if (this.params.debug > 0) {
159 console.log("afterLoadData: " + firstTime);
160 }
161 }
162 };
163
164 x.afterChangesObserved = function(firstTime) {
165 if (this.params && this.params.debug) {
166 if (this.params.debug > 0) {
167 console.log("afterChangesObserved");
168 }
169 }
170 };
171
172 x.afterInit = function() {
173 if (this.params && this.params.debug) {
174 if (this.params.debug > 0) {
175 console.log("afterInit");
176 }
177 }
178 };
179 },
180
181 afterCellMetaCallback: function(x) {
182
183 x.afterSetCellMeta = function(r, c, key, val) {
184
185 if (HTMLWidgets.shinyMode && key === "comment") {
186 if (this.params && this.params.debug) {
187 if (this.params.debug > 0) {
188 console.log("afterSetCellMeta: Shiny.onInputChange: " + this.rootElement.id);
189 }
190 }
191 Shiny.onInputChange(this.rootElement.id + "_comment", {
192 data: this.getData(),
193 comment: { r: r + 1, c: c + 1, key: key, val: val},
194 params: this.params
195 });
196 }
197
198 };
199 },
200
201 afterSelectCallback: function(x) {
202
203 x.afterSelectionEnd = function(r, c, r2, c2) {
204
205 var r_all = [];
206 var c_all = [];
207 if (HTMLWidgets.shinyMode) {
208 if ( r2 < r ) { r2 = [r, r = r2][0]; }
209 for ( var i=r; i <= r2; i++ ) {
210 r_all.push( this.toPhysicalRow(i) + 1 );
211 }
212 if ( c2 < c ) { c2 = [c, c = c2][0]; }
213 for ( var ii=c; ii <= c2; ii++ ) {
214 c_all.push( this.toPhysicalColumn(ii) + 1 );
215 }
216
217 if (this.params && this.params.debug) {
218 if (this.params.debug > 0) {
219 console.log("afterSelectionEnd: Shiny.onInputChange: " + this.rootElement.id);
220 }
221 }
222 Shiny.onInputChange(this.rootElement.id + "_select:rhandsontable.customSelectDeserializer", {
223 data: this.getData(),
224 select: { r: r + 1,
225 c: c + 1,
226 r2: r2 + 1,
227 c2: c2 + 1,
228 rAll: r_all,
229 cAll: c_all },
230 params: this.params
231 });
232 }
233
234 };
235 },
236
237 afterRowAndColChange: function(x) {
238
239 x.afterCreateRow = function(ind, ct) {
240
241 if (HTMLWidgets.shinyMode) {
242
243 if (this.params && this.params.columns) {
244 for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
245 this.setDataAtCell(ind, i, this.params.columns[i].default);
246 }
247 }
248
249 if (this.params && this.params.debug) {
250 if (this.params.debug > 0) {
251 console.log("afterCreateRow: Shiny.onInputChange: " + this.rootElement.id);
252 }
253 }
254 Shiny.onInputChange(this.rootElement.id, {
255 data: this.getData(),
256 changes: { event: "afterCreateRow", ind: ind, ct: ct },
257 params: this.params
258 });
259 }
260 };
261
262 x.afterRemoveRow = function(ind, ct) {
263
264 if (HTMLWidgets.shinyMode) {
265 if (this.params && this.params.debug) {
266 if (this.params.debug > 0) {
267 console.log("afterRemoveRow: Shiny.onInputChange: " + this.rootElement.id);
268 }
269 }
270 Shiny.onInputChange(this.rootElement.id, {
271 data: this.getData(),
272 changes: { event: "afterRemoveRow", ind: ind, ct: ct },
273 params: this.params
274 });
275 }
276 };
277
278 x.afterCreateCol = function(ind, ct) {
279
280 if (HTMLWidgets.shinyMode) {
281 if (this.params && this.params.debug) {
282 if (this.params.debug > 0) {
283 console.log("afterCreateCol: Shiny.onInputChange: " + this.rootElement.id);
284 }
285 }
286 Shiny.onInputChange(this.rootElement.id, {
287 data: this.getData(),
288 changes: { event: "afterCreateCol", ind: ind, ct: ct },
289 params: this.params
290 });
291 }
292 };
293
294 x.afterRemoveCol = function(ind, ct) {
295
296 if (HTMLWidgets.shinyMode)
297 if (this.params && this.params.debug) {
298 if (this.params.debug > 0) {
299 console.log("afterRemoveCol: Shiny.onInputChange: " + this.rootElement.id);
300 }
301 }
302 Shiny.onInputChange(this.rootElement.id, {
303 data: this.getData(),
304 changes: { event: "afterRemoveCol", ind: ind, ct: ct },
305 params: this.params
306 });
307 };
308
309 x.afterRowMove = function(ind, ct) {
310
311 if (HTMLWidgets.shinyMode)
312 if (this.params && this.params.debug) {
313 if (this.params.debug > 0) {
314 console.log("afterRowMove: Shiny.onInputChange: " + this.rootElement.id);
315 }
316 }
317 Shiny.onInputChange(this.rootElement.id, {
318 data: this.getData(),
319 changes: { event: "afterRowMove", ind: ind, ct: ct },
320 params: this.params
321 });
322 };
323
324 },
325
326 // see http://handsontable.com/demo/heatmaps.html
327 initHeatmap: function(firstTime, source) {
328 this.heatmap = [];
329
330 for(var i = 0, colCount = this.countCols(); i < colCount ; i++) {
331 this.heatmap[i] = generateHeatmapData.call(this, i);
332 }
333 },
334
335 updateHeatmap: function(change, source) {
336 this.heatmap[change[0][1]] = generateHeatmapData.call(this, change[0][1]);
337 }
338
339 });
340
341 function generateHeatmapData(colId) {
342
343 var values = this.getDataAtCol(colId);
344
345 return {
346 min: Math.min.apply(null, values),
347 max: Math.max.apply(null, values)
348 };
349 }
350
351 // https://stackoverflow.com/questions/22477612/converting-array-of-objects-into-array-of-arrays
352 function toArray(input) {
353 var result = input.map(function(obj) {
354 return Object.keys(obj).map(function(key) {
355 return obj[key];
356 });
357 });
358 return result;
359 }
360
361 // csv logic adapted from https://github.com/juantascon/jquery-handsontable-csv
362 function csvString(instance, sep, dec) {
363
364 var headers = instance.getColHeader();
365
366 var csv = headers.join(sep) + "\n";
367
368 for (var i = 0; i < instance.countRows(); i++) {
369 var row = [];
370 for (var h in headers) {
371 var col = instance.propToCol(h);
372 var value = instance.getDataAtRowProp(i, col);
373 if ( !isNaN(value) ) {
374 value = value.toString().replace(".", dec);
375 }
376 row.push(value);
377 }
378
379 csv += row.join(sep);
380 csv += "\n";
381 }
382
383 return csv;
384 }
385
386 function customRenderer(instance, TD, row, col, prop, value, cellProperties) {
387 if (['date', 'handsontable', 'dropdown'].indexOf(cellProperties.type) > -1) {
388 type = 'autocomplete';
389 } else {
390 type = cellProperties.type;
391 }
392 Handsontable.renderers.getRenderer(type)(instance, TD, row, col, prop, value, cellProperties);
393 }
394
395 function renderSparkline(instance, td, row, col, prop, value, cellProperties) {
396 try {
397 val = JSON.parse(value);
398
399 nm = 'sparklines_r' + row + '_c' + col;
400 td.innerHTML = '<span class=\"' + nm + '\"></span>';
401
402 // adjust for cell padding
403 if (val.options && val.options.type &&
404 ['bar', 'tristate'].indexOf(val.options.type[0]) > -1) {
405 val.options.barSpacing = 1;
406 val.options.barWidth = Math.max(1, Math.round((instance.getColWidth(col) - 8 - (val.values.length - 1)) / val.values.length));
407 } else {
408 if (!val.options) {
409 val.options = {};
410 }
411 val.options.width = (instance.getColWidth(col) - 8) + "px";
412 }
413
414 $('.' + nm).sparkline(val.values, val.options);
415 } catch(err) {
416 td.innerHTML = '';
417 }
418
419 return td;
420 }
421
422 // http://docs.handsontable.com/0.16.1/demo-custom-renderers.html
423 function strip_tags(input, allowed) {
424 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
425 commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
426
427 // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
428 allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
429
430 return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
431 return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
432 });
433 }
434
435 function safeHtmlRenderer(instance, td, row, col, prop, value, cellProperties) {
436 var escaped = Handsontable.helper.stringify(value);
437 if (instance.getSettings().allowedTags) {
438 tags = instance.getSettings().allowedTags;
439 } else {
440 tags = '<em><b><strong><a><big>';
441 }
442 escaped = strip_tags(escaped, tags); //be sure you only allow certain HTML tags to avoid XSS threats (you should also remove unwanted HTML attributes)
443 td.innerHTML = escaped;
444
445 return td;
446 }
447
448
449 // if the table is in an ineractive session, shiny mode,
450 // then we register the handler for setting data interactively
451 if (HTMLWidgets.shinyMode) {
452 Shiny.addCustomMessageHandler(
453 "handler_setDataAtCell",
454 function(message) {
455 var hot = window.HTMLWidgets.find('#' + message.id).hot;
456 var count = message.size;
457
458 // setDataAtCell is overloaded so there's different parameters to send one value
459 if( count == 1 ) {
460 hot.setDataAtCell(message.row, message.col, message.val);
461 return;
462 }
463 // if sending 2+ values to the table then we use an array of arrays
464 var datArr = [];
465 // when the table is sorted we need to send the values to their visual locations
466 if ( hot.getPlugin('columnSorting').isSorted() ) {
467 for ( var i=0; i < count; i++ ) {
468 datArr[datArr.length] = [hot.toVisualRow(message.row[i]),
469 hot.toVisualColumn(message.col[i]),
470 message.val[i]];
471 }
472 } else {
473 // when not sorted we just use the values from R
474 for ( var i=0; i < count; i++ ) {
475 datArr[datArr.length] = [message.row[i], message.col[i], message.val[i]];
476 }
477 }
478 hot.setDataAtCell(datArr);
479 });
480 }
481