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 +}