forked from FamousWolf/week-planner-card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
354 lines (331 loc) · 15.4 KB
/
editor.js
File metadata and controls
354 lines (331 loc) · 15.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { html, LitElement } from "lit";
import styles from './editor.styles';
export class WeekPlannerCardEditor extends LitElement {
static styles = styles;
connectedCallback() {
super.connectedCallback();
this.loadCustomElements();
}
async loadCustomElements() {
if (!customElements.get("ha-entity-picker")) {
await customElements.get("hui-entities-card").getConfigElement();
}
}
static get properties() {
return {
hass: {},
_config: {},
};
}
setConfig(config) {
this._config = config;
}
render() {
if (!this.hass || !this._config) {
return html``;
}
return html`
<div style="display: flex; flex-direction: column">
${this.addTextField('title', 'Title')}
${this.addExpansionPanel(
'Calendars',
html`
${this.getConfigValue('calendars').map((calendar, index) => {
return html`
${this.addExpansionPanel(
`Calendar: ${calendar.name ?? calendar.entity}`,
html`
${this.addEntityPickerField('calendars.' + index + '.entity', 'Entity', ['calendar'])}
${this.addTextField('calendars.' + index + '.name', 'Name')}
${this.addTextField('calendars.' + index + '.color', 'Color')}
${this.addIconPickerField('calendars.' + index + '.icon', 'Icon')}
${this.addTextField('calendars.' + index + '.eventTitleField', 'Event title field', 'text', 'summary')}
${this.addTextField('calendars.' + index + '.filter', 'Filter events (regex)')}
${this.addTextField('calendars.' + index + '.filterText', 'Filter event text (regex)')}
${this.addBooleanField('calendars.' + index + '.hideInLegend', 'Hide in legend')}
${this.addButton('Remove calendar', 'mdi:trash-can', () => {
const config = Object.assign({}, this._config);
if (config.calendars.length === 1) {
config.calendars = [];
} else {
delete config.calendars[index];
}
this._config = config;
this.dispatchConfigChangedEvent();
})}
`
)}
`
})}
${this.addButton('Add calendar', 'mdi:plus', () => {
const index = this.getConfigValue('calendars').length;
this.setConfigValue('calendars.' + index, {});
})}
`
)}
${this.addExpansionPanel(
'Days',
html`
${this.addTextField('days', 'Days')}
${this.addSelectField('startingDay', 'Starting day', [
{
value: 'today',
label: 'Today',
}, {
value: 'tomorrow',
label: 'Tomorrow',
}, {
value: 'yesterday',
label: 'Yesterday',
}, {
value: 'sunday',
label: 'Sunday',
}, {
value: 'monday',
label: 'Monday',
}, {
value: 'tuesday',
label: 'Tuesday',
}, {
value: 'wednesday',
label: 'Wednesday',
}, {
value: 'thursday',
label: 'Thursday',
}, {
value: 'friday',
label: 'Friday',
}, {
value: 'saturday',
label: 'Saturday',
}
], true)}
${this.addTextField('startingDayOffset', 'Starting day offset', 'number')}
${this.addBooleanField('hideWeekend', 'Hide weekend')}
${this.addBooleanField('hideDaysWithoutEvents', 'Hide days without events except for today')}
${this.addBooleanField('hideTodayWithoutEvents', 'Also hide today without events')}
${this.addTextField('maxDayEvents', 'Maximum number of events per day (0 is no maximum)', 'number', 0)}
`
)}
${this.addExpansionPanel(
'Events',
html`
${this.addTextField('maxEvents', 'Maximum number of events (0 is no maximum)', 'number', 0)}
${this.addBooleanField('hidePastEvents', 'Hide past events')}
${this.addTextField('filter', 'Filter events (regex)')}
${this.addTextField('filterText', 'Filter event text (regex)')}
${this.addBooleanField('combineSimilarEvents', 'Combine similar events')}
${this.addBooleanField('showTitle', 'Show title in overview', true)}
${this.addBooleanField('showDescription', 'Show description in overview')}
${this.addBooleanField('showLocation', 'Show location in overview')}
${this.addTextField('locationLink', 'Override location link base URL')}
`
)}
${this.addExpansionPanel(
'Date/time formats',
html`
<p>These formats use <a href="https://moment.github.io/luxon/#/formatting?id=table-of-tokens" target="_blank">Luxon format tokens</a></p>
${this.addTextField('locale', 'Locale')}
${this.addTextField('dateFormat', 'Date format')}
${this.addTextField('timeFormat', 'Time format')}
${this.addTextField('dayFormat', 'Override day number')}
`
)}
${this.addExpansionPanel(
'Weather',
html`
${this.addEntityPickerField('weather.entity', 'Weather entity', ['weather'])}
${this.addBooleanField('weather.showCondition', 'Show condition icon')}
${this.addBooleanField('weather.showTemperature', 'Show temperature')}
${this.addBooleanField('weather.showLowTemperature', 'Show low temperature')}
${this.addBooleanField('weather.roundTemperature', 'Round temperatures to nearest integer')}
${this.addBooleanField('weather.useTwiceDaily', 'Use twice daily if entity does not support daily')}
`
)}
${this.addExpansionPanel(
'Override columns',
html`
<p>The number of columns is based on the size of the card.</p>
${this.addTextField('columns.extraLarge', 'Extra large (>= 1920px)', 'number')}
${this.addTextField('columns.large', 'Large (>= 1280px)', 'number')}
${this.addTextField('columns.medium', 'Medium (>= 1024px)', 'number')}
${this.addTextField('columns.small', 'Small (>= 640px)', 'number')}
${this.addTextField('columns.extraSmall', 'Extra small (< 640px)', 'number')}
`
)}
${this.addExpansionPanel(
'Appearance',
html`
${this.addBooleanField('noCardBackground', 'No card background')}
${this.addTextField('eventBackground', 'Override events background color')}
${this.addBooleanField('compact', 'Compact mode')}
`
)}
${this.addExpansionPanel(
'Legend',
html`
${this.addBooleanField('showLegend', 'Show legend')}
${this.addBooleanField('legendToggle', 'Toggle calendars by clicking on the legend')}
`
)}
${this.addExpansionPanel(
'Texts',
html`
${this.addTextField('texts.fullDay', 'Entire day')}
${this.addTextField('texts.noEvents', 'No events')}
${this.addTextField('texts.moreEvents', 'More events')}
${this.addTextField('texts.today', 'Today')}
${this.addTextField('texts.tomorrow', 'Tomorrow')}
${this.addTextField('texts.yesterday', 'Yesterday')}
${this.addTextField('texts.sunday', 'Sunday')}
${this.addTextField('texts.monday', 'Monday')}
${this.addTextField('texts.tuesday', 'Tuesday')}
${this.addTextField('texts.wednesday', 'Wednesday')}
${this.addTextField('texts.thursday', 'Thursday')}
${this.addTextField('texts.friday', 'Friday')}
${this.addTextField('texts.saturday', 'Saturday')}
`
)}
${this.addExpansionPanel(
'Miscellaneous',
html`
${this.addTextField('updateInterval', 'Override update interval', 'number')}
`
)}
</div>
`;
}
addTextField(name, label, type, defaultValue) {
return html`
<ha-textfield
name="${name}"
label="${label ?? name}"
type="${type ?? 'text'}"
value="${this.getConfigValue(name, defaultValue)}"
@keyup="${this._valueChanged}"
@change="${this._valueChanged}"
/>
`;
}
addEntityPickerField(name, label, includeDomains, defaultValue) {
return html`
<ha-entity-picker
.hass="${this.hass}"
name="${name}"
label="${label ?? name}"
value="${this.getConfigValue(name, defaultValue)}"
.includeDomains="${includeDomains}"
@change="${this._valueChanged}"
/>
`;
}
addIconPickerField(name, label, defaultValue) {
return html`
<ha-icon-picker
.hass="${this.hass}"
name="${name}"
label="${label ?? name}"
value="${this.getConfigValue(name, defaultValue)}"
@change="${this._valueChanged}"
/>
`;
}
addSelectField(name, label, options, clearable, defaultValue) {
return html`
<ha-select
name="${name}"
label="${label ?? name}"
value="${this.getConfigValue(name, defaultValue)}"
.clearable="${clearable}"
@change="${this._valueChanged}"
@closed="${(event) => { event.stopPropagation(); } /* Prevent a bug where the editor dialog also closes. See https://github.com/material-components/material-web/issues/1150 */}"
>
${options.map((option) => {
return html`
<mwc-list-item
value="${option.value}"
>${option.label ?? option.value}</mwc-list-item>
`;
})}
</ha-select>
`;
}
addBooleanField(name, label, defaultValue) {
return html`
<ha-formfield
label="${label ?? name}"
>
<ha-switch
name="${name}"
.checked="${this.getConfigValue(name, defaultValue)}"
value="true"
@change="${this._valueChanged}"
/>
</ha-formfield>
`;
}
addExpansionPanel(header, content, expanded) {
return html`
<ha-expansion-panel
header="${header}"
.expanded="${expanded ?? false}"
outlined="true"
>
<div style="display: flex; flex-direction: column">
${content}
</div>
</ha-expansion-panel>
`;
}
addButton(text, icon, clickFunction) {
return html`
<ha-button
@click="${clickFunction}"
>
<ha-icon icon="${icon}"></ha-icon>
${text}
</ha-button>
`;
}
_valueChanged(event) {
const target = event.target;
let value = target.value;
if (target.tagName === 'HA-SWITCH') {
value = target.checked;
}
this.setConfigValue(target.attributes.name.value, value);
}
getConfigValue(key, defaultValue) {
if (!this._config) {
return '';
}
defaultValue = defaultValue ?? '';
return key.split('.').reduce((o, i) => o[i] ?? defaultValue, this._config) ?? defaultValue;
}
setConfigValue(key, value) {
const config = Object.assign({}, this._config);
const keyParts = key.split('.');
const lastKeyPart = keyParts.pop();
const lastObject = keyParts.reduce((objectPart, keyPart) => {
if (!objectPart[keyPart]) {
objectPart[keyPart] = {};
}
return objectPart[keyPart];
}, config);
if (value === '') {
delete lastObject[lastKeyPart];
} else {
lastObject[lastKeyPart] = value;
}
this._config = config;
this.dispatchConfigChangedEvent();
}
dispatchConfigChangedEvent() {
const configChangedEvent = new CustomEvent("config-changed", {
detail: { config: this._config },
bubbles: true,
composed: true,
});
this.dispatchEvent(configChangedEvent);
}
}