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
This commit is contained in:
Bram Kragten 2020-03-20 17:53:30 +01:00 committed by GitHub
parent c54f2b66da
commit 15a144f17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 19 deletions

View File

@ -13,6 +13,7 @@ import "../../../components/ha-card";
import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCard, LovelaceCardEditor } from "../types";
import { styleMap } from "lit-html/directives/style-map"; import { styleMap } from "lit-html/directives/style-map";
import { IframeCardConfig } from "./types"; import { IframeCardConfig } from "./types";
import parseAspectRatio from "../../../common/util/parse-aspect-ratio";
@customElement("hui-iframe-card") @customElement("hui-iframe-card")
export class HuiIframeCard extends LitElement implements LovelaceCard { export class HuiIframeCard extends LitElement implements LovelaceCard {
@ -29,7 +30,8 @@ export class HuiIframeCard extends LitElement implements LovelaceCard {
aspect_ratio: "50%", aspect_ratio: "50%",
}; };
} }
@property({ type: Boolean, reflect: true })
public isPanel = false;
@property() protected _config?: IframeCardConfig; @property() protected _config?: IframeCardConfig;
public getCardSize(): number { public getCardSize(): number {
@ -55,14 +57,22 @@ export class HuiIframeCard extends LitElement implements LovelaceCard {
return html``; 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` return html`
<ha-card .header="${this._config.title}"> <ha-card .header="${this._config.title}">
<div <div
id="root" id="root"
style="${styleMap({ style="${styleMap({
"padding-top": aspectRatio, "padding-top": padding,
})}" })}"
> >
<iframe src="${this._config.url}"></iframe> <iframe src="${this._config.url}"></iframe>
@ -73,6 +83,11 @@ export class HuiIframeCard extends LitElement implements LovelaceCard {
static get styles(): CSSResult { static get styles(): CSSResult {
return css` return css`
:host([ispanel]) ha-card {
width: 100%;
height: 100%;
}
ha-card { ha-card {
overflow: hidden; overflow: hidden;
} }
@ -82,6 +97,10 @@ export class HuiIframeCard extends LitElement implements LovelaceCard {
position: relative; position: relative;
} }
:host([ispanel]) #root {
height: 100%;
}
iframe { iframe {
position: absolute; position: absolute;
border: none; border: none;

View File

@ -456,15 +456,8 @@ class HuiMapCard extends LitElement implements LovelaceCard {
static get styles(): CSSResult { static get styles(): CSSResult {
return css` return css`
:host([ispanel]) ha-card { :host([ispanel]) ha-card {
left: 0;
top: 0;
width: 100%; 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%; height: 100%;
position: absolute;
} }
ha-card { ha-card {

View File

@ -81,11 +81,10 @@ export class HuiIframeCardEditor extends LitElement
)} (${this.hass.localize( )} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional" "ui.panel.lovelace.editor.card.config.optional"
)})" )})"
.value="${this._aspect_ratio.replace("%", "")}" .value="${this._aspect_ratio}"
.configValue="${"aspect_ratio"}" .configValue="${"aspect_ratio"}"
@value-changed="${this._valueChanged}" @value-changed="${this._valueChanged}"
><div slot="suffix">%</div></paper-input ></paper-input>
>
</div> </div>
</div> </div>
`; `;
@ -96,17 +95,13 @@ export class HuiIframeCardEditor extends LitElement
return; return;
} }
const target = ev.target! as EditorTarget; const target = ev.target! as EditorTarget;
let value = target.value; const value = target.value;
if (target.configValue! === "aspect_ratio" && target.value) {
value += "%";
}
if (this[`_${target.configValue}`] === value) { if (this[`_${target.configValue}`] === value) {
return; return;
} }
if (target.configValue) { if (target.configValue) {
if (target.value === "") { if (value === "") {
delete this._config[target.configValue!]; delete this._config[target.configValue!];
} else { } else {
this._config = { ...this._config, [target.configValue!]: value }; this._config = { ...this._config, [target.configValue!]: value };