Skip to content

Commit 35c614a

Browse files
committed
simplifying defaults (1/2)
1 parent 8b7699a commit 35c614a

File tree

3 files changed

+75
-131
lines changed

3 files changed

+75
-131
lines changed

src/parse-config/defaults.ts

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { merge } from "lodash";
22
import { Config, InputConfig } from "../types";
33
import { parseColorScheme } from "./parse-color-scheme";
44
import { getEntityIndex } from "./parse-config";
5-
import getThemedLayout, { defaultLayout, HATheme } from "./themed-layout";
5+
import getThemedLayout, { HATheme } from "./themed-layout";
66

77
const defaultEntityRequired = {
88
entity: "",
@@ -59,15 +59,73 @@ const defaultYamlRequired = {
5959
yaxes: {},
6060
},
6161
};
62-
const defaultYamlOptional = {
62+
63+
//
64+
65+
const defaultExtraYAxes: Partial<Plotly.LayoutAxis> = {
66+
// automargin: true, // it makes zooming very jumpy
67+
side: "right",
68+
overlaying: "y",
69+
showgrid: false,
70+
visible: false,
71+
// This makes sure that the traces are rendered above the right y axis,
72+
// including the marker and its text. Useful for show_value. See cliponaxis in entity
73+
layer: "below traces",
74+
};
75+
76+
const defaultYamlOptional: {
77+
layout: Partial<Plotly.Layout>;
78+
config: Partial<Plotly.Config>;
79+
} = {
6380
config: {
6481
displaylogo: false,
6582
scrollZoom: true,
6683
modeBarButtonsToRemove: ["resetScale2d", "toImage", "lasso2d", "select2d"],
6784
},
6885
layout: {
69-
xaxis: { autorange: false },
86+
height: 285,
87+
dragmode: "pan",
88+
xaxis: {
89+
autorange: false,
90+
type: "date",
91+
// automargin: true, // it makes zooming very jumpy
92+
},
93+
yaxis: {
94+
// automargin: true, // it makes zooming very jumpy
95+
},
96+
yaxis2: {
97+
// automargin: true, // it makes zooming very jumpy
98+
...defaultExtraYAxes,
99+
visible: true,
100+
},
101+
...Object.fromEntries(
102+
Array.from({ length: 27 }).map((_, i) => [
103+
`yaxis${i + 3}`,
104+
{ ...defaultExtraYAxes },
105+
])
106+
),
107+
legend: {
108+
orientation: "h",
109+
bgcolor: "transparent",
110+
x: 0,
111+
y: 1,
112+
yanchor: "bottom",
113+
},
114+
title: {
115+
y: 1,
116+
pad: {
117+
t: 15,
118+
},
119+
},
120+
modebar: {
121+
// vertical so it doesn't occlude the legend
122+
orientation: "v",
123+
},
70124
margin: {
125+
b: 50,
126+
t: 0,
127+
l: 60,
128+
// @ts-expect-error functions are not a plotly thing, only this card
71129
r: ({ getFromConfig }) => {
72130
const entities = getFromConfig(`entities`);
73131
const usesRightAxis = entities.some(({ yaxis }) => yaxis === "y2");
@@ -78,11 +136,14 @@ const defaultYamlOptional = {
78136
},
79137
};
80138

81-
export function addPreParsingDefaults(yaml: InputConfig): InputConfig {
139+
export function addPreParsingDefaults(
140+
yaml: InputConfig,
141+
css_vars: HATheme
142+
): InputConfig {
82143
const out = merge(
83144
{},
84145
yaml,
85-
{ layout: {} },
146+
{ layout: yaml.ha_theme ? getThemedLayout(css_vars) : {} },
86147
defaultYamlRequired,
87148
yaml.raw_plotly_config ? {} : defaultYamlOptional,
88149
yaml
@@ -117,14 +178,7 @@ export function addPreParsingDefaults(yaml: InputConfig): InputConfig {
117178
return out;
118179
}
119180

120-
export function addPostParsingDefaults({
121-
yaml,
122-
css_vars,
123-
}: {
124-
yaml: Config;
125-
css_vars: HATheme;
126-
}): Config {
127-
// 3rd pass: decorate
181+
export function addPostParsingDefaults(yaml: Config): Config {
128182
/**
129183
* These cannot be done via defaults because they are functions and
130184
* functions would be overwritten if the user sets a configuration on a parent
@@ -138,8 +192,6 @@ export function addPostParsingDefaults({
138192
const layout = merge(
139193
{},
140194
yaml.layout,
141-
yaml.raw_plotly_config ? {} : defaultLayout,
142-
yaml.ha_theme ? getThemedLayout(css_vars) : {},
143195
yaml.raw_plotly_config
144196
? {}
145197
: {

src/parse-config/parse-config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ConfigParser {
4545
this.yaml = {};
4646
this.errors = [];
4747
this.hass = hass;
48-
this.yaml_with_defaults = addPreParsingDefaults(input_yaml);
48+
this.yaml_with_defaults = addPreParsingDefaults(input_yaml, css_vars);
4949
// 2nd pass: evaluate functions
5050

5151
this.fnParam = {
@@ -69,10 +69,7 @@ class ConfigParser {
6969
}
7070
}
7171
//TODO: mutates
72-
this.yaml = addPostParsingDefaults({
73-
yaml: this.yaml,
74-
css_vars,
75-
});
72+
this.yaml = addPostParsingDefaults(this.yaml);
7673

7774
return { errors: this.errors, parsed: this.yaml };
7875
}

src/parse-config/themed-layout.ts

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,6 @@ export type HATheme = {
66
"secondary-text-color": string;
77
};
88

9-
const defaultExtraYAxes: Partial<Plotly.LayoutAxis> = {
10-
// automargin: true, // it makes zooming very jumpy
11-
side: "right",
12-
overlaying: "y",
13-
showgrid: false,
14-
visible: false,
15-
// This makes sure that the traces are rendered above the right y axis,
16-
// including the marker and its text. Useful for show_value. See cliponaxis in entity
17-
layer: "below traces",
18-
};
19-
20-
export const defaultLayout: Partial<Plotly.Layout> = {
21-
height: 285,
22-
dragmode: "pan",
23-
xaxis: {
24-
autorange: false,
25-
type: "date",
26-
// automargin: true, // it makes zooming very jumpy
27-
},
28-
yaxis: {
29-
// automargin: true, // it makes zooming very jumpy
30-
},
31-
yaxis2: {
32-
// automargin: true, // it makes zooming very jumpy
33-
...defaultExtraYAxes,
34-
visible: true,
35-
},
36-
yaxis3: { ...defaultExtraYAxes },
37-
yaxis4: { ...defaultExtraYAxes },
38-
yaxis5: { ...defaultExtraYAxes },
39-
yaxis6: { ...defaultExtraYAxes },
40-
yaxis7: { ...defaultExtraYAxes },
41-
yaxis8: { ...defaultExtraYAxes },
42-
yaxis9: { ...defaultExtraYAxes },
43-
// @ts-expect-error (the types are missing yaxes > 9)
44-
yaxis10: { ...defaultExtraYAxes },
45-
yaxis11: { ...defaultExtraYAxes },
46-
yaxis12: { ...defaultExtraYAxes },
47-
yaxis13: { ...defaultExtraYAxes },
48-
yaxis14: { ...defaultExtraYAxes },
49-
yaxis15: { ...defaultExtraYAxes },
50-
yaxis16: { ...defaultExtraYAxes },
51-
yaxis17: { ...defaultExtraYAxes },
52-
yaxis18: { ...defaultExtraYAxes },
53-
yaxis19: { ...defaultExtraYAxes },
54-
yaxis20: { ...defaultExtraYAxes },
55-
yaxis21: { ...defaultExtraYAxes },
56-
yaxis22: { ...defaultExtraYAxes },
57-
yaxis23: { ...defaultExtraYAxes },
58-
yaxis24: { ...defaultExtraYAxes },
59-
yaxis25: { ...defaultExtraYAxes },
60-
yaxis26: { ...defaultExtraYAxes },
61-
yaxis27: { ...defaultExtraYAxes },
62-
yaxis28: { ...defaultExtraYAxes },
63-
yaxis29: { ...defaultExtraYAxes },
64-
yaxis30: { ...defaultExtraYAxes },
65-
margin: {
66-
b: 50,
67-
t: 0,
68-
l: 60,
69-
//r: 60,
70-
},
71-
legend: {
72-
orientation: "h",
73-
bgcolor: "transparent",
74-
x: 0,
75-
y: 1,
76-
yanchor: "bottom",
77-
},
78-
title: {
79-
y: 1,
80-
pad: {
81-
t: 15,
82-
},
83-
},
84-
modebar: {
85-
// vertical so it doesn't occlude the legend
86-
orientation: "v",
87-
},
88-
};
89-
909
const themeAxisStyle = {
9110
tickcolor: "rgba(127,127,127,.3)",
9211
gridcolor: "rgba(127,127,127,.3)",
@@ -106,35 +25,11 @@ export default function getThemedLayout(
10625
},
10726
xaxis: { ...themeAxisStyle },
10827
yaxis: { ...themeAxisStyle },
109-
yaxis2: { ...themeAxisStyle },
110-
yaxis3: { ...themeAxisStyle },
111-
yaxis4: { ...themeAxisStyle },
112-
yaxis5: { ...themeAxisStyle },
113-
yaxis6: { ...themeAxisStyle },
114-
yaxis7: { ...themeAxisStyle },
115-
yaxis8: { ...themeAxisStyle },
116-
yaxis9: { ...themeAxisStyle },
117-
// @ts-expect-error (the types are missing yaxes > 9)
118-
yaxis10: { ...themeAxisStyle },
119-
yaxis11: { ...themeAxisStyle },
120-
yaxis12: { ...themeAxisStyle },
121-
yaxis13: { ...themeAxisStyle },
122-
yaxis14: { ...themeAxisStyle },
123-
yaxis15: { ...themeAxisStyle },
124-
yaxis16: { ...themeAxisStyle },
125-
yaxis17: { ...themeAxisStyle },
126-
yaxis18: { ...themeAxisStyle },
127-
yaxis19: { ...themeAxisStyle },
128-
yaxis20: { ...themeAxisStyle },
129-
yaxis21: { ...themeAxisStyle },
130-
yaxis22: { ...themeAxisStyle },
131-
yaxis23: { ...themeAxisStyle },
132-
yaxis24: { ...themeAxisStyle },
133-
yaxis25: { ...themeAxisStyle },
134-
yaxis26: { ...themeAxisStyle },
135-
yaxis27: { ...themeAxisStyle },
136-
yaxis28: { ...themeAxisStyle },
137-
yaxis29: { ...themeAxisStyle },
138-
yaxis30: { ...themeAxisStyle },
28+
...Object.fromEntries(
29+
Array.from({ length: 28 }).map((_, i) => [
30+
`yaxis${i + 2}`,
31+
{ ...themeAxisStyle },
32+
])
33+
),
13934
};
14035
}

0 commit comments

Comments
 (0)