@progress/kendo-angular-dateinputs
Version:
Kendo UI for Angular Date Inputs Package - Everything you need to add date selection functionality to apps (DatePicker, TimePicker, DateInput, DateRangePicker, DateTimePicker, Calendar, and MultiViewCalendar).
205 lines (204 loc) • 8.71 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Injectable } from '@angular/core';
import { IntlService } from '@progress/kendo-angular-intl';
import { addMonths, addYears, cloneDate, createDate, durationInYears, firstMonthOfYear, lastDayOfMonth, lastMonthOfYear } from '@progress/kendo-date-math';
import { Action } from '../models/navigation-action.enum';
import { EMPTY_SELECTIONRANGE } from '../models/selection-range.interface';
import { getToday, isInSelectionRange, range } from '../../util';
import { isPresent } from '../../common/utils';
import * as i0 from "@angular/core";
import * as i1 from "@progress/kendo-angular-intl";
const EMPTY_DATA = [[]];
const CELLS_LENGTH = 4;
const ROWS_LENGTH = 3;
const ACTIONS = {
[Action.Left]: (date) => addMonths(date, -1),
[Action.Up]: (date) => addMonths(date, -4),
[Action.Right]: (date) => addMonths(date, 1),
[Action.Down]: (date) => addMonths(date, 4),
[Action.PrevView]: (date) => addYears(date, -1),
[Action.NextView]: (date) => addYears(date, 1),
[Action.FirstInView]: (date) => firstMonthOfYear(date),
[Action.LastInView]: (date) => lastMonthOfYear(date)
};
/**
* @hidden
*/
export class YearViewService {
_intlService;
constructor(_intlService) {
this._intlService = _intlService;
}
addToDate(min, skip) {
return addYears(min, skip);
}
datesList(start, count) {
return range(0, count).map(i => addYears(start, i));
}
data(options) {
const { cellUID, focusedDate, isActiveView, max, min, selectedDates, selectionRange = EMPTY_SELECTIONRANGE, viewDate, allowReverse } = options;
if (!viewDate) {
return EMPTY_DATA;
}
const months = this.abbrMonthNames();
const firstDate = firstMonthOfYear(viewDate);
const lastDate = lastMonthOfYear(viewDate);
const currentYear = firstDate.getFullYear();
const cells = range(0, CELLS_LENGTH);
const today = getToday();
return range(0, ROWS_LENGTH).map(rowOffset => {
const baseDate = addMonths(firstDate, rowOffset * CELLS_LENGTH);
return cells.map(cellOffset => {
const cellDate = this.normalize(addMonths(baseDate, cellOffset), min, max);
const changedYear = currentYear < cellDate.getFullYear();
if (!this.isInRange(cellDate, min, max) || changedYear) {
return null;
}
let isRangeStart = false;
let isRangeEnd = false;
if (allowReverse) {
if ((this.isEqual(cellDate, selectionRange.start) && selectionRange.start <= selectionRange.end) ||
(this.isEqual(cellDate, selectionRange.end) && selectionRange.end <= selectionRange.start)) {
isRangeStart = true;
}
if ((this.isEqual(cellDate, selectionRange.start) && selectionRange.start >= selectionRange.end) ||
(this.isEqual(cellDate, selectionRange.end) && selectionRange.end >= selectionRange.start)) {
isRangeEnd = true;
}
}
else {
isRangeStart = this.isEqual(cellDate, selectionRange.start);
isRangeEnd = this.isEqual(cellDate, selectionRange.end);
}
const isInMiddle = !isRangeStart && !isRangeEnd;
let isRangeMid;
if (allowReverse) {
isRangeMid = isInMiddle && (isInSelectionRange(cellDate, selectionRange) || isInSelectionRange(cellDate, { start: selectionRange.end, end: selectionRange.start }));
}
else {
isRangeMid = isInMiddle && isInSelectionRange(cellDate, selectionRange);
}
return {
formattedValue: months[cellDate.getMonth()],
id: `${cellUID}${cellDate.getTime()}`,
isFocused: this.isEqual(cellDate, focusedDate),
isSelected: isActiveView && selectedDates.some(date => this.isEqual(cellDate, date)),
isWeekend: false,
isRangeStart: isRangeStart,
isRangeMid: isRangeMid,
isRangeEnd: isRangeEnd,
isRangeSplitEnd: isRangeMid && this.isEqual(cellDate, lastDate),
isRangeSplitStart: isRangeMid && this.isEqual(cellDate, firstDate),
isToday: this.isEqual(cellDate, today),
title: this.cellTitle(cellDate),
value: cellDate,
allowReverse: allowReverse
};
});
});
}
isEqual(candidate, expected) {
if (!candidate || !expected) {
return false;
}
return candidate.getFullYear() === expected.getFullYear() &&
candidate.getMonth() === expected.getMonth();
}
isInArray(date, dates) {
if (!dates.length) {
return false;
}
const year = date.getFullYear();
return dates[0].getFullYear() <= year && year <= dates[dates.length - 1].getFullYear();
}
isInRange(candidate, min, max) {
const candidateValue = createDate(candidate.getFullYear(), candidate.getMonth(), 1);
const aboveMin = !min || createDate(min.getFullYear(), min.getMonth(), 1) <= candidateValue;
const belowMax = !max || candidateValue <= createDate(max.getFullYear(), max.getMonth(), 1);
return aboveMin && belowMax;
}
beginningOfPeriod(date) {
if (!date) {
return date;
}
return createDate(date.getFullYear(), 0, 1);
}
lastDayOfPeriod(date) {
const month = lastMonthOfYear(date);
return lastDayOfMonth(month);
}
isRangeStart(value) {
return value.getFullYear() % 10 === 0;
}
move(value, action) {
const modifier = ACTIONS[action];
if (!modifier) {
return value;
}
return modifier(value);
}
cellTitle(value) {
return `${value.getFullYear()} ${this.value(value)}`;
}
navigationTitle(value) {
return this.title(value);
}
title(current) {
return current ? current.getFullYear().toString() : '';
}
rowLength() {
return CELLS_LENGTH;
}
skip(value, min) {
return durationInYears(min, value);
}
total(min, max) {
return durationInYears(min, max) + 1;
}
value(current) {
return current ? this.abbrMonthNames()[current.getMonth()] : '';
}
viewDate(date, max, viewsCount = 1) {
const viewsInRange = this.total(date, max);
if (viewsInRange < viewsCount) {
const yearsToSubtract = viewsCount - viewsInRange;
return addYears(date, -1 * yearsToSubtract);
}
return date;
}
dateRange = (start, end) => {
if (!isPresent(start) || !isPresent(end)) {
return [];
}
const result = [];
let current = start;
while (current <= end) {
result.push(current);
current = addMonths(current, 1);
}
return result;
};
abbrMonthNames() {
return this._intlService.dateFormatNames({ nameType: 'abbreviated', type: 'months' });
}
normalize(cellDate, min, max) {
if (cellDate < min && this.isEqual(cellDate, min)) {
return cloneDate(min);
}
if (cellDate > max && this.isEqual(cellDate, max)) {
return cloneDate(max);
}
return cellDate;
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: YearViewService, deps: [{ token: i1.IntlService }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: YearViewService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: YearViewService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return [{ type: i1.IntlService }]; } });