mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add resize observer to full calendar, fix missing styles (#18381)
This commit is contained in:
parent
ccba7a7623
commit
2b67731906
@ -4,6 +4,7 @@ import allLocales from "@fullcalendar/core/locales-all";
|
|||||||
import dayGridPlugin from "@fullcalendar/daygrid";
|
import dayGridPlugin from "@fullcalendar/daygrid";
|
||||||
import interactionPlugin from "@fullcalendar/interaction";
|
import interactionPlugin from "@fullcalendar/interaction";
|
||||||
import listPlugin from "@fullcalendar/list";
|
import listPlugin from "@fullcalendar/list";
|
||||||
|
import { ResizeController } from "@lit-labs/observers/resize-controller";
|
||||||
import "@material/mwc-button";
|
import "@material/mwc-button";
|
||||||
import {
|
import {
|
||||||
mdiPlus,
|
mdiPlus,
|
||||||
@ -13,11 +14,11 @@ import {
|
|||||||
mdiViewWeek,
|
mdiViewWeek,
|
||||||
} from "@mdi/js";
|
} from "@mdi/js";
|
||||||
import {
|
import {
|
||||||
css,
|
|
||||||
CSSResultGroup,
|
CSSResultGroup,
|
||||||
html,
|
|
||||||
LitElement,
|
LitElement,
|
||||||
PropertyValues,
|
PropertyValues,
|
||||||
|
css,
|
||||||
|
html,
|
||||||
nothing,
|
nothing,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
@ -37,6 +38,7 @@ import type {
|
|||||||
CalendarEvent,
|
CalendarEvent,
|
||||||
} from "../../data/calendar";
|
} from "../../data/calendar";
|
||||||
import { CalendarEntityFeature } from "../../data/calendar";
|
import { CalendarEntityFeature } from "../../data/calendar";
|
||||||
|
import { TimeZone } from "../../data/translation";
|
||||||
import { haStyle } from "../../resources/styles";
|
import { haStyle } from "../../resources/styles";
|
||||||
import type {
|
import type {
|
||||||
CalendarViewChanged,
|
CalendarViewChanged,
|
||||||
@ -46,7 +48,6 @@ import type {
|
|||||||
} from "../../types";
|
} from "../../types";
|
||||||
import { showCalendarEventDetailDialog } from "./show-dialog-calendar-event-detail";
|
import { showCalendarEventDetailDialog } from "./show-dialog-calendar-event-detail";
|
||||||
import { showCalendarEventEditDialog } from "./show-dialog-calendar-event-editor";
|
import { showCalendarEventEditDialog } from "./show-dialog-calendar-event-editor";
|
||||||
import { TimeZone } from "../../data/translation";
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HTMLElementTagNameMap {
|
interface HTMLElementTagNameMap {
|
||||||
@ -63,6 +64,7 @@ const defaultFullCalendarConfig: CalendarOptions = {
|
|||||||
initialView: "dayGridMonth",
|
initialView: "dayGridMonth",
|
||||||
dayMaxEventRows: true,
|
dayMaxEventRows: true,
|
||||||
height: "parent",
|
height: "parent",
|
||||||
|
handleWindowResize: false,
|
||||||
locales: allLocales,
|
locales: allLocales,
|
||||||
views: {
|
views: {
|
||||||
listWeek: {
|
listWeek: {
|
||||||
@ -101,8 +103,23 @@ export class HAFullCalendar extends LitElement {
|
|||||||
|
|
||||||
@state() private _activeView = this.initialView;
|
@state() private _activeView = this.initialView;
|
||||||
|
|
||||||
public updateSize(): void {
|
// @ts-ignore
|
||||||
this.calendar?.updateSize();
|
private _resizeController = new ResizeController(this, {
|
||||||
|
callback: () => this.calendar?.updateSize(),
|
||||||
|
});
|
||||||
|
|
||||||
|
disconnectedCallback(): void {
|
||||||
|
super.disconnectedCallback();
|
||||||
|
this.calendar?.destroy();
|
||||||
|
this.calendar = undefined;
|
||||||
|
this.renderRoot.querySelector("style[data-fullcalendar]")?.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback(): void {
|
||||||
|
super.connectedCallback();
|
||||||
|
if (this.hasUpdated && !this.calendar) {
|
||||||
|
this._loadCalendar(this._activeView);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
@ -241,10 +258,10 @@ export class HAFullCalendar extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated(): void {
|
protected firstUpdated(): void {
|
||||||
this._loadCalendar();
|
this._loadCalendar(this.initialView);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _loadCalendar() {
|
private async _loadCalendar(initialView: FullCalendarView) {
|
||||||
const luxonPlugin =
|
const luxonPlugin =
|
||||||
this.hass.locale.time_zone === TimeZone.local
|
this.hass.locale.time_zone === TimeZone.local
|
||||||
? undefined
|
? undefined
|
||||||
@ -262,7 +279,7 @@ export class HAFullCalendar extends LitElement {
|
|||||||
? "local"
|
? "local"
|
||||||
: this.hass.config.time_zone,
|
: this.hass.config.time_zone,
|
||||||
firstDay: firstWeekdayIndex(this.hass.locale),
|
firstDay: firstWeekdayIndex(this.hass.locale),
|
||||||
initialView: this.initialView,
|
initialView,
|
||||||
eventDisplay: this.eventDisplay,
|
eventDisplay: this.eventDisplay,
|
||||||
eventTimeFormat: {
|
eventTimeFormat: {
|
||||||
hour: useAmPm(this.hass.locale) ? "numeric" : "2-digit",
|
hour: useAmPm(this.hass.locale) ? "numeric" : "2-digit",
|
||||||
|
@ -4,11 +4,11 @@ import interactionPlugin from "@fullcalendar/interaction";
|
|||||||
import timeGridPlugin from "@fullcalendar/timegrid";
|
import timeGridPlugin from "@fullcalendar/timegrid";
|
||||||
import { addDays, isSameDay, isSameWeek, nextDay } from "date-fns";
|
import { addDays, isSameDay, isSameWeek, nextDay } from "date-fns";
|
||||||
import {
|
import {
|
||||||
css,
|
|
||||||
CSSResultGroup,
|
CSSResultGroup,
|
||||||
html,
|
|
||||||
LitElement,
|
LitElement,
|
||||||
PropertyValues,
|
PropertyValues,
|
||||||
|
css,
|
||||||
|
html,
|
||||||
nothing,
|
nothing,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
@ -16,15 +16,13 @@ import { firstWeekdayIndex } from "../../../../common/datetime/first_weekday";
|
|||||||
import { formatTime24h } from "../../../../common/datetime/format_time";
|
import { formatTime24h } from "../../../../common/datetime/format_time";
|
||||||
import { useAmPm } from "../../../../common/datetime/use_am_pm";
|
import { useAmPm } from "../../../../common/datetime/use_am_pm";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { debounce } from "../../../../common/util/debounce";
|
|
||||||
import "../../../../components/ha-icon-picker";
|
import "../../../../components/ha-icon-picker";
|
||||||
import "../../../../components/ha-textfield";
|
import "../../../../components/ha-textfield";
|
||||||
import { Schedule, ScheduleDay, weekdays } from "../../../../data/schedule";
|
import { Schedule, ScheduleDay, weekdays } from "../../../../data/schedule";
|
||||||
import { haStyle } from "../../../../resources/styles";
|
|
||||||
import { HomeAssistant } from "../../../../types";
|
|
||||||
import { loadPolyfillIfNeeded } from "../../../../resources/resize-observer.polyfill";
|
|
||||||
import { TimeZone } from "../../../../data/translation";
|
import { TimeZone } from "../../../../data/translation";
|
||||||
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
|
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
|
||||||
|
import { haStyle } from "../../../../resources/styles";
|
||||||
|
import { HomeAssistant } from "../../../../types";
|
||||||
|
|
||||||
const defaultFullCalendarConfig: CalendarOptions = {
|
const defaultFullCalendarConfig: CalendarOptions = {
|
||||||
plugins: [timeGridPlugin, interactionPlugin],
|
plugins: [timeGridPlugin, interactionPlugin],
|
||||||
@ -70,8 +68,6 @@ class HaScheduleForm extends LitElement {
|
|||||||
|
|
||||||
private _item?: Schedule;
|
private _item?: Schedule;
|
||||||
|
|
||||||
private _resizeObserver?: ResizeObserver;
|
|
||||||
|
|
||||||
set item(item: Schedule) {
|
set item(item: Schedule) {
|
||||||
this._item = item;
|
this._item = item;
|
||||||
if (item) {
|
if (item) {
|
||||||
@ -106,41 +102,6 @@ class HaScheduleForm extends LitElement {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public connectedCallback(): void {
|
|
||||||
super.connectedCallback();
|
|
||||||
this.updateComplete.then(() => this._attachObserver());
|
|
||||||
}
|
|
||||||
|
|
||||||
public disconnectedCallback(): void {
|
|
||||||
super.disconnectedCallback();
|
|
||||||
if (this._resizeObserver) {
|
|
||||||
this._resizeObserver.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private _measureForm() {
|
|
||||||
const form = this.shadowRoot!.querySelector(".form");
|
|
||||||
if (!form) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.calendar?.updateSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
|
||||||
if (!this._resizeObserver) {
|
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
|
||||||
debounce(() => this._measureForm(), 250, false)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const form = this.shadowRoot!.querySelector(".form");
|
|
||||||
if (!form) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this._resizeObserver.observe(form);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
if (!this.hass) {
|
if (!this.hass) {
|
||||||
return nothing;
|
return nothing;
|
||||||
@ -233,13 +194,6 @@ class HaScheduleForm extends LitElement {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.calendar!.render();
|
this.calendar!.render();
|
||||||
|
|
||||||
// Update size after fully rendered to avoid a bad render in the more info
|
|
||||||
this.updateComplete.then(() =>
|
|
||||||
window.setTimeout(() => {
|
|
||||||
this.calendar!.updateSize();
|
|
||||||
}, 500)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private get _events() {
|
private get _events() {
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
PropertyValues,
|
PropertyValues,
|
||||||
nothing,
|
nothing,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, query, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { getColorByIndex } from "../../../common/color/colors";
|
import { getColorByIndex } from "../../../common/color/colors";
|
||||||
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
|
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
|
||||||
import { HASSDomEvent } from "../../../common/dom/fire_event";
|
import { HASSDomEvent } from "../../../common/dom/fire_event";
|
||||||
@ -24,7 +24,6 @@ import type {
|
|||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
} from "../../../types";
|
} from "../../../types";
|
||||||
import "../../calendar/ha-full-calendar";
|
import "../../calendar/ha-full-calendar";
|
||||||
import type { HAFullCalendar } from "../../calendar/ha-full-calendar";
|
|
||||||
import { findEntities } from "../common/find-entities";
|
import { findEntities } from "../common/find-entities";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
||||||
import "../components/hui-warning";
|
import "../components/hui-warning";
|
||||||
@ -74,8 +73,6 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
@state() private _error?: string = undefined;
|
@state() private _error?: string = undefined;
|
||||||
|
|
||||||
@query("ha-full-calendar", true) private _calendar?: HAFullCalendar;
|
|
||||||
|
|
||||||
private _startDate?: Date;
|
private _startDate?: Date;
|
||||||
|
|
||||||
private _endDate?: Date;
|
private _endDate?: Date;
|
||||||
@ -210,8 +207,6 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
|
|||||||
}
|
}
|
||||||
this._narrow = card.offsetWidth < 870;
|
this._narrow = card.offsetWidth < 870;
|
||||||
this._veryNarrow = card.offsetWidth < 350;
|
this._veryNarrow = card.offsetWidth < 350;
|
||||||
|
|
||||||
this._calendar?.updateSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user