|
| 1 | +import { state, persistState } from './state.js'; |
| 2 | +import { |
| 3 | + goToPage, |
| 4 | + onFilterChange, |
| 5 | + onPageSizeChange, |
| 6 | + onDateFormatChange, |
| 7 | + startColumnResize, |
| 8 | + onColumnFilterKeydown, |
| 9 | + applyColumnFilter, |
| 10 | + onColumnHeaderClick, |
| 11 | + toggleColumnPin, |
| 12 | + toggleRowPin, |
| 13 | + onSelectAllClick, |
| 14 | + onColumnSort, |
| 15 | + onRowNumberClick, |
| 16 | + onCellClick, |
| 17 | + onCellDoubleClick |
| 18 | +} from './grid-actions.js'; |
| 19 | +import { openCellPreview } from './edit.js'; |
| 20 | + |
| 21 | +export function initGridControls() { |
| 22 | + document.getElementById('filterInput')?.addEventListener('keyup', onFilterChange); |
| 23 | + document.getElementById('pageSizeSelect')?.addEventListener('change', onPageSizeChange); |
| 24 | + document.getElementById('dateFormatSelect')?.addEventListener('change', onDateFormatChange); |
| 25 | + |
| 26 | + document.getElementById('btnFirst')?.addEventListener('click', () => goToPage(0)); |
| 27 | + document.getElementById('btnPrev')?.addEventListener('click', () => goToPage(state.currentPageIndex - 1)); |
| 28 | + document.getElementById('btnNext')?.addEventListener('click', () => goToPage(state.currentPageIndex + 1)); |
| 29 | + document.getElementById('btnLast')?.addEventListener('click', () => goToPage(state.totalPageCount - 1)); |
| 30 | +} |
| 31 | + |
| 32 | +export function initGridInteraction() { |
| 33 | + const container = document.getElementById('gridContainer'); |
| 34 | + if (!container) return; |
| 35 | + |
| 36 | + // --- Mousedown Delegation (Resize) --- |
| 37 | + container.addEventListener('mousedown', (event) => { |
| 38 | + if (event.target.classList.contains('resize-handle')) { |
| 39 | + event.stopPropagation(); |
| 40 | + const headerCell = event.target.closest('.header-cell'); |
| 41 | + if (headerCell && headerCell.dataset.column) { |
| 42 | + startColumnResize(event, headerCell.dataset.column); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + // --- Keydown Delegation (Filters) --- |
| 48 | + container.addEventListener('keydown', (event) => { |
| 49 | + if (event.target.classList.contains('column-filter')) { |
| 50 | + const colName = event.target.dataset.column; |
| 51 | + if (colName) onColumnFilterKeydown(event, colName); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + // --- Click Delegation --- |
| 56 | + container.addEventListener('click', (event) => { |
| 57 | + const target = event.target; |
| 58 | + |
| 59 | + // --- Header Interactions --- |
| 60 | + if (target.closest('.grid-header')) { |
| 61 | + // 1. Filter Apply Button |
| 62 | + if (target.closest('.filter-apply-btn')) { |
| 63 | + event.stopPropagation(); |
| 64 | + const headerCell = target.closest('.header-cell'); |
| 65 | + if (headerCell && headerCell.dataset.column) { |
| 66 | + applyColumnFilter(headerCell.dataset.column); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // 2. Prevent sort when clicking inputs/bottom area |
| 72 | + if (target.closest('.header-bottom') || target.closest('.column-filter')) { |
| 73 | + event.stopPropagation(); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // 3. Column Selection Icon |
| 78 | + if (target.closest('.select-column-icon')) { |
| 79 | + event.stopPropagation(); |
| 80 | + const headerCell = target.closest('.header-cell'); |
| 81 | + if (headerCell && headerCell.dataset.column) { |
| 82 | + onColumnHeaderClick(event, headerCell.dataset.column); |
| 83 | + } |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // 4. Header Pin Icon |
| 88 | + if (target.closest('.pin-icon')) { |
| 89 | + event.stopPropagation(); |
| 90 | + const headerCell = target.closest('.header-cell'); |
| 91 | + if (headerCell && headerCell.dataset.column) { |
| 92 | + toggleColumnPin(event, headerCell.dataset.column); |
| 93 | + } |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // 5. Select All (Row Number Header) |
| 98 | + if (target.closest('.row-number-header')) { |
| 99 | + onSelectAllClick(event); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // 6. Sort (Header Top) |
| 104 | + const headerTop = target.closest('.header-top'); |
| 105 | + if (headerTop) { |
| 106 | + const headerCell = headerTop.closest('.header-cell'); |
| 107 | + if (headerCell && headerCell.dataset.column) { |
| 108 | + onColumnSort(headerCell.dataset.column); |
| 109 | + } |
| 110 | + return; |
| 111 | + } |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // --- Body Interactions --- |
| 116 | + |
| 117 | + // 1. Row Pin Icon |
| 118 | + if (target.closest('.pin-icon')) { |
| 119 | + const rowEl = target.closest('.data-row'); |
| 120 | + if (rowEl) { |
| 121 | + const rowId = rowEl.dataset.rowid; |
| 122 | + // Handle type conversion |
| 123 | + const safeRowId = resolveRowIdType(rowId); |
| 124 | + toggleRowPin(event, safeRowId); |
| 125 | + } |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // 2. Expand Icon |
| 130 | + if (target.closest('.expand-icon')) { |
| 131 | + const cellEl = target.closest('.data-cell'); |
| 132 | + if (cellEl) { |
| 133 | + const rowIdx = parseInt(cellEl.dataset.rowidx, 10); |
| 134 | + const colIdx = parseInt(cellEl.dataset.colidx, 10); |
| 135 | + const rowId = resolveRowIdType(cellEl.closest('.data-row').dataset.rowid); |
| 136 | + openCellPreview(rowIdx, colIdx, rowId); |
| 137 | + } |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // 3. Row Number Cell |
| 142 | + if (target.closest('.row-number')) { |
| 143 | + const rowEl = target.closest('.data-row'); |
| 144 | + if (rowEl) { |
| 145 | + const rowId = resolveRowIdType(rowEl.dataset.rowid); |
| 146 | + const rowIdx = parseInt(rowEl.dataset.rowidx, 10); |
| 147 | + onRowNumberClick(event, rowId, rowIdx); |
| 148 | + } |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + // 4. Data Cell |
| 153 | + const cellEl = target.closest('.data-cell'); |
| 154 | + if (cellEl) { |
| 155 | + const rowIdx = parseInt(cellEl.dataset.rowidx, 10); |
| 156 | + const colIdx = parseInt(cellEl.dataset.colidx, 10); |
| 157 | + const rowEl = cellEl.closest('.data-row'); |
| 158 | + const rowId = resolveRowIdType(rowEl.dataset.rowid); |
| 159 | + onCellClick(event, rowIdx, colIdx, rowId); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + }); |
| 164 | + |
| 165 | + // Double Click Handler |
| 166 | + container.addEventListener('dblclick', (event) => { |
| 167 | + const cellEl = event.target.closest('.data-cell'); |
| 168 | + if (cellEl && !cellEl.classList.contains('row-number')) { |
| 169 | + const rowIdx = parseInt(cellEl.dataset.rowidx, 10); |
| 170 | + const colIdx = parseInt(cellEl.dataset.colidx, 10); |
| 171 | + const rowEl = cellEl.closest('.data-row'); |
| 172 | + const rowId = resolveRowIdType(rowEl.dataset.rowid); |
| 173 | + onCellDoubleClick(event, rowIdx, colIdx, rowId); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + // Lazy overflow detection |
| 178 | + container.addEventListener('mouseover', (event) => { |
| 179 | + const cell = event.target.closest('.data-cell'); |
| 180 | + if (cell && !cell.classList.contains('checked-overflow')) { |
| 181 | + const textSpan = cell.querySelector('.cell-text'); |
| 182 | + if (textSpan) { |
| 183 | + const hasOverflow = textSpan.scrollWidth > textSpan.clientWidth; |
| 184 | + cell.classList.toggle('has-overflow', hasOverflow); |
| 185 | + cell.classList.add('checked-overflow'); |
| 186 | + } |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + // Track scroll position for state persistence |
| 191 | + container.addEventListener('scroll', () => { |
| 192 | + state.scrollPosition.left = container.scrollLeft; |
| 193 | + state.scrollPosition.top = container.scrollTop; |
| 194 | + persistState(); |
| 195 | + }, { passive: true }); |
| 196 | +} |
| 197 | + |
| 198 | +function resolveRowIdType(idStr) { |
| 199 | + if (idStr === undefined || idStr === null) return idStr; |
| 200 | + // Row IDs in SQLite are integers, but if using 'WITHOUT ROWID' tables, PK could be anything. |
| 201 | + // However, for standard tables, we try to keep them as numbers to match what the backend returns. |
| 202 | + const num = Number(idStr); |
| 203 | + return !isNaN(num) && idStr.trim() !== '' ? num : idStr; |
| 204 | +} |
0 commit comments