Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// use selected date on initial rendering if set
let viewDate = state.view.date;
if (state.view.mode === 'month') {
const monthsCalendar = new Array(displayMonths);
for (
let calendarIndex = 0;
calendarIndex < displayMonths;
calendarIndex++
) {
// todo: for unlinked calendars it will be harder
monthsCalendar[calendarIndex] = formatMonthsCalendar(
viewDate,
getFormatOptions(state)
);
viewDate = shiftDate(viewDate, { year: 1 });
}
return Object.assign({}, state, { monthsCalendar });
}
if (state.view.mode === 'year') {
const yearsCalendarModel = new Array(displayMonths);
for (
let calendarIndex = 0;
calendarIndex < displayMonths;
calendarIndex++
) {
// todo: for unlinked calendars it will be harder
yearsCalendarModel[calendarIndex] = formatYearsCalendar(
viewDate,
getFormatOptions(state)
// todo: add check for linked calendars
yearsCalendar.hideLeftArrow =
options.yearIndex > 0 && options.yearIndex !== options.displayMonths;
yearsCalendar.hideRightArrow =
options.yearIndex < options.displayMonths &&
options.yearIndex + 1 !== options.displayMonths;
yearsCalendar.disableLeftArrow = isYearDisabled(
shiftDate(yearsCalendar.years[0][0].date, { year: -1 }),
options.minDate,
options.maxDate
);
const i = yearsCalendar.years.length - 1;
const j = yearsCalendar.years[i].length - 1;
yearsCalendar.disableRightArrow = isYearDisabled(
shiftDate(yearsCalendar.years[i][j].date, { year: 1 }),
options.minDate,
options.maxDate
);
return yearsCalendar;
}
if (state.view.mode === 'year') {
const yearsCalendarModel = new Array(displayMonths);
for (
let calendarIndex = 0;
calendarIndex < displayMonths;
calendarIndex++
) {
// todo: for unlinked calendars it will be harder
yearsCalendarModel[calendarIndex] = formatYearsCalendar(
viewDate,
getFormatOptions(state),
state.minMode === 'year' ? getYearsCalendarInitialDate(state, calendarIndex) : undefined
);
viewDate = shiftDate(viewDate, { year: yearsPerCalendar });
}
return Object.assign({}, state, { yearsCalendarModel });
}
return state;
}
) {
monthCalendar.months[rowIndex][monthIndex] = newMonth;
}
});
}
);
// todo: add check for linked calendars
monthCalendar.hideLeftArrow =
options.monthIndex > 0 && options.monthIndex !== options.displayMonths;
monthCalendar.hideRightArrow =
options.monthIndex < options.displayMonths &&
options.monthIndex + 1 !== options.displayMonths;
monthCalendar.disableLeftArrow = isYearDisabled(
shiftDate(monthCalendar.months[0][0].date, { year: -1 }),
options.minDate,
options.maxDate
);
monthCalendar.disableRightArrow = isYearDisabled(
shiftDate(monthCalendar.months[0][0].date, { year: 1 }),
options.minDate,
options.maxDate
);
return monthCalendar;
}
return Object.assign({}, state, { monthsModel });
}
if (state.view.mode === 'month') {
const monthsCalendar = new Array(displayMonths);
for (
let calendarIndex = 0;
calendarIndex < displayMonths;
calendarIndex++
) {
// todo: for unlinked calendars it will be harder
monthsCalendar[calendarIndex] = formatMonthsCalendar(
viewDate,
getFormatOptions(state)
);
viewDate = shiftDate(viewDate, { year: 1 });
}
return Object.assign({}, state, { monthsCalendar });
}
if (state.view.mode === 'year') {
const yearsCalendarModel = new Array(displayMonths);
for (
let calendarIndex = 0;
calendarIndex < displayMonths;
calendarIndex++
) {
// todo: for unlinked calendars it will be harder
yearsCalendarModel[calendarIndex] = formatYearsCalendar(
viewDate,
week.days[dayIndex] = newDay;
}
});
});
// todo: add check for linked calendars
formattedMonth.hideLeftArrow =
options.isDisabled ||
(options.monthIndex > 0 && options.monthIndex !== options.displayMonths);
formattedMonth.hideRightArrow =
options.isDisabled ||
(options.monthIndex < options.displayMonths &&
options.monthIndex + 1 !== options.displayMonths);
formattedMonth.disableLeftArrow = isMonthDisabled(
shiftDate(formattedMonth.month, { month: -1 }),
options.minDate,
options.maxDate
);
formattedMonth.disableRightArrow = isMonthDisabled(
shiftDate(formattedMonth.month, { month: 1 }),
options.minDate,
options.maxDate
);
return formattedMonth;
}
function calculateInitialDate(viewDate: Date, previousInitialDate?: Date): Date {
if (previousInitialDate
&& viewDate.getFullYear() >= previousInitialDate.getFullYear()
&& viewDate.getFullYear() < previousInitialDate.getFullYear() + yearsPerCalendar) {
return previousInitialDate;
}
return shiftDate(viewDate, { year: initialYearShift });
}
function shiftViewDate(state: BsDatepickerState, action: Action): Date {
if (state.view.mode === 'year' && state.minMode === 'year') {
const initialDate = getYearsCalendarInitialDate(state, 0);
const middleDate = shiftDate(initialDate, { year: -initialYearShift });
return shiftDate(middleDate, action.payload);
}
return shiftDate(startOf(state.view.date, 'month'), action.payload);
}
export function createMatrix(
options: MatrixOptions,
fn: CreateMatrixCb
): T[][] {
let prevValue = options.initialDate;
const matrix: T[][] = new Array(options.height);
for (let i = 0; i < options.height; i++) {
matrix[i] = new Array(options.width);
for (let j = 0; j < options.width; j++) {
matrix[i][j] = fn(prevValue);
prevValue = shiftDate(prevValue, options.shift);
}
}
return matrix;
}