Skip to content

Commit 7da04cc

Browse files
committed
feat(#77): top level options in the visual editor
1 parent 772cf09 commit 7da04cc

File tree

2 files changed

+111
-30
lines changed

2 files changed

+111
-30
lines changed

src/editor/index.ts

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { customElement, property, state } from 'lit/decorators';
77
import { repeat } from 'lit/directives/repeat';
88
import { EntityConfig, SankeyChartConfig, SectionConfig } from '../types';
99
import { localize } from '../localize/localize';
10-
import { getEntityId } from '../utils';
10+
import { getEntityId, normalizeConfig } from '../utils';
1111
import './entity';
1212
import { EntityConfigOrStr } from '../types';
13+
import { UNIT_PREFIXES } from '../const';
1314

1415
@customElement('sankey-chart-editor')
1516
export class SankeyChartEditor extends LitElement implements LovelaceCardEditor {
@@ -70,6 +71,8 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
7071
[target.configValue]: target.checked !== undefined ? target.checked : target.value,
7172
};
7273
}
74+
} else {
75+
this._config = { ...ev.detail.value };
7376
}
7477
this._updateConfig();
7578
}
@@ -141,12 +144,63 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
141144
fireEvent(this, 'config-changed', { config: this._config });
142145
}
143146

147+
private _computeSchema() {
148+
return [
149+
// {
150+
// type: 'grid',
151+
// name: '',
152+
// schema: [
153+
// { name: 'autoconfig', selector: { boolean: {} } },
154+
// { name: 'autoconfig.print_yaml', selector: { boolean: {} } },
155+
// ],
156+
// },
157+
{ name: 'title', selector: { text: {} } },
158+
{ name: 'show_names', selector: { boolean: {} } },
159+
{ name: 'show_icons', selector: { boolean: {} } },
160+
{ name: 'show_states', selector: { boolean: {} } },
161+
{ name: 'show_units', selector: { boolean: {} } },
162+
{ name: 'energy_date_selection', selector: { boolean: {} } },
163+
{
164+
type: 'grid',
165+
name: '',
166+
schema: [
167+
{ name: 'wide', selector: { boolean: {} } },
168+
{ name: 'height', selector: { number: { mode: 'box', unit_of_measurement: 'px' } } },
169+
{ name: 'min_box_height', selector: { number: { mode: 'box', unit_of_measurement: 'px' } } },
170+
{ name: 'min_box_distance', selector: { number: { mode: 'box', unit_of_measurement: 'px' } } },
171+
],
172+
},
173+
{
174+
type: 'grid',
175+
name: '',
176+
schema: [
177+
{ name: 'min_state', selector: { number: { mode: 'box' } } },
178+
{ name: 'round', selector: { number: { mode: 'box', unit_of_measurement: localize('editor.decimals') } } },
179+
{ name: 'throttle', selector: { number: { mode: 'box', unit_of_measurement: 'ms' } } },
180+
{
181+
name: 'unit_prefix',
182+
selector: {
183+
select: {
184+
mode: 'dropdown',
185+
options: [{ value: '' }, ...Object.keys(UNIT_PREFIXES).map(key => ({ value: key, label: key }))],
186+
},
187+
},
188+
},
189+
],
190+
},
191+
];
192+
}
193+
194+
private _computeLabel = (schema: { name: string }) => {
195+
return localize('editor.fields.' + schema.name);
196+
};
197+
144198
protected render(): TemplateResult | void {
145199
if (!this.hass || !this._helpers) {
146200
return html``;
147201
}
148202

149-
const config = this._config || ({} as SankeyChartConfig);
203+
const config = normalizeConfig(this._config || ({} as SankeyChartConfig));
150204
const { autoconfig } = config;
151205
const sections: SectionConfig[] = config.sections || [];
152206

@@ -166,31 +220,40 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
166220
return html`
167221
<div class="card-config">
168222
<div class="options">
169-
<h3>${localize('editor.autoconfig')}</h3>
170-
<ha-formfield .label=${localize('editor.enable')}>
171-
<ha-switch
172-
.checked=${!!autoconfig}
173-
.configValue=${(conf, val: boolean) => {
174-
const newConf = { ...conf };
175-
if (val && !conf.autoconfig) {
176-
newConf.autoconfig = { print_yaml: false };
177-
} else if (!val && conf.autoconfig) {
178-
delete newConf.autoconfig;
179-
}
180-
return newConf;
181-
}}
182-
@change=${this._valueChanged}
183-
></ha-switch>
184-
</ha-formfield>
185-
${autoconfig
186-
? html`<ha-formfield .label=${localize('editor.print')}>
187-
<ha-switch
188-
.checked=${!!autoconfig?.print_yaml}
189-
.configValue=${(conf, print_yaml) => ({ ...conf, autoconfig: { print_yaml } })}
190-
@change=${this._valueChanged}
191-
></ha-switch>
192-
</ha-formfield>`
193-
: nothing}
223+
<div class="autoconfig">
224+
<ha-formfield .label=${localize('editor.fields.autoconfig')}>
225+
<ha-switch
226+
.checked=${!!autoconfig}
227+
.configValue=${(conf, val: boolean) => {
228+
const newConf = { ...conf };
229+
if (val && !conf.autoconfig) {
230+
newConf.autoconfig = { print_yaml: false };
231+
} else if (!val && conf.autoconfig) {
232+
delete newConf.autoconfig;
233+
}
234+
return newConf;
235+
}}
236+
@change=${this._valueChanged}
237+
></ha-switch>
238+
</ha-formfield>
239+
${autoconfig
240+
? html`<ha-formfield .label=${localize('editor.fields.print_yaml')}>
241+
<ha-switch
242+
.checked=${!!autoconfig?.print_yaml}
243+
.configValue=${(conf, print_yaml) => ({ ...conf, autoconfig: { print_yaml } })}
244+
@change=${this._valueChanged}
245+
></ha-switch>
246+
</ha-formfield>`
247+
: nothing}
248+
</div>
249+
250+
<ha-form
251+
.hass=${this.hass}
252+
.data=${config}
253+
.schema=${this._computeSchema()}
254+
.computeLabel=${this._computeLabel}
255+
@value-changed=${this._valueChanged}
256+
></ha-form>
194257
</div>
195258
${autoconfig ? nothing : this._renderSections(sections)}
196259
<p>${localize('editor.yaml_disclaimer')}</p>
@@ -269,6 +332,11 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
269332
display: grid;
270333
margin-bottom: 20px;
271334
}
335+
.autoconfig {
336+
display: flex;
337+
justify-content: space-between;
338+
margin-bottom: 16px;
339+
}
272340
.sections {
273341
display: flex;
274342
flex-direction: column;

src/localize/languages/en.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@
1010
"editor": {
1111
"yaml_disclaimer": "Please use yaml mode for the other options",
1212
"docs": "Documentation",
13-
"autoconfig": "Autoconfig",
14-
"enable": "Enable",
15-
"print": "Print auto generated config yaml",
1613
"sections": "Sections",
1714
"section": "Section",
1815
"add_section": "+ Add section",
1916
"add_entity": "+ Add entity",
2017
"entity_editor": "Entity editor",
18+
"decimals": "decimals",
2119
"fields": {
20+
"autoconfig": "Autoconfig",
21+
"print_yaml": "Print auto generated config yaml",
22+
"show_names": "Show names",
23+
"show_icons": "Show icons",
24+
"show_states": "Show states",
25+
"show_units": "Show units",
26+
"energy_date_selection": "Sync with energy_date_selection component",
27+
"height": "Height",
28+
"wide": "Wide",
29+
"min_box_height": "Min box height",
30+
"min_box_distance": "Min box distance",
31+
"min_state": "Min state",
32+
"round": "Round",
33+
"throttle": "Throttle",
34+
"unit_prefix": "Unit prefix",
2235
"entity": "Entity",
2336
"type": "Type",
2437
"children": "Children",

0 commit comments

Comments
 (0)