UNPKG

@fluentui/react-northstar

Version:
100 lines (92 loc) 4.12 kB
"use strict"; exports.__esModule = true; exports.getDayGrid = void 0; var _dateMath = require("../dateMath/dateMath"); var _dateValues = require("../dateValues/dateValues"); var _getDateRangeTypeToUse = require("./getDateRangeTypeToUse"); var _getBoundedDateRange = require("./getBoundedDateRange"); var _isRestrictedDate = require("./isRestrictedDate"); /** * Generates a grid of days, given the `options`. * Returns one additional week at the begining from the previous range * and one at the end from the future range * @param options - parameters to specify date related restrictions for the resulting grid */ var getDayGrid = function getDayGrid(options) { var selectedDate = options.selectedDate, dateRangeType = options.dateRangeType, firstDayOfWeek = options.firstDayOfWeek, today = options.today, minDate = options.minDate, maxDate = options.maxDate, weeksToShow = options.weeksToShow, workWeekDays = options.workWeekDays, daysToSelectInDayView = options.daysToSelectInDayView, restrictedDates = options.restrictedDates, markedDays = options.markedDays; var restrictedDateOptions = { minDate: minDate, maxDate: maxDate, restrictedDates: restrictedDates }; var todaysDate = today || new Date(); var navigatedDate = options.navigatedDate ? options.navigatedDate : todaysDate; var date; if (weeksToShow && weeksToShow <= 4) { // if showing less than a full month, just use date == navigatedDate date = new Date(navigatedDate.getFullYear(), navigatedDate.getMonth(), navigatedDate.getDate()); } else { date = new Date(navigatedDate.getFullYear(), navigatedDate.getMonth(), 1); } var weeks = []; // Cycle the date backwards to get to the first day of the week. while (date.getDay() !== firstDayOfWeek) { date.setDate(date.getDate() - 1); } // add the transition week as last week of previous range date = (0, _dateMath.addDays)(date, -_dateValues.DAYS_IN_WEEK); // a flag to indicate whether all days of the week are outside the month var isAllDaysOfWeekOutOfMonth = false; // in work week view if the days aren't contiguous we use week view instead var selectedDateRangeType = (0, _getDateRangeTypeToUse.getDateRangeTypeToUse)(dateRangeType, workWeekDays, firstDayOfWeek); var selectedDates = []; if (!!selectedDate) { selectedDates = (0, _dateMath.getDateRangeArray)(selectedDate, selectedDateRangeType, firstDayOfWeek, workWeekDays, daysToSelectInDayView); selectedDates = (0, _getBoundedDateRange.getBoundedDateRange)(selectedDates, minDate, maxDate); } var shouldGetWeeks = true; for (var weekIndex = 0; shouldGetWeeks; weekIndex++) { var week = []; isAllDaysOfWeekOutOfMonth = true; var _loop = function _loop() { var originalDate = new Date(date.getTime()); var dayInfo = { key: date.toString(), date: date.getDate().toString(), originalDate: originalDate, isInMonth: date.getMonth() === navigatedDate.getMonth(), isToday: (0, _dateMath.compareDates)(todaysDate, date), isSelected: (0, _dateMath.isInDateRangeArray)(date, selectedDates), isInBounds: !(0, _isRestrictedDate.isRestrictedDate)(date, restrictedDateOptions), isMarked: (markedDays == null ? void 0 : markedDays.some(function (markedDay) { return (0, _dateMath.compareDates)(originalDate, markedDay); })) || false }; week.push(dayInfo); if (dayInfo.isInMonth) { isAllDaysOfWeekOutOfMonth = false; } date.setDate(date.getDate() + 1); }; for (var dayIndex = 0; dayIndex < _dateValues.DAYS_IN_WEEK; dayIndex++) { _loop(); } // We append the condition of the loop depending upon the showSixWeeksByDefault prop. shouldGetWeeks = weeksToShow ? weekIndex < weeksToShow + 1 : !isAllDaysOfWeekOutOfMonth || weekIndex === 0; // we don't check shouldGetWeeks before pushing because we want to add one extra week for transition state weeks.push(week); } return weeks; }; exports.getDayGrid = getDayGrid; //# sourceMappingURL=getDayGrid.js.map