ngx-bootstrap
Version:
Native Angular Bootstrap Components
230 lines • 9.83 kB
JavaScript
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 { shiftDate, setDate } from '../../bs-moment/utils/date-setters';
import { canSwitchMode } from '../engine/view-mode';
import { formatMonthsCalendar } from '../engine/format-months-calendar';
import { flagMonthsCalendar } from '../engine/flag-months-calendar';
import { formatYearsCalendar, yearsPerCalendar } from '../engine/format-years-calendar';
import { flagYearsCalendar } from '../engine/flag-years-calendar';
import { isArray } from '../../bs-moment/utils/type-checks';
import { startOf } from '../../bs-moment/utils/start-end-of';
export function bsDatepickerReducer(state, action) {
if (state === void 0) { state = initialDatepickerState; }
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): {
var date = shiftDate(startOf(state.view.date, 'month'), action.payload);
var newState = {
view: {
mode: state.view.mode,
date: date
}
};
return Object.assign({}, state, newState);
}
case (BsDatepickerActions.NAVIGATE_TO): {
var payload = action.payload;
var date = setDate(state.view.date, payload.unit);
var mode = payload.viewMode;
var newState = { view: { date: date, mode: mode } };
return Object.assign({}, state, newState);
}
case (BsDatepickerActions.CHANGE_VIEWMODE): {
if (!canSwitchMode(action.payload)) {
return state;
}
var date = state.view.date;
var mode = action.payload;
var newState = { view: { date: date, mode: mode } };
return Object.assign({}, state, newState);
}
case (BsDatepickerActions.HOVER): {
return Object.assign({}, state, { hoveredDate: action.payload });
}
case (BsDatepickerActions.SELECT): {
var newState = {
selectedDate: action.payload,
view: state.view
};
if (action.payload) {
newState.view = {
date: action.payload,
mode: state.view.mode
};
}
return Object.assign({}, state, newState);
}
case (BsDatepickerActions.SET_OPTIONS): {
var newState = action.payload;
// looks not really good
if (newState.value) {
newState.view = state.view;
if (isArray(newState.value)) {
newState.view = {
mode: state.view.mode,
date: newState.value[0]
};
newState.selectedRange = newState.value;
}
else {
newState.view = {
mode: state.view.mode,
date: newState.value
};
newState.selectedDate = newState.value;
}
}
return Object.assign({}, state, newState);
}
// date range picker
case (BsDatepickerActions.SELECT_RANGE): {
return Object.assign({}, state, { selectedRange: action.payload });
}
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
});
}
default:
return state;
}
}
function calculateReducer(state) {
// how many calendars
var displayMonths = state.displayMonths;
// use selected date on initial rendering if set
var viewDate = state.view.date;
if (state.view.mode === 'day') {
var monthsModel = new Array(displayMonths);
for (var 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: monthsModel });
}
if (state.view.mode === 'month') {
var monthsCalendar = new Array(displayMonths);
for (var 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: monthsCalendar });
}
if (state.view.mode === 'year') {
var yearsCalendarModel = new Array(displayMonths);
for (var calendarIndex = 0; calendarIndex < displayMonths; calendarIndex++) {
// todo: for unlinked calendars it will be harder
yearsCalendarModel[calendarIndex] = formatYearsCalendar(viewDate, getFormatOptions(state));
viewDate = shiftDate(viewDate, { year: yearsPerCalendar });
}
return Object.assign({}, state, { yearsCalendarModel: yearsCalendarModel });
}
return state;
}
function formatReducer(state, action) {
if (state.view.mode === 'day') {
var formattedMonths = state.monthsModel
.map(function (month, monthIndex) { return formatDaysCalendar(month, getFormatOptions(state), monthIndex); });
return Object.assign({}, state, { formattedMonths: formattedMonths });
}
// how many calendars
var displayMonths = state.displayMonths;
// check initial rendering
// use selected date on initial rendering if set
var viewDate = state.view.date;
if (state.view.mode === 'month') {
var monthsCalendar = new Array(displayMonths);
for (var 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: monthsCalendar });
}
if (state.view.mode === 'year') {
var yearsCalendarModel = new Array(displayMonths);
for (var 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: yearsCalendarModel });
}
return state;
}
function flagReducer(state, action) {
if (state.view.mode === 'day') {
var flaggedMonths = state.formattedMonths
.map(function (formattedMonth, monthIndex) { return flagDaysCalendar(formattedMonth, {
isDisabled: state.isDisabled,
minDate: state.minDate,
maxDate: state.maxDate,
hoveredDate: state.hoveredDate,
selectedDate: state.selectedDate,
selectedRange: state.selectedRange,
displayMonths: state.displayMonths,
monthIndex: monthIndex
}); });
return Object.assign({}, state, { flaggedMonths: flaggedMonths });
}
if (state.view.mode === 'month') {
var flaggedMonthsCalendar = state.monthsCalendar
.map(function (formattedMonth, monthIndex) { return flagMonthsCalendar(formattedMonth, {
isDisabled: state.isDisabled,
minDate: state.minDate,
maxDate: state.maxDate,
hoveredMonth: state.hoveredMonth,
displayMonths: state.displayMonths,
monthIndex: monthIndex
}); });
return Object.assign({}, state, { flaggedMonthsCalendar: flaggedMonthsCalendar });
}
if (state.view.mode === 'year') {
var yearsCalendarFlagged = state.yearsCalendarModel
.map(function (formattedMonth, yearIndex) { return flagYearsCalendar(formattedMonth, {
isDisabled: state.isDisabled,
minDate: state.minDate,
maxDate: state.maxDate,
hoveredYear: state.hoveredYear,
displayMonths: state.displayMonths,
yearIndex: yearIndex
}); });
return Object.assign({}, state, { yearsCalendarFlagged: yearsCalendarFlagged });
}
return state;
}
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
};
}
//# sourceMappingURL=bs-datepicker.reducer.js.map