Skip to content

Commit ec550bd

Browse files
author
MHA
committed
feat: preview and translations
1 parent 9d8d87c commit ec550bd

File tree

11 files changed

+122
-94
lines changed

11 files changed

+122
-94
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Display the options of an input_select entity as a clickable list card.
66
In other words: the content of the dropdown menu is displayed as a card.
77

88
Some use cases:
9-
* Select with (too) many options
9+
* Select with too many options to show in dropdown
1010
* Options with long titles
1111
* Have all options directly shown
1212
* You dont't want the extra click to open the dropdown menu

dist/select-list-card.js

Lines changed: 40 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "select-list-card",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Lovelace select-list-card",
55
"keywords": [
66
"home-assistant",

src/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const CARD_VERSION = '0.2.1';
1+
export const CARD_VERSION = '0.2.2';

src/editor.ts

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
import { LitElement, html, customElement, property, TemplateResult, CSSResult, css } from 'lit-element';
22
import { HomeAssistant, fireEvent, LovelaceCardEditor } from 'custom-card-helpers';
3-
3+
import { localize } from './localize/localize';
44
import { SelectListCardConfig } from './types';
55

6-
const options = {
7-
required: {
8-
icon: 'tune',
9-
name: 'Required',
10-
secondary: 'Required options for this card to function',
11-
show: true,
12-
},
13-
appearance: {
14-
icon: 'palette',
15-
name: 'Appearance',
16-
secondary: 'Customize the name, max-height, truncate',
17-
show: true,
18-
},
19-
};
20-
216
@customElement('select-list-card-editor')
227
export class SelectListCardEditor extends LitElement implements LovelaceCardEditor {
238
@property() public hass?: HomeAssistant;
249
@property() private _config?: SelectListCardConfig;
25-
@property() private _toggle?: boolean;
2610

2711
public setConfig(config: SelectListCardConfig): void {
2812
this._config = config;
2913
}
3014

31-
get _name(): string {
15+
get _title(): string {
3216
if (this._config) {
33-
return this._config.name || '';
17+
return this._config.title || '';
3418
}
3519

3620
return '';
@@ -62,7 +46,7 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
6246

6347
get _max_options(): number {
6448
if (this._config) {
65-
return this._config.max_options || 5;
49+
return typeof this._config.max_options !== 'undefined' ? this._config.max_options : 5;
6650
}
6751
return 5;
6852
}
@@ -80,7 +64,7 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
8064
<div class="values">
8165
<div class="row">
8266
<paper-dropdown-menu
83-
label="Entity (Required)"
67+
label="${localize('editor.entity')}"
8468
@value-changed=${this._valueChanged}
8569
.configValue=${'entity'}
8670
>
@@ -95,15 +79,15 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
9579
</div>
9680
<div class="row">
9781
<paper-input
98-
label="Name (Optional)"
99-
.value=${this._name}
100-
.configValue=${'name'}
82+
label="${localize('editor.title')}"
83+
.value=${this._title}
84+
.configValue=${'title'}
10185
@value-changed=${this._valueChanged}
10286
></paper-input>
10387
</div>
10488
<div class="row">
10589
<paper-input
106-
label="Max options (0 = show all)"
90+
label="${localize('editor.max_options')}"
10791
.value=${this._max_options}
10892
.configValue=${'max_options'}
10993
type="number"
@@ -116,36 +100,23 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
116100
.checked=${this._truncate}
117101
.configValue=${'truncate'}
118102
@change=${this._valueChanged}
119-
>Truncate long names?</ha-switch
103+
>${localize('editor.truncate')}</ha-switch
120104
>
121105
</div>
122106
<div class="row">
123107
<ha-switch
124-
aria-label=${`Toggle scroll to selected ${this._truncate ? 'off' : 'on'}`}
108+
aria-label=${`Toggle scroll to selected ${this._scroll_to_selected ? 'off' : 'on'}`}
125109
.checked=${this._scroll_to_selected}
126110
.configValue=${'scroll_to_selected'}
127111
@change=${this._valueChanged}
128-
>Scroll to selected?</ha-switch
112+
>${localize('editor.scroll_to_selected')}</ha-switch
129113
>
130114
</div>
131115
</div>
132116
</div>
133117
`;
134118
}
135119

136-
private _toggleOption(ev): void {
137-
this._toggleThing(ev, options);
138-
}
139-
140-
private _toggleThing(ev, optionList): void {
141-
const show = !optionList[ev.target.option].show;
142-
for (const [key] of Object.entries(optionList)) {
143-
optionList[key].show = false;
144-
}
145-
optionList[ev.target.option].show = show;
146-
this._toggle = !this._toggle;
147-
}
148-
149120
private _valueChanged(ev): void {
150121
if (!this._config || !this.hass) {
151122
return;
@@ -156,7 +127,7 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
156127
return;
157128
}
158129
if (target.configValue) {
159-
if (value === '') {
130+
if (value === '' && target.configValue !== 'entity') {
160131
delete this._config[target.configValue];
161132
} else {
162133
this._config = {

src/localize/languages/en.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
{
22
"common": {
33
"version": "Version",
4+
"name": "Select list card",
5+
"description": "Display the options of an input_select entity as a clickable list card."
6+
},
7+
"error": {
8+
"missing_entity": "Entity is required",
49
"invalid_configuration": "Invalid configuration",
5-
"show_warning": "Show Warning"
10+
"not_available": "Entity is not available"
11+
},
12+
"editor": {
13+
"entity": "Entity (required)",
14+
"title": "Card title (optional)",
15+
"max_options": "NUmber of options shown (0 = all)",
16+
"truncate": "Truncate long option name",
17+
"scroll_to_selected": "Selected option always in view"
618
}
719
}

src/localize/languages/nb.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/localize/languages/nl.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
{
3+
"common": {
4+
"version": "Versie",
5+
"name": "Select lijst kaart",
6+
"description": "Toon de opties van een input_select entiteit in een kaart"
7+
},
8+
"error": {
9+
"missing_entity": "Het specificeren van een entiteit is verplicht!",
10+
"invalid_configuration": "Geen geldige configuratie",
11+
"not_available": "Entiteit is niet beschikbaar"
12+
},
13+
"editor": {
14+
"entity": "Entiteit (verplicht)",
15+
"title": "Kaart titel (optioneel)",
16+
"max_options": "Aantal opties zichtbaar (0 = alles)",
17+
"truncate": "Kap lange optie name af",
18+
"scroll_to_selected": "Geselecteerde optie altijd zichtbaar"
19+
}
20+
}

src/localize/localize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as en from './languages/en.json';
2-
import * as nb from './languages/nb.json';
2+
import * as nl from './languages/nl.json';
33

44
var languages = {
55
en: en,
6-
nb: nb,
6+
nl: nl,
77
};
88

99
export function localize(string: string, search: string = '', replace: string = '') {

src/select-list-card.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ console.info(
2424
(window as any).customCards = (window as any).customCards || [];
2525
(window as any).customCards.push({
2626
type: 'select-list-card',
27-
name: 'Select list Card',
28-
description: 'Display an input_select as a list',
27+
name: `${localize('common.name')}`,
28+
description: `${localize('common.description')}`,
29+
preview: true,
2930
});
3031

3132
@customElement('select-list-card')
@@ -34,24 +35,32 @@ export class SelectListCard extends LitElement {
3435
return document.createElement('select-list-card-editor') as LovelaceCardEditor;
3536
}
3637

37-
public static getStubConfig(): object {
38-
return {};
38+
public static getStubConfig(hass, entities): object {
39+
const entity = entities.find(item => item.startsWith('input_select'));
40+
const dummy = hass;
41+
return {
42+
entity: entity || '',
43+
name: '',
44+
truncate: true,
45+
scroll_to_selected: false,
46+
max_options: 3,
47+
};
3948
}
4049

4150
@property() public hass!: HomeAssistant;
4251
@property() private config!: SelectListCardConfig;
4352

4453
public setConfig(config: SelectListCardConfig): void {
4554
if (!config || !config.entity || !config.entity.startsWith('input_select') || config.show_error) {
46-
throw new Error(localize('common.invalid_configuration'));
55+
throw new Error(localize('error.invalid_configuration'));
4756
}
4857

4958
if (config.test_gui) {
5059
getLovelace().setEditMode(true);
5160
}
5261

5362
this.config = {
54-
name: '',
63+
title: '',
5564
truncate: true,
5665
scroll_to_selected: true,
5766
max_options: 5,
@@ -64,11 +73,24 @@ export class SelectListCard extends LitElement {
6473
}
6574

6675
protected render(): TemplateResult | void {
76+
if (!this.config.entity) {
77+
return html`
78+
<ha-card>
79+
<div class="preview not-available">
80+
<div class="metadata">
81+
<div class="not-available">
82+
${localize('error.not_available')}
83+
</div>
84+
<div>
85+
</div>
86+
</ha-card>
87+
`;
88+
}
6789
const selected = this.hass.states[this.config.entity].state;
6890
const options = this.hass.states[this.config.entity].attributes.options;
69-
const style = this.config.max_options === 0 ? '' : `max-height: ${(this.config.max_options || 5) * 48}px`;
91+
const style = this.config.max_options === 0 ? '' : `max-height: ${(this.config.max_options || 5) * 48 + 16}px`;
7092
return html`
71-
<ha-card .header=${this.config.name} aria-label=${`Select list card: ${this.config.entity}`}>
93+
<ha-card .header=${this.config.title} aria-label=${`Select list card: ${this.config.entity}`}>
7294
<paper-listbox
7395
@iron-select=${this._selectedOptionChanged}
7496
.selected=${options.indexOf(selected)}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LovelaceCardConfig } from 'custom-card-helpers';
22

33
export interface SelectListCardConfig extends LovelaceCardConfig {
44
type: string;
5-
name?: string;
5+
title?: string;
66
entity: string;
77
truncate?: boolean;
88
scroll_to_selected?: boolean;

0 commit comments

Comments
 (0)