-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathselection.ts
More file actions
103 lines (95 loc) · 3.13 KB
/
selection.ts
File metadata and controls
103 lines (95 loc) · 3.13 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
// Selection and Filter Logic for Models Table
export function initializeSelection() {
const rowCheckboxes = () => Array.from(document.querySelectorAll('.row-checkbox')) as HTMLInputElement[];
const selectAll = document.getElementById('select-all') as HTMLInputElement;
const filterBtn = document.getElementById('filter-selected') as HTMLButtonElement;
const selected = new Set<string>();
let filterActive = false;
// Helper: get row key
function getRowKey(tr: HTMLTableRowElement): string {
return tr.getAttribute('data-provider-id') + '::' + tr.getAttribute('data-model-id')!;
}
// Update selection state and UI
function updateSelectionUI() {
rowCheckboxes().forEach(cb => {
const tr = cb.closest('tr') as HTMLTableRowElement;
const key = getRowKey(tr);
if (selected.has(key)) {
cb.checked = true;
tr.classList.add('selected-row');
} else {
cb.checked = false;
tr.classList.remove('selected-row');
}
});
// Update select-all
if (selected.size === 0) {
selectAll.checked = false;
selectAll.indeterminate = false;
} else if (selected.size === rowCheckboxes().length) {
selectAll.checked = true;
selectAll.indeterminate = false;
} else {
selectAll.checked = false;
selectAll.indeterminate = true;
}
selectAll.disabled = filterActive;
// Update filter button
filterBtn.disabled = selectAll.checked;
filterBtn.textContent = filterActive ? 'Show All' : 'Show Selected Only';
}
// Row checkbox click
document.addEventListener('change', function (e) {
const target = e.target as HTMLInputElement;
if (target.classList.contains('row-checkbox')) {
const tr = target.closest('tr') as HTMLTableRowElement;
const key = getRowKey(tr);
if (target.checked) {
selected.add(key);
} else {
selected.delete(key);
}
updateSelectionUI();
if (filterActive) applyFilter();
}
});
// Select all checkbox
selectAll.addEventListener('change', function (e) {
if (selectAll.checked) {
rowCheckboxes().forEach(cb => {
const tr = cb.closest('tr') as HTMLTableRowElement;
selected.add(getRowKey(tr));
});
} else {
selected.clear();
}
updateSelectionUI();
if (filterActive) applyFilter();
});
// Filter button
filterBtn.addEventListener('click', function () {
filterActive = !filterActive;
applyFilter();
updateSelectionUI();
});
function applyFilter() {
rowCheckboxes().forEach(cb => {
const tr = cb.closest('tr') as HTMLTableRowElement;
const key = getRowKey(tr);
if (filterActive) {
tr.style.display = selected.has(key) ? '' : 'none';
} else {
tr.style.display = '';
}
});
}
// Keyboard accessibility: space/enter on row
document.addEventListener('keydown', function (e) {
if ((e.key === ' ' || e.key === 'Enter') && (document.activeElement as HTMLElement)?.classList.contains('row-checkbox')) {
e.preventDefault();
(document.activeElement as HTMLInputElement).click();
}
});
// Initial UI update
updateSelectionUI();
}