UNPKG

igniteui-webcomponents

Version:

Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.

256 lines 7.83 kB
export var DatePart; (function (DatePart) { DatePart["Month"] = "month"; DatePart["Year"] = "year"; DatePart["Date"] = "date"; DatePart["Hours"] = "hours"; DatePart["Minutes"] = "minutes"; DatePart["Seconds"] = "seconds"; DatePart["AmPm"] = "amPm"; })(DatePart || (DatePart = {})); export const DatePartType = { Month: 'month', Year: 'year', Date: 'date', Hours: 'hours', Minutes: 'minutes', Seconds: 'seconds', AmPm: 'amPm', Literal: 'literal', }; export const DEFAULT_DATE_PARTS_SPIN_DELTAS = Object.freeze({ date: 1, month: 1, year: 1, hours: 1, minutes: 1, seconds: 1, }); const TIME_BOUNDS = { hours: { min: 0, max: 23 }, minutes: { min: 0, max: 59 }, seconds: { min: 0, max: 59 }, }; const DATE_BOUNDS = { month: { min: 0, max: 11 }, date: { min: 1, max: 31 }, }; function daysInMonth(year, month) { return new Date(year, month + 1, 0).getDate(); } function padValue(value, length) { return String(value).padStart(length, '0'); } function toTwelveHourFormat(hours) { const h = hours % 12; return h === 0 ? 12 : h; } function spinTimePart(date, delta, max, min, setter, getter, spinLoop) { let value = getter.call(date) + delta; if (value > max) { value = spinLoop ? value % (max + 1) : max; } else if (value < min) { value = spinLoop ? max + 1 + (value % (max + 1)) : min; } setter.call(date, value); } class DatePartBase { constructor(type, options) { this.type = type; this.start = options.start; this.end = options.end; this.format = options.format; } } class YearPart extends DatePartBase { constructor(options) { super(DatePartType.Year, options); } validate(_value) { return _value >= 0; } spin(delta, options) { const { date } = options; const maxDate = daysInMonth(date.getFullYear() + delta, date.getMonth()); if (date.getDate() > maxDate) { date.setDate(maxDate); } date.setFullYear(date.getFullYear() + delta); } getValue(date) { const length = this.format.length; const year = length === 2 ? date.getFullYear().toString().slice(-2) : date.getFullYear(); return padValue(year, length); } } class MonthPart extends DatePartBase { constructor(options) { super(DatePartType.Month, options); } validate(value) { return value >= DATE_BOUNDS.month.min && value <= DATE_BOUNDS.month.max; } spin(delta, options) { const { date, spinLoop } = options; const { min, max } = DATE_BOUNDS.month; const maxDate = daysInMonth(date.getFullYear(), date.getMonth() + delta); if (date.getDate() > maxDate) { date.setDate(maxDate); } let month = date.getMonth() + delta; if (month > max) { month = spinLoop ? (month % max) - 1 : max; } else if (month < min) { month = spinLoop ? max + (month % max) + 1 : min; } date.setMonth(month); } getValue(date) { return padValue(date.getMonth() + 1, this.format.length); } } class DateOfMonthPart extends DatePartBase { constructor(options) { super(DatePartType.Date, options); } validate(value, context) { if (value < DATE_BOUNDS.date.min) { return false; } if (context?.year !== undefined && context?.month !== undefined) { const maxDays = daysInMonth(context.year, context.month); return value <= maxDays; } return value <= DATE_BOUNDS.date.max; } spin(delta, options) { const { date, spinLoop } = options; const maxDate = daysInMonth(date.getFullYear(), date.getMonth()); const { min } = DATE_BOUNDS.date; let dayOfMonth = date.getDate() + delta; if (dayOfMonth > maxDate) { dayOfMonth = spinLoop ? dayOfMonth % maxDate : maxDate; } else if (dayOfMonth < min) { dayOfMonth = spinLoop ? maxDate + (dayOfMonth % maxDate) : min; } date.setDate(dayOfMonth); } getValue(date) { return padValue(date.getDate(), this.format.length); } } class HoursPart extends DatePartBase { constructor(options) { super(DatePartType.Hours, options); } validate(value) { return value >= TIME_BOUNDS.hours.min && value <= TIME_BOUNDS.hours.max; } spin(delta, options) { const { date, spinLoop } = options; const { min, max } = TIME_BOUNDS.hours; spinTimePart(date, delta, max, min, date.setHours, date.getHours, spinLoop); } getValue(date) { const isTwelveHour = this.format.includes('h'); const hours = isTwelveHour ? toTwelveHourFormat(date.getHours()) : date.getHours(); return padValue(hours, this.format.length); } } class MinutesPart extends DatePartBase { constructor(options) { super(DatePartType.Minutes, options); } validate(value) { return value >= TIME_BOUNDS.minutes.min && value <= TIME_BOUNDS.minutes.max; } spin(delta, options) { const { date, spinLoop } = options; const { min, max } = TIME_BOUNDS.minutes; spinTimePart(date, delta, max, min, date.setMinutes, date.getMinutes, spinLoop); } getValue(date) { return padValue(date.getMinutes(), this.format.length); } } class SecondsPart extends DatePartBase { constructor(options) { super(DatePartType.Seconds, options); } validate(value) { return value >= TIME_BOUNDS.seconds.min && value <= TIME_BOUNDS.seconds.max; } spin(delta, options) { const { date, spinLoop } = options; const { min, max } = TIME_BOUNDS.seconds; spinTimePart(date, delta, max, min, date.setSeconds, date.getSeconds, spinLoop); } getValue(date) { return padValue(date.getSeconds(), this.format.length); } } class AmPmPart extends DatePartBase { constructor(options) { super(DatePartType.AmPm, options); } validate(_value) { return true; } spin(_delta, options) { const { date, amPmValue, originalDate } = options; if (!amPmValue) return; const isAM = amPmValue.toLowerCase() === 'am'; const newHours = date.getHours() + (isAM ? 12 : -12); date.setHours(newHours); if (originalDate && date.getDate() !== originalDate.getDate()) { date.setTime(originalDate.getTime()); } } getValue(date) { return date.getHours() >= 12 ? 'PM' : 'AM'; } } class LiteralPart extends DatePartBase { constructor(options) { super(DatePartType.Literal, options); } validate(_value) { return true; } spin(_delta, _options) { } getValue(_date) { return this.format; } } export function createDatePart(type, options) { switch (type) { case DatePartType.Year: return new YearPart(options); case DatePartType.Month: return new MonthPart(options); case DatePartType.Date: return new DateOfMonthPart(options); case DatePartType.Hours: return new HoursPart(options); case DatePartType.Minutes: return new MinutesPart(options); case DatePartType.Seconds: return new SecondsPart(options); case DatePartType.AmPm: return new AmPmPart(options); case DatePartType.Literal: return new LiteralPart(options); default: throw new Error(`Unknown date part type: ${type}`); } } //# sourceMappingURL=date-part.js.map