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
301 lines (267 loc) · 9.63 KB
/
table_widget.js
File metadata and controls
301 lines (267 loc) · 9.63 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
/*
* 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_ASCENDING: "sort_ascending",
SORT_COLUMN: "sort_column",
TABLE_HTML: "table_html",
};
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 }) {
// Main container with a unique class for CSS scoping
el.classList.add("bigframes-widget");
// Add error message container at the top
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");
// 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:";
// Page size options
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);
}
/** Updates the footer states and page label based on the model. */
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) {
// Unknown total rows
rowCountLabel.textContent = "Total rows unknown";
pageIndicator.textContent = `Page ${(
currentPage + 1
).toLocaleString()} of many`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = false; // Allow navigation until we hit the end
} else if (rowCount === 0) {
// Empty dataset
rowCountLabel.textContent = "0 total rows";
pageIndicator.textContent = "Page 1 of 1";
prevPage.disabled = true;
nextPage.disabled = true;
} else {
// Known total rows
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;
}
/**
* Handles page navigation.
* @param {number} direction - The direction to navigate (-1 for previous, 1 for next).
*/
function handlePageChange(direction) {
const currentPage = model.get(ModelProperty.PAGE);
model.set(ModelProperty.PAGE, currentPage + direction);
model.save_changes();
}
/**
* Handles page size changes.
* @param {number} newSize - The new page size.
*/
function handlePageSizeChange(newSize) {
model.set(ModelProperty.PAGE_SIZE, newSize);
model.set(ModelProperty.PAGE, 0); // Reset to first page
model.save_changes();
}
/** Updates the HTML in the table container and refreshes button states. */
function handleTableHTMLChange() {
// Note: Using innerHTML is safe here because the content is generated
// by a trusted backend (DataFrame.to_html).
tableContainer.innerHTML = model.get(ModelProperty.TABLE_HTML);
// Get sortable columns from backend
const sortableColumns = model.get(ModelProperty.ORDERABLE_COLUMNS);
const currentSortColumn = model.get(ModelProperty.SORT_COLUMN);
const currentSortAscending = model.get(ModelProperty.SORT_ASCENDING);
// Add click handlers to column headers for sorting
const headers = tableContainer.querySelectorAll("th");
headers.forEach((header) => {
const headerDiv = header.querySelector("div");
const columnName = headerDiv.textContent.trim();
// Only add sorting UI for sortable columns
if (columnName && sortableColumns.includes(columnName)) {
header.style.cursor = "pointer";
// Create a span for the indicator
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)
if (currentSortColumn === columnName) {
indicator = currentSortAscending ? "▲" : "▼";
indicatorSpan.style.visibility = "visible"; // Sorted arrows always visible
} else {
indicatorSpan.style.visibility = "hidden"; // Unsorted dot hidden by default
}
indicatorSpan.textContent = indicator;
// Add indicator to the header, replacing the old one if it exists
const existingIndicator = headerDiv.querySelector(".sort-indicator");
if (existingIndicator) {
headerDiv.removeChild(existingIndicator);
}
headerDiv.appendChild(indicatorSpan);
// Add hover effects for unsorted columns only
header.addEventListener("mouseover", () => {
if (currentSortColumn !== columnName) {
indicatorSpan.style.visibility = "visible";
}
});
header.addEventListener("mouseout", () => {
if (currentSortColumn !== columnName) {
indicatorSpan.style.visibility = "hidden";
}
});
// Add click handler for three-state toggle
header.addEventListener(Event.CLICK, () => {
if (currentSortColumn === columnName) {
if (currentSortAscending) {
// Currently ascending → switch to descending
model.set(ModelProperty.SORT_ASCENDING, false);
} else {
// Currently descending → clear sort (back to unsorted)
model.set(ModelProperty.SORT_COLUMN, "");
model.set(ModelProperty.SORT_ASCENDING, true);
}
} else {
// Not currently sorted → sort ascending
model.set(ModelProperty.SORT_COLUMN, columnName);
model.set(ModelProperty.SORT_ASCENDING, true);
}
model.save_changes();
});
}
});
const table = tableContainer.querySelector("table");
if (table) {
const tableBody = table.querySelector("tbody");
/**
* Handles row hover events.
* @param {!Event} event - The mouse event.
* @param {boolean} isHovering - True to add hover class, false to remove.
*/
function handleRowHover(event, isHovering) {
const cell = event.target.closest("td");
if (cell) {
const row = cell.closest("tr");
const origRowId = row.dataset.origRow;
if (origRowId) {
const allCellsInGroup = tableBody.querySelectorAll(
`tr[data-orig-row="${origRowId}"] td`,
);
allCellsInGroup.forEach((c) => {
c.classList.toggle("row-hover", isHovering);
});
}
}
}
if (tableBody) {
tableBody.addEventListener("mouseover", (event) =>
handleRowHover(event, true),
);
tableBody.addEventListener("mouseout", (event) =>
handleRowHover(event, false),
);
}
}
updateButtonStates();
}
// Add error message handler
function handleErrorMessageChange() {
const errorMsg = model.get(ModelProperty.ERROR_MESSAGE);
if (errorMsg) {
errorContainer.textContent = errorMsg;
errorContainer.style.display = "block";
} else {
errorContainer.style.display = "none";
}
}
// Add event listeners
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);
}
});
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);
// Assemble the DOM
paginationContainer.appendChild(prevPage);
paginationContainer.appendChild(pageIndicator);
paginationContainer.appendChild(nextPage);
pageSizeContainer.appendChild(pageSizeLabel);
pageSizeContainer.appendChild(pageSizeInput);
footer.appendChild(rowCountLabel);
footer.appendChild(paginationContainer);
footer.appendChild(pageSizeContainer);
el.appendChild(errorContainer);
el.appendChild(tableContainer);
el.appendChild(footer);
// Initial render
handleTableHTMLChange();
handleErrorMessageChange();
}
export default { render };