diff --git a/src/panels/lovelace/editor/card-editor/hui-card-element-editor.ts b/src/panels/lovelace/editor/card-editor/hui-card-element-editor.ts index 7c17bddea7..6300c50e11 100644 --- a/src/panels/lovelace/editor/card-editor/hui-card-element-editor.ts +++ b/src/panels/lovelace/editor/card-editor/hui-card-element-editor.ts @@ -1,11 +1,22 @@ -import { customElement } from "lit/decorators"; +import "@polymer/paper-tabs/paper-tab"; +import "@polymer/paper-tabs/paper-tabs"; +import { CSSResultGroup, TemplateResult, css, html, nothing } from "lit"; +import { customElement, property, state } from "lit/decorators"; import { LovelaceCardConfig } from "../../../../data/lovelace/config/card"; import { getCardElementClass } from "../../create-element/create-card-element"; import type { LovelaceCardEditor, LovelaceConfigForm } from "../../types"; import { HuiElementEditor } from "../hui-element-editor"; +import "./hui-card-visibility-editor"; + +const TABS = ["config", "visibility"] as const; @customElement("hui-card-element-editor") export class HuiCardElementEditor extends HuiElementEditor { + @state() private _curTab: (typeof TABS)[number] = TABS[0]; + + @property({ type: Boolean, attribute: "show-visibility-tab" }) + public showVisibilityTab = false; + protected async getConfigElement(): Promise { const elClass = await getCardElementClass(this.configElementType!); @@ -27,6 +38,73 @@ export class HuiCardElementEditor extends HuiElementEditor { return undefined; } + + private _handleTabSelected(ev: CustomEvent): void { + if (!ev.detail.value) { + return; + } + this._curTab = ev.detail.value.id; + } + + private _configChanged(ev: CustomEvent): void { + ev.stopPropagation(); + this.value = ev.detail.value; + } + + protected renderConfigElement(): TemplateResult { + if (!this.showVisibilityTab) return super.renderConfigElement(); + + let content: TemplateResult<1> | typeof nothing = nothing; + + switch (this._curTab) { + case "config": + content = html`${super.renderConfigElement()}`; + break; + case "visibility": + content = html` + + `; + break; + } + return html` + + ${TABS.map( + (tab, index) => html` + + ${this.hass.localize( + `ui.panel.lovelace.editor.edit_card.tab-${tab}` + )} + + ` + )} + + ${content} + `; + } + + static get styles(): CSSResultGroup { + return [ + HuiElementEditor.styles, + css` + paper-tabs { + --paper-tabs-selection-bar-color: var(--primary-color); + color: var(--primary-text-color); + text-transform: uppercase; + margin-bottom: 16px; + border-bottom: 1px solid var(--divider-color); + } + `, + ]; + } } declare global { diff --git a/src/panels/lovelace/editor/card-editor/hui-card-visibility-editor.ts b/src/panels/lovelace/editor/card-editor/hui-card-visibility-editor.ts new file mode 100644 index 0000000000..9e114dae57 --- /dev/null +++ b/src/panels/lovelace/editor/card-editor/hui-card-visibility-editor.ts @@ -0,0 +1,51 @@ +import { LitElement, html } from "lit"; +import { customElement, property } from "lit/decorators"; +import { fireEvent } from "../../../../common/dom/fire_event"; +import "../../../../components/ha-alert"; +import { LovelaceCardConfig } from "../../../../data/lovelace/config/card"; +import { HomeAssistant } from "../../../../types"; +import { Condition } from "../../common/validate-condition"; +import "../conditions/ha-card-conditions-editor"; + +@customElement("hui-card-visibility-editor") +export class HuiCardVisibilityEditor extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public config!: LovelaceCardConfig; + + render() { + const conditions = this.config.visibility ?? []; + return html` + + ${this.hass.localize( + `ui.panel.lovelace.editor.edit_card.visibility.explanation` + )} + + + + `; + } + + private _valueChanged(ev: CustomEvent): void { + ev.stopPropagation(); + const conditions = ev.detail.value as Condition[]; + const newConfig: LovelaceCardConfig = { + ...this.config, + visibility: conditions, + }; + if (newConfig.visibility?.length === 0) { + delete newConfig.visibility; + } + fireEvent(this, "value-changed", { value: newConfig }); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-card-visibility-editor": HuiCardVisibilityEditor; + } +} diff --git a/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts b/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts index e437a81362..c2794e6f2c 100644 --- a/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts +++ b/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts @@ -234,6 +234,7 @@ export class HuiDialogEditCard
extends LitElement { return this.value ? (this.value as any).type : undefined; } + protected renderConfigElement(): TemplateResult { + return html`${this._configElement}`; + } + protected render(): TemplateResult { return html`
@@ -211,7 +216,7 @@ export abstract class HuiElementEditor extends LitElement { class="center margin-bot" > ` - : this._configElement} + : this.renderConfigElement()}
` : html` diff --git a/src/translations/en.json b/src/translations/en.json index ec84ebd489..23be084a37 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5518,7 +5518,12 @@ "decrease_position": "Decrease card position", "increase_position": "Increase card position", "options": "More options", - "search_cards": "Search cards" + "search_cards": "Search cards", + "tab-config": "Config", + "tab-visibility": "Visibility", + "visibility": { + "explanation": "The card will be shown when ALL conditions below are fulfilled. If no conditions are set, the card will always be shown." + } }, "move_card": { "header": "Choose a view to move the card to",