From 15a144f17a8696533dc666e7a3c1fe0fb92f473d Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 20 Mar 2020 17:53:30 +0100 Subject: [PATCH] Add iframe panel mode and align aspect ratio option with map card (#5289) * Add iframe panel mode and align aspect ratio option with map card * lint --- src/panels/lovelace/cards/hui-iframe-card.ts | 25 ++++++++++++++++--- src/panels/lovelace/cards/hui-map-card.ts | 7 ------ .../config-elements/hui-iframe-card-editor.ts | 13 +++------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/panels/lovelace/cards/hui-iframe-card.ts b/src/panels/lovelace/cards/hui-iframe-card.ts index 7e804f0842..65882e0774 100644 --- a/src/panels/lovelace/cards/hui-iframe-card.ts +++ b/src/panels/lovelace/cards/hui-iframe-card.ts @@ -13,6 +13,7 @@ import "../../../components/ha-card"; import { LovelaceCard, LovelaceCardEditor } from "../types"; import { styleMap } from "lit-html/directives/style-map"; import { IframeCardConfig } from "./types"; +import parseAspectRatio from "../../../common/util/parse-aspect-ratio"; @customElement("hui-iframe-card") export class HuiIframeCard extends LitElement implements LovelaceCard { @@ -29,7 +30,8 @@ export class HuiIframeCard extends LitElement implements LovelaceCard { aspect_ratio: "50%", }; } - + @property({ type: Boolean, reflect: true }) + public isPanel = false; @property() protected _config?: IframeCardConfig; public getCardSize(): number { @@ -55,14 +57,22 @@ export class HuiIframeCard extends LitElement implements LovelaceCard { return html``; } - const aspectRatio = this._config.aspect_ratio || "50%"; + let padding = ""; + if (!this.isPanel && this._config.aspect_ratio) { + const ratio = parseAspectRatio(this._config.aspect_ratio); + if (ratio && ratio.w > 0 && ratio.h > 0) { + padding = `${((100 * ratio.h) / ratio.w).toFixed(2)}%`; + } + } else if (!this.isPanel) { + padding = "50%"; + } return html`
@@ -73,6 +83,11 @@ export class HuiIframeCard extends LitElement implements LovelaceCard { static get styles(): CSSResult { return css` + :host([ispanel]) ha-card { + width: 100%; + height: 100%; + } + ha-card { overflow: hidden; } @@ -82,6 +97,10 @@ export class HuiIframeCard extends LitElement implements LovelaceCard { position: relative; } + :host([ispanel]) #root { + height: 100%; + } + iframe { position: absolute; border: none; diff --git a/src/panels/lovelace/cards/hui-map-card.ts b/src/panels/lovelace/cards/hui-map-card.ts index ad1269d974..d59be48059 100644 --- a/src/panels/lovelace/cards/hui-map-card.ts +++ b/src/panels/lovelace/cards/hui-map-card.ts @@ -456,15 +456,8 @@ class HuiMapCard extends LitElement implements LovelaceCard { static get styles(): CSSResult { return css` :host([ispanel]) ha-card { - left: 0; - top: 0; width: 100%; - /** - * In panel mode we want a full height map. Since parent #view - * only sets min-height, we need absolute positioning here - */ height: 100%; - position: absolute; } ha-card { 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 index 913e1705a5..dab2a372de 100644 --- a/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts @@ -81,11 +81,10 @@ export class HuiIframeCardEditor extends LitElement )} (${this.hass.localize( "ui.panel.lovelace.editor.card.config.optional" )})" - .value="${this._aspect_ratio.replace("%", "")}" + .value="${this._aspect_ratio}" .configValue="${"aspect_ratio"}" @value-changed="${this._valueChanged}" - >
%
+ >
`; @@ -96,17 +95,13 @@ export class HuiIframeCardEditor extends LitElement return; } const target = ev.target! as EditorTarget; - let value = target.value; - - if (target.configValue! === "aspect_ratio" && target.value) { - value += "%"; - } + const value = target.value; if (this[`_${target.configValue}`] === value) { return; } if (target.configValue) { - if (target.value === "") { + if (value === "") { delete this._config[target.configValue!]; } else { this._config = { ...this._config, [target.configValue!]: value };