diff --git a/src/panels/lovelace/cards/hui-iframe-card.ts b/src/panels/lovelace/cards/hui-iframe-card.ts index 93a643fc53..bc67b4f8d4 100644 --- a/src/panels/lovelace/cards/hui-iframe-card.ts +++ b/src/panels/lovelace/cards/hui-iframe-card.ts @@ -2,18 +2,26 @@ import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; import "../../../components/ha-card"; -import { LovelaceCard } from "../types"; +import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCardConfig } from "../../../data/lovelace"; import { TemplateResult } from "lit-html"; import { styleMap } from "lit-html/directives/styleMap"; -interface Config extends LovelaceCardConfig { +export interface Config extends LovelaceCardConfig { aspect_ratio?: string; title?: string; url: string; } export class HuiIframeCard extends LitElement implements LovelaceCard { + public static async getConfigElement(): Promise { + await import("../editor/config-elements/hui-iframe-card-editor"); + return document.createElement("hui-iframe-card-editor"); + } + public static getStubConfig(): object { + return { url: "https://www.home-assistant.io", aspect_ratio: "50%" }; + } + protected _config?: Config; static get properties(): PropertyDeclarations { diff --git a/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts new file mode 100644 index 0000000000..fe6ba662cc --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts @@ -0,0 +1,108 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; +import "@polymer/paper-input/paper-input"; + +import { struct } from "../../common/structs/struct"; +import { EntitiesEditorEvent, EditorTarget } from "../types"; +import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin"; +import { HomeAssistant } from "../../../../types"; +import { LovelaceCardEditor } from "../../types"; +import { fireEvent } from "../../../../common/dom/fire_event"; +import { Config } from "../../cards/hui-iframe-card"; +import { configElementStyle } from "./config-elements-style"; + +const cardConfigStruct = struct({ + type: "string", + title: "string?", + url: "string?", + aspect_ratio: "string?", +}); + +export class HuiIframeCardEditor extends hassLocalizeLitMixin(LitElement) + implements LovelaceCardEditor { + public hass?: HomeAssistant; + private _config?: Config; + + public setConfig(config: Config): void { + config = cardConfigStruct(config); + + this._config = { type: "iframe", ...config }; + } + + static get properties(): PropertyDeclarations { + return { hass: {}, _config: {} }; + } + + get _title(): string { + return this._config!.title || ""; + } + + get _url(): string { + return this._config!.url || ""; + } + + get _aspect_ratio(): string { + return this._config!.aspect_ratio || ""; + } + + protected render(): TemplateResult { + if (!this.hass) { + return html``; + } + + return html` + ${configElementStyle} +
+
+ + +
+ +
+ `; + } + + private _valueChanged(ev: EntitiesEditorEvent): void { + if (!this._config || !this.hass) { + return; + } + const target = ev.target! as EditorTarget; + let value = target.value; + + if (target.configValue! === "aspect_ratio" && target.value) { + value += "%"; + } + + if (this[`_${target.configValue}`] === value) { + return; + } + if (target.configValue) { + this._config = { ...this._config, [target.configValue!]: value }; + } + fireEvent(this, "config-changed", { config: this._config }); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-iframe-card-editor": HuiIframeCardEditor; + } +} + +customElements.define("hui-iframe-card-editor", HuiIframeCardEditor);