This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtable_widget.js
More file actions
269 lines (236 loc) · 8.64 KB
/
Copy pathtable_widget.js
File metadata and controls
269 lines (236 loc) · 8.64 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
/**
* 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 = {
PAGE: "page",
PAGE_SIZE: "page_size",
ROW_COUNT: "row_count",
TABLE_HTML: "table_html",
SORT_COLUMN: "sort_column",
SORT_ASCENDING: "sort_ascending",
ERROR_MESSAGE: "error_message",
ORDERABLE_COLUMNS: "orderable_columns",
};
const Event = {
CHANGE: "change",
CHANGE_TABLE_HTML: `change:${ModelProperty.TABLE_HTML}`,
CLICK: "click",
};
/**
* Renders the interactive table widget.
* @param {{
* model: any,
* el: HTMLElement
* }} options
*/
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");
errorContainer.style.display = "none";
errorContainer.style.color = "red";
errorContainer.style.padding = "8px";
errorContainer.style.marginBottom = "8px";
errorContainer.style.border = "1px solid red";
errorContainer.style.borderRadius = "4px";
errorContainer.style.backgroundColor = "#ffebee";
const tableContainer = document.createElement("div");
const footer = document.createElement("div");
// Footer: Total rows label
const rowCountLabel = document.createElement("div");
// Footer: Pagination controls
const paginationContainer = document.createElement("div");
const prevPage = document.createElement("button");
const paginationLabel = document.createElement("span");
const nextPage = document.createElement("button");
// Footer: Page size controls
const pageSizeContainer = document.createElement("div");
const pageSizeLabel = document.createElement("label");
const pageSizeSelect = document.createElement("select");
// Add CSS classes
tableContainer.classList.add("table-container");
footer.classList.add("footer");
paginationContainer.classList.add("pagination");
pageSizeContainer.classList.add("page-size");
// Configure pagination buttons
prevPage.type = "button";
nextPage.type = "button";
prevPage.textContent = "Prev";
nextPage.textContent = "Next";
// Configure page size selector
pageSizeLabel.textContent = "Page Size";
for (const size of [10, 25, 50, 100]) {
const option = document.createElement("option");
option.value = size;
option.textContent = size;
if (size === model.get(ModelProperty.PAGE_SIZE)) {
option.selected = true;
}
pageSizeSelect.appendChild(option);
}
/** Updates the footer states and page label based on the model. */
function updateButtonStates() {
const rowCount = model.get(ModelProperty.ROW_COUNT);
const pageSize = model.get(ModelProperty.PAGE_SIZE);
const currentPage = model.get(ModelProperty.PAGE);
if (rowCount === null) {
// Unknown total rows
rowCountLabel.textContent = "Total rows unknown";
paginationLabel.textContent = `Page ${(currentPage + 1).toLocaleString()} of many`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = false; // Allow navigation until we hit the end
} else {
// Known total rows
const totalPages = Math.ceil(rowCount / pageSize);
rowCountLabel.textContent = `${rowCount.toLocaleString()} total rows`;
paginationLabel.textContent = `Page ${(currentPage + 1).toLocaleString()} of ${rowCount.toLocaleString()}`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = currentPage >= totalPages - 1;
}
pageSizeSelect.value = pageSize;
}
/**
* Increments or decrements the page in the model.
* @param {number} direction - `1` for next, `-1` for previous.
*/
function handlePageChange(direction) {
const current = model.get(ModelProperty.PAGE);
const next = current + direction;
model.set(ModelProperty.PAGE, next);
model.save_changes();
}
/**
* Handles changes to the page size from the dropdown.
* @param {number} size - The new page size.
*/
function handlePageSizeChange(size) {
const currentSize = model.get(ModelProperty.PAGE_SIZE);
if (size !== currentSize) {
model.set(ModelProperty.PAGE_SIZE, size);
model.save_changes();
}
}
function handleTableHTMLChange() {
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();
});
}
});
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));
pageSizeSelect.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();
}
});
// Assemble the DOM
paginationContainer.appendChild(prevPage);
paginationContainer.appendChild(paginationLabel);
paginationContainer.appendChild(nextPage);
pageSizeContainer.appendChild(pageSizeLabel);
pageSizeContainer.appendChild(pageSizeSelect);
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 };