From 3a196203c3ddf1c723109475a2d4a0b255aeb95e Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Thu, 18 Oct 2018 08:37:01 -0500 Subject: [PATCH] Convert hui-iframe-card to LitElement/TypeScript (#1801) * Convert hui-iframe-card to LitElement/TypeScript * style cleanup * Address review comments * Addressed review comments --- src/panels/lovelace/cards/hui-iframe-card.js | 58 ------------- src/panels/lovelace/cards/hui-iframe-card.ts | 81 +++++++++++++++++++ .../lovelace/common/create-card-element.js | 2 +- 3 files changed, 82 insertions(+), 59 deletions(-) delete mode 100644 src/panels/lovelace/cards/hui-iframe-card.js create mode 100644 src/panels/lovelace/cards/hui-iframe-card.ts diff --git a/src/panels/lovelace/cards/hui-iframe-card.js b/src/panels/lovelace/cards/hui-iframe-card.js deleted file mode 100644 index de2073b746..0000000000 --- a/src/panels/lovelace/cards/hui-iframe-card.js +++ /dev/null @@ -1,58 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; - -class HuiIframeCard extends PolymerElement { - static get template() { - return html` - - -
- -
-
- `; - } - - static get properties() { - return { - _config: Object, - }; - } - - ready() { - super.ready(); - if (this._config) this._buildConfig(); - } - - setConfig(config) { - this._config = config; - if (this.$) this._buildConfig(); - } - - _buildConfig() { - const config = this._config; - - this.$.root.style.paddingTop = config.aspect_ratio || "50%"; - } - - getCardSize() { - return 1 + this.offsetHeight / 50; - } -} -customElements.define("hui-iframe-card", HuiIframeCard); diff --git a/src/panels/lovelace/cards/hui-iframe-card.ts b/src/panels/lovelace/cards/hui-iframe-card.ts new file mode 100644 index 0000000000..ef79c0ce71 --- /dev/null +++ b/src/panels/lovelace/cards/hui-iframe-card.ts @@ -0,0 +1,81 @@ +import { html, LitElement } from "@polymer/lit-element"; + +import "../../../components/ha-card.js"; + +import { HassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; +import { LovelaceCard, LovelaceConfig } from "../types.js"; + +interface Config extends LovelaceConfig { + aspect_ratio?: string; + title?: string; + url: string; +} + +export class HuiIframeCard extends HassLocalizeLitMixin(LitElement) + implements LovelaceCard { + protected config?: Config; + + static get properties() { + return { + config: {}, + }; + } + + public getCardSize() { + return 1 + this.offsetHeight / 50; + } + + public setConfig(config: Config) { + if (!config.url) { + throw new Error("URL required"); + } + + this.config = config; + } + + protected render() { + if (!this.config) { + return html``; + } + + return html` + ${this.renderStyle()} + +
+ +
+
+ `; + } + + private renderStyle() { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-iframe-card": HuiIframeCard; + } +} + +customElements.define("hui-iframe-card", HuiIframeCard); diff --git a/src/panels/lovelace/common/create-card-element.js b/src/panels/lovelace/common/create-card-element.js index c474e36d04..a29faf1b9b 100644 --- a/src/panels/lovelace/common/create-card-element.js +++ b/src/panels/lovelace/common/create-card-element.js @@ -9,7 +9,7 @@ import "../cards/hui-error-card.js"; import "../cards/hui-glance-card.ts"; import "../cards/hui-history-graph-card.js"; import "../cards/hui-horizontal-stack-card.js"; -import "../cards/hui-iframe-card.js"; +import "../cards/hui-iframe-card.ts"; import "../cards/hui-map-card.js"; import "../cards/hui-markdown-card.js"; import "../cards/hui-media-control-card.js";