Skip to content

Commit

Permalink
Prevent year navigation beyond available data range in calendar
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
TyroneAEM committed Aug 15, 2024
1 parent 60bae09 commit 3276ba4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion scripts/program-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -637,4 +648,4 @@ function calculateScroll(type, viewStartYear, displayYear, displayQuarter, numYe

function isValidDate(dateObj) {
return dateObj instanceof Date && !isNaN(dateObj);
}
}

0 comments on commit 3276ba4

Please sign in to comment.