-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathhui-calendar-card-editor.ts
More file actions
160 lines (144 loc) · 4.33 KB
/
hui-calendar-card-editor.ts
File metadata and controls
160 lines (144 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import {
array,
assert,
assign,
boolean,
object,
optional,
string,
union,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/entity/ha-entities-picker";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { CalendarCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
title: optional(union([string(), boolean()])),
initial_view: optional(string()),
schedule_day_view: optional(boolean()),
theme: optional(string()),
entities: array(string()),
})
);
const views = ["dayGridMonth", "dayGridDay", "listWeek"] as const;
@customElement("hui-calendar-card-editor")
export class HuiCalendarCardEditor
extends LitElement
implements LovelaceCardEditor
{
@property({ attribute: false }) public hass?: HomeAssistant;
@state() private _config?: CalendarCardConfig;
public setConfig(config: CalendarCardConfig): void {
assert(config, cardConfigStruct);
this._config = config;
}
private _schema = memoizeOne(
(localize: LocalizeFunc) =>
[
{
name: "",
type: "grid",
schema: [
{ name: "title", required: false, selector: { text: {} } },
{
name: "initial_view",
required: false,
selector: {
select: {
options: views.map((view) => ({
value: view,
label: localize(
`ui.panel.lovelace.editor.card.calendar.views.${view}`
),
})),
},
},
},
{
name: "schedule_day_view",
required: false,
selector: { boolean: {} },
},
],
},
{ name: "theme", required: false, selector: { theme: {} } },
] as const
);
protected render() {
if (!this.hass || !this._config) {
return nothing;
}
const schema = this._schema(this.hass.localize);
const data = { initial_view: "dayGridMonth", ...this._config };
return html`
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${schema}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
<h3>
${this.hass.localize(
"ui.panel.lovelace.editor.card.calendar.calendar_entities"
) +
" (" +
this.hass!.localize("ui.panel.lovelace.editor.card.config.required") +
")"}
</h3>
<ha-entities-picker
.hass=${this.hass!}
.value=${this._config.entities}
.includeDomains=${["calendar"]}
@value-changed=${this._entitiesChanged}
>
</ha-entities-picker>
`;
}
private _valueChanged(ev: CustomEvent): void {
const config = ev.detail.value;
fireEvent(this, "config-changed", { config });
}
private _entitiesChanged(ev): void {
const config = { ...this._config!, entities: ev.detail.value };
fireEvent(this, "config-changed", { config });
}
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
) => {
if (schema.name === "title") {
return this.hass!.localize("ui.panel.lovelace.editor.card.generic.title");
}
if (schema.name === "theme") {
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
}
return this.hass!.localize(
`ui.panel.lovelace.editor.card.calendar.${schema.name}`
);
};
static styles = css`
ha-form {
display: block;
overflow: auto;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"hui-calendar-card-editor": HuiCalendarCardEditor;
}
}