From c80ce1acb8d8d57690e27bc387fc280672195024 Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 10 May 2025 17:44:45 +0200 Subject: [PATCH] Scroll to current date V3 --- static/app.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/static/app.js b/static/app.js index 42f4917..cd93a41 100644 --- a/static/app.js +++ b/static/app.js @@ -58,17 +58,19 @@ function renderTable() { const lastMatch = pastDates.length > 0 ? pastDates.reduce((a, b) => parseDate(a) > parseDate(b) ? a : b) : null; // Find next match (soonest future date) const nextMatch = futureDates.length > 0 ? futureDates.reduce((a, b) => parseDate(a) < parseDate(b) ? a : b) : null; - // Remaining dates - const otherDates = data.dates.filter(date => date !== lastMatch && date !== nextMatch); - // Sort remaining dates descending (most recent first) - otherDates.sort((a, b) => parseDate(b) - parseDate(a)); - // Compose ordered list - const orderedDates = []; - if (lastMatch) orderedDates.push(lastMatch); - if (nextMatch) orderedDates.push(nextMatch); - orderedDates.push(...otherDates); - orderedDates.forEach((date, rowIdx) => { + // Render all dates in original chronological order + let closestIdx = 0; + let minDiff = Infinity; + data.dates.forEach((date, rowIdx) => { + const diff = Math.abs(parseDate(date) - today); + if (diff < minDiff) { + minDiff = diff; + closestIdx = rowIdx; + } + }); + data.dates.forEach((date, rowIdx) => { const tr = document.createElement('tr'); + if (rowIdx === closestIdx) tr.id = 'current-match-row'; // Date cell const dateTd = document.createElement('td'); dateTd.innerText = date; @@ -137,6 +139,11 @@ function renderTable() { }); table.appendChild(tbody); 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() {