From 3276ba4e1edddc488617aa920b5f395d39bf008a Mon Sep 17 00:00:00 2001 From: Tyrone Tse Date: Thu, 15 Aug 2024 12:28:06 -0500 Subject: [PATCH] Prevent year navigation beyond available data range in calendar - Added logic in `changePeriod` function to restrict year navigation within the bounds of available calendar data. - Defined `maxYear` and `minYear` based on the calendar data's start and end dates. - Ensured that users cannot select a year beyond the maximum or below the minimum year available in the calendar data. --- scripts/program-calendar.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/program-calendar.js b/scripts/program-calendar.js index c61b324..0e3cf1d 100644 --- a/scripts/program-calendar.js +++ b/scripts/program-calendar.js @@ -330,6 +330,8 @@ function changePeriod(event) { const contentWrapper = document.querySelector('.calendar-content-wrapper'); const view = contentWrapper.dataset.view; const currentYear = parseInt(yearEl.dataset.year); + const maxYear = viewEnd.getUTCFullYear(); // Use the maximum year from the viewEnd variable + const minYear = viewStart.getUTCFullYear(); // Use the minimum year from the viewStart variable let newPeriod, newYear, newQuarter; @@ -349,6 +351,15 @@ function changePeriod(event) { newYear = (direction == 'right') ? (currentYear + 1) : (currentYear - 1); newQuarter = 1; } + + // Prevent the year from going beyond the maximum or minimum year + if (newYear > maxYear) { + newYear = maxYear; + newQuarter = 4; // If max year, set to the last quarter + } else if (newYear < minYear) { + newYear = minYear; + newQuarter = 1; // If min year, set to the first quarter + } newPeriod = { 'year': newYear, 'quarter': newQuarter }; refreshCalendar(newPeriod, view); } @@ -637,4 +648,4 @@ function calculateScroll(type, viewStartYear, displayYear, displayQuarter, numYe function isValidDate(dateObj) { return dateObj instanceof Date && !isNaN(dateObj); -} \ No newline at end of file +}