@3mo/date-time-fields
Version:
Date time fields let people select dates, date-ranges, and times.
77 lines (76 loc) • 3.5 kB
JavaScript
import { __decorate } from "tslib";
import { Controller, html } from '@a11d/lit';
import { observeIntersection } from '@3mo/intersection-observer';
import { MemoizeExpiring as memoizeExpiring } from 'typescript-memoize';
import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
export class CalendarDatesController extends Controller {
static get today() { return new DateTime().dayStart; }
static *generate(start, count, step) {
for (let i = 0; i < count; i++) {
yield start.add({ [step]: i });
}
}
static get sampleWeek() { return this._sampleWeek; }
static generateWeek() {
const sample = [...CalendarDatesController.generate(CalendarDatesController.today, CalendarDatesController.today.daysInWeek * 2, 'days')];
const indexOfFirstWeekStart = sample.findIndex(d => d.dayOfWeek === 1);
const daysInWeek = sample[0].daysInWeek;
CalendarDatesController._sampleWeek = sample.slice(indexOfFirstWeekStart, indexOfFirstWeekStart + daysInWeek).map(d => d.dayStart);
}
observerIntersectionNavigation(date, ...views) {
return !views.includes(this.host.view) ? html.nothing : observeIntersection(data => {
if (!this.disableObservers && data.some(entry => entry.isIntersecting)) {
this.navigationDate = date;
}
});
}
constructor(host) {
super(host);
this.host = host;
this.disableObservers = false;
this.days = new Array();
this.months = new Array();
this.years = new Array();
this.navigationDate = CalendarDatesController.today;
}
get data() {
switch (this.host.view) {
case FieldDateTimePrecision.Year:
return this.years;
case FieldDateTimePrecision.Month:
return this.months;
default:
return this.days;
}
}
get navigationDate() { return this._navigationDate; }
set navigationDate(value) {
let changed = false;
const daysOffset = 75;
if (this.host.view === FieldDateTimePrecision.Day && (!this.days.length || value.isBefore(this.days.at(daysOffset)) || value.isAfter(this.days.at(-daysOffset)))) {
this.days = [...CalendarDatesController.generate(value.yearStart.add({ years: -1 }), value.daysInYear * 3, 'days')];
changed = true;
}
const monthsOffset = 25;
if (this.host.view === FieldDateTimePrecision.Month && (!this.months.length || value.isBefore(this.months.at(monthsOffset)) || value.isAfter(this.months.at(-monthsOffset)))) {
this.months = [...CalendarDatesController.generate(value.yearStart.add({ years: -10 }), value.monthsInYear * 20, 'months')];
changed = true;
}
const yearsOffset = 15;
if (this.host.view === FieldDateTimePrecision.Year && (!this.years.length || value.isBefore(this.years.at(yearsOffset)) || value.isAfter(this.years.at(-yearsOffset)))) {
this.years = [...CalendarDatesController.generate(value.yearStart.add({ years: -100 }), 200, 'years')];
changed = true;
}
if (changed) {
this.host.requestUpdate();
}
this._navigationDate = value.dayStart;
}
}
CalendarDatesController._sampleWeek = new Array();
(() => {
CalendarDatesController.generateWeek();
})();
__decorate([
memoizeExpiring(60000)
], CalendarDatesController, "today", null);