-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathdata-load.js
More file actions
707 lines (671 loc) · 27.9 KB
/
data-load.js
File metadata and controls
707 lines (671 loc) · 27.9 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import {sfConn, apiVersion} from "./inspector.js";
import {formatDateCell, getDateFormatOptions, isRecordId} from "./utils.js";
const greyOutSkippedColumns = localStorage.getItem("greyOutSkippedColumns") === "true" && !window.location.href.includes("data-export");
// Inspired by C# System.Linq.Enumerable
export function Enumerable(iterable) {
this[Symbol.iterator] = iterable[Symbol.iterator].bind(iterable);
}
Enumerable.prototype = {
__proto__: function*(){}.prototype,
*map(f) {
for (let e of this) {
yield f(e);
}
},
*filter(f) {
for (let e of this) {
if (f(e)) {
yield e;
}
}
},
*flatMap(f) {
for (let e of this) {
yield* f(e);
}
},
*concat(other) {
yield* this;
yield* other;
},
some() {
for (let e of this) {
return true;
}
return false;
},
toArray() {
return Array.from(this);
}
};
Enumerable.prototype.map.prototype = Enumerable.prototype;
Enumerable.prototype.filter.prototype = Enumerable.prototype;
Enumerable.prototype.flatMap.prototype = Enumerable.prototype;
Enumerable.prototype.concat.prototype = Enumerable.prototype;
// @param didUpdate: A callback function to listen for updates to describe data
export function DescribeInfo(spinFor, didUpdate) {
function initialState() {
return {
data: {global: {globalStatus: "pending", globalDescribe: null}, sobjects: null},
tool: {global: {globalStatus: "pending", globalDescribe: null}, sobjects: null}
};
}
let sobjectAllDescribes = initialState();
function getGlobal(useToolingApi) {
let apiDescribes = sobjectAllDescribes[useToolingApi ? "tool" : "data"];
if (apiDescribes.global.globalStatus == "pending") {
apiDescribes.global.globalStatus = "loading";
console.log(useToolingApi ? "getting tooling objects" : "getting objects");
spinFor(sfConn.rest(useToolingApi ? "/services/data/v" + apiVersion + "/tooling/sobjects/" : "/services/data/v" + apiVersion + "/sobjects/").then(res => {
apiDescribes.global.globalStatus = "ready";
apiDescribes.global.globalDescribe = res;
apiDescribes.sobjects = new Map();
for (let sobjectDescribe of res.sobjects) {
apiDescribes.sobjects.set(sobjectDescribe.name.toLowerCase(), {global: sobjectDescribe, sobject: {sobjectStatus: "pending", sobjectDescribe: null}});
}
didUpdate();
}, () => {
apiDescribes.global.globalStatus = "loadfailed";
didUpdate();
}));
}
return apiDescribes;
}
// Makes global and sobject describe API calls, and caches the results.
// If the result of an API call is not already cashed, empty data is returned immediately, and the API call is made asynchronously.
// The caller is notified using the didUpdate callback or the spinFor promise when the API call completes, so it can make the call again to get the cached results.
return {
// Returns an object with two properties:
// - globalStatus: a string with one of the following values:
// "pending": (has not started loading, never returned by this function)
// "loading": Describe info for the api is being downloaded
// "loadfailed": Downloading of describe info for the api failed
// "ready": Describe info is available
// - globalDescribe: contains a DescribeGlobalResult if it has been loaded
describeGlobal(useToolingApi) {
return getGlobal(useToolingApi).global;
},
// Returns an object with two properties:
// - sobjectStatus: a string with one of the following values:
// "pending": (has not started loading, never returned by this function)
// "notfound": The object does not exist
// "loading": Describe info for the object is being downloaded
// "loadfailed": Downloading of describe info for the object failed
// "ready": Describe info is available
// - sobjectDescribe: contains a DescribeSObjectResult if the object exists and has been loaded
describeSobject(useToolingApi, sobjectName) {
let apiDescribes = getGlobal(useToolingApi);
if (!apiDescribes.sobjects) {
return {sobjectStatus: apiDescribes.global.globalStatus, sobjectDescribe: null};
}
let sobjectInfo = apiDescribes.sobjects.get(sobjectName.toLowerCase());
if (!sobjectInfo) {
return {sobjectStatus: "notfound", sobjectDescribe: null};
}
if (sobjectInfo.sobject.sobjectStatus == "pending") {
sobjectInfo.sobject.sobjectStatus = "loading";
console.log("getting fields for " + sobjectInfo.global.name);
spinFor(sfConn.rest(sobjectInfo.global.urls.describe).then(res => {
sobjectInfo.sobject.sobjectStatus = "ready";
sobjectInfo.sobject.sobjectDescribe = res;
didUpdate();
}, () => {
sobjectInfo.sobject.sobjectStatus = "loadfailed";
didUpdate();
}));
}
return sobjectInfo.sobject;
},
reloadAll() {
sobjectAllDescribes = initialState();
didUpdate();
}
};
}
// Pluralize a numeric value by adding an s (or optional suffix) if it is not 1
export function s(num, suffix = "s") {
return num == 1 ? "" : suffix;
}
function renderCell(rt, cell, td) {
function popLink(recordInfo, label) {
let a = document.createElement("a");
a.href = "about:blank";
a.title = "Show all data";
a.addEventListener("click", e => {
e.preventDefault();
let pop = document.createElement("div");
pop.className = "slds-dropdown slds-dropdown_left slds-dropdown_actions";
let ul = document.createElement("ul");
ul.className = "slds-dropdown__list";
pop.appendChild(ul);
td.appendChild(pop);
let {objectTypes, recordId} = recordInfo();
let objectType = undefined;
function setLinks(linkOptions = {isCopy: true, isQueryRecord: true, isShowAllData: true, isViewInSalesforce: true}) {
// Show All Data link
if (linkOptions.isShowAllData) {
let liShow = document.createElement("li");
liShow.className = "slds-dropdown__item sfir-justify-left";
ul.appendChild(liShow);
let aShow = document.createElement("a");
let args = new URLSearchParams();
args.set("host", rt.sfHost);
args.set("objectType", objectType);
if (rt.isTooling) {
args.set("useToolingApi", "1");
}
if (recordId) {
args.set("recordId", recordId);
}
aShow.href = "inspect.html?" + args;
aShow.target = "_blank";
aShow.textContent = "Show all data";
aShow.className = "view-inspector";
let aShowIcon = document.createElement("div");
aShowIcon.className = "icon";
liShow.appendChild(aShow);
aShow.prepend(aShowIcon);
ul.appendChild(liShow);
}
// Query Record link
if (linkOptions.isQueryRecord) {
let liQuery = document.createElement("li");
liQuery.className = "slds-dropdown__item sfir-justify-left";
ul.appendChild(liQuery);
let aQuery = document.createElement("a");
let query = "SELECT Id FROM " + objectType + " WHERE Id = '" + recordId + "'";
let queryArgs = new URLSearchParams();
if (rt.isTooling) {
queryArgs.set("useToolingApi", "1");
}
queryArgs.set("host", rt.sfHost);
queryArgs.set("query", query);
aQuery.href = "data-export.html?" + queryArgs;
aQuery.target = "_blank";
aQuery.textContent = "Query Record";
aQuery.className = "query-record";
let aQueryIcon = document.createElement("div");
aQueryIcon.className = "icon";
liQuery.appendChild(aQuery);
aQuery.prepend(aQueryIcon);
ul.appendChild(liQuery);
}
// View in Salesforce link
if (linkOptions.isViewInSalesforce && recordId && isRecordId(recordId) && !recordId.endsWith("0000000000AAA")) {
let liView = document.createElement("li");
liView.className = "slds-dropdown__item sfir-justify-left";
ul.appendChild(liView);
let aView = document.createElement("a");
aView.href = "https://" + rt.sfHost + "/" + recordId;
//debug log specific link
if (recordId.startsWith("07L")) {
aView.href = "https://" + rt.sfHost + "/one/one.app#/alohaRedirect/p/setup/layout/ApexDebugLogDetailEdit/d?apex_log_id=" + recordId;
}
aView.target = "_blank";
aView.textContent = "View in Salesforce";
aView.className = "view-salesforce";
let aViewIcon = document.createElement("div");
aViewIcon.className = "icon";
liView.appendChild(aView);
aView.prepend(aViewIcon);
ul.appendChild(liView);
}
// Download Event Log or Copy Id
if (linkOptions.isCopy) {
if (isEventLogFile(recordId)) {
let liDownload = document.createElement("li");
liDownload.className = "slds-dropdown__item sfir-justify-left";
ul.appendChild(liDownload);
let aDownload = document.createElement("a");
aDownload.id = recordId;
aDownload.target = "_blank";
aDownload.textContent = "Download File";
aDownload.className = "download-salesforce";
let aDownloadIcon = document.createElement("div");
aDownloadIcon.className = "icon";
liDownload.appendChild(aDownload);
aDownload.prepend(aDownloadIcon);
aDownload.addEventListener("click", e => {
sfConn.rest(e.target.id, {responseType: "text/csv"}).then(data => {
let downloadLink = document.createElement("a");
downloadLink.download = recordId.split("/")[6];
downloadLink.href = "data:text/csv;charset=utf-8," + data;
downloadLink.click();
});
ul.appendChild(liDownload);
td.removeChild(pop);
});
} else {
let liCopy = document.createElement("li");
liCopy.className = "slds-dropdown__item sfir-justify-left";
ul.appendChild(liCopy);
let aCopy = document.createElement("a");
aCopy.className = "copy-id";
aCopy.textContent = "Copy Id";
aCopy.id = recordId;
let aCopyIcon = document.createElement("div");
aCopyIcon.className = "icon";
liCopy.appendChild(aCopy);
aCopy.prepend(aCopyIcon);
aCopy.addEventListener("click", e => {
navigator.clipboard.writeText(e.target.id);
td.removeChild(pop);
});
ul.appendChild(liCopy);
}
}
}
const defaultOptions = {
isCopy: true,
isQueryRecord: true,
isShowAllData: true,
isViewInSalesforce: true
};
if (objectTypes.length === 1 && objectTypes[0] !== "Unknown") {
objectType = objectTypes[0];
setLinks(defaultOptions);
} else if (recordId && isRecordId(recordId)) {
sfConn.rest(`/services/data/v${apiVersion}/ui-api/records/${recordId}?layoutTypes=Compact`).then(res => {
objectType = res.apiName;
setLinks(defaultOptions);
}).catch(() => {
objectType = null;
defaultOptions.isQueryRecord = false;
defaultOptions.isShowAllData = false;
setLinks(defaultOptions);
});
} else {
defaultOptions.isQueryRecord = false;
defaultOptions.isShowAllData = false;
objectType = null;
setLinks(defaultOptions);
}
function closer(ev) {
if (ev != e && ev.target.closest(".pop-menu") != pop) {
removeEventListener("click", closer);
pop.remove();
}
}
addEventListener("click", closer);
});
a.textContent = label;
td.appendChild(a);
}
function isEventLogFile(text) {
// test the text to identify if this is a path to an eventLogFile
return /^\/services\/data\/v[0-9]{2,3}.[0-9]{1}\/sobjects\/EventLogFile\/[a-z0-9]{5}0000[a-z0-9]{9}\/LogFile$/i.exec(text);
}
if (typeof cell == "object" && cell != null && cell.attributes && cell.attributes.type) {
if (cell.attributes.type == "AggregateResult") {
td.textContent = cell.attributes.type;
return;
}
popLink(
() => {
let recordId = null;
if (cell.attributes.url) {
recordId = cell.attributes.url.replace(/.*\//, "");
}
let objectTypes = [cell.attributes.type];
return {objectTypes, recordId};
},
cell.attributes.type
);
} else if (typeof cell == "string" && isRecordId(cell)) {
popLink(
() => {
let recordId = cell;
let {globalDescribe} = rt.describeInfo.describeGlobal(rt.isTooling);
let objectTypes;
if (globalDescribe) {
let keyPrefix = recordId.substring(0, 3);
objectTypes = globalDescribe.sobjects.filter(sobject => sobject.keyPrefix == keyPrefix).map(sobject => sobject.name);
} else {
objectTypes = [];
}
return {objectTypes, recordId};
},
cell
);
} else if (typeof cell == "string" && isEventLogFile(cell)) {
popLink(
() => {
let recordId = cell;
let objectTypes = [];
return {objectTypes, recordId};
},
cell
);
} else if (cell == null) {
td.textContent = "";
} else {
const formatted = formatDateCell(cell, rt.dateFormatOptions || getDateFormatOptions());
td.textContent = formatted !== null ? formatted : cell;
}
}
/*
A table that contains millions of records will freeze the browser if we try to render the entire table at once.
Therefore we implement a table within a scrollable area, where the cells are only rendered, when they are scrolled into view.
Limitations:
* It is not possible to select or search the contents of the table outside the rendered area. The user will need to copy to Excel or CSV to do that.
* Since we initially estimate the size of each cell and then update as we render them, the table will sometimes "jump" as the user scrolls.
* There is no line wrapping within the cells. A cell with a lot of text will be very wide.
Implementation:
Since we don't know the height of each row before we render it, we assume to begin with that it is fairly small, and we then grow it to fit the rendered content, as the user scrolls.
We never schrink the height of a row, to ensure that it stabilzes as the user scrolls. The heights are stored in the `rowHeights` array.
To avoid re-rendering the visible part on every scroll, we render an area that is slightly larger than the viewport, and we then only re-render, when the viewport moves outside the rendered area.
Since we don't know the height of each row before we render it, we don't know exactly how many rows to render.
However since we never schrink the height of a row, we never render too few rows, and since we update the height estimates after each render, we won't repeatedly render too many rows.
The initial estimate of the height of each row should be large enough to ensure we don't render too many rows in our initial render.
We only measure the current size at the end of each render, to minimize the number of synchronous layouts the browser needs to make.
We support adding new rows to the end of the table, and new cells to the end of a row, but not deleting existing rows, and we do not reduce the height of a row if the existing content changes.
Each row may be visible or hidden.
In addition to keeping track of the height of each cell, we keep track of the total height in order to adjust the height of the scrollable area, and we keep track of the position of the scrolled area.
After a scroll we search for the position of the new rendered area using the position of the old scrolled area, which should be the least amount of work when the user scrolls in one direction.
The table must have at least one row, since the code keeps track of the first rendered row.
We assume that the height of the cells we measure sum up to the height of the table.
We do the exact same logic for columns, as we do for rows.
We assume that the size of a cell is not influenced by the size of other cells. Therefore we style cells with `white-space: pre`.
@param element A scrollable DOM element to render the table within.
ScrollTable initScrollTable(DOMElement element);
interface Table {
Cell[][] table; // a two-dimensional array of table rows and cells
boolean[] rowVisibilities; // For each row, true if it is visible, or false if it is hidden
boolean[] colVisibilities; // For each column, true if it is visible, or false if it is hidden
// Refactor: The following three attributes are only used by renderCell, they should be moved to a different interface
boolean isTooling;
DescribeInfo describeInfo;
String sfHost;
}
void renderCell(Table table, Cell cell, DOMElement element); // Render cell within element
interface Cell {
// Anything, passed to the renderCell function
}
interface ScrollTable {
void viewportChange(); // Must be called whenever the size of viewport changes.
void dataChange(Table newData); // Must be called whenever the data changes. (even if it is the same object)
}
*/
export function initScrollTable(scroller) {
let data = null;
let scrolled = document.createElement("div");
scrolled.className = "scrolltable-scrolled";
scroller.appendChild(scrolled);
let initialRowHeight = 15;
let initialColWidth = 50;
// Dynamic buffer calculation based on viewport size
let bufferHeight = Math.min(500, scroller.offsetHeight);
let bufferWidth = Math.min(500, scroller.offsetWidth);
let headerRows = 1;
let headerCols = 0;
let rowHeights = [];
let rowVisible = [];
let rowCount = 0;
let totalHeight = 0;
let firstRowIdx = 0;
let firstRowTop = 0;
let lastRowIdx = 0;
let lastRowTop = 0;
let colWidths = [];
let colVisible = [];
let colCount = 0;
let totalWidth = 0;
let firstColIdx = 0;
let firstColLeft = 0;
let lastColIdx = 0;
let lastColLeft = 0;
function updateBuffers() {
// Recalculate buffers when viewport changes
bufferHeight = Math.min(500, scroller.offsetHeight);
bufferWidth = Math.min(500, scroller.offsetWidth);
console.log("Buffers updated:", {bufferHeight, bufferWidth});
}
function dataChange(newData) {
console.log("Data changed");
data = newData;
if (data == null || data.rowVisibilities.length == 0 || data.colVisibilities.length == 0) {
rowHeights = [];
rowVisible = [];
rowCount = 0;
totalHeight = 0;
firstRowIdx = 0;
firstRowTop = 0;
lastRowIdx = 0;
lastRowTop = 0;
colWidths = [];
colVisible = [];
colCount = 0;
totalWidth = 0;
firstColIdx = 0;
firstColLeft = 0;
lastColIdx = 0;
lastColLeft = 0;
renderData({force: true});
} else {
let newRowCount = data.rowVisibilities.length;
for (let r = rowCount; r < newRowCount; r++) {
rowHeights[r] = initialRowHeight;
rowVisible[r] = 0;
}
rowCount = newRowCount;
for (let r = 0; r < rowCount; r++) {
let newVisible = Number(data.rowVisibilities[r]);
let visibilityChange = newVisible - rowVisible[r];
totalHeight += visibilityChange * rowHeights[r];
if (r < firstRowIdx) {
firstRowTop += visibilityChange * rowHeights[r];
}
rowVisible[r] = newVisible;
}
let newColCount = data.colVisibilities.length;
for (let c = colCount; c < newColCount; c++) {
colWidths[c] = initialColWidth;
colVisible[c] = 0;
}
colCount = newColCount;
for (let c = 0; c < colCount; c++) {
let newVisible = Number(data.colVisibilities[c]);
let visibilityChange = newVisible - colVisible[c];
totalWidth += visibilityChange * colWidths[c];
if (c < firstColIdx) {
firstColLeft += visibilityChange * colWidths[c];
}
colVisible[c] = newVisible;
}
renderData({force: true});
}
updateBuffers(); // Ensure buffers are updated when data changes
}
let scrollTop = 0;
let scrollLeft = 0;
let offsetHeight = 0;
let offsetWidth = 0;
function viewportChange() {
// Enhanced viewport change detection
let newScrollTop = scroller.scrollTop;
let newScrollLeft = scroller.scrollLeft;
let newOffsetHeight = scroller.offsetHeight;
let newOffsetWidth = scroller.offsetWidth;
if (scrollTop !== newScrollTop || scrollLeft !== newScrollLeft
|| offsetHeight !== newOffsetHeight || offsetWidth !== newOffsetWidth) {
console.log("Viewport changed:", {
scrollTop: newScrollTop,
scrollLeft: newScrollLeft,
offsetHeight: newOffsetHeight,
offsetWidth: newOffsetWidth
});
scrollTop = newScrollTop;
scrollLeft = newScrollLeft;
offsetHeight = newOffsetHeight;
offsetWidth = newOffsetWidth;
updateBuffers();
renderData({force: false});
}
}
function renderData({force}) {
try {
console.log("Rendering data. Force:", force);
scrollTop = scroller.scrollTop;
scrollLeft = scroller.scrollLeft;
offsetHeight = scroller.offsetHeight;
offsetWidth = scroller.offsetWidth;
if (rowCount == 0 || colCount == 0) {
scrolled.textContent = "";
scrolled.style.height = "0px";
scrolled.style.width = "0px";
return;
}
if (!force && firstRowTop <= scrollTop && (lastRowTop >= scrollTop + offsetHeight || lastRowIdx == rowCount) && firstColLeft <= scrollLeft && (lastColLeft >= scrollLeft + offsetWidth || lastColIdx == colCount)) {
return;
}
console.log("Rendering table");
while (firstRowTop < scrollTop - bufferHeight && firstRowIdx < rowCount - 1) {
firstRowTop += rowVisible[firstRowIdx] * rowHeights[firstRowIdx];
firstRowIdx++;
}
while (firstRowTop > scrollTop - bufferHeight && firstRowIdx > 0) {
firstRowIdx--;
firstRowTop -= rowVisible[firstRowIdx] * rowHeights[firstRowIdx];
}
while (firstColLeft < scrollLeft - bufferWidth && firstColIdx < colCount - 1) {
firstColLeft += colVisible[firstColIdx] * colWidths[firstColIdx];
firstColIdx++;
}
while (firstColLeft > scrollLeft - bufferWidth && firstColIdx > 0) {
firstColIdx--;
firstColLeft -= colVisible[firstColIdx] * colWidths[firstColIdx];
}
lastRowIdx = firstRowIdx;
lastRowTop = firstRowTop;
while (lastRowTop < scrollTop + offsetHeight + bufferHeight && lastRowIdx < rowCount) {
lastRowTop += rowVisible[lastRowIdx] * rowHeights[lastRowIdx];
lastRowIdx++;
}
lastColIdx = firstColIdx;
lastColLeft = firstColLeft;
while (lastColLeft < scrollLeft + offsetWidth + bufferWidth && lastColIdx < colCount) {
lastColLeft += colVisible[lastColIdx] * colWidths[lastColIdx];
lastColIdx++;
}
scrolled.textContent = "";
let table = document.createElement("table");
table.className = "slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered slds-is-relative";
let cellsVisible = false;
// Ensure firstRowIdx never goes below headerRows
firstRowIdx = Math.max(headerRows, firstRowIdx);
// Render header rows separately to ensure they're always visible
for (let r = 0; r < headerRows; r++) {
if (rowVisible[r] == 0) continue;
let row = data.table[r];
let tr = document.createElement("tr");
tr.className = "slds-line-height_reset";
tr.style.position = "sticky";
tr.style.top = "0";
tr.style.zIndex = "9";
for (let c = firstColIdx; c < lastColIdx; c++) {
if (colVisible[c] == 0) continue;
let cell = row[c];
let td = document.createElement("td");
let cellClasses = `scrolltable-cell header ${(cell.startsWith("_") && greyOutSkippedColumns) ? "skipped" : ""}`;
if (data.preventLineWrap !== false) {
cellClasses += " prevent-line-wrap";
}
td.className = cellClasses;
td.style.minWidth = colWidths[c] + "px";
td.style.height = rowHeights[r] + "px";
renderCell(data, cell, td);
tr.appendChild(td);
}
table.appendChild(tr);
}
// Render data rows
for (let r = Math.max(headerRows, firstRowIdx); r < lastRowIdx; r++) {
if (rowVisible[r] == 0) {
continue;
}
let row = data.table[r];
let tr = document.createElement("tr");
tr.className = "slds-line-height_reset";
for (let c = firstColIdx; c < lastColIdx; c++) {
if (colVisible[c] == 0) {
continue;
}
let cell = row[c];
let td = document.createElement("td");
let cellClasses = "scrolltable-cell";
if (c < headerCols) {
cellClasses += " header";
}
if (data.preventLineWrap !== false) {
cellClasses += " prevent-line-wrap";
}
td.className = cellClasses;
td.style.minWidth = colWidths[c] + "px";
td.style.height = rowHeights[r] + "px";
renderCell(data, cell, td);
tr.appendChild(td);
cellsVisible = true;
}
table.appendChild(tr);
}
// Adjust table position to prevent header overlap at the top
let tableTop = Math.max(0, firstRowTop);
table.style.top = tableTop + "px";
table.style.left = firstColLeft + "px";
scrolled.appendChild(table);
if (cellsVisible) {
// Start adjusting heights from the first data row, not header
let tr = table.children[headerRows];
for (let r = Math.max(headerRows, firstRowIdx); r < lastRowIdx; r++) {
if (rowVisible[r] == 0) {
continue;
}
let rowRect = tr.firstElementChild.getBoundingClientRect();
let oldHeight = rowHeights[r];
let newHeight = Math.max(oldHeight, rowRect.height);
rowHeights[r] = newHeight;
totalHeight += newHeight - oldHeight;
lastRowTop += newHeight - oldHeight;
tr = tr.nextElementSibling;
}
let td = table.firstElementChild.firstElementChild;
for (let c = firstColIdx; c < lastColIdx; c++) {
if (colVisible[c] == 0) {
continue;
}
let colRect = td.getBoundingClientRect();
let oldWidth = colWidths[c];
let newWidth = Math.max(oldWidth, colRect.width);
colWidths[c] = newWidth;
totalWidth += newWidth - oldWidth;
lastColLeft += newWidth - oldWidth;
td = td.nextElementSibling;
}
}
console.log("Render complete");
} catch (error) {
console.error("Error in renderData:", error);
// Enhanced error logging
console.log("Current state:", {
rowCount,
colCount,
firstRowIdx,
lastRowIdx,
firstColIdx,
lastColIdx,
scrollTop,
scrollLeft,
offsetHeight,
offsetWidth
});
}
}
dataChange(null);
scroller.addEventListener("scroll", viewportChange);
// Added resize event listener to handle viewport changes
window.addEventListener("resize", viewportChange);
return {
viewportChange,
dataChange
};
}