Fix for when the preview element was an error element (#4969)

* Fix for when the preview element was an error element

* Comments

* Update hui-dialog-edit-card.ts
This commit is contained in:
Bram Kragten 2020-02-25 21:51:07 +01:00 committed by GitHub
parent 8a61442cf2
commit 25d6427aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 18 deletions

View File

@ -13,7 +13,7 @@ import { LovelaceCard } from "../types";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { ErrorCardConfig } from "./types"; import { ErrorCardConfig } from "./types";
export const createErrorCardElement = (config) => { export const createErrorCardElement = (config: ErrorCardConfig) => {
const el = document.createElement("hui-error-card"); const el = document.createElement("hui-error-card");
el.setConfig(config); el.setConfig(config);
return el; return el;
@ -46,7 +46,11 @@ export class HuiErrorCard extends LitElement implements LovelaceCard {
return html` return html`
${this._config.error} ${this._config.error}
${this._config.origConfig
? html`
<pre>${safeDump(this._config.origConfig)}</pre> <pre>${safeDump(this._config.origConfig)}</pre>
`
: ""}
`; `;
} }

View File

@ -1,7 +1,5 @@
import "@polymer/paper-input/paper-textarea"; import "@polymer/paper-input/paper-textarea";
import deepClone from "deep-clone-simple";
import { createCardElement } from "../../create-element/create-card-element"; import { createCardElement } from "../../create-element/create-card-element";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { LovelaceCardConfig } from "../../../../data/lovelace"; import { LovelaceCardConfig } from "../../../../data/lovelace";
@ -15,6 +13,10 @@ export class HuiCardPreview extends HTMLElement {
private _element?: LovelaceCard; private _element?: LovelaceCard;
private _config?: LovelaceCardConfig; private _config?: LovelaceCardConfig;
private get _error() {
return this._element?.tagName === "HUI-ERROR-CARD";
}
constructor() { constructor() {
super(); super();
this.addEventListener("ll-rebuild", () => { this.addEventListener("ll-rebuild", () => {
@ -37,12 +39,9 @@ export class HuiCardPreview extends HTMLElement {
} }
set error(error: ConfigError) { set error(error: ConfigError) {
const configValue = createErrorCardConfig( this._createCard(
`${error.type}: ${error.message}`, createErrorCardConfig(`${error.type}: ${error.message}`, undefined)
undefined
); );
this._createCard(configValue);
} }
set config(configValue: LovelaceCardConfig) { set config(configValue: LovelaceCardConfig) {
@ -66,9 +65,10 @@ export class HuiCardPreview extends HTMLElement {
return; return;
} }
if (curConfig && configValue.type === curConfig.type) { // in case the element was an error element we always want to recreate it
if (!this._error && curConfig && configValue.type === curConfig.type) {
try { try {
this._element.setConfig(deepClone(configValue)); this._element.setConfig(configValue);
} catch (err) { } catch (err) {
this._createCard(createErrorCardConfig(err.message, configValue)); this._createCard(createErrorCardConfig(err.message, configValue));
} }

View File

@ -8,6 +8,8 @@ import {
property, property,
} from "lit-element"; } from "lit-element";
import deepFreeze from "deep-freeze";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { HASSDomEvent } from "../../../../common/dom/fire_event"; import { HASSDomEvent } from "../../../../common/dom/fire_event";
import { import {
@ -55,6 +57,9 @@ export class HuiDialogEditCard extends LitElement {
this._viewConfig = params.lovelaceConfig.views[view]; this._viewConfig = params.lovelaceConfig.views[view];
this._cardConfig = this._cardConfig =
card !== undefined ? this._viewConfig.cards![card] : undefined; card !== undefined ? this._viewConfig.cards![card] : undefined;
if (this._cardConfig && !Object.isFrozen(this._cardConfig)) {
this._cardConfig = deepFreeze(this._cardConfig);
}
} }
private get _cardEditorEl(): HuiCardEditor | null { private get _cardEditorEl(): HuiCardEditor | null {
@ -248,19 +253,20 @@ export class HuiDialogEditCard extends LitElement {
} }
private _handleCardPicked(ev) { private _handleCardPicked(ev) {
this._cardConfig = ev.detail.config; const config = ev.detail.config;
if (this._params!.entities && this._params!.entities.length > 0) { if (this._params!.entities && this._params!.entities.length > 0) {
if (Object.keys(this._cardConfig!).includes("entities")) { if (Object.keys(config).includes("entities")) {
this._cardConfig!.entities = this._params!.entities; config.entities = this._params!.entities;
} else if (Object.keys(this._cardConfig!).includes("entity")) { } else if (Object.keys(config).includes("entity")) {
this._cardConfig!.entity = this._params!.entities[0]; config.entity = this._params!.entities[0];
} }
} }
this._cardConfig = deepFreeze(config);
this._error = ev.detail.error; this._error = ev.detail.error;
} }
private _handleConfigChanged(ev) { private _handleConfigChanged(ev) {
this._cardConfig = ev.detail.config; this._cardConfig = deepFreeze(ev.detail.config);
this._error = ev.detail.error; this._error = ev.detail.error;
} }

View File

@ -9,6 +9,8 @@ import {
query, query,
} from "lit-element"; } from "lit-element";
import deepFreeze from "deep-freeze";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { LovelaceCardConfig } from "../../../../data/lovelace"; import { LovelaceCardConfig } from "../../../../data/lovelace";
import "./hui-card-editor"; import "./hui-card-editor";
@ -52,6 +54,9 @@ export class HuiDialogSuggestCard extends LitElement {
{}, {},
true true
); );
if (!Object.isFrozen(this._cardConfig)) {
this._cardConfig = deepFreeze(this._cardConfig);
}
if (this._dialog) { if (this._dialog) {
this._dialog.open(); this._dialog.open();
} }

View File

@ -1,5 +1,5 @@
import "@material/mwc-button"; import "@material/mwc-button";
import * as deepFreeze from "deep-freeze"; import deepFreeze from "deep-freeze";
import { import {
fetchConfig, fetchConfig,