Migrate card preview to UpdatingElement (#5884)

This commit is contained in:
Bram Kragten 2020-05-15 09:19:24 +02:00 committed by GitHub
parent 5503853445
commit b61cf60faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 71 deletions

View File

@ -248,19 +248,12 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
} }
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
if (changedProps.has("_setTemp")) {
return true;
}
return hasConfigOrEntityChanged(this, changedProps); return hasConfigOrEntityChanged(this, changedProps);
} }
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
super.updated(changedProps); super.updated(changedProps);
if (changedProps.has("_setTemp")) {
this.rescale_svg();
}
if ( if (
!this._config || !this._config ||
!this.hass || !this.hass ||
@ -283,23 +276,18 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
applyThemesOnElement(this, this.hass.themes, this._config.theme); applyThemesOnElement(this, this.hass.themes, this._config.theme);
} }
const stateObj = this.hass!.states[this._config!.entity]; const stateObj = this.hass.states[this._config.entity];
if (!stateObj) { if (!stateObj) {
return; return;
} }
const newTemp = this._getSetTemp(stateObj);
if ( if (!oldHass || oldHass.states[this._config.entity] !== stateObj) {
Array.isArray(this._setTemp) && this._setTemp = this._getSetTemp(stateObj);
Array.isArray(newTemp) && this._rescale_svg();
(this._setTemp[0] !== newTemp[0] || this._setTemp[1] !== newTemp[1])
) {
this._setTemp = newTemp;
} else if (this._setTemp !== newTemp) {
this._setTemp = newTemp;
} }
} }
private rescale_svg() { private _rescale_svg() {
// Set the viewbox of the SVG containing the set temperature to perfectly // Set the viewbox of the SVG containing the set temperature to perfectly
// fit the text // fit the text
// That way it will auto-scale correctly // That way it will auto-scale correctly

View File

@ -4,16 +4,16 @@ import { LovelaceCardConfig } from "../../../../data/lovelace";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { createCardElement } from "../../create-element/create-card-element"; import { createCardElement } from "../../create-element/create-card-element";
import { LovelaceCard } from "../../types"; import { LovelaceCard } from "../../types";
import { ConfigError } from "../types";
import { createErrorCardConfig } from "../../create-element/create-element-base"; import { createErrorCardConfig } from "../../create-element/create-element-base";
import { property, PropertyValues, UpdatingElement } from "lit-element";
export class HuiCardPreview extends HTMLElement { export class HuiCardPreview extends UpdatingElement {
private _hass?: HomeAssistant; @property() public hass?: HomeAssistant;
@property() public config?: LovelaceCardConfig;
private _element?: LovelaceCard; private _element?: LovelaceCard;
private _config?: LovelaceCardConfig;
private get _error() { private get _error() {
return this._element?.tagName === "HUI-ERROR-CARD"; return this._element?.tagName === "HUI-ERROR-CARD";
} }
@ -22,59 +22,60 @@ export class HuiCardPreview extends HTMLElement {
super(); super();
this.addEventListener("ll-rebuild", () => { this.addEventListener("ll-rebuild", () => {
this._cleanup(); this._cleanup();
if (this._config) { if (this.config) {
this.config = this._config; this._createCard(this.config);
} }
}); });
} }
set hass(hass: HomeAssistant) { protected update(changedProperties: PropertyValues) {
if (!this._hass || this._hass.language !== hass.language) { super.update(changedProperties);
this.style.direction = computeRTL(hass) ? "rtl" : "ltr";
}
this._hass = hass; if (changedProperties.has("config")) {
if (this._element) { const oldConfig = changedProperties.get("config") as
this._element.hass = hass; | undefined
} | LovelaceCardConfig;
}
set error(error: ConfigError) { if (!this.config) {
this._createCard( this._cleanup();
createErrorCardConfig(`${error.type}: ${error.message}`, undefined) return;
); }
}
if (!this.config.type) {
set config(configValue: LovelaceCardConfig) { this._createCard(
const curConfig = this._config; createErrorCardConfig("No card type found", this.config)
this._config = configValue; );
return;
if (!configValue) { }
this._cleanup();
return; if (!this._element) {
} this._createCard(this.config);
return;
if (!configValue.type) { }
this._createCard(
createErrorCardConfig("No card type found", configValue) // in case the element was an error element we always want to recreate it
); if (!this._error && oldConfig && this.config.type === oldConfig.type) {
return; try {
} this._element.setConfig(this.config);
} catch (err) {
if (!this._element) { this._createCard(createErrorCardConfig(err.message, this.config));
this._createCard(configValue); }
return; } else {
} this._createCard(this.config);
}
// in case the element was an error element we always want to recreate it }
if (!this._error && curConfig && configValue.type === curConfig.type) {
try { if (changedProperties.has("hass")) {
this._element.setConfig(configValue); const oldHass = changedProperties.get("hass") as
} catch (err) { | HomeAssistant
this._createCard(createErrorCardConfig(err.message, configValue)); | undefined;
if (!oldHass || oldHass.language !== this.hass!.language) {
this.style.direction = computeRTL(this.hass!) ? "rtl" : "ltr";
}
if (this._element) {
this._element.hass = this.hass;
} }
} else {
this._createCard(configValue);
} }
} }
@ -82,8 +83,8 @@ export class HuiCardPreview extends HTMLElement {
this._cleanup(); this._cleanup();
this._element = createCardElement(configValue); this._element = createCardElement(configValue);
if (this._hass) { if (this.hass) {
this._element!.hass = this._hass; this._element!.hass = this.hass;
} }
this.appendChild(this._element!); this.appendChild(this._element!);

View File

@ -25,6 +25,7 @@ import type { ConfigChangedEvent, HuiCardEditor } from "./hui-card-editor";
import "./hui-card-picker"; import "./hui-card-picker";
import "./hui-card-preview"; import "./hui-card-preview";
import type { EditCardDialogParams } from "./show-edit-card-dialog"; import type { EditCardDialogParams } from "./show-edit-card-dialog";
import "@polymer/paper-dialog-scrollable/paper-dialog-scrollable";
declare global { declare global {
// for fire event // for fire event