diff --git a/src/panels/lovelace/common/create-hui-element.js b/src/panels/lovelace/common/create-hui-element.js index 502a4aa17f..01bdded3ea 100644 --- a/src/panels/lovelace/common/create-hui-element.js +++ b/src/panels/lovelace/common/create-hui-element.js @@ -1,5 +1,5 @@ import "../elements/hui-icon-element"; -import "../elements/hui-image-element.js"; +import "../elements/hui-image-element"; import "../elements/hui-service-button-element"; import "../elements/hui-state-badge-element.js"; import "../elements/hui-state-icon-element.js"; diff --git a/src/panels/lovelace/elements/hui-image-element.js b/src/panels/lovelace/elements/hui-image-element.js deleted file mode 100644 index 67b67ba60f..0000000000 --- a/src/panels/lovelace/elements/hui-image-element.js +++ /dev/null @@ -1,66 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; - -import "../components/hui-image.js"; - -import ElementClickMixin from "../mixins/element-click-mixin.js"; -import { longPressBind } from "../common/directives/long-press-directive"; - -/* - * @appliesMixin ElementClickMixin - */ -class HuiImageElement extends ElementClickMixin(PolymerElement) { - static get template() { - return html` - - - `; - } - - static get properties() { - return { - hass: Object, - _config: Object, - }; - } - - ready() { - super.ready(); - longPressBind(this); - this.addEventListener("ha-click", () => - this.handleClick(this.hass, this._config, false) - ); - this.addEventListener("ha-hold", () => - this.handleClick(this.hass, this._config, true) - ); - } - - setConfig(config) { - if (!config) { - throw Error("Error in element configuration"); - } - - this.classList.toggle("clickable", config.tap_action !== "none"); - this._config = config; - } -} -customElements.define("hui-image-element", HuiImageElement); diff --git a/src/panels/lovelace/elements/hui-image-element.ts b/src/panels/lovelace/elements/hui-image-element.ts new file mode 100644 index 0000000000..1b79f5a14c --- /dev/null +++ b/src/panels/lovelace/elements/hui-image-element.ts @@ -0,0 +1,94 @@ +import { html, LitElement } from "@polymer/lit-element"; + +import "../components/hui-image.js"; + +import { computeTooltip } from "../../../common/string/compute-tooltip"; +import { handleClick } from "../../../common/dom/handle-click"; +import { longPress } from "../common/directives/long-press-directive"; +import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; +import { LovelaceElement, LovelaceElementConfig } from "./types.js"; +import { HomeAssistant } from "../../../types.js"; +import { TemplateResult } from "lit-html"; + +interface Config extends LovelaceElementConfig { + image?: string; + state_image?: string; + camera_image?: string; + filter?: string; + state_filter?: string; + aspect_ratio?: string; +} + +export class HuiImageElement extends hassLocalizeLitMixin(LitElement) + implements LovelaceElement { + public hass?: HomeAssistant; + private _config?: Config; + + static get properties() { + return { hass: {}, _config: {} }; + } + + public setConfig(config: Config): void { + if (!config) { + throw Error("Error in element configuration"); + } + + this.classList.toggle("clickable", config.tap_action !== "none"); + this._config = config; + } + + protected render(): TemplateResult { + if (!this._config) { + return html``; + } + + return html` + ${this.renderStyle()} + + `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } + + private _handleClick() { + handleClick(this, this.hass!, this._config!, false); + } + + private _handleHold() { + handleClick(this, this.hass!, this._config!, true); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-image-element": HuiImageElement; + } +} + +customElements.define("hui-image-element", HuiImageElement);