Skip to content

Commit

Permalink
Rename build to load
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Jun 27, 2024
1 parent b71888b commit 5097df0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 28 deletions.
40 changes: 18 additions & 22 deletions src/panels/lovelace/cards/hui-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export class HuiCard extends ReactiveElement {

@property({ attribute: false }) public hass?: HomeAssistant;

public build() {
public load() {
if (!this.config) {
throw new Error("Cannot build card without config");
}
this._buildElement(this.config);
this._loadElement(this.config);
}

private _element?: LovelaceCard;
Expand Down Expand Up @@ -81,43 +81,39 @@ export class HuiCard extends ReactiveElement {
return this._element?.getLayoutOptions?.() ?? {};
}

private _createElement(config: LovelaceCardConfig) {
const element = createCardElement(config);
private _loadElement(config: LovelaceCardConfig) {
this._element = createCardElement(config);
if (this.hass) {
element.hass = this.hass;
this._element.hass = this.hass;
}
element.preview = this.preview;
this._element.preview = this.preview;
// For backwards compatibility
(element as any).editMode = this.preview;
(this._element as any).editMode = this.preview;
// Update element when the visibility of the card changes (e.g. conditional card or filter card)
element.addEventListener("card-visibility-changed", (ev: Event) => {
this._element.addEventListener("card-visibility-changed", (ev: Event) => {
ev.stopPropagation();
this._updateVisibility();
});
element.addEventListener(
this._element.addEventListener(
"ll-upgrade",
(ev: Event) => {
ev.stopPropagation();
element.hass = this.hass;
if (this.hass) {
this._element!.hass = this.hass;
}
fireEvent(this, "card-updated");
},
{ once: true }
);
element.addEventListener(
this._element.addEventListener(
"ll-rebuild",
(ev: Event) => {
ev.stopPropagation();
this._buildElement(config);
this._loadElement(config);
fireEvent(this, "card-updated");
},
{ once: true }
);
return element;
}

private _buildElement(config: LovelaceCardConfig) {
this._element = this._createElement(config);

while (this.lastChild) {
this.removeChild(this.lastChild);
}
Expand All @@ -128,7 +124,7 @@ export class HuiCard extends ReactiveElement {
super.willUpdate(changedProps);

if (!this._element) {
this.build();
this.load();
}
}

Expand All @@ -141,7 +137,7 @@ export class HuiCard extends ReactiveElement {
if (this.config !== oldConfig && this.config) {
const typeChanged = this.config?.type !== oldConfig?.type;
if (typeChanged) {
this._buildElement(this.config);
this._loadElement(this.config);
} else {
this._element?.setConfig(this.config);
fireEvent(this, "card-updated");
Expand All @@ -154,7 +150,7 @@ export class HuiCard extends ReactiveElement {
this._element.hass = this.hass;
}
} catch (e: any) {
this._buildElement(createErrorCardConfig(e.message, null));
this._loadElement(createErrorCardConfig(e.message, null));
}
}
if (changedProps.has("preview")) {
Expand All @@ -163,7 +159,7 @@ export class HuiCard extends ReactiveElement {
// For backwards compatibility
(this._element as any).editMode = this.preview;
} catch (e: any) {
this._buildElement(createErrorCardConfig(e.message, null));
this._loadElement(createErrorCardConfig(e.message, null));
}
}
if (changedProps.has("isPanel")) {
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/cards/hui-conditional-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HuiConditionalCard extends HuiConditionalBase implements LovelaceCard {
element.hass = this.hass;
element.preview = this.preview;
element.config = cardConfig;
element.build();
element.load();
return element;
}

Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/cards/hui-entity-filter-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class HuiEntityFilterCard
element.hass = this.hass;
element.preview = this.preview;
element.config = cardConfig;
element.build();
element.load();
return element;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/cards/hui-stack-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export abstract class HuiStackCard<T extends StackCardConfig = StackCardConfig>
const element = document.createElement("hui-card");
element.hass = this.hass;
element.config = cardConfig;
element.build();
element.load();
return element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class HuiCardLayoutEditor extends LitElement {
this._defaultLayoutOptions =
this._cardElement?.getElementLayoutOptions();
});
this._cardElement.build();
this._cardElement.load();
this._defaultLayoutOptions = this._cardElement.getElementLayoutOptions();
} catch (err) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/sections/hui-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class HuiSection extends ReactiveElement {
ev.stopPropagation();
this._cards = [...this._cards];
});
element.build();
element.load();
return element;
}

Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/views/hui-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class HUIView extends ReactiveElement {
ev.stopPropagation();
this._cards = [...this._cards];
});
element.build();
element.load();
return element;
}

Expand Down

0 comments on commit 5097df0

Please sign in to comment.