Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/panels/lovelace/cards/hui-conditional-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createCardElement } from "../create-element/create-card-element";
import { LovelaceCard, LovelaceCardEditor } from "../types";
import { computeCardSize } from "../common/compute-card-size";
import { ConditionalCardConfig } from "./types";
import { LovelaceCardConfig } from "../../../data/lovelace";

@customElement("hui-conditional-card")
class HuiConditionalCard extends HuiConditionalBase implements LovelaceCard {
Expand All @@ -19,7 +20,8 @@ class HuiConditionalCard extends HuiConditionalBase implements LovelaceCard {
return {
type: "conditional",
conditions: [],
card: { type: "" },
// @ts-ignore
card: {},
};
}

Expand All @@ -30,17 +32,35 @@ class HuiConditionalCard extends HuiConditionalBase implements LovelaceCard {
throw new Error("No card configured.");
}

if (this._element && this._element.parentElement) {
this.removeChild(this._element);
}

this._element = createCardElement(config.card) as LovelaceCard;
this.appendChild(this._element);
this._element = this._createCardElement(config.card);
}

public getCardSize(): number {
return computeCardSize(this._element as LovelaceCard);
}

private _createCardElement(cardConfig: LovelaceCardConfig) {
const element = createCardElement(cardConfig) as LovelaceCard;
if (this.hass) {
element.hass = this.hass;
}
element.addEventListener(
"ll-rebuild",
(ev) => {
ev.stopPropagation();
this._rebuildCard(cardConfig);
},
{ once: true }
);
return element;
}

private _rebuildCard(config: LovelaceCardConfig): void {
this._element = this._createCardElement(config);
if (this.lastChild) {
this.replaceChild(this._element, this.lastChild);
}
}
}

declare global {
Expand Down
10 changes: 10 additions & 0 deletions src/panels/lovelace/cards/hui-entity-filter-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { evaluateFilter } from "../common/evaluate-filter";

class EntityFilterCard extends HTMLElement implements LovelaceCard {
public isPanel?: boolean;
private _editMode = false;
private _element?: LovelaceCard;
private _config?: EntityFilterCardConfig;
private _configEntities?: EntityFilterEntityConfig[];
Expand Down Expand Up @@ -51,6 +52,14 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard {
}
}

set editMode(editMode: boolean) {
this._editMode = editMode;
if (!this._element) {
return;
}
this._element.editMode = editMode;
}

set hass(hass: HomeAssistant) {
if (!hass || !this._config) {
return;
Expand Down Expand Up @@ -114,6 +123,7 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard {
}

element.isPanel = this.isPanel;
element.editMode = this._editMode;
element.hass = hass;
}

Expand Down
35 changes: 20 additions & 15 deletions src/panels/lovelace/cards/hui-stack-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CSSResult,
css,
property,
PropertyValues,
} from "lit-element";

import { createCardElement } from "../create-element/create-card-element";
Expand All @@ -25,21 +26,10 @@ export abstract class HuiStackCard extends LitElement implements LovelaceCard {
return { cards: [] };
}

@property() public hass?: HomeAssistant;
@property() public editMode?: boolean;
@property() protected _cards?: LovelaceCard[];
@property() private _config?: StackCardConfig;
private _hass?: HomeAssistant;

set hass(hass: HomeAssistant) {
this._hass = hass;

if (!this._cards) {
return;
}

for (const element of this._cards) {
element.hass = this._hass;
}
}

public getCardSize(): number {
return 1;
Expand All @@ -56,6 +46,21 @@ export abstract class HuiStackCard extends LitElement implements LovelaceCard {
});
}

protected updated(changedProps: PropertyValues) {
super.updated(changedProps);
if (
!this._cards ||
(!changedProps.has("hass") && !changedProps.has("editMode"))
) {
return;
}

for (const element of this._cards) {
element.hass = this.hass;
element.editMode = this.editMode;
}
}

protected render(): TemplateResult {
if (!this._config || !this._cards) {
return html``;
Expand Down Expand Up @@ -87,8 +92,8 @@ export abstract class HuiStackCard extends LitElement implements LovelaceCard {

private _createCardElement(cardConfig: LovelaceCardConfig) {
const element = createCardElement(cardConfig) as LovelaceCard;
if (this._hass) {
element.hass = this._hass;
if (this.hass) {
element.hass = this.hass;
}
element.addEventListener(
"ll-rebuild",
Expand Down
18 changes: 14 additions & 4 deletions src/panels/lovelace/components/hui-conditional-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ConditionalCardConfig } from "../cards/types";
@customElement("hui-conditional-base")
export class HuiConditionalBase extends UpdatingElement {
@property() public hass?: HomeAssistant;
@property() public editMode?: boolean;
@property() protected _config?: ConditionalCardConfig | ConditionalRowConfig;
protected _element?: LovelaceCard | LovelaceRow;

Expand All @@ -30,22 +31,31 @@ export class HuiConditionalBase extends UpdatingElement {
throw new Error("Conditions are invalid.");
}

if (this.lastChild) {
this.removeChild(this.lastChild);
}

this._config = config;
this.style.display = "none";
}

protected update(): void {
if (!this._element || !this.hass || !this._config) {
return;
}

const visible = checkConditionsMet(this._config.conditions, this.hass);
this._element.editMode = this.editMode;

const visible =
this.editMode || checkConditionsMet(this._config.conditions, this.hass);

this.style.setProperty("display", visible ? "" : "none");

if (visible) {
this._element.hass = this.hass;
if (!this._element.parentElement) {
this.appendChild(this._element);
}
}

this.style.setProperty("display", visible ? "" : "none");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "@polymer/paper-tabs";
import { struct } from "../../common/structs/struct";
import { HomeAssistant } from "../../../../types";
import { LovelaceCardEditor } from "../../types";
import { StackCardConfig } from "../../cards/types";
import { ConditionalCardConfig } from "../../cards/types";
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
import { LovelaceConfig } from "../../../../data/lovelace";

Expand Down Expand Up @@ -41,13 +41,13 @@ export class HuiConditionalCardEditor extends LitElement
implements LovelaceCardEditor {
@property() public hass?: HomeAssistant;
@property() public lovelace?: LovelaceConfig;
@property() private _config?: StackCardConfig;
@property() private _config?: ConditionalCardConfig;
@property() private _GUImode = true;
@property() private _guiModeAvailable? = true;
@property() private _cardTab: boolean = false;
@query("hui-card-editor") private _cardEditorEl?: HuiCardEditor;

public setConfig(config: StackCardConfig): void {
public setConfig(config: ConditionalCardConfig): void {
this._config = cardConfigStruct(config);
}

Expand Down Expand Up @@ -75,7 +75,7 @@ export class HuiConditionalCardEditor extends LitElement
${this._cardTab
? html`
<div class="card">
${this._config.card.type
${this._config.card.type !== undefined
? html`
<div class="card-options">
<mwc-button
Expand Down Expand Up @@ -225,6 +225,7 @@ export class HuiConditionalCardEditor extends LitElement
if (!this._config) {
return;
}
// @ts-ignore
this._config.card = {};
fireEvent(this, "config-changed", { config: this._config });
}
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/entity-rows/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type LovelaceRowConfig =

export interface LovelaceRow extends HTMLElement {
hass?: HomeAssistant;
editMode?: boolean;
setConfig(config: LovelaceRowConfig);
}

Expand Down
5 changes: 0 additions & 5 deletions src/panels/lovelace/special-rows/hui-conditional-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ class HuiConditionalRow extends HuiConditionalBase implements LovelaceRow {
throw new Error("No row configured.");
}

if (this._element && this._element.parentElement) {
this.removeChild(this._element);
}

this._element = createRowElement(config.row) as LovelaceRow;
this.appendChild(this._element);
}
}

Expand Down