From 454d81facce1edc62dce10d1271793254fe98a8b Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Tue, 31 Mar 2020 07:30:52 -0400 Subject: [PATCH] View Editor: Badge Preview (#5335) * Badge Preview * Move Badge preview * Clean * Error card + clean * remove try catch --- .../lovelace/editor/hui-badge-preview.ts | 95 +++++++++++++++++++ .../editor/view-editor/hui-edit-view.ts | 25 ++++- 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/panels/lovelace/editor/hui-badge-preview.ts diff --git a/src/panels/lovelace/editor/hui-badge-preview.ts b/src/panels/lovelace/editor/hui-badge-preview.ts new file mode 100644 index 0000000000..b8d0dc728d --- /dev/null +++ b/src/panels/lovelace/editor/hui-badge-preview.ts @@ -0,0 +1,95 @@ +import { HomeAssistant } from "../../../types"; +import { LovelaceBadgeConfig } from "../../../data/lovelace"; +import { ConfigError } from "./types"; +import { computeRTL } from "../../../common/util/compute_rtl"; +import { LovelaceBadge } from "../types"; +import { createBadgeElement } from "../create-element/create-badge-element"; +import { createErrorBadgeConfig } from "../badges/hui-error-badge"; + +import "../../../components/entity/ha-state-label-badge"; + +export class HuiBadgePreview extends HTMLElement { + private _hass?: HomeAssistant; + private _element?: LovelaceBadge; + private _config?: LovelaceBadgeConfig; + + private get _error() { + return this._element?.tagName === "HUI-ERROR-CARD"; + } + + constructor() { + super(); + this.addEventListener("ll-rebuild", () => { + this._cleanup(); + if (this._config) { + this.config = this._config; + } + }); + } + + set hass(hass: HomeAssistant) { + if (!this._hass || this._hass.language !== hass.language) { + this.style.direction = computeRTL(hass) ? "rtl" : "ltr"; + } + + this._hass = hass; + if (this._element) { + this._element.hass = hass; + } + } + + set error(error: ConfigError) { + this._createBadge( + createErrorBadgeConfig(`${error.type}: ${error.message}`) + ); + } + + set config(configValue: LovelaceBadgeConfig) { + const curConfig = this._config; + this._config = configValue; + + if (!configValue) { + this._cleanup(); + return; + } + + if (!this._element) { + this._createBadge(configValue); + return; + } + + // in case the element was an error element we always want to recreate it + if (!this._error && curConfig && configValue.type === curConfig.type) { + this._element.setConfig(configValue); + } else { + this._createBadge(configValue); + } + } + + private _createBadge(configValue: LovelaceBadgeConfig): void { + this._cleanup(); + this._element = createBadgeElement(configValue); + + if (this._hass) { + this._element!.hass = this._hass; + } + + this.appendChild(this._element!); + } + + private _cleanup() { + if (!this._element) { + return; + } + this.removeChild(this._element); + this._element = undefined; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-badge-preview": HuiBadgePreview; + } +} + +customElements.define("hui-badge-preview", HuiBadgePreview); diff --git a/src/panels/lovelace/editor/view-editor/hui-edit-view.ts b/src/panels/lovelace/editor/view-editor/hui-edit-view.ts index 50f54e8d18..e2cf4064fc 100644 --- a/src/panels/lovelace/editor/view-editor/hui-edit-view.ts +++ b/src/panels/lovelace/editor/view-editor/hui-edit-view.ts @@ -23,6 +23,7 @@ import { haStyleDialog } from "../../../../resources/styles"; import "../../components/hui-entity-editor"; import "./hui-view-editor"; import "./hui-view-visibility-editor"; +import "../hui-badge-preview"; import { HomeAssistant } from "../../../../types"; import { LovelaceViewConfig, @@ -123,6 +124,20 @@ export class HuiEditView extends LitElement { break; case "tab-badges": content = html` + ${this._badges?.length + ? html` +
+ ${this._badges.map((badgeConfig) => { + return html` + + `; + })} +
+ ` + : ""} - `, + .preview-badges { + display: flex; + justify-content: center; + margin: 8px 16px; + flex-wrap: wrap; + } + `, ]; } }