Fix broken config switch in demo (#20971)

pull/20970/head
Paul Bottein 2024-06-03 13:37:32 +02:00 committed by GitHub
parent e7f3393ec6
commit 3a855f95ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 12 deletions

View File

@ -19,15 +19,15 @@ export const mockLovelace = (
hass.mockWS("lovelace/resources", () => Promise.resolve([])); hass.mockWS("lovelace/resources", () => Promise.resolve([]));
}; };
customElements.whenDefined("hui-view").then(() => { customElements.whenDefined("hui-card").then(() => {
// eslint-disable-next-line // eslint-disable-next-line
const HUIView = customElements.get("hui-view"); const HUIView = customElements.get("hui-card");
// Patch HUI-VIEW to make the lovelace object available to the demo card // Patch HUI-VIEW to make the lovelace object available to the demo card
const oldCreateCard = HUIView!.prototype.createCardElement; const oldCreateCard = HUIView!.prototype.createElement;
HUIView!.prototype.createCardElement = function (config) { HUIView!.prototype.createElement = function (config) {
const el = oldCreateCard.call(this, config); const el = oldCreateCard.call(this, config);
if (el.tagName === "HA-DEMO-CARD") { if (config.type === "custom:ha-demo-card") {
(el as HADemoCard).lovelace = this.lovelace; (el as HADemoCard).lovelace = this.lovelace;
} }
return el; return el;

View File

@ -59,14 +59,20 @@ export class HuiCard extends ReactiveElement {
return configOptions; return configOptions;
} }
// Public to make demo happy
public createElement(config: LovelaceCardConfig) {
const element = createCardElement(config) as LovelaceCard;
element.hass = this.hass;
element.editMode = this.lovelace.editMode;
return element;
}
public setConfig(config: LovelaceCardConfig): void { public setConfig(config: LovelaceCardConfig): void {
if (this._config === config) { if (this._config === config) {
return; return;
} }
this._config = config; this._config = config;
this._element = createCardElement(config); this._element = this.createElement(config);
this._element.hass = this.hass;
this._element.editMode = this.lovelace.editMode;
while (this.lastChild) { while (this.lastChild) {
this.removeChild(this.lastChild); this.removeChild(this.lastChild);

View File

@ -74,8 +74,7 @@ export class HUIView extends ReactiveElement {
private _viewConfigTheme?: string; private _viewConfigTheme?: string;
// Public to make demo happy private _createCardElement(cardConfig: LovelaceCardConfig) {
public createCardElement(cardConfig: LovelaceCardConfig) {
const element = document.createElement("hui-card"); const element = document.createElement("hui-card");
element.hass = this.hass; element.hass = this.hass;
element.lovelace = this.lovelace; element.lovelace = this.lovelace;
@ -371,7 +370,7 @@ export class HUIView extends ReactiveElement {
} }
this._cards = config.cards.map((cardConfig) => { this._cards = config.cards.map((cardConfig) => {
const element = this.createCardElement(cardConfig); const element = this._createCardElement(cardConfig);
return element; return element;
}); });
} }
@ -393,7 +392,7 @@ export class HUIView extends ReactiveElement {
cardElToReplace: HuiCard, cardElToReplace: HuiCard,
config: LovelaceCardConfig config: LovelaceCardConfig
): void { ): void {
const newCardEl = this.createCardElement(config); const newCardEl = this._createCardElement(config);
if (cardElToReplace.parentElement) { if (cardElToReplace.parentElement) {
cardElToReplace.parentElement!.replaceChild(newCardEl, cardElToReplace); cardElToReplace.parentElement!.replaceChild(newCardEl, cardElToReplace);
} }