|
| 1 | +import { html, nothing } from "lit"; |
| 2 | +import { customElement, state } from "lit/decorators.js"; |
| 3 | +import { assert } from "superstruct"; |
| 4 | +import { LovelaceCardEditor, fireEvent } from "../../ha"; |
| 5 | +import setupCustomlocalize from "../../localize"; |
| 6 | +import { computeActionsFormSchema } from "../../shared/config/actions-config"; |
| 7 | +import { MushroomBaseElement } from "../../utils/base-element"; |
| 8 | +import { GENERIC_LABELS } from "../../utils/form/generic-fields"; |
| 9 | +import { HaFormSchema } from "../../utils/form/ha-form"; |
| 10 | +import { loadHaComponents } from "../../utils/loader"; |
| 11 | +import { LEGACY_TEMPLATE_CARD_EDITOR_NAME } from "./const"; |
| 12 | +import { |
| 13 | + LegacyTemplateCardConfig, |
| 14 | + legacyTemplateCardConfigStruct, |
| 15 | +} from "./legacy-template-card-config"; |
| 16 | + |
| 17 | +export const TEMPLATE_LABELS = [ |
| 18 | + "badge_icon", |
| 19 | + "badge_color", |
| 20 | + "content", |
| 21 | + "primary", |
| 22 | + "secondary", |
| 23 | + "multiline_secondary", |
| 24 | + "picture", |
| 25 | +]; |
| 26 | + |
| 27 | +const SCHEMA: HaFormSchema[] = [ |
| 28 | + { name: "entity", selector: { entity: {} } }, |
| 29 | + { |
| 30 | + name: "icon", |
| 31 | + selector: { template: {} }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "icon_color", |
| 35 | + selector: { template: {} }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "primary", |
| 39 | + selector: { template: {} }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "secondary", |
| 43 | + selector: { template: {} }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "badge_icon", |
| 47 | + selector: { template: {} }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "badge_color", |
| 51 | + selector: { template: {} }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "picture", |
| 55 | + selector: { template: {} }, |
| 56 | + }, |
| 57 | + { |
| 58 | + type: "grid", |
| 59 | + name: "", |
| 60 | + schema: [ |
| 61 | + { name: "layout", selector: { mush_layout: {} } }, |
| 62 | + { name: "fill_container", selector: { boolean: {} } }, |
| 63 | + { name: "multiline_secondary", selector: { boolean: {} } }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + ...computeActionsFormSchema(), |
| 67 | +]; |
| 68 | + |
| 69 | +@customElement(LEGACY_TEMPLATE_CARD_EDITOR_NAME) |
| 70 | +export class TemplateCardEditor |
| 71 | + extends MushroomBaseElement |
| 72 | + implements LovelaceCardEditor |
| 73 | +{ |
| 74 | + @state() private _config?: LegacyTemplateCardConfig; |
| 75 | + |
| 76 | + connectedCallback() { |
| 77 | + super.connectedCallback(); |
| 78 | + void loadHaComponents(); |
| 79 | + } |
| 80 | + |
| 81 | + public setConfig(config: LegacyTemplateCardConfig): void { |
| 82 | + assert(config, legacyTemplateCardConfigStruct); |
| 83 | + this._config = config; |
| 84 | + } |
| 85 | + |
| 86 | + private _computeLabel = (schema: HaFormSchema) => { |
| 87 | + const customLocalize = setupCustomlocalize(this.hass!); |
| 88 | + |
| 89 | + if (schema.name === "entity") { |
| 90 | + return `${this.hass!.localize( |
| 91 | + "ui.panel.lovelace.editor.card.generic.entity" |
| 92 | + )} (${customLocalize("editor.card.template.entity_extra")})`; |
| 93 | + } |
| 94 | + if (GENERIC_LABELS.includes(schema.name)) { |
| 95 | + return customLocalize(`editor.card.generic.${schema.name}`); |
| 96 | + } |
| 97 | + if (TEMPLATE_LABELS.includes(schema.name)) { |
| 98 | + return customLocalize(`editor.card.template.${schema.name}`); |
| 99 | + } |
| 100 | + return this.hass!.localize( |
| 101 | + `ui.panel.lovelace.editor.card.generic.${schema.name}` |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + protected render() { |
| 106 | + if (!this.hass || !this._config) { |
| 107 | + return nothing; |
| 108 | + } |
| 109 | + |
| 110 | + return html` |
| 111 | + <ha-form |
| 112 | + .hass=${this.hass} |
| 113 | + .data=${this._config} |
| 114 | + .schema=${SCHEMA} |
| 115 | + .computeLabel=${this._computeLabel} |
| 116 | + @value-changed=${this._valueChanged} |
| 117 | + ></ha-form> |
| 118 | + `; |
| 119 | + } |
| 120 | + |
| 121 | + private _valueChanged(ev: CustomEvent): void { |
| 122 | + fireEvent(this, "config-changed", { config: ev.detail.value }); |
| 123 | + } |
| 124 | +} |
0 commit comments