Scroll to current date V3

This commit is contained in:
Greg 2025-05-10 17:44:45 +02:00
parent 3377970fea
commit c80ce1acb8

View File

@ -58,17 +58,19 @@ function renderTable() {
const lastMatch = pastDates.length > 0 ? pastDates.reduce((a, b) => parseDate(a) > parseDate(b) ? a : b) : null; const lastMatch = pastDates.length > 0 ? pastDates.reduce((a, b) => parseDate(a) > parseDate(b) ? a : b) : null;
// Find next match (soonest future date) // Find next match (soonest future date)
const nextMatch = futureDates.length > 0 ? futureDates.reduce((a, b) => parseDate(a) < parseDate(b) ? a : b) : null; const nextMatch = futureDates.length > 0 ? futureDates.reduce((a, b) => parseDate(a) < parseDate(b) ? a : b) : null;
// Remaining dates // Render all dates in original chronological order
const otherDates = data.dates.filter(date => date !== lastMatch && date !== nextMatch); let closestIdx = 0;
// Sort remaining dates descending (most recent first) let minDiff = Infinity;
otherDates.sort((a, b) => parseDate(b) - parseDate(a)); data.dates.forEach((date, rowIdx) => {
// Compose ordered list const diff = Math.abs(parseDate(date) - today);
const orderedDates = []; if (diff < minDiff) {
if (lastMatch) orderedDates.push(lastMatch); minDiff = diff;
if (nextMatch) orderedDates.push(nextMatch); closestIdx = rowIdx;
orderedDates.push(...otherDates); }
orderedDates.forEach((date, rowIdx) => { });
data.dates.forEach((date, rowIdx) => {
const tr = document.createElement('tr'); const tr = document.createElement('tr');
if (rowIdx === closestIdx) tr.id = 'current-match-row';
// Date cell // Date cell
const dateTd = document.createElement('td'); const dateTd = document.createElement('td');
dateTd.innerText = date; dateTd.innerText = date;
@ -137,6 +139,11 @@ function renderTable() {
}); });
table.appendChild(tbody); table.appendChild(tbody);
container.appendChild(table); container.appendChild(table);
// Scroll to the most current match row after rendering
setTimeout(() => {
const row = document.getElementById('current-match-row');
if (row) row.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 0);
} }
document.getElementById('add-date').onclick = function() { document.getElementById('add-date').onclick = function() {