-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ghostTable.js
483 lines (412 loc) · 12.6 KB
/
ghostTable.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
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
481
482
483
import { addClass, outerHeight } from './../helpers/dom/element';
import { arrayEach } from './../helpers/array';
/**
* @class GhostTable
*/
class GhostTable {
constructor(hotInstance) {
/**
* Handsontable instance.
*
* @type {Core}
*/
this.hot = hotInstance;
/**
* Container element where every table will be injected.
*
* @type {HTMLElement|null}
*/
this.container = null;
/**
* Flag which determine is table was injected to DOM.
*
* @type {boolean}
*/
this.injected = false;
/**
* Added rows collection.
*
* @type {Array}
*/
this.rows = [];
/**
* Added columns collection.
*
* @type {Array}
*/
this.columns = [];
/**
* Samples prepared for calculations.
*
* @type {Map}
* @default {null}
*/
this.samples = null;
/**
* Ghost table settings.
*
* @type {object}
* @default {Object}
*/
this.settings = {
useHeaders: true
};
}
/**
* Add row.
*
* @param {number} row Row index.
* @param {Map} samples Samples Map object.
*/
addRow(row, samples) {
if (this.columns.length) {
throw new Error('Doesn\'t support multi-dimensional table');
}
if (!this.rows.length) {
this.container = this.createContainer(this.hot.rootElement.className);
}
const rowObject = { row };
this.rows.push(rowObject);
this.samples = samples;
this.table = this.createTable(this.hot.table.className);
this.table.colGroup.appendChild(this.createColGroupsCol());
this.table.tr.appendChild(this.createRow(row));
this.container.container.appendChild(this.table.fragment);
rowObject.table = this.table.table;
}
/**
* Add a row consisting of the column headers.
*
* @param {Map} samples A map with sampled table values.
*/
addColumnHeadersRow(samples) {
const colHeader = this.hot.getColHeader(0);
if (colHeader !== null && colHeader !== void 0) {
const rowObject = { row: -1 };
this.rows.push(rowObject);
this.container = this.createContainer(this.hot.rootElement.className);
this.samples = samples;
this.table = this.createTable(this.hot.table.className);
this.table.colGroup.appendChild(this.createColGroupsCol());
this.appendColumnHeadersRow();
this.container.container.appendChild(this.table.fragment);
rowObject.table = this.table.table;
}
}
/**
* Add column.
*
* @param {number} column Column index.
* @param {Map} samples A map with sampled table values.
*/
addColumn(column, samples) {
if (this.rows.length) {
throw new Error('Doesn\'t support multi-dimensional table');
}
if (!this.columns.length) {
this.container = this.createContainer(this.hot.rootElement.className);
}
const columnObject = { col: column };
this.columns.push(columnObject);
this.samples = samples;
this.table = this.createTable(this.hot.table.className);
if (this.getSetting('useHeaders') && this.hot.getColHeader(column) !== null) {
// Please keep in mind that the renderable column index equal to the visual columns index for the GhostTable.
// We render all columns.
this.hot.view.appendColHeader(column, this.table.th);
}
this.table.tBody.appendChild(this.createCol(column));
this.container.container.appendChild(this.table.fragment);
columnObject.table = this.table.table;
}
/**
* Get calculated heights.
*
* @param {Function} callback Callback which will be fired for each calculated row.
*/
getHeights(callback) {
if (!this.injected) {
this.injectTable();
}
arrayEach(this.rows, (row) => {
// -1 <- reduce border-top from table
callback(row.row, outerHeight(row.table) - 1);
});
}
/**
* Get calculated widths.
*
* @param {Function} callback Callback which will be fired for each calculated column.
*/
getWidths(callback) {
if (!this.injected) {
this.injectTable();
}
arrayEach(this.columns, (column) => {
// The GhostTable class is responsible for calculating the columns' width based on the
// contents rendered in the cells. In some cases, when the column's width calculated by
// the browser is a decimal point with a fractional component. For example, 35.32px.
// The usage of the `.offsetWidth` (or our helper `outerWidth`) is incorrect.
// The `outerWidth` in the mentioned example (35.32px) would return 35 pixels that
// would cause the text to not fit in the cell, thus increasing the row height.
// That's why the `getBoundingClientRect` method is used. The method returns the number
// that is rounded up to make sure that there will be a space for the cell's content.
const { width } = column.table.getBoundingClientRect();
callback(column.col, Math.ceil(width));
});
}
/**
* Set the Ghost Table settings to the provided object.
*
* @param {object} settings New Ghost Table Settings.
*/
setSettings(settings) {
this.settings = settings;
}
/**
* Set a single setting of the Ghost Table.
*
* @param {string} name Setting name.
* @param {*} value Setting value.
*/
setSetting(name, value) {
if (!this.settings) {
this.settings = {};
}
this.settings[name] = value;
}
/**
* Get the Ghost Table settings.
*
* @returns {object|null}
*/
getSettings() {
return this.settings;
}
/**
* Get a single Ghost Table setting.
*
* @param {string} name The setting name to get.
* @returns {boolean|null}
*/
getSetting(name) {
if (this.settings) {
return this.settings[name];
}
return null;
}
/**
* Create colgroup col elements.
*
* @returns {DocumentFragment}
*/
createColGroupsCol() {
const fragment = this.hot.rootDocument.createDocumentFragment();
if (this.hot.hasRowHeaders()) {
fragment.appendChild(this.createColElement(-1));
}
this.samples.forEach((sample) => {
arrayEach(sample.strings, (string) => {
fragment.appendChild(this.createColElement(string.col));
});
});
return fragment;
}
/**
* Create table row element.
*
* @param {number} row Row index.
* @returns {DocumentFragment} Returns created table row elements.
*/
createRow(row) {
const { rootDocument } = this.hot;
const fragment = rootDocument.createDocumentFragment();
const th = rootDocument.createElement('th');
if (this.hot.hasRowHeaders()) {
this.hot.view.appendRowHeader(row, th);
fragment.appendChild(th);
}
this.samples.forEach((sample) => {
arrayEach(sample.strings, (string) => {
const column = string.col;
const cellProperties = this.hot.getCellMeta(row, column);
cellProperties.col = column;
cellProperties.row = row;
const renderer = this.hot.getCellRenderer(cellProperties);
const td = rootDocument.createElement('td');
// Indicate that this element is created and supported by GhostTable. It can be useful to
// exclude rendering performance costly logic or exclude logic which doesn't work within a hidden table.
td.setAttribute('ghost-table', 1);
renderer(this.hot, td, row, column, this.hot.colToProp(column), string.value, cellProperties);
fragment.appendChild(td);
});
});
return fragment;
}
/**
* Creates DOM elements for headers and appends them to the THEAD element of the table.
*/
appendColumnHeadersRow() {
const { rootDocument } = this.hot;
const domFragment = rootDocument.createDocumentFragment();
const columnHeaders = [];
if (this.hot.hasRowHeaders()) {
const th = rootDocument.createElement('th');
columnHeaders.push([-1, th]);
domFragment.appendChild(th);
}
this.samples.forEach((sample) => {
arrayEach(sample.strings, (string) => {
const column = string.col;
const th = rootDocument.createElement('th');
columnHeaders.push([column, th]);
domFragment.appendChild(th);
});
});
// Appending DOM elements for headers
this.table.tHead.appendChild(domFragment);
arrayEach(columnHeaders, (columnHeader) => {
const [column, th] = columnHeader;
// Using source method for filling a header with value.
this.hot.view.appendColHeader(column, th);
});
}
/**
* Create table column elements.
*
* @param {number} column Column index.
* @returns {DocumentFragment} Returns created column table column elements.
*/
createCol(column) {
const { rootDocument } = this.hot;
const fragment = rootDocument.createDocumentFragment();
this.samples.forEach((sample) => {
arrayEach(sample.strings, (string) => {
const row = string.row;
const cellProperties = this.hot.getCellMeta(row, column);
cellProperties.col = column;
cellProperties.row = row;
const renderer = this.hot.getCellRenderer(cellProperties);
const td = rootDocument.createElement('td');
const tr = rootDocument.createElement('tr');
// Indicate that this element is created and supported by GhostTable. It can be useful to
// exclude rendering performance costly logic or exclude logic which doesn't work within a hidden table.
td.setAttribute('ghost-table', 1);
renderer(this.hot, td, row, column, this.hot.colToProp(column), string.value, cellProperties);
tr.appendChild(td);
fragment.appendChild(tr);
});
});
return fragment;
}
/**
* Remove table from document and reset internal state.
*/
clean() {
this.rows.length = 0;
this.rows[-1] = void 0;
this.columns.length = 0;
if (this.samples) {
this.samples.clear();
}
this.samples = null;
this.removeTable();
}
/**
* Inject generated table into document.
*
* @param {HTMLElement} [parent=null] The element to which the ghost table is injected.
*/
injectTable(parent = null) {
if (!this.injected) {
(parent || this.hot.rootElement).appendChild(this.container.fragment);
this.injected = true;
}
}
/**
* Remove table from document.
*/
removeTable() {
if (this.injected && this.container.container.parentNode) {
this.container.container.parentNode.removeChild(this.container.container);
this.container = null;
this.injected = false;
}
}
/**
* Create col element.
*
* @param {number} column Column index.
* @returns {HTMLElement}
*/
createColElement(column) {
const col = this.hot.rootDocument.createElement('col');
col.style.width = `${this.hot.view._wt.wtTable.getStretchedColumnWidth(column)}px`;
return col;
}
/**
* Create table element.
*
* @param {string} className The CSS classes to add.
* @returns {object}
*/
createTable(className = '') {
const { rootDocument } = this.hot;
const fragment = rootDocument.createDocumentFragment();
const table = rootDocument.createElement('table');
const tHead = rootDocument.createElement('thead');
const tBody = rootDocument.createElement('tbody');
const colGroup = rootDocument.createElement('colgroup');
const tr = rootDocument.createElement('tr');
const th = rootDocument.createElement('th');
if (this.isVertical()) {
table.appendChild(colGroup);
}
if (this.isHorizontal()) {
tr.appendChild(th);
tHead.appendChild(tr);
table.style.tableLayout = 'auto';
table.style.width = 'auto';
}
table.appendChild(tHead);
if (this.isVertical()) {
tBody.appendChild(tr);
}
table.appendChild(tBody);
addClass(table, className);
fragment.appendChild(table);
return { fragment, table, tHead, tBody, colGroup, tr, th };
}
/**
* Create container for tables.
*
* @param {string} className The CSS classes to add.
* @returns {object}
*/
createContainer(className = '') {
const { rootDocument } = this.hot;
const fragment = rootDocument.createDocumentFragment();
const container = rootDocument.createElement('div');
const containerClassName = `htGhostTable htAutoSize ${className.trim()}`;
addClass(container, containerClassName);
fragment.appendChild(container);
return { fragment, container };
}
/**
* Checks if table is raised vertically (checking rows).
*
* @returns {boolean}
*/
isVertical() {
return !!(this.rows.length && !this.columns.length);
}
/**
* Checks if table is raised horizontally (checking columns).
*
* @returns {boolean}
*/
isHorizontal() {
return !!(this.columns.length && !this.rows.length);
}
}
export default GhostTable;