/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ // tslint:disable:max-file-line-count import { initialDatepickerState } from './bs-datepicker.state'; import { BsDatepickerActions } from './bs-datepicker.actions'; import { calcDaysCalendar } from '../engine/calc-days-calendar'; import { formatDaysCalendar } from '../engine/format-days-calendar'; import { flagDaysCalendar } from '../engine/flag-days-calendar'; import { setFullDate, shiftDate, isArray, isDateValid, startOf, getLocale, isAfter, isBefore } from 'ngx-bootstrap/chronos'; import { canSwitchMode } from '../engine/view-mode'; import { formatMonthsCalendar } from '../engine/format-months-calendar'; import { flagMonthsCalendar } from '../engine/flag-months-calendar'; import { formatYearsCalendar, initialYearShift, yearsPerCalendar } from '../engine/format-years-calendar'; import { flagYearsCalendar } from '../engine/flag-years-calendar'; import { getYearsCalendarInitialDate } from '../utils/bs-calendar-utils'; /* tslint:disable-next-line: cyclomatic-complexity */ /** * @param {?=} state * @param {?=} action * @return {?} */ export function bsDatepickerReducer(state = initialDatepickerState, action) { switch (action.type) { case BsDatepickerActions.CALCULATE: { return calculateReducer(state); } case BsDatepickerActions.FORMAT: { return formatReducer(state, action); } case BsDatepickerActions.FLAG: { return flagReducer(state, action); } case BsDatepickerActions.NAVIGATE_OFFSET: { return navigateOffsetReducer(state, action); } case BsDatepickerActions.NAVIGATE_TO: { /** @type {?} */ const payload = action.payload; /** @type {?} */ const date = setFullDate(state.view.date, payload.unit); /** @type {?} */ let newState; /** @type {?} */ let mode; if (canSwitchMode(payload.viewMode, state.minMode)) { mode = payload.viewMode; newState = { view: { date, mode } }; } else { mode = state.view.mode; newState = { selectedDate: date, view: { date, mode } }; } return Object.assign({}, state, newState); } case BsDatepickerActions.CHANGE_VIEWMODE: { if (!canSwitchMode(action.payload, state.minMode)) { return state; } /** @type {?} */ const date = state.view.date; /** @type {?} */ const mode = action.payload; /** @type {?} */ const newState = { view: { date, mode } }; return Object.assign({}, state, newState); } case BsDatepickerActions.HOVER: { return Object.assign({}, state, { hoveredDate: action.payload }); } case BsDatepickerActions.SELECT: { /** @type {?} */ const newState = { selectedDate: action.payload, view: state.view }; /** @type {?} */ const mode = state.view.mode; /** @type {?} */ const _date = action.payload || state.view.date; /** @type {?} */ const date = getViewDate(_date, state.minDate, state.maxDate); newState.view = { mode, date }; return Object.assign({}, state, newState); } case BsDatepickerActions.SET_OPTIONS: { /** @type {?} */ const newState = action.payload; // preserve view mode /** @type {?} */ const mode = newState.minMode ? newState.minMode : state.view.mode; /** @type {?} */ const _viewDate = isDateValid(newState.value) && newState.value || isArray(newState.value) && isDateValid(newState.value[0]) && newState.value[0] || state.view.date; /** @type {?} */ const date = getViewDate(_viewDate, newState.minDate, newState.maxDate); newState.view = { mode, date }; // update selected value if (newState.value) { // if new value is array we work with date range if (isArray(newState.value)) { newState.selectedRange = newState.value; } // if new value is a date -> datepicker if (newState.value instanceof Date) { newState.selectedDate = newState.value; } // provided value is not supported :) // need to report it somehow } return Object.assign({}, state, newState); } // date range picker case BsDatepickerActions.SELECT_RANGE: { /** @type {?} */ const newState = { selectedRange: action.payload, view: state.view }; /** @type {?} */ const mode = state.view.mode; /** @type {?} */ const _date = action.payload && action.payload[0] || state.view.date; /** @type {?} */ const date = getViewDate(_date, state.minDate, state.maxDate); newState.view = { mode, date }; return Object.assign({}, state, newState); } case BsDatepickerActions.SET_MIN_DATE: { return Object.assign({}, state, { minDate: action.payload }); } case BsDatepickerActions.SET_MAX_DATE: { return Object.assign({}, state, { maxDate: action.payload }); } case BsDatepickerActions.SET_IS_DISABLED: { return Object.assign({}, state, { isDisabled: action.payload }); } case BsDatepickerActions.SET_DATE_CUSTOM_CLASSES: { return Object.assign({}, state, { dateCustomClasses: action.payload }); } default: return state; } } /** * @param {?} state * @return {?} */ function calculateReducer(state) { // how many calendars /** @type {?} */ const displayMonths = state.displayMonths; // use selected date on initial rendering if set /** @type {?} */ let viewDate = state.view.date; if (state.view.mode === 'day') { state.monthViewOptions.firstDayOfWeek = getLocale(state.locale).firstDayOfWeek(); /** @type {?} */ const monthsModel = new Array(displayMonths); for (let monthIndex = 0; monthIndex < displayMonths; monthIndex++) { // todo: for unlinked calendars it will be harder monthsModel[monthIndex] = calcDaysCalendar(viewDate, state.monthViewOptions); viewDate = shiftDate(viewDate, { month: 1 }); } return Object.assign({}, state, { monthsModel }); } if (state.view.mode === 'month') { /** @type {?} */ 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') { /** @type {?} */ 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; } /** * @param {?} state * @param {?} action * @return {?} */ function formatReducer(state, action) { if (state.view.mode === 'day') { /** @type {?} */ const formattedMonths = state.monthsModel.map((/** * @param {?} month * @param {?} monthIndex * @return {?} */ (month, monthIndex) => formatDaysCalendar(month, getFormatOptions(state), monthIndex))); return Object.assign({}, state, { formattedMonths }); } // how many calendars /** @type {?} */ const displayMonths = state.displayMonths; // check initial rendering // use selected date on initial rendering if set /** @type {?} */ let viewDate = state.view.date; if (state.view.mode === 'month') { /** @type {?} */ 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') { /** @type {?} */ 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)); viewDate = shiftDate(viewDate, { year: 16 }); } return Object.assign({}, state, { yearsCalendarModel }); } return state; } /** * @param {?} state * @param {?} action * @return {?} */ function flagReducer(state, action) { if (state.view.mode === 'day') { /** @type {?} */ const flaggedMonths = state.formattedMonths.map((/** * @param {?} formattedMonth * @param {?} monthIndex * @return {?} */ (formattedMonth, monthIndex) => flagDaysCalendar(formattedMonth, { isDisabled: state.isDisabled, minDate: state.minDate, maxDate: state.maxDate, daysDisabled: state.daysDisabled, datesDisabled: state.datesDisabled, hoveredDate: state.hoveredDate, selectedDate: state.selectedDate, selectedRange: state.selectedRange, displayMonths: state.displayMonths, dateCustomClasses: state.dateCustomClasses, monthIndex }))); return Object.assign({}, state, { flaggedMonths }); } if (state.view.mode === 'month') { /** @type {?} */ const flaggedMonthsCalendar = state.monthsCalendar.map((/** * @param {?} formattedMonth * @param {?} monthIndex * @return {?} */ (formattedMonth, monthIndex) => flagMonthsCalendar(formattedMonth, { isDisabled: state.isDisabled, minDate: state.minDate, maxDate: state.maxDate, hoveredMonth: state.hoveredMonth, selectedDate: state.selectedDate, displayMonths: state.displayMonths, monthIndex }))); return Object.assign({}, state, { flaggedMonthsCalendar }); } if (state.view.mode === 'year') { /** @type {?} */ const yearsCalendarFlagged = state.yearsCalendarModel.map((/** * @param {?} formattedMonth * @param {?} yearIndex * @return {?} */ (formattedMonth, yearIndex) => flagYearsCalendar(formattedMonth, { isDisabled: state.isDisabled, minDate: state.minDate, maxDate: state.maxDate, hoveredYear: state.hoveredYear, selectedDate: state.selectedDate, displayMonths: state.displayMonths, yearIndex }))); return Object.assign({}, state, { yearsCalendarFlagged }); } return state; } /** * @param {?} state * @param {?} action * @return {?} */ function navigateOffsetReducer(state, action) { /** @type {?} */ const newState = { view: { mode: state.view.mode, date: shiftViewDate(state, action) } }; return Object.assign({}, state, newState); } /** * @param {?} state * @param {?} action * @return {?} */ function shiftViewDate(state, action) { if (state.view.mode === 'year' && state.minMode === 'year') { /** @type {?} */ const initialDate = getYearsCalendarInitialDate(state, 0); /** @type {?} */ const middleDate = shiftDate(initialDate, { year: -initialYearShift }); return shiftDate(middleDate, action.payload); } return shiftDate(startOf(state.view.date, 'month'), action.payload); } /** * @param {?} state * @return {?} */ function getFormatOptions(state) { return { locale: state.locale, monthTitle: state.monthTitle, yearTitle: state.yearTitle, dayLabel: state.dayLabel, monthLabel: state.monthLabel, yearLabel: state.yearLabel, weekNumbers: state.weekNumbers }; } /** * if view date is provided (bsValue|ngModel) it should be shown * if view date is not provider: * if minDate>currentDate (default view value), show minDate * if maxDate