ngx-bootstrap
Version:
Native Angular Bootstrap Components
1,499 lines (1,479 loc) • 448 kB
JavaScript
import { Injectable, Component, EventEmitter, Directive, ElementRef, Input, Output, Renderer2, ViewContainerRef, ChangeDetectorRef, forwardRef, Host, ChangeDetectionStrategy, NgModule, ViewChild } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { __extends, __values, __spread } from 'tslib';
import { filter, map } from 'rxjs/operators';
import { getFullYear, getMonth, getDay, isFirstDayOfWeek, isAfter, isBefore, shiftDate, endOf, startOf, getFirstDayOfMonth, formatDate, getLocale, isDisabledDay, isSameDay, isSameMonth, isSameYear, setFullDate, isArray, isDateValid, parseDate, isDate } from 'ngx-bootstrap/chronos';
import { MiniStore, MiniState } from 'ngx-bootstrap/mini-ngrx';
import { ComponentLoaderFactory } from 'ngx-bootstrap/component-loader';
import { NG_VALIDATORS, NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { PositioningService } from 'ngx-bootstrap/positioning';
import { isBs3 } from 'ngx-bootstrap/utils';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* For date range picker there are `BsDaterangepickerConfig` which inherits all properties,
* except `displayMonths`, for range picker it default to `2`
*/
var BsDatepickerConfig = /** @class */ (function () {
function BsDatepickerConfig() {
/**
* CSS class which will be applied to datepicker container,
* usually used to set color theme
*/
this.containerClass = 'theme-green';
// DatepickerRenderOptions
this.displayMonths = 1;
/**
* Allows to hide week numbers in datepicker
*/
this.showWeekNumbers = true;
this.dateInputFormat = 'L';
// range picker
this.rangeSeparator = ' - ';
/**
* Date format for date range input field
*/
this.rangeInputFormat = 'L';
// DatepickerFormatOptions
this.monthTitle = 'MMMM';
this.yearTitle = 'YYYY';
this.dayLabel = 'D';
this.monthLabel = 'MMMM';
this.yearLabel = 'YYYY';
this.weekNumbers = 'w';
}
BsDatepickerConfig.decorators = [
{ type: Injectable }
];
return BsDatepickerConfig;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/
BsDatepickerAbstractComponent = /** @class */ (function () {
function BsDatepickerAbstractComponent() {
this._customRangesFish = [];
}
Object.defineProperty(BsDatepickerAbstractComponent.prototype, "minDate", {
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._effects.setMinDate(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(BsDatepickerAbstractComponent.prototype, "maxDate", {
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._effects.setMaxDate(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(BsDatepickerAbstractComponent.prototype, "daysDisabled", {
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._effects.setDaysDisabled(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(BsDatepickerAbstractComponent.prototype, "isDisabled", {
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._effects.setDisabled(value);
},
enumerable: true,
configurable: true
});
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.setViewMode = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.navigateTo = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.dayHoverHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.weekHoverHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.monthHoverHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.yearHoverHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} day
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.daySelectHandler = /**
* @param {?} day
* @return {?}
*/
function (day) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.monthSelectHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype.yearSelectHandler = /**
* @param {?} event
* @return {?}
*/
function (event) { };
/* tslint:disable-next-line: no-any */
/**
* @param {?} event
* @return {?}
*/
BsDatepickerAbstractComponent.prototype._stopPropagation = /**
* @param {?} event
* @return {?}
*/
function (event) {
event.stopPropagation();
};
return BsDatepickerAbstractComponent;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var BsDatepickerActions = /** @class */ (function () {
function BsDatepickerActions() {
}
/**
* @return {?}
*/
BsDatepickerActions.prototype.calculate = /**
* @return {?}
*/
function () {
return { type: BsDatepickerActions.CALCULATE };
};
/**
* @return {?}
*/
BsDatepickerActions.prototype.format = /**
* @return {?}
*/
function () {
return { type: BsDatepickerActions.FORMAT };
};
/**
* @return {?}
*/
BsDatepickerActions.prototype.flag = /**
* @return {?}
*/
function () {
return { type: BsDatepickerActions.FLAG };
};
/**
* @param {?} date
* @return {?}
*/
BsDatepickerActions.prototype.select = /**
* @param {?} date
* @return {?}
*/
function (date) {
return {
type: BsDatepickerActions.SELECT,
payload: date
};
};
/**
* @param {?} event
* @return {?}
*/
BsDatepickerActions.prototype.changeViewMode = /**
* @param {?} event
* @return {?}
*/
function (event) {
return {
type: BsDatepickerActions.CHANGE_VIEWMODE,
payload: event
};
};
/**
* @param {?} event
* @return {?}
*/
BsDatepickerActions.prototype.navigateTo = /**
* @param {?} event
* @return {?}
*/
function (event) {
return {
type: BsDatepickerActions.NAVIGATE_TO,
payload: event
};
};
/**
* @param {?} step
* @return {?}
*/
BsDatepickerActions.prototype.navigateStep = /**
* @param {?} step
* @return {?}
*/
function (step) {
return {
type: BsDatepickerActions.NAVIGATE_OFFSET,
payload: step
};
};
/**
* @param {?} options
* @return {?}
*/
BsDatepickerActions.prototype.setOptions = /**
* @param {?} options
* @return {?}
*/
function (options) {
return {
type: BsDatepickerActions.SET_OPTIONS,
payload: options
};
};
// date range picker
/**
* @param {?} value
* @return {?}
*/
BsDatepickerActions.prototype.selectRange = /**
* @param {?} value
* @return {?}
*/
function (value) {
return {
type: BsDatepickerActions.SELECT_RANGE,
payload: value
};
};
/**
* @param {?} event
* @return {?}
*/
BsDatepickerActions.prototype.hoverDay = /**
* @param {?} event
* @return {?}
*/
function (event) {
return {
type: BsDatepickerActions.HOVER,
payload: event.isHovered ? event.cell.date : null
};
};
/**
* @param {?} date
* @return {?}
*/
BsDatepickerActions.prototype.minDate = /**
* @param {?} date
* @return {?}
*/
function (date) {
return {
type: BsDatepickerActions.SET_MIN_DATE,
payload: date
};
};
/**
* @param {?} date
* @return {?}
*/
BsDatepickerActions.prototype.maxDate = /**
* @param {?} date
* @return {?}
*/
function (date) {
return {
type: BsDatepickerActions.SET_MAX_DATE,
payload: date
};
};
/**
* @param {?} days
* @return {?}
*/
BsDatepickerActions.prototype.daysDisabled = /**
* @param {?} days
* @return {?}
*/
function (days) {
return {
type: BsDatepickerActions.SET_DAYSDISABLED,
payload: days
};
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerActions.prototype.isDisabled = /**
* @param {?} value
* @return {?}
*/
function (value) {
return {
type: BsDatepickerActions.SET_IS_DISABLED,
payload: value
};
};
/**
* @param {?} locale
* @return {?}
*/
BsDatepickerActions.prototype.setLocale = /**
* @param {?} locale
* @return {?}
*/
function (locale) {
return {
type: BsDatepickerActions.SET_LOCALE,
payload: locale
};
};
BsDatepickerActions.CALCULATE = '[datepicker] calculate dates matrix';
BsDatepickerActions.FORMAT = '[datepicker] format datepicker values';
BsDatepickerActions.FLAG = '[datepicker] set flags';
BsDatepickerActions.SELECT = '[datepicker] select date';
BsDatepickerActions.NAVIGATE_OFFSET = '[datepicker] shift view date';
BsDatepickerActions.NAVIGATE_TO = '[datepicker] change view date';
BsDatepickerActions.SET_OPTIONS = '[datepicker] update render options';
BsDatepickerActions.HOVER = '[datepicker] hover date';
BsDatepickerActions.CHANGE_VIEWMODE = '[datepicker] switch view mode';
BsDatepickerActions.SET_MIN_DATE = '[datepicker] set min date';
BsDatepickerActions.SET_MAX_DATE = '[datepicker] set max date';
BsDatepickerActions.SET_DAYSDISABLED = '[datepicker] set days disabled';
BsDatepickerActions.SET_IS_DISABLED = '[datepicker] set is disabled';
BsDatepickerActions.SET_LOCALE = '[datepicker] set datepicker locale';
BsDatepickerActions.SELECT_RANGE = '[daterangepicker] select dates range';
BsDatepickerActions.decorators = [
{ type: Injectable }
];
return BsDatepickerActions;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var BsLocaleService = /** @class */ (function () {
function BsLocaleService() {
this._defaultLocale = 'en';
this._locale = new BehaviorSubject(this._defaultLocale);
this._localeChange = this._locale.asObservable();
}
Object.defineProperty(BsLocaleService.prototype, "locale", {
get: /**
* @return {?}
*/
function () {
return this._locale;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BsLocaleService.prototype, "localeChange", {
get: /**
* @return {?}
*/
function () {
return this._localeChange;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BsLocaleService.prototype, "currentLocale", {
get: /**
* @return {?}
*/
function () {
return this._locale.getValue();
},
enumerable: true,
configurable: true
});
/**
* @param {?} locale
* @return {?}
*/
BsLocaleService.prototype.use = /**
* @param {?} locale
* @return {?}
*/
function (locale) {
if (locale === this.currentLocale) {
return;
}
this._locale.next(locale);
};
BsLocaleService.decorators = [
{ type: Injectable }
];
return BsLocaleService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var BsDatepickerEffects = /** @class */ (function () {
function BsDatepickerEffects(_actions, _localeService) {
this._actions = _actions;
this._localeService = _localeService;
this._subs = [];
}
/**
* @param {?} _bsDatepickerStore
* @return {?}
*/
BsDatepickerEffects.prototype.init = /**
* @param {?} _bsDatepickerStore
* @return {?}
*/
function (_bsDatepickerStore) {
this._store = _bsDatepickerStore;
return this;
};
/** setters */
/**
* setters
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setValue = /**
* setters
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.select(value));
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setRangeValue = /**
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.selectRange(value));
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setMinDate = /**
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.minDate(value));
return this;
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setMaxDate = /**
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.maxDate(value));
return this;
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setDaysDisabled = /**
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.daysDisabled(value));
return this;
};
/**
* @param {?} value
* @return {?}
*/
BsDatepickerEffects.prototype.setDisabled = /**
* @param {?} value
* @return {?}
*/
function (value) {
this._store.dispatch(this._actions.isDisabled(value));
return this;
};
/* Set rendering options */
/**
* @param {?} _config
* @return {?}
*/
BsDatepickerEffects.prototype.setOptions = /**
* @param {?} _config
* @return {?}
*/
function (_config) {
var /** @type {?} */ _options = Object.assign({ locale: this._localeService.currentLocale }, _config);
this._store.dispatch(this._actions.setOptions(_options));
return this;
};
/** view to mode bindings */
/**
* view to mode bindings
* @param {?} container
* @return {?}
*/
BsDatepickerEffects.prototype.setBindings = /**
* view to mode bindings
* @param {?} container
* @return {?}
*/
function (container) {
container.daysCalendar = this._store
.select(function (state) { return state.flaggedMonths; })
.pipe(filter(function (months) { return !!months; }));
// month calendar
container.monthsCalendar = this._store
.select(function (state) { return state.flaggedMonthsCalendar; })
.pipe(filter(function (months) { return !!months; }));
// year calendar
container.yearsCalendar = this._store
.select(function (state) { return state.yearsCalendarFlagged; })
.pipe(filter(function (years) { return !!years; }));
container.viewMode = this._store.select(function (state) { return state.view.mode; });
container.options = this._store
.select(function (state) { return state.showWeekNumbers; })
.pipe(map(function (showWeekNumbers) { return ({ showWeekNumbers: showWeekNumbers }); }));
return this;
};
/** event handlers */
/**
* event handlers
* @param {?} container
* @return {?}
*/
BsDatepickerEffects.prototype.setEventHandlers = /**
* event handlers
* @param {?} container
* @return {?}
*/
function (container) {
var _this = this;
container.setViewMode = function (event) {
_this._store.dispatch(_this._actions.changeViewMode(event));
};
container.navigateTo = function (event) {
_this._store.dispatch(_this._actions.navigateStep(event.step));
};
container.dayHoverHandler = function (event) {
var /** @type {?} */ _cell = /** @type {?} */ (event.cell);
if (_cell.isOtherMonth || _cell.isDisabled) {
return;
}
_this._store.dispatch(_this._actions.hoverDay(event));
_cell.isHovered = event.isHovered;
};
container.monthHoverHandler = function (event) {
event.cell.isHovered = event.isHovered;
};
container.yearHoverHandler = function (event) {
event.cell.isHovered = event.isHovered;
};
container.monthSelectHandler = function (event) {
if (event.isDisabled) {
return;
}
_this._store.dispatch(_this._actions.navigateTo({
unit: {
month: getMonth(event.date),
year: getFullYear(event.date)
},
viewMode: 'day'
}));
};
container.yearSelectHandler = function (event) {
if (event.isDisabled) {
return;
}
_this._store.dispatch(_this._actions.navigateTo({
unit: {
year: getFullYear(event.date)
},
viewMode: 'month'
}));
};
return this;
};
/**
* @return {?}
*/
BsDatepickerEffects.prototype.registerDatepickerSideEffects = /**
* @return {?}
*/
function () {
var _this = this;
this._subs.push(this._store.select(function (state) { return state.view; }).subscribe(function (view) {
_this._store.dispatch(_this._actions.calculate());
}));
// format calendar values on month model change
this._subs.push(this._store
.select(function (state) { return state.monthsModel; })
.pipe(filter(function (monthModel) { return !!monthModel; }))
.subscribe(function (month) { return _this._store.dispatch(_this._actions.format()); }));
// flag day values
this._subs.push(this._store
.select(function (state) { return state.formattedMonths; })
.pipe(filter(function (month) { return !!month; }))
.subscribe(function (month) { return _this._store.dispatch(_this._actions.flag()); }));
// flag day values
this._subs.push(this._store
.select(function (state) { return state.selectedDate; })
.pipe(filter(function (selectedDate) { return !!selectedDate; }))
.subscribe(function (selectedDate) { return _this._store.dispatch(_this._actions.flag()); }));
// flag for date range picker
this._subs.push(this._store
.select(function (state) { return state.selectedRange; })
.pipe(filter(function (selectedRange) { return !!selectedRange; }))
.subscribe(function (selectedRange) { return _this._store.dispatch(_this._actions.flag()); }));
// monthsCalendar
this._subs.push(this._store
.select(function (state) { return state.monthsCalendar; })
.subscribe(function () { return _this._store.dispatch(_this._actions.flag()); }));
// years calendar
this._subs.push(this._store
.select(function (state) { return state.yearsCalendarModel; })
.pipe(filter(function (state) { return !!state; }))
.subscribe(function () { return _this._store.dispatch(_this._actions.flag()); }));
// on hover
this._subs.push(this._store
.select(function (state) { return state.hoveredDate; })
.pipe(filter(function (hoveredDate) { return !!hoveredDate; }))
.subscribe(function (hoveredDate) { return _this._store.dispatch(_this._actions.flag()); }));
// on locale change
this._subs.push(this._localeService.localeChange
.subscribe(function (locale) { return _this._store.dispatch(_this._actions.setLocale(locale)); }));
return this;
};
/**
* @return {?}
*/
BsDatepickerEffects.prototype.destroy = /**
* @return {?}
*/
function () {
try {
for (var _a = __values(this._subs), _b = _a.next(); !_b.done; _b = _a.next()) {
var sub = _b.value;
sub.unsubscribe();
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
var e_1, _c;
};
BsDatepickerEffects.decorators = [
{ type: Injectable }
];
/** @nocollapse */
BsDatepickerEffects.ctorParameters = function () { return [
{ type: BsDatepickerActions, },
{ type: BsLocaleService, },
]; };
return BsDatepickerEffects;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ defaultMonthOptions = {
width: 7,
height: 6
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ _initialView = { date: new Date(), mode: 'day' };
var /** @type {?} */ initialDatepickerState = Object.assign(new BsDatepickerConfig(), {
locale: 'en',
view: _initialView,
selectedRange: [],
monthViewOptions: defaultMonthOptions
});
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} date
* @param {?} options
* @return {?}
*/
function getStartingDayOfCalendar(date, options) {
if (isFirstDayOfWeek(date, options.firstDayOfWeek)) {
return date;
}
var /** @type {?} */ weekDay = getDay(date);
var /** @type {?} */ offset = calculateDateOffset(weekDay, options.firstDayOfWeek);
return shiftDate(date, { day: -offset });
}
/**
* @param {?} weekday
* @param {?} startingDayOffset
* @return {?}
*/
function calculateDateOffset(weekday, startingDayOffset) {
if (startingDayOffset === 0) {
return weekday;
}
var /** @type {?} */ offset = weekday - startingDayOffset % 7;
return offset < 0 ? offset + 7 : offset;
}
/**
* @param {?} date
* @param {?} min
* @param {?} max
* @return {?}
*/
function isMonthDisabled(date, min, max) {
var /** @type {?} */ minBound = min && isBefore(endOf(date, 'month'), min, 'day');
var /** @type {?} */ maxBound = max && isAfter(startOf(date, 'month'), max, 'day');
return minBound || maxBound;
}
/**
* @param {?} date
* @param {?} min
* @param {?} max
* @return {?}
*/
function isYearDisabled(date, min, max) {
var /** @type {?} */ minBound = min && isBefore(endOf(date, 'year'), min, 'day');
var /** @type {?} */ maxBound = max && isAfter(startOf(date, 'year'), max, 'day');
return minBound || maxBound;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @template T
* @param {?} options
* @param {?} fn
* @return {?}
*/
function createMatrix(options, fn) {
var /** @type {?} */ prevValue = options.initialDate;
var /** @type {?} */ matrix = new Array(options.height);
for (var /** @type {?} */ i = 0; i < options.height; i++) {
matrix[i] = new Array(options.width);
for (var /** @type {?} */ j = 0; j < options.width; j++) {
matrix[i][j] = fn(prevValue);
prevValue = shiftDate(prevValue, options.shift);
}
}
return matrix;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} startingDate
* @param {?} options
* @return {?}
*/
function calcDaysCalendar(startingDate, options) {
var /** @type {?} */ firstDay = getFirstDayOfMonth(startingDate);
var /** @type {?} */ initialDate = getStartingDayOfCalendar(firstDay, options);
var /** @type {?} */ matrixOptions = {
width: options.width,
height: options.height,
initialDate: initialDate,
shift: { day: 1 }
};
var /** @type {?} */ daysMatrix = createMatrix(matrixOptions, function (date) { return date; });
return {
daysMatrix: daysMatrix,
month: firstDay
};
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} daysCalendar
* @param {?} formatOptions
* @param {?} monthIndex
* @return {?}
*/
function formatDaysCalendar(daysCalendar, formatOptions, monthIndex) {
return {
month: daysCalendar.month,
monthTitle: formatDate(daysCalendar.month, formatOptions.monthTitle, formatOptions.locale),
yearTitle: formatDate(daysCalendar.month, formatOptions.yearTitle, formatOptions.locale),
weekNumbers: getWeekNumbers(daysCalendar.daysMatrix, formatOptions.weekNumbers, formatOptions.locale),
weekdays: getShiftedWeekdays(formatOptions.locale),
weeks: daysCalendar.daysMatrix.map(function (week, weekIndex) {
return ({
days: week.map(function (date, dayIndex) {
return ({
date: date,
label: formatDate(date, formatOptions.dayLabel, formatOptions.locale),
monthIndex: monthIndex,
weekIndex: weekIndex,
dayIndex: dayIndex
});
})
});
})
};
}
/**
* @param {?} daysMatrix
* @param {?} format
* @param {?} locale
* @return {?}
*/
function getWeekNumbers(daysMatrix, format, locale) {
return daysMatrix.map(function (days) { return (days[0] ? formatDate(days[0], format, locale) : ''); });
}
/**
* @param {?} locale
* @return {?}
*/
function getShiftedWeekdays(locale) {
var /** @type {?} */ _locale = getLocale(locale);
var /** @type {?} */ weekdays = /** @type {?} */ (_locale.weekdaysShort());
var /** @type {?} */ firstDayOfWeek = _locale.firstDayOfWeek();
return __spread(weekdays.slice(firstDayOfWeek), weekdays.slice(0, firstDayOfWeek));
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} formattedMonth
* @param {?} options
* @return {?}
*/
function flagDaysCalendar(formattedMonth, options) {
formattedMonth.weeks.forEach(function (week) {
/* tslint:disable-next-line: cyclomatic-complexity */
week.days.forEach(function (day, dayIndex) {
// datepicker
var /** @type {?} */ isOtherMonth = !isSameMonth(day.date, formattedMonth.month);
var /** @type {?} */ isHovered = !isOtherMonth && isSameDay(day.date, options.hoveredDate);
// date range picker
var /** @type {?} */ isSelectionStart = !isOtherMonth &&
options.selectedRange &&
isSameDay(day.date, options.selectedRange[0]);
var /** @type {?} */ isSelectionEnd = !isOtherMonth &&
options.selectedRange &&
isSameDay(day.date, options.selectedRange[1]);
var /** @type {?} */ isSelected = (!isOtherMonth && isSameDay(day.date, options.selectedDate)) ||
isSelectionStart ||
isSelectionEnd;
var /** @type {?} */ isInRange = !isOtherMonth &&
options.selectedRange &&
isDateInRange(day.date, options.selectedRange, options.hoveredDate);
var /** @type {?} */ isDisabled = options.isDisabled ||
isBefore(day.date, options.minDate, 'day') ||
isAfter(day.date, options.maxDate, 'day') ||
isDisabledDay(day.date, options.daysDisabled);
var /** @type {?} */ currentDate = new Date();
var /** @type {?} */ isToday = !isOtherMonth && isSameDay(day.date, currentDate);
// decide update or not
var /** @type {?} */ newDay = Object.assign({}, day, {
isOtherMonth: isOtherMonth,
isHovered: isHovered,
isSelected: isSelected,
isSelectionStart: isSelectionStart,
isSelectionEnd: isSelectionEnd,
isInRange: isInRange,
isDisabled: isDisabled,
isToday: isToday
});
if (day.isOtherMonth !== newDay.isOtherMonth ||
day.isHovered !== newDay.isHovered ||
day.isSelected !== newDay.isSelected ||
day.isSelectionStart !== newDay.isSelectionStart ||
day.isSelectionEnd !== newDay.isSelectionEnd ||
day.isDisabled !== newDay.isDisabled ||
day.isInRange !== newDay.isInRange) {
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;
}
/**
* @param {?} date
* @param {?} selectedRange
* @param {?} hoveredDate
* @return {?}
*/
function isDateInRange(date, selectedRange, hoveredDate) {
if (!date || !selectedRange[0]) {
return false;
}
if (selectedRange[1]) {
return date > selectedRange[0] && date <= selectedRange[1];
}
if (hoveredDate) {
return date > selectedRange[0] && date <= hoveredDate;
}
return false;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} mode
* @param {?=} minMode
* @return {?}
*/
function canSwitchMode(mode, minMode) {
return minMode ? mode >= minMode : true;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ height = 4;
var /** @type {?} */ width = 3;
var /** @type {?} */ shift = { month: 1 };
/**
* @param {?} viewDate
* @param {?} formatOptions
* @return {?}
*/
function formatMonthsCalendar(viewDate, formatOptions) {
var /** @type {?} */ initialDate = startOf(viewDate, 'year');
var /** @type {?} */ matrixOptions = { width: width, height: height, initialDate: initialDate, shift: shift };
var /** @type {?} */ monthMatrix = createMatrix(matrixOptions, function (date) {
return ({
date: date,
label: formatDate(date, formatOptions.monthLabel, formatOptions.locale)
});
});
return {
months: monthMatrix,
monthTitle: '',
yearTitle: formatDate(viewDate, formatOptions.yearTitle, formatOptions.locale)
};
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} monthCalendar
* @param {?} options
* @return {?}
*/
function flagMonthsCalendar(monthCalendar, options) {
monthCalendar.months.forEach(function (months, rowIndex) {
months.forEach(function (month, monthIndex) {
var /** @type {?} */ isHovered = isSameMonth(month.date, options.hoveredMonth);
var /** @type {?} */ isDisabled = options.isDisabled ||
isMonthDisabled(month.date, options.minDate, options.maxDate);
var /** @type {?} */ newMonth = Object.assign(/*{},*/ month, {
isHovered: isHovered,
isDisabled: isDisabled
});
if (month.isHovered !== newMonth.isHovered ||
month.isDisabled !== newMonth.isDisabled) {
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;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ height$1 = 4;
var /** @type {?} */ width$1 = 4;
var /** @type {?} */ yearsPerCalendar = height$1 * width$1;
var /** @type {?} */ initialShift = (Math.floor(yearsPerCalendar / 2) - 1) * -1;
var /** @type {?} */ shift$1 = { year: 1 };
/**
* @param {?} viewDate
* @param {?} formatOptions
* @return {?}
*/
function formatYearsCalendar(viewDate, formatOptions) {
var /** @type {?} */ initialDate = shiftDate(viewDate, { year: initialShift });
var /** @type {?} */ matrixOptions = { width: width$1, height: height$1, initialDate: initialDate, shift: shift$1 };
var /** @type {?} */ yearsMatrix = createMatrix(matrixOptions, function (date) {
return ({
date: date,
label: formatDate(date, formatOptions.yearLabel, formatOptions.locale)
});
});
var /** @type {?} */ yearTitle = formatYearRangeTitle(yearsMatrix, formatOptions);
return {
years: yearsMatrix,
monthTitle: '',
yearTitle: yearTitle
};
}
/**
* @param {?} yearsMatrix
* @param {?} formatOptions
* @return {?}
*/
function formatYearRangeTitle(yearsMatrix, formatOptions) {
var /** @type {?} */ from = formatDate(yearsMatrix[0][0].date, formatOptions.yearTitle, formatOptions.locale);
var /** @type {?} */ to = formatDate(yearsMatrix[height$1 - 1][width$1 - 1].date, formatOptions.yearTitle, formatOptions.locale);
return from + " - " + to;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?} yearsCalendar
* @param {?} options
* @return {?}
*/
function flagYearsCalendar(yearsCalendar, options) {
yearsCalendar.years.forEach(function (years, rowIndex) {
years.forEach(function (year, yearIndex) {
var /** @type {?} */ isHovered = isSameYear(year.date, options.hoveredYear);
var /** @type {?} */ isDisabled = options.isDisabled ||
isYearDisabled(year.date, options.minDate, options.maxDate);
var /** @type {?} */ newMonth = Object.assign(/*{},*/ year, { isHovered: isHovered, isDisabled: isDisabled });
if (year.isHovered !== newMonth.isHovered ||
year.isDisabled !== newMonth.isDisabled) {
yearsCalendar.years[rowIndex][yearIndex] = newMonth;
}
});
});
// 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);
var /** @type {?} */ i = yearsCalendar.years.length - 1;
var /** @type {?} */ j = yearsCalendar.years[i].length - 1;
yearsCalendar.disableRightArrow = isYearDisabled(shiftDate(yearsCalendar.years[i][j].date, { year: 1 }), options.minDate, options.maxDate);
return yearsCalendar;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @param {?=} state
* @param {?=} action
* @return {?}
*/
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 /** @type {?} */ date = shiftDate(startOf(state.view.date, 'month'), action.payload);
var /** @type {?} */ newState = {
view: {
mode: state.view.mode,
date: date
}
};
return Object.assign({}, state, newState);
}
case BsDatepickerActions.NAVIGATE_TO: {
var /** @type {?} */ payload = action.payload;
var /** @type {?} */ date = setFullDate(state.view.date, payload.unit);
var /** @type {?} */ newState = void 0;
var /** @type {?} */ mode = void 0;
if (canSwitchMode(payload.viewMode, state.minMode)) {
mode = payload.viewMode;
newState = { view: { date: date, mode: mode } };
}
else {
mode = state.view.mode;
newState = { selectedDate: date, view: { date: date, mode: mode } };
}
return Object.assign({}, state, newState);
}
case BsDatepickerActions.CHANGE_VIEWMODE: {
if (!canSwitchMode(action.payload, state.minMode)) {
return state;
}
var /** @type {?} */ date = state.view.date;
var /** @type {?} */ mode = action.payload;
var /** @type {?} */ 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 /** @type {?} */ newState = {
selectedDate: action.payload,
view: state.view
};
var /** @type {?} */ mode = state.view.mode;
var /** @type {?} */ _date = action.payload || state.view.date;
var /** @type {?} */ date = getViewDate(_date, state.minDate, state.maxDate);
newState.view = { mode: mode, date: date };
return Object.assign({}, state, newState);
}
case BsDatepickerActions.SET_OPTIONS: {
var /** @type {?} */ newState = action.payload;
// preserve view mode
var /** @type {?} */ mode = newState.minMode ? newState.minMode : state.view.mode;
var /** @type {?} */ _viewDate = isDateValid(newState.value) && newState.value
|| isArray(newState.value) && isDateValid(newState.value[0]) && newState.value[0]
|| state.view.date;
var /** @type {?} */ date = getViewDate(_viewDate, newState.minDate, newState.maxDate);
newState.view = { mode: mode, date: 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: {
var /** @type {?} */ newState = {
selectedRange: action.payload,
view: state.view
};
var /** @type {?} */ mode = state.view.mode;
var /** @type {?} */ _date = action.payload && action.payload[0] || state.view.date;
var /** @type {?} */ date = getViewDate(_date, state.minDate, state.maxDate);
newState.view = { mode: mode, date: 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
});
}
default:
return state;
}
}
/**
* @param {?} state
* @return {?}
*/
function calculateReducer(state) {
// how many calendars
var /** @type {?} */ displayMonths = state.displayMonths;
// use selected date on initial rendering if set
var /** @type {?} */ viewDate = state.view.date;
if (state.view.mode === 'day') {
state.monthViewOptions.firstDayOfWeek = getLocale(state.locale).firstDayOfWeek();
var /** @type {?} */ monthsModel = new Array(displayMonths);
for (var /** @type {?} */ 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 /** @type {?} */ monthsCalendar = new Array(displayMonths);
for (var /** @type {?} */ 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 /** @type {?} */ yearsCalendarModel = new Array(displayMonths);
for (var /** @type {?} */ 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;
}
/**
* @param {?} state
* @param {?} action
* @return {?}
*/
function formatReducer(state, action) {
if (state.view.mode === 'day') {
var /** @type {?} */ formattedMonths = state.monthsModel.map(function (month, monthIndex) {
return formatDaysCalendar(month, getFormatOptions(state), monthIndex);
});
return Object.assign({}, state, { formattedMonths: formattedMonths });
}
// how many calendars
var /** @type {?} */ displayMonths = state.displayMonths;
// check initial rendering
// use selected date on initial rendering if set
var /** @type {?} */ viewDate = state.view.date;
if (state.view.mode === 'month') {
var /** @type {?} */ monthsCalendar = new Array(displayMonths);
for (var /** @type {?} */ 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 /** @type {?} */ yearsCalendarModel = new Array(displayMonths);
for (var /** @type {?} */ 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;
}
/**
* @param {?} state
* @param {?} action
* @return {?}
*/
function flagReducer(state, action) {
if (state.view.mode === 'day') {
var /** @type {?} */ flaggedMonths = state.formattedMonths.map(function (formattedMonth, monthIndex) {
return flagDaysCalendar(formattedMonth, {
isDisabled: state.isDisabled,
minDate: state.minDate,
maxDate: state.maxDate,
daysDisabled: state.daysDisabled,
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 /** @type {?} */ 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 /** @type {?} */ 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;
}
/**
* @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<currentDate(default view value) show maxDate
* @param {?} viewDate
* @param {?} minDate
* @param {?} maxDate
* @return {?}
*/
function getViewDate(viewDate, minDate, maxDate) {
var /** @type {?} */ _date = Array.isArray(viewDate) ? viewDate[0] : viewDate;
if (minDate && isAfter(minDate, _date, 'day')) {
return minDate;
}
if (maxDate && isBefore(maxDate, _date, 'day')) {
return maxDate;
}
return _date;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var BsDatepickerStore = /** @class */ (function (_super) {
__extends(BsDatepickerStore, _super);
function BsDatepickerStore() {
var _this = this;
var /** @type {?} */ _dispatcher = new BehaviorSubject({
type: '[datepicker] dispatcher init'
});
var /** @type {?} */ state = new MiniState(initialDatepickerState, _dispatcher, bsDatepickerReducer);
_this = _super.call(this, _dispatcher, bsDatepickerReducer, state) || this;
return _this;
}
BsDatepickerStore.decorators = [
{ type: Injectable }
];
/** @nocollapse */
BsDatepickerStore.ctorParameters = function () { return []; };
return BsDatepickerStore;
}(MiniStore));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var BsDatepickerContainerComponent = /**