Skip to content

Commit 39e3050

Browse files
committed
Refactoring
1 parent c91d170 commit 39e3050

2 files changed

Lines changed: 50 additions & 68 deletions

File tree

src/editor.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
4242
return Object.keys(this.hass.states).filter((id) => id.startsWith(type));
4343
}
4444

45+
private renderDropdownMenu(
46+
configValue: string,
47+
selectedEntity: string | undefined,
48+
entities: string[],
49+
) {
50+
return html` <div class="option">
51+
<ha-select
52+
.label=${localize('editor.' + configValue)}
53+
@selected=${this.valueChanged}
54+
.configValue=${configValue}
55+
.value=${selectedEntity}
56+
@closed=${(e: Event) => e.stopPropagation()}
57+
fixedMenuPosition
58+
naturalMenuWidth
59+
>
60+
${entities.map(
61+
(entity) =>
62+
html` <mwc-list-item .value=${entity}>${entity}</mwc-list-item>`,
63+
)}
64+
</ha-select>
65+
</div>`;
66+
}
67+
4568
protected render(): Template {
4669
if (!this.hass) {
4770
return nothing;
@@ -77,43 +100,12 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
77100
</ha-select>
78101
</div>
79102
80-
<div class="option">
81-
<ha-select
82-
.label=${localize('editor.map')}
83-
@selected=${this.valueChanged}
84-
.configValue=${'map'}
85-
.value=${this.config.map}
86-
@closed=${(e: Event) => e.stopPropagation()}
87-
fixedMenuPosition
88-
naturalMenuWidth
89-
>
90-
${cameraEntities.map(
91-
(entity) =>
92-
html` <mwc-list-item .value=${entity}
93-
>${entity}</mwc-list-item
94-
>`,
95-
)}
96-
</ha-select>
97-
</div>
98-
99-
<div class="option">
100-
<ha-select
101-
.label=${localize('editor.water_level')}
102-
@selected=${this.valueChanged}
103-
.configValue=${'water_level'}
104-
.value=${this.config.water_level}
105-
@closed=${(e: Event) => e.stopPropagation()}
106-
fixedMenuPosition
107-
naturalMenuWidth
108-
>
109-
${selectEntities.map(
110-
(entity) =>
111-
html` <mwc-list-item .value=${entity}
112-
>${entity}</mwc-list-item
113-
>`,
114-
)}
115-
</ha-select>
116-
</div>
103+
${this.renderDropdownMenu('map', this.config.map, cameraEntities)}
104+
${this.renderDropdownMenu(
105+
'water_level',
106+
this.config.water_level,
107+
selectEntities,
108+
)}
117109
118110
<div class="option">
119111
<paper-input

src/vacuum-card.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,7 @@ export class VacuumCard extends LitElement {
241241
return nothing;
242242
}
243243

244-
const selected = sources.indexOf(source);
245-
246-
return html`
247-
<div class="tip">
248-
<ha-button-menu @click="${(e: Event) => e.stopPropagation()}">
249-
<div slot="trigger">
250-
<ha-icon icon="mdi:fan"></ha-icon>
251-
<span class="icon-title">
252-
${localize(`source.${source.toLowerCase()}`) || source}
253-
</span>
254-
</div>
255-
${sources.map(
256-
(item, index) => html`
257-
<mwc-list-item
258-
?activated=${selected === index}
259-
value=${item}
260-
@click=${this.handleSpeed}
261-
>
262-
${localize(`source.${item.toLowerCase()}`) || item}
263-
</mwc-list-item>
264-
`,
265-
)}
266-
</ha-button-menu>
267-
</div>
268-
`;
244+
return this.renderDropDown(source, sources, 'mdi:fan', this.handleSpeed);
269245
}
270246

271247
private renderWaterLevel(): Template {
@@ -275,24 +251,38 @@ export class VacuumCard extends LitElement {
275251
return nothing;
276252
}
277253

278-
const selected = entity.attributes.options.indexOf(entity.state);
254+
return this.renderDropDown(
255+
entity.state,
256+
entity.attributes.options,
257+
'mdi:water',
258+
this.handleSelect,
259+
);
260+
}
261+
262+
private renderDropDown(
263+
selectedObject: string,
264+
objects: any,
265+
icon: string,
266+
onSelected: Function,
267+
): Template {
268+
const selected = objects.indexOf(selectedObject);
279269

280270
return html`
281271
<div class="tip">
282272
<ha-button-menu @click="${(e: Event) => e.stopPropagation()}">
283273
<div slot="trigger">
284-
<ha-icon icon="mdi:water"></ha-icon>
274+
<ha-icon icon="${icon}"></ha-icon>
285275
<span class="icon-title">
286-
${localize(`source.${entity.state.toLowerCase()}`) ||
287-
entity.state}
276+
${localize(`source.${selectedObject.toLowerCase()}`) ||
277+
selectedObject}
288278
</span>
289279
</div>
290-
${entity.attributes.options.map(
280+
${objects.map(
291281
(item: string, index: number) => html`
292282
<mwc-list-item
293283
?activated=${selected === index}
294284
value=${item}
295-
@click=${this.handleSelect}
285+
@click=${onSelected}
296286
>
297287
${localize(`source.${item.toLowerCase()}`) || item}
298288
</mwc-list-item>

0 commit comments

Comments
 (0)