UNPKG

@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).

132 lines (131 loc) 5.37 kB
/**----------------------------------------------------------------------------------------- * 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 { isEqual } from '@progress/kendo-date-math'; import { IntlService } from '@progress/kendo-angular-intl'; import { MIDNIGHT_DATE } from '../../defaults'; import { range, setMilliseconds } from '../../util'; import * as i0 from "@angular/core"; import * as i1 from "@progress/kendo-angular-intl"; const MILLISECONDS_IN_SECOND = 1000; const clampToRange = (rangeValue) => (value) => value % rangeValue; const clamp = clampToRange(MILLISECONDS_IN_SECOND); const stepper = (start, step) => (idx) => clamp(start + (idx * step)); const distanceFromMin = (value, min) => clamp(MILLISECONDS_IN_SECOND + value - min); const limit = (borderValue) => (barrier, value) => { const useBarrier = !value || (barrier.getHours() === value.getHours() && barrier.getMinutes() === value.getMinutes() && barrier.getSeconds() === value.getSeconds()); return useBarrier ? barrier : setMilliseconds(barrier, borderValue); }; const limitDown = limit(0); const limitUp = limit(MILLISECONDS_IN_SECOND - 1); /** * @hidden */ export class MillisecondsService { intl; toListItem; min; max; step; insertUndividedMax = false; constructor(intl) { this.intl = intl; } apply(value, candidate) { return setMilliseconds(value, candidate.getMilliseconds()); } configure(settings) { const { insertUndividedMax = this.insertUndividedMax, min = this.min, max = this.max, step = this.step } = settings; this.insertUndividedMax = insertUndividedMax; this.toListItem = (millisecond) => { const date = setMilliseconds(MIDNIGHT_DATE, millisecond); return { text: this.intl.formatDate(date, "SSS"), value: date }; }; this.min = min; this.max = max; this.step = step; } data(selectedValue) { const [min] = this.range(selectedValue); const getMillisecond = stepper(min, this.step); const convertToItem = (idx) => (this.toListItem(getMillisecond(idx))); const data = range(0, this.countFromMin(selectedValue)).map(convertToItem); this.addLast(data); this.addMissing(data, selectedValue); return data; } isRangeChanged(min, max) { return !isEqual(this.min, min) || !isEqual(this.max, max); } limitRange(min, max, value) { return [limitDown(min, value), limitUp(max, value)]; } total(value) { const last = this.insertUndividedMax && this.isLastMissing(value) ? 1 : 0; const missing = this.isMissing(value) ? 1 : 0; return this.countFromMin(value) + missing + last; } selectedIndex(value) { return Math.ceil(this.divideByStep(value)); } valueInList(value) { if (!value) { return true; } const matchMax = this.insertUndividedMax && this.lastMillisecond(value) === value.getMilliseconds(); return matchMax || !this.isMissing(value); } divideByStep(value) { return distanceFromMin(value.getMilliseconds(), this.min.getMilliseconds()) / this.step; } addLast(data, value) { if (this.insertUndividedMax && this.isLastMissing(value)) { data.push(this.toListItem(this.lastMillisecond(value))); } return data; } addMissing(data, value) { if (this.valueInList(value)) { return data; } const missingItem = this.toListItem(value.getMilliseconds()); data.splice(this.selectedIndex(value), 0, missingItem); return data; } countFromMin(value) { const [min, max] = this.range(value); return Math.floor(distanceFromMin(max, min) / this.step) + 1; /* include min */ } isMissing(value) { if (!value) { return false; } return this.selectedIndex(value) !== this.divideByStep(value); } isLastMissing(value) { return this.isMissing(setMilliseconds(this.max, this.lastMillisecond(value))); } lastMillisecond(value) { return this.range(value)[1]; } range(value) { const [min, max] = this.limitRange(this.min, this.max, value); return [min.getMilliseconds(), max.getMilliseconds()]; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: MillisecondsService, deps: [{ token: i1.IntlService }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: MillisecondsService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: MillisecondsService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: i1.IntlService }]; } });