import { dispatch, upgrade } from '../../../../node_modules/@browserkids/dom/index.js'; export default class CalendarView extends HTMLElement { constructor() { super(); upgrade(this, ` `); const { calendar, preferences } = window; calendar.on('hide', this.onTodayClicked.bind(this)); preferences.on('changed', this.onPreferencesChanged.bind(this)); setInterval(this.update.bind(this), 1000); this.selectedMonth = null; this.lastUpdate = null; this.onTodayClicked(); } async update(force = false) { const { selectedMonth } = this; const { calendar } = window; const now = await calendar.getDate(); const isFirstTime = this.lastUpdate === null; const isSameHour = await calendar.isSameHour(this.lastUpdate || now, now); if (force === false && isFirstTime === false && isSameHour) { return; } this.lastUpdate = now; dispatch(window, 'postupdate', { selectedMonth }); } onKeyDown(e) { const { key, metaKey } = e; const { app, calendar, preferences } = window; const { $navigation } = this.$refs; if (key === 'Escape') { if ($navigation.hidden) { calendar.hide(); } else { $navigation.hidden = true; } } else if (key === 'w' && metaKey === false) { this.onToggle({ weeks: true }); } else if (key === ',' && metaKey) { preferences.show(); } else if (key === 'q' && metaKey) { app.quit(); } } onPreferencesChanged() { this.update(true); } onToggle({ weeks }) { const { $month } = this.$refs; if (weeks) { $month.classList.toggle('show-weeks'); } this.onPostRender(); } onPostRender() { const { app } = window; app.resizeWindow({ width: this.offsetWidth, height: this.offsetHeight, }); } onShowPreferences() { const { preferences } = window; preferences.show(); return this; } onTodayClicked() { this.onChange(); } onLegendClicked() { this.$refs.$navigation.hidden = false; } async onChange({ detail } = {}) { const { calendar } = window; this.selectedMonth = await calendar.getDate(detail ? { date: this.selectedMonth, ...detail } : {}); this.update(true); if (detail?.diff?.years === undefined) { this.$refs.$navigation.hidden = true; } } }