This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtable_widget.js
More file actions
349 lines (298 loc) · 11.7 KB
/
table_widget.js
File metadata and controls
349 lines (298 loc) · 11.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
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ModelProperty = {
ERROR_MESSAGE: 'error_message',
ORDERABLE_COLUMNS: 'orderable_columns',
PAGE: 'page',
PAGE_SIZE: 'page_size',
ROW_COUNT: 'row_count',
SORT_COLUMNS: 'sort_columns',
SORT_ASCENDING: 'sort_ascending',
TABLE_HTML: 'table_html',
MAX_COLUMNS: 'max_columns',
};
const Event = {
CHANGE: 'change',
CHANGE_TABLE_HTML: 'change:table_html',
CLICK: 'click',
};
/**
* Renders the interactive table widget.
* @param {{ model: any, el: !HTMLElement }} props - The widget properties.
*/
function render({ model, el }) {
el.classList.add('bigframes-widget');
const errorContainer = document.createElement('div');
errorContainer.classList.add('error-message');
const tableContainer = document.createElement('div');
tableContainer.classList.add('table-container');
const footer = document.createElement('footer');
footer.classList.add('footer');
/** Detects theme and applies necessary style overrides. */
function updateTheme() {
const body = document.body;
const isDark =
body.classList.contains('vscode-dark') ||
body.classList.contains('theme-dark') ||
body.dataset.theme === 'dark' ||
body.getAttribute('data-vscode-theme-kind') === 'vscode-dark';
if (isDark) {
el.classList.add('bigframes-dark-mode');
} else {
el.classList.remove('bigframes-dark-mode');
}
}
updateTheme();
// Re-check after mount to ensure parent styling is applied.
setTimeout(updateTheme, 300);
const observer = new MutationObserver(updateTheme);
observer.observe(document.body, {
attributes: true,
attributeFilter: ['class', 'data-theme', 'data-vscode-theme-kind'],
});
// Settings controls container
const settingsContainer = document.createElement('div');
settingsContainer.classList.add('settings');
// Pagination controls
const paginationContainer = document.createElement('div');
paginationContainer.classList.add('pagination');
const prevPage = document.createElement('button');
const pageIndicator = document.createElement('span');
pageIndicator.classList.add('page-indicator');
const nextPage = document.createElement('button');
const rowCountLabel = document.createElement('span');
rowCountLabel.classList.add('row-count');
// Page size controls
const pageSizeContainer = document.createElement('div');
pageSizeContainer.classList.add('page-size');
const pageSizeLabel = document.createElement('label');
const pageSizeInput = document.createElement('select');
prevPage.textContent = '<';
nextPage.textContent = '>';
pageSizeLabel.textContent = 'Page size:';
const pageSizes = [10, 25, 50, 100];
for (const size of pageSizes) {
const option = document.createElement('option');
option.value = size;
option.textContent = size;
if (size === model.get(ModelProperty.PAGE_SIZE)) {
option.selected = true;
}
pageSizeInput.appendChild(option);
}
// Max columns controls
const maxColumnsContainer = document.createElement('div');
maxColumnsContainer.classList.add('max-columns');
const maxColumnsLabel = document.createElement('label');
const maxColumnsInput = document.createElement('select');
maxColumnsLabel.textContent = 'Max columns:';
// 0 represents "All" (all columns)
const maxColumnOptions = [5, 10, 15, 20, 0];
for (const cols of maxColumnOptions) {
const option = document.createElement('option');
option.value = cols;
option.textContent = cols === 0 ? 'All' : cols;
const currentMax = model.get(ModelProperty.MAX_COLUMNS);
// Handle None/null from python as 0/All
const currentMaxVal =
currentMax === null || currentMax === undefined ? 0 : currentMax;
if (cols === currentMaxVal) {
option.selected = true;
}
maxColumnsInput.appendChild(option);
}
function updateButtonStates() {
const currentPage = model.get(ModelProperty.PAGE);
const pageSize = model.get(ModelProperty.PAGE_SIZE);
const rowCount = model.get(ModelProperty.ROW_COUNT);
if (rowCount === null) {
rowCountLabel.textContent = 'Total rows unknown';
pageIndicator.textContent = `Page ${(currentPage + 1).toLocaleString()} of many`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = false;
} else if (rowCount === 0) {
rowCountLabel.textContent = '0 total rows';
pageIndicator.textContent = 'Page 1 of 1';
prevPage.disabled = true;
nextPage.disabled = true;
} else {
const totalPages = Math.ceil(rowCount / pageSize);
rowCountLabel.textContent = `${rowCount.toLocaleString()} total rows`;
pageIndicator.textContent = `Page ${(currentPage + 1).toLocaleString()} of ${totalPages.toLocaleString()}`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = currentPage >= totalPages - 1;
}
pageSizeInput.value = pageSize;
}
function handlePageChange(direction) {
const currentPage = model.get(ModelProperty.PAGE);
model.set(ModelProperty.PAGE, currentPage + direction);
model.save_changes();
}
function handlePageSizeChange(newSize) {
model.set(ModelProperty.PAGE_SIZE, newSize);
model.set(ModelProperty.PAGE, 0);
model.save_changes();
}
let isHeightInitialized = false;
function handleTableHTMLChange() {
tableContainer.innerHTML = model.get(ModelProperty.TABLE_HTML);
// After the first render, dynamically set the container height to fit the
// initial page (usually 10 rows) and then lock it.
setTimeout(() => {
if (!isHeightInitialized) {
const table = tableContainer.querySelector('table');
if (table) {
const tableHeight = table.offsetHeight;
// Add a small buffer(e.g. 2px) for borders to avoid scrollbars.
if (tableHeight > 0) {
tableContainer.style.height = `${tableHeight + 2}px`;
isHeightInitialized = true;
}
}
}
}, 0);
const sortableColumns = model.get(ModelProperty.ORDERABLE_COLUMNS);
const currentSortColumns = model.get(ModelProperty.SORT_COLUMNS) || [];
const currentSortAscending = model.get(ModelProperty.SORT_ASCENDING) || [];
const getSortIndex = (colName) => currentSortColumns.indexOf(colName);
const headers = tableContainer.querySelectorAll('th');
headers.forEach((header) => {
const headerDiv = header.querySelector('div');
const columnName = headerDiv.textContent.trim();
if (columnName && sortableColumns.includes(columnName)) {
header.style.cursor = 'pointer';
const indicatorSpan = document.createElement('span');
indicatorSpan.classList.add('sort-indicator');
indicatorSpan.style.paddingLeft = '5px';
// Determine sort indicator and initial visibility
let indicator = '●'; // Default: unsorted (dot)
const sortIndex = getSortIndex(columnName);
if (sortIndex !== -1) {
const isAscending = currentSortAscending[sortIndex];
indicator = isAscending ? '▲' : '▼';
indicatorSpan.style.visibility = 'visible'; // Sorted arrows always visible
} else {
indicatorSpan.style.visibility = 'hidden';
}
indicatorSpan.textContent = indicator;
const existingIndicator = headerDiv.querySelector('.sort-indicator');
if (existingIndicator) {
headerDiv.removeChild(existingIndicator);
}
headerDiv.appendChild(indicatorSpan);
header.addEventListener('mouseover', () => {
if (getSortIndex(columnName) === -1) {
indicatorSpan.style.visibility = 'visible';
}
});
header.addEventListener('mouseout', () => {
if (getSortIndex(columnName) === -1) {
indicatorSpan.style.visibility = 'hidden';
}
});
header.addEventListener(Event.CLICK, (event) => {
const sortIndex = getSortIndex(columnName);
let newSortColumns = [...currentSortColumns];
let newSortAscending = [...currentSortAscending];
if (event.shiftKey) {
if (sortIndex !== -1) {
// Already sorted. Toggle or Remove.
if (newSortAscending[sortIndex]) {
// Asc -> Desc
newSortAscending[sortIndex] = false;
} else {
// Desc -> Remove
newSortColumns.splice(sortIndex, 1);
newSortAscending.splice(sortIndex, 1);
}
} else {
// Not sorted -> Append Asc
newSortColumns.push(columnName);
newSortAscending.push(true);
}
} else {
// No shift key. Single column mode.
if (sortIndex !== -1 && newSortColumns.length === 1) {
// Already only this column. Toggle or Remove.
if (newSortAscending[sortIndex]) {
newSortAscending[sortIndex] = false;
} else {
newSortColumns = [];
newSortAscending = [];
}
} else {
// Start fresh with this column
newSortColumns = [columnName];
newSortAscending = [true];
}
}
model.set(ModelProperty.SORT_ASCENDING, newSortAscending);
model.set(ModelProperty.SORT_COLUMNS, newSortColumns);
model.save_changes();
});
}
});
updateButtonStates();
}
function handleErrorMessageChange() {
const errorMsg = model.get(ModelProperty.ERROR_MESSAGE);
if (errorMsg) {
errorContainer.textContent = errorMsg;
errorContainer.style.display = 'block';
} else {
errorContainer.style.display = 'none';
}
}
prevPage.addEventListener(Event.CLICK, () => handlePageChange(-1));
nextPage.addEventListener(Event.CLICK, () => handlePageChange(1));
pageSizeInput.addEventListener(Event.CHANGE, (e) => {
const newSize = Number(e.target.value);
if (newSize) {
handlePageSizeChange(newSize);
}
});
maxColumnsInput.addEventListener(Event.CHANGE, (e) => {
const newVal = Number(e.target.value);
model.set(ModelProperty.MAX_COLUMNS, newVal);
model.save_changes();
});
model.on(Event.CHANGE_TABLE_HTML, handleTableHTMLChange);
model.on(`change:${ModelProperty.ROW_COUNT}`, updateButtonStates);
model.on(`change:${ModelProperty.ERROR_MESSAGE}`, handleErrorMessageChange);
model.on(`change:_initial_load_complete`, (val) => {
if (val) updateButtonStates();
});
model.on(`change:${ModelProperty.PAGE}`, updateButtonStates);
paginationContainer.appendChild(prevPage);
paginationContainer.appendChild(pageIndicator);
paginationContainer.appendChild(nextPage);
pageSizeContainer.appendChild(pageSizeLabel);
pageSizeContainer.appendChild(pageSizeInput);
maxColumnsContainer.appendChild(maxColumnsLabel);
maxColumnsContainer.appendChild(maxColumnsInput);
settingsContainer.appendChild(maxColumnsContainer);
settingsContainer.appendChild(pageSizeContainer);
footer.appendChild(rowCountLabel);
footer.appendChild(paginationContainer);
footer.appendChild(settingsContainer);
el.appendChild(errorContainer);
el.appendChild(tableContainer);
el.appendChild(footer);
handleTableHTMLChange();
handleErrorMessageChange();
}
export default { render };