UNPKG

@progress/kendo-react-dateinputs

Version:
171 lines • 8.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _a; var kendo_date_math_1 = require("@progress/kendo-date-math"); var NavigationAction_1 = require("../models/NavigationAction"); var SelectionRange_1 = require("../models/SelectionRange"); var utils_1 = require("../../utils"); var EMPTY_DATA = [[]]; var CELLS_LENGTH = 7; var ROWS_LENGTH = 6; var SATURDAY = 6; var SUNDAY = 0; var ACTIONS = (_a = {}, _a[NavigationAction_1.Action.Left] = function (date) { return kendo_date_math_1.addDays(date, -1); }, _a[NavigationAction_1.Action.Up] = function (date) { return kendo_date_math_1.addWeeks(date, -1); }, _a[NavigationAction_1.Action.Right] = function (date) { return kendo_date_math_1.addDays(date, 1); }, _a[NavigationAction_1.Action.Down] = function (date) { return kendo_date_math_1.addWeeks(date, 1); }, _a[NavigationAction_1.Action.PrevView] = function (date) { return kendo_date_math_1.addMonths(date, -1); }, _a[NavigationAction_1.Action.NextView] = function (date) { return kendo_date_math_1.addMonths(date, 1); }, _a[NavigationAction_1.Action.FirstInView] = function (date) { return kendo_date_math_1.firstDayOfMonth(date); }, _a[NavigationAction_1.Action.LastInView] = function (date) { return kendo_date_math_1.lastDayOfMonth(date); }, _a); /** * @hidden */ var MonthViewService = /** @class */ (function () { function MonthViewService(intl) { this.intl = intl; } MonthViewService.prototype.addToDate = function (min, skip) { return kendo_date_math_1.addMonths(min, skip); }; MonthViewService.prototype.datesList = function (start, count) { return utils_1.range(0, count).map(function (i) { return kendo_date_math_1.addMonths(start, i); }); }; MonthViewService.prototype.data = function (options) { var _this = this; var cellUID = options.cellUID, focusedDate = options.focusedDate, isActiveView = options.isActiveView, max = options.max, min = options.min, selectedDate = options.selectedDate, _a = options.selectionRange, selectionRange = _a === void 0 ? SelectionRange_1.EMPTY_SELECTIONRANGE : _a, viewDate = options.viewDate; if (!viewDate) { return EMPTY_DATA; } var firstMonthDate = kendo_date_math_1.firstDayOfMonth(viewDate); var lastMonthDate = kendo_date_math_1.lastDayOfMonth(viewDate); var backward = -1; var date = kendo_date_math_1.dayOfWeek(firstMonthDate, this.intl.firstDay(), backward); var cells = utils_1.range(0, CELLS_LENGTH); var today = utils_1.getToday(); return utils_1.range(0, ROWS_LENGTH).map(function (rowOffset) { var baseDate = kendo_date_math_1.addDays(date, rowOffset * CELLS_LENGTH); return cells.map(function (cellOffset) { var cellDate = _this.normalize(kendo_date_math_1.addDays(baseDate, cellOffset), min, max); var otherMonth = cellDate < firstMonthDate || cellDate > lastMonthDate; var outOfRange = cellDate < min || cellDate > max; if (otherMonth || outOfRange) { return null; } var isRangeStart = _this.isEqual(cellDate, selectionRange.start); var isRangeEnd = _this.isEqual(cellDate, selectionRange.end); var isInMiddle = !isRangeStart && !isRangeEnd; var isRangeMid = isInMiddle && utils_1.isInSelectionRange(cellDate, selectionRange); var isSelected = isActiveView && (!Array.isArray(selectedDate) ? utils_1.isInRange(selectedDate, min, max) && _this.isEqual(cellDate, selectedDate) : _this.isSelectedFromArray(cellDate, selectedDate, min, max)); return { formattedValue: _this.value(cellDate), id: "" + cellUID + cellDate.getTime(), isFocused: _this.isEqual(cellDate, focusedDate), isSelected: isSelected, isWeekend: _this.isWeekend(cellDate), isRangeStart: isRangeStart, isRangeMid: isRangeMid, isRangeEnd: isRangeEnd, isRangeSplitStart: isRangeMid && _this.isEqual(cellDate, firstMonthDate), isRangeSplitEnd: isRangeMid && _this.isEqual(cellDate, lastMonthDate), isToday: _this.isEqual(cellDate, today), title: _this.cellTitle(cellDate), value: cellDate }; }); }); }; MonthViewService.prototype.isEqual = function (candidate, expected) { if (!candidate || !expected) { return false; } return kendo_date_math_1.getDate(candidate).getTime() === kendo_date_math_1.getDate(expected).getTime(); }; MonthViewService.prototype.isSelectedFromArray = function (candidate, dates, min, max) { var _this = this; var result = false; dates.forEach(function (date) { if (utils_1.isInRange(candidate, min, max) && _this.isEqual(candidate, date)) { result = true; } }); return result; }; MonthViewService.prototype.isInArray = function (date, dates) { return !!dates.length && kendo_date_math_1.firstDayOfMonth(dates[0]) <= date && date <= kendo_date_math_1.lastDayOfMonth(dates[dates.length - 1]); }; MonthViewService.prototype.isInRange = function (candidate, min, max) { var value = kendo_date_math_1.getDate(candidate); var aboveMin = !min || kendo_date_math_1.getDate(min) <= value; var belowMax = !max || value <= kendo_date_math_1.getDate(max); return aboveMin && belowMax; }; MonthViewService.prototype.isInSameView = function (candidate, value) { return kendo_date_math_1.durationInMonths(candidate, value) === 0; }; MonthViewService.prototype.isRangeStart = function (value) { return !value.getMonth(); }; MonthViewService.prototype.move = function (value, action) { var modifier = ACTIONS[action]; if (!modifier) { return value; } return modifier(value); }; MonthViewService.prototype.cellTitle = function (value) { return this.intl.formatDate(value, 'D'); }; MonthViewService.prototype.navigationTitle = function (value) { if (!value) { return ''; } return this.isRangeStart(value) ? value.getFullYear().toString() : this.abbrMonthNames()[value.getMonth()]; }; MonthViewService.prototype.title = function (current) { return this.wideMonthNames()[current.getMonth()] + " " + current.getFullYear(); }; MonthViewService.prototype.rowLength = function (prependCell) { return CELLS_LENGTH + (prependCell ? 1 : 0); }; MonthViewService.prototype.skip = function (value, min) { return kendo_date_math_1.durationInMonths(min, value); }; MonthViewService.prototype.total = function (min, max) { return kendo_date_math_1.durationInMonths(min, max) + 1; }; MonthViewService.prototype.value = function (current) { return current ? current.getDate().toString() : ''; }; MonthViewService.prototype.viewDate = function (date, max, border) { if (border === void 0) { border = 1; } var renderTwoViews = kendo_date_math_1.durationInMonths(date, max) < border; return renderTwoViews ? kendo_date_math_1.addMonths(date, -1) : date; }; MonthViewService.prototype.isWeekend = function (date) { var day = date.getDay(); return day === SATURDAY || day === SUNDAY; }; MonthViewService.prototype.abbrMonthNames = function () { return this.intl.dateFormatNames({ nameType: 'abbreviated', type: 'months' }); }; MonthViewService.prototype.normalize = function (cellDate, min, max) { if (cellDate < min && this.isEqual(cellDate, min)) { return kendo_date_math_1.getDate(min); } if (cellDate > max && this.isEqual(cellDate, max)) { return kendo_date_math_1.getDate(max); } return cellDate; }; MonthViewService.prototype.wideMonthNames = function () { return this.intl.dateFormatNames({ nameType: 'wide', type: 'months' }); }; return MonthViewService; }()); exports.MonthViewService = MonthViewService; //# sourceMappingURL=MonthViewService.js.map