-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOccasionsCardEditor.js
133 lines (114 loc) · 4.46 KB
/
OccasionsCardEditor.js
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
import { LitElement, html, css } from 'https://unpkg.com/lit@2.2.7/index.js?module';
export class OccasionsCardEditor extends LitElement {
constructor() {
super();
this._hass = null;
this.config = { occasions: [] };
}
static get properties() {
return {
_hass: { type: Object }, // Home Assistant object
config: { type: Object }, // Configuration object
schemaBase: { type: Array }, // Schema
occasionSchema: { type: Array }, // Schema
};
}
get schemaBase() {
return [
{ name: 'title', label: this._hass.localize('ui.panel.lovelace.editor.card.generic.title'), selector: { text: { } } },
{ name: 'numberofdays', label: this._hass.localize('ui.panel.lovelace.editor.card.generic.days_to_show'), selector: { number: {} } },
];
}
get occasionSchema() {
return [
{ name: 'name', label: this._hass.localize('ui.common.name'), selector: { text: {} } },
{ name: 'date', label: this._hass.localize('ui.components.selectors.selector.types.date'), selector: { date: {} } },
{ name: 'icon', label: this._hass.localize(' ui.panel.lovelace.editor.card.generic.icon'), selector: { icon: {} } },
{ name: 'hide_count', label: this._hass.localize('ui.common.hide') + ' ' + this._hass.localize('component.counter.entity_component._.name'), selector: { boolean: {} } }
];
}
static styles = css`
div {
padding: 10px;
}
mwc-icon-button {
vertical-align: middle;
}
mwc-button.warning {
--mdc-theme-primary: red;
}
`;
setConfig(config) {
this.config = config || { occasions: [] };
if (!this.config.occasions) {
this.config.occasions = [];
}
this.render(); // Trigger re-render when config is set
}
set hass(hass) {
this._hass = hass;
this.render(); // Trigger re-render when hass is set
}
render() {
// Make sure necessary data is available
if (!this._hass || !this.config) {
return;
}
return html`
<ha-form .hass=${this._hass} .data=${this.config} .computeLabel=${this._computeLabel} .schema=${this.schemaBase} @value-changed=${this._valueChanged}></ha-form>
<div></div>
${this.config.occasions.map((occasion, index) => html`
<ha-expansion-panel outlined="true" header="${occasion.name}" icon="mdi:bug">
<div></div>
<ha-form
.hass=${this._hass}
.data=${occasion}
.computeLabel=${this._computeLabel}
.schema=${this.occasionSchema}
.key=${index}
@value-changed=${e => this._occasionChanged(e, index)}
></ha-form>
<mwc-button class="warning" @click=${() => this._deleteOccasion(index)}>${this._hass.localize('ui.components.todo.item.delete')}</mwc-button>
<div></div>
</ha-expansion-panel>
`)}
<mwc-button @click=${this._addOccasion}>${this._hass.localize('ui.components.todo.item.add')}</mwc-button>
`;
}
_computeLabel(event, data) {
if(event.label)
return event.label;
return event.name;
}
_occasionChanged(event, index) {
const newValue = event.detail.value;
const newOccasions = [...this.config.occasions];
newOccasions[index] = newValue;
this.config = { ...this.config, occasions: newOccasions };
this._requestUpdate();
}
_valueChanged(event) {
const updatedValue = event.detail.value;
this.config = { ...this.config, ...updatedValue };
this._requestUpdate();
}
_requestUpdate() {
// Dispatch config-changed event
this.dispatchEvent( new CustomEvent('config-changed', { detail: { config: this.config }, bubbles: true, composed: true, }));
}
_addOccasion() {
this.config.occasions.push({
name: 'New Event',
date: new Date().toISOString().substring(0,10),
icon: 'mdi:calendar',
count: false
});
this._requestUpdate();
}
_deleteOccasion(index) {
const newOccasions = [...this.config.occasions];
newOccasions.splice(index, 1);
this.config = { ...this.config, occasions: newOccasions };
this._requestUpdate();
}
}