/
tableRenderer.js
575 lines (483 loc) · 16.9 KB
/
tableRenderer.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import * as dom from './../../../dom.js';
let isRenderedColumnHeaders = {};
/**
* @class WalkontableTableRenderer
*/
class WalkontableTableRenderer {
/**
* @param {WalkontableTable} wtTable
*/
constructor(wtTable) {
this.wtTable = wtTable;
this.wot = wtTable.instance;
// legacy support
this.instance = wtTable.instance;
this.rowFilter = wtTable.rowFilter;
this.columnFilter = wtTable.columnFilter;
this.TABLE = wtTable.TABLE;
this.THEAD = wtTable.THEAD;
this.TBODY = wtTable.TBODY;
this.COLGROUP = wtTable.COLGROUP;
this.rowHeaders = [];
this.rowHeaderCount = 0;
this.columnHeaders = [];
this.columnHeaderCount = 0;
this.fixedRowsTop = 0;
}
/**
*
*/
render() {
if (!this.wtTable.isWorkingOnClone()) {
this.wot.getSetting('beforeDraw', true);
}
this.rowHeaders = this.wot.getSetting('rowHeaders');
this.rowHeaderCount = this.rowHeaders.length;
this.fixedRowsTop = this.wot.getSetting('fixedRowsTop');
this.columnHeaders = this.wot.getSetting('columnHeaders');
this.columnHeaderCount = this.columnHeaders.length;
let columnsToRender = this.wtTable.getRenderedColumnsCount();
let rowsToRender = this.wtTable.getRenderedRowsCount();
let totalColumns = this.wot.getSetting('totalColumns');
let totalRows = this.wot.getSetting('totalRows');
let workspaceWidth;
let adjusted = false;
if (totalColumns > 0) {
// prepare COL and TH elements for rendering
this.adjustAvailableNodes();
adjusted = true;
// assign row header classnames
this.renderColGroups();
this.renderColumnHeaders();
//Render table rows
this.renderRows(totalRows, rowsToRender, columnsToRender);
if (!this.wtTable.isWorkingOnClone()) {
workspaceWidth = this.wot.wtViewport.getWorkspaceWidth();
this.wot.wtViewport.containerWidth = null;
} else {
this.adjustColumnHeaderHeights();
}
this.adjustColumnWidths(columnsToRender);
}
if (!adjusted) {
this.adjustAvailableNodes();
}
this.removeRedundantRows(rowsToRender);
if (!this.wtTable.isWorkingOnClone()) {
this.markOversizedRows();
this.wot.wtViewport.createVisibleCalculators();
this.wot.wtOverlays.refresh(false);
this.wot.wtOverlays.applyToDOM();
if (workspaceWidth !== this.wot.wtViewport.getWorkspaceWidth()) {
//workspace width changed though to shown/hidden vertical scrollbar. Let's reapply stretching
this.wot.wtViewport.containerWidth = null;
let firstRendered = this.wtTable.getFirstRenderedColumn();
let lastRendered = this.wtTable.getLastRenderedColumn();
for (let i = firstRendered; i < lastRendered; i++) {
let width = this.wtTable.getStretchedColumnWidth(i);
let renderedIndex = this.columnFilter.sourceToRendered(i);
this.COLGROUP.childNodes[renderedIndex + this.rowHeaderCount].style.width = width + 'px';
}
}
this.wot.getSetting('onDraw', true);
}
}
/**
* @param {Number} renderedRowsCount
*/
removeRedundantRows(renderedRowsCount) {
while (this.wtTable.tbodyChildrenLength > renderedRowsCount) {
this.TBODY.removeChild(this.TBODY.lastChild);
this.wtTable.tbodyChildrenLength--;
}
}
/**
* @param {Number} totalRows
* @param {Number} rowsToRender
* @param {Number} columnsToRender
*/
renderRows(totalRows, rowsToRender, columnsToRender) {
let lastTD, TR;
let visibleRowIndex = 0;
let sourceRowIndex = this.rowFilter.renderedToSource(visibleRowIndex);
let isWorkingOnClone = this.wtTable.isWorkingOnClone();
while (sourceRowIndex < totalRows && sourceRowIndex >= 0) {
if (visibleRowIndex > 1000) {
throw new Error('Security brake: Too much TRs. Please define height for your table, which will enforce scrollbars.');
}
if (rowsToRender !== void 0 && visibleRowIndex === rowsToRender) {
// We have as much rows as needed for this clone
break;
}
TR = this.getOrCreateTrForRow(visibleRowIndex, TR);
// Render row headers
this.renderRowHeaders(sourceRowIndex, TR);
// Add and/or remove TDs to TR to match the desired number
this.adjustColumns(TR, columnsToRender + this.rowHeaderCount);
lastTD = this.renderCells(sourceRowIndex, TR, columnsToRender);
if (!isWorkingOnClone) {
// Reset the oversized row cache for this row
this.resetOversizedRow(sourceRowIndex);
}
if (TR.firstChild) {
// if I have 2 fixed columns with one-line content and the 3rd column has a multiline content, this is
// the way to make sure that the overlay will has same row height
let height = this.wot.wtTable.getRowHeight(sourceRowIndex);
if (height) {
// Decrease height. 1 pixel will be "replaced" by 1px border top
height--;
TR.firstChild.style.height = height + 'px';
} else {
TR.firstChild.style.height = '';
}
}
visibleRowIndex++;
sourceRowIndex = this.rowFilter.renderedToSource(visibleRowIndex);
}
}
/**
* Reset the oversized row cache for the provided index
*
* @param {Number} sourceRow Row index
*/
resetOversizedRow(sourceRow) {
if (this.wot.getSetting('externalRowCalculator')) {
return;
}
if (this.wot.wtViewport.oversizedRows && this.wot.wtViewport.oversizedRows[sourceRow]) {
this.wot.wtViewport.oversizedRows[sourceRow] = void 0;
}
}
/**
* Check if any of the rendered rows is higher than expected, and if so, cache them
*/
markOversizedRows() {
if (this.wot.getSetting('externalRowCalculator')) {
return;
}
let rowCount = this.instance.wtTable.TBODY.childNodes.length;
let expectedTableHeight = rowCount * this.instance.wtSettings.settings.defaultRowHeight;
let actualTableHeight = dom.innerHeight(this.instance.wtTable.TBODY) - 1;
let previousRowHeight;
let rowInnerHeight;
let sourceRowIndex;
let currentTr;
let rowHeader;
if (expectedTableHeight === actualTableHeight) {
// If the actual table height equals rowCount * default single row height, no row is oversized -> no need to iterate over them
return;
}
while (rowCount) {
rowCount--;
sourceRowIndex = this.instance.wtTable.rowFilter.renderedToSource(rowCount);
previousRowHeight = this.instance.wtTable.getRowHeight(sourceRowIndex);
currentTr = this.instance.wtTable.getTrForRow(sourceRowIndex);
rowHeader = currentTr.querySelector('th');
if (rowHeader) {
rowInnerHeight = dom.innerHeight(rowHeader);
} else {
rowInnerHeight = dom.innerHeight(currentTr);
}
if ((!previousRowHeight && this.instance.wtSettings.settings.defaultRowHeight < rowInnerHeight ||
previousRowHeight < rowInnerHeight)) {
this.instance.wtViewport.oversizedRows[sourceRowIndex] = ++rowInnerHeight;
}
}
}
/**
*
*/
adjustColumnHeaderHeights() {
let columnHeaders = this.wot.getSetting('columnHeaders');
let childs = this.wot.wtTable.THEAD.childNodes;
let oversizedCols = this.wot.wtViewport.oversizedColumnHeaders;
for (let i = 0, len = columnHeaders.length; i < len; i++) {
if (oversizedCols[i]) {
if (childs[i].childNodes.length === 0) {
return;
}
childs[i].childNodes[0].style.height = oversizedCols[i] + 'px';
}
}
}
/**
* Check if column header for the specified column is higher than expected, and if so, cache it
*
* @param {Number} col Index of column
*/
markIfOversizedColumnHeader(col) {
let level = this.wot.getSetting('columnHeaders').length;
let defaultRowHeight = this.wot.wtSettings.settings.defaultRowHeight;
let sourceColIndex;
let previousColHeaderHeight;
let currentHeader;
let currentHeaderHeight;
sourceColIndex = this.wot.wtTable.columnFilter.renderedToSource(col);
while (level) {
level--;
previousColHeaderHeight = this.wot.wtTable.getColumnHeaderHeight(level);
currentHeader = this.wot.wtTable.getColumnHeader(sourceColIndex, level);
if (!currentHeader) {
continue;
}
//currentHeaderHeight = defaultRowHeight;
currentHeaderHeight = dom.innerHeight(currentHeader);
if (!previousColHeaderHeight && defaultRowHeight < currentHeaderHeight || previousColHeaderHeight < currentHeaderHeight) {
this.wot.wtViewport.oversizedColumnHeaders[level] = currentHeaderHeight;
}
}
}
/**
* @param {Number} sourceRowIndex
* @param {HTMLTableRowElement} TR
* @param {Number} columnsToRender
* @returns {HTMLTableCellElement}
*/
renderCells(sourceRowIndex, TR, columnsToRender) {
let TD;
let sourceColIndex;
for (let visibleColIndex = 0; visibleColIndex < columnsToRender; visibleColIndex++) {
sourceColIndex = this.columnFilter.renderedToSource(visibleColIndex);
if (visibleColIndex === 0) {
TD = TR.childNodes[this.columnFilter.sourceColumnToVisibleRowHeadedColumn(sourceColIndex)];
} else {
TD = TD.nextSibling; //http://jsperf.com/nextsibling-vs-indexed-childnodes
}
// If the number of headers has been reduced, we need to replace excess TH with TD
if (TD.nodeName == 'TH') {
TD = replaceThWithTd(TD, TR);
}
if (!dom.hasClass(TD, 'hide')) {
TD.className = '';
}
TD.removeAttribute('style');
this.wot.wtSettings.settings.cellRenderer(sourceRowIndex, sourceColIndex, TD);
}
return TD;
}
/**
* @param {Number} columnsToRender
*/
adjustColumnWidths(columnsToRender) {
let scrollbarCompensation = 0;
let sourceInstance = this.wot.cloneSource ? this.wot.cloneSource : this.wot;
let mainHolder = sourceInstance.wtTable.holder;
if (mainHolder.offsetHeight < mainHolder.scrollHeight) {
scrollbarCompensation = dom.getScrollbarWidth();
}
this.wot.wtViewport.columnsRenderCalculator.refreshStretching(this.wot.wtViewport.getViewportWidth() - scrollbarCompensation);
for (let renderedColIndex = 0; renderedColIndex < columnsToRender; renderedColIndex++) {
let width = this.wtTable.getStretchedColumnWidth(this.columnFilter.renderedToSource(renderedColIndex));
this.COLGROUP.childNodes[renderedColIndex + this.rowHeaderCount].style.width = width + 'px';
}
}
/**
* @param {HTMLTableCellElement} TR
*/
appendToTbody(TR) {
this.TBODY.appendChild(TR);
this.wtTable.tbodyChildrenLength++;
}
/**
* @param {Number} rowIndex
* @param {HTMLTableRowElement} currentTr
* @returns {HTMLTableCellElement}
*/
getOrCreateTrForRow(rowIndex, currentTr) {
let TR;
if (rowIndex >= this.wtTable.tbodyChildrenLength) {
TR = this.createRow();
this.appendToTbody(TR);
} else if (rowIndex === 0) {
TR = this.TBODY.firstChild;
} else {
// http://jsperf.com/nextsibling-vs-indexed-childnodes
TR = currentTr.nextSibling;
}
return TR;
}
/**
* @returns {HTMLTableCellElement}
*/
createRow() {
let TR = document.createElement('TR');
for (let visibleColIndex = 0; visibleColIndex < this.rowHeaderCount; visibleColIndex++) {
TR.appendChild(document.createElement('TH'));
}
return TR;
}
/**
* @param {Number} row
* @param {Number} col
* @param {HTMLTableCellElement} TH
*/
renderRowHeader(row, col, TH) {
TH.className = '';
TH.removeAttribute('style');
this.rowHeaders[col](row, TH, col);
}
/**
* @param {Number} row
* @param {HTMLTableCellElement} TR
*/
renderRowHeaders(row, TR) {
for (let TH = TR.firstChild, visibleColIndex = 0; visibleColIndex < this.rowHeaderCount; visibleColIndex++) {
// If the number of row headers increased we need to create TH or replace an existing TD node with TH
if (!TH) {
TH = document.createElement('TH');
TR.appendChild(TH);
} else if (TH.nodeName == 'TD') {
TH = replaceTdWithTh(TH, TR);
}
this.renderRowHeader(row, visibleColIndex, TH);
// http://jsperf.com/nextsibling-vs-indexed-childnodes
TH = TH.nextSibling;
}
}
/**
* Adjust the number of COL and TH elements to match the number of columns and headers that need to be rendered
*/
adjustAvailableNodes() {
this.adjustColGroups();
this.adjustThead();
}
/**
* Renders the column headers
*/
renderColumnHeaders() {
let overlayName = this.wot.getOverlayName();
if (!this.columnHeaderCount) {
return;
}
let columnCount = this.wtTable.getRenderedColumnsCount();
for (let i = 0; i < this.columnHeaderCount; i++) {
let TR = this.getTrForColumnHeaders(i);
for (let renderedColumnIndex = (-1) * this.rowHeaderCount; renderedColumnIndex < columnCount; renderedColumnIndex++) {
let sourceCol = this.columnFilter.renderedToSource(renderedColumnIndex);
this.renderColumnHeader(i, sourceCol, TR.childNodes[renderedColumnIndex + this.rowHeaderCount]);
if (!isRenderedColumnHeaders[overlayName] && !this.wtTable.isWorkingOnClone()) {
this.markIfOversizedColumnHeader(renderedColumnIndex);
}
}
}
isRenderedColumnHeaders[overlayName] = true;
}
/**
* Adjusts the number of COL elements to match the number of columns that need to be rendered
*/
adjustColGroups() {
let columnCount = this.wtTable.getRenderedColumnsCount();
while (this.wtTable.colgroupChildrenLength < columnCount + this.rowHeaderCount) {
this.COLGROUP.appendChild(document.createElement('COL'));
this.wtTable.colgroupChildrenLength++;
}
while (this.wtTable.colgroupChildrenLength > columnCount + this.rowHeaderCount) {
this.COLGROUP.removeChild(this.COLGROUP.lastChild);
this.wtTable.colgroupChildrenLength--;
}
}
/**
* Adjusts the number of TH elements in THEAD to match the number of headers and columns that need to be rendered
*/
adjustThead() {
let columnCount = this.wtTable.getRenderedColumnsCount();
let TR = this.THEAD.firstChild;
if (this.columnHeaders.length) {
for (let i = 0, len = this.columnHeaders.length; i < len; i++) {
TR = this.THEAD.childNodes[i];
if (!TR) {
TR = document.createElement('TR');
this.THEAD.appendChild(TR);
}
this.theadChildrenLength = TR.childNodes.length;
while (this.theadChildrenLength < columnCount + this.rowHeaderCount) {
TR.appendChild(document.createElement('TH'));
this.theadChildrenLength++;
}
while (this.theadChildrenLength > columnCount + this.rowHeaderCount) {
TR.removeChild(TR.lastChild);
this.theadChildrenLength--;
}
}
let theadChildrenLength = this.THEAD.childNodes.length;
if (theadChildrenLength > this.columnHeaders.length) {
for (let i = this.columnHeaders.length; i < theadChildrenLength; i++) {
this.THEAD.removeChild(this.THEAD.lastChild);
}
}
} else if (TR) {
dom.empty(TR);
}
}
/**
* @param {Number} index
* @returns {HTMLTableCellElement}
*/
getTrForColumnHeaders(index) {
return this.THEAD.childNodes[index];
}
/**
* @param {Number} row
* @param {Number} col
* @param {HTMLTableCellElement} TH
* @returns {*}
*/
renderColumnHeader(row, col, TH) {
TH.className = '';
TH.removeAttribute('style');
return this.columnHeaders[row](col, TH, row);
}
/**
* Assign the row header classnames to the columns being row headers
*/
renderColGroups() {
for (let colIndex = 0; colIndex < this.wtTable.colgroupChildrenLength; colIndex++) {
if (colIndex < this.rowHeaderCount) {
dom.addClass(this.COLGROUP.childNodes[colIndex], 'rowHeader');
} else {
dom.removeClass(this.COLGROUP.childNodes[colIndex], 'rowHeader');
}
}
}
/**
* Add and/or remove the TDs to match the desired number
*
* @param {HTMLTableCellElement} TR Table row in question
* @param {Number} desiredCount The desired number of TDs in the TR
*/
adjustColumns(TR, desiredCount) {
let count = TR.childNodes.length;
while (count < desiredCount) {
let TD = document.createElement('TD');
TR.appendChild(TD);
count++;
}
while (count > desiredCount) {
TR.removeChild(TR.lastChild);
count--;
}
}
/**
* @param {Number} columnsToRender
*/
removeRedundantColumns(columnsToRender) {
while (this.wtTable.tbodyChildrenLength > columnsToRender) {
this.TBODY.removeChild(this.TBODY.lastChild);
this.wtTable.tbodyChildrenLength--;
}
}
}
function replaceTdWithTh(TD, TR) {
let TH = document.createElement('TH');
TR.insertBefore(TH, TD);
TR.removeChild(TD);
return TH;
}
function replaceThWithTd(TH, TR) {
let TD = document.createElement('TD');
TR.insertBefore(TD, TH);
TR.removeChild(TH);
return TD;
}
export {WalkontableTableRenderer};
window.WalkontableTableRenderer = WalkontableTableRenderer;