Skip to content

Commit 0dc7c72

Browse files
committed
Rename old template card to legacy
1 parent 60fe181 commit 0dc7c72

15 files changed

+1432
-1418
lines changed

src/cards/chips-card/chips/template-chip-editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { HaFormSchema } from "../../../utils/form/ha-form";
88
import { computeChipEditorComponentName } from "../../../utils/lovelace/chip/chip-element";
99
import { TemplateChipConfig } from "../../../utils/lovelace/chip/types";
1010
import { LovelaceChipEditor } from "../../../utils/lovelace/types";
11-
import { TEMPLATE_LABELS } from "../../template-card/template-card-editor";
11+
import { TEMPLATE_LABELS } from "../../legacy-template-card/legacy-template-card-editor";
1212

1313
const SCHEMA: HaFormSchema[] = [
1414
{ name: "entity", selector: { entity: {} } },
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PREFIX_NAME } from "../../const";
2+
3+
export const LEGACY_TEMPLATE_CARD_NAME = `${PREFIX_NAME}-legacy-template-card`;
4+
export const LEGACY_TEMPLATE_CARD_EDITOR_NAME = `${LEGACY_TEMPLATE_CARD_NAME}-editor`;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
array,
3+
assign,
4+
boolean,
5+
object,
6+
optional,
7+
string,
8+
union,
9+
} from "superstruct";
10+
import { LovelaceCardConfig } from "../../ha";
11+
import {
12+
ActionsSharedConfig,
13+
actionsSharedConfigStruct,
14+
} from "../../shared/config/actions-config";
15+
import {
16+
AppearanceSharedConfig,
17+
appearanceSharedConfigStruct,
18+
} from "../../shared/config/appearance-config";
19+
import { lovelaceCardConfigStruct } from "../../shared/config/lovelace-card-config";
20+
21+
export type LegacyTemplateCardConfig = LovelaceCardConfig &
22+
AppearanceSharedConfig &
23+
ActionsSharedConfig & {
24+
entity?: string;
25+
icon?: string;
26+
icon_color?: string;
27+
primary?: string;
28+
secondary?: string;
29+
badge_icon?: string;
30+
badge_color?: string;
31+
picture?: string;
32+
multiline_secondary?: boolean;
33+
entity_id?: string | string[];
34+
};
35+
36+
export const legacyTemplateCardConfigStruct = assign(
37+
lovelaceCardConfigStruct,
38+
assign(appearanceSharedConfigStruct, actionsSharedConfigStruct),
39+
object({
40+
entity: optional(string()),
41+
icon: optional(string()),
42+
icon_color: optional(string()),
43+
primary: optional(string()),
44+
secondary: optional(string()),
45+
badge_icon: optional(string()),
46+
badge_color: optional(string()),
47+
picture: optional(string()),
48+
multiline_secondary: optional(boolean()),
49+
entity_id: optional(union([string(), array(string())])),
50+
})
51+
);
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)