@@ -77,68 +77,84 @@ rail_station_row:
77
77
- background : var(--accent-text-color)
78
78
extra_styles : |
79
79
[[[
80
- var extra_styles = ''
80
+ function getColor(entity) {
81
+ const attr = entity.attributes[variables.condition_attribute];
82
+ if (typeof attr == 'undefined' || attr == null) {
83
+ return variables.off_color;
84
+ } else if (attr >= variables.condition_threshold) {
85
+ return variables.mid_color;
86
+ } else {
87
+ return variables.on_color;
88
+ }
89
+ }
90
+
91
+ function buildStyle(id, styleObject) {
92
+ const styles = Object.keys(styleObject).map((key) => `${key}:${styleObject[key]}`).join(';');
93
+ return `#${id}{${styles}}`;
94
+ }
95
+
96
+ let extraStyles = '';
97
+
81
98
for (let rail = 0; rail <= 4; rail++) {
82
- var style = `
83
- #rail${ rail } {
84
- display: ${ (variables['rail' + rail + '_entity'] == null) ? 'none' : 'block' };
85
- position: absolute;
86
- left: ${ rail * 12 }px;
87
- top:
88
- ${ ((variables.station_rail == rail) && (variables.terminate_at.includes('top')))
89
- ? 'calc( 50% - ' + (4 * !(variables.terminate_at.includes('terminal'))) + 'px )' : '0' };
90
- width: ${ ((variables.station_rail == rail) && !(variables.terminate_at.includes("terminal"))) ? (6 + (4 - rail) * 12) + "px" : "0" };
91
- height:
92
- ${ variables.station_rail == rail
93
- ? variables.terminate_at.includes("terminal")
94
- ? '50%'
95
- : variables.terminate_at.includes("top")
96
- ? 'calc( 50% + 4px )'
97
- : variables.terminate_at.includes("bottom")
98
- ? 'calc( 50% - 4px )'
99
- : '100%'
100
- : '100%'
101
- };
102
- box-shadow: 0px 0px 0px 2px rgba(var(--rgb-primary-background-color),0.5);
103
- --rail-color:
104
- ${ variables['rail' + rail + '_entity'] !== null
105
- ? states[variables['rail' + rail + '_entity']].attributes[variables.condition_attribute] !== undefined
106
- ? states[variables['rail' + rail + '_entity']].attributes[variables.condition_attribute] >= variables.condition_threshold
107
- ? variables.mid_color
108
- : variables.on_color
109
- : variables.off_color
110
- : 'transparent'
111
- };
112
- border-left: 8px solid var(--rail-color);
113
- border-top: ${ ((variables.station_rail == rail) && (variables.terminate_at == "top")) ? "8px solid var(--rail-color)" : "none" };
114
- border-bottom: ${ ((variables.station_rail == rail) && (variables.terminate_at == "bottom")) ? "8px solid var(--rail-color)" : "none" };
115
- border-radius:
116
- ${ variables.station_rail == rail
117
- ? variables.terminate_at == "top"
118
- ? (6 * (5 - rail)) + "px 0 0 0"
119
- : variables.terminate_at == "bottom"
120
- ? "0 0 0 " + (6 * (5 - rail)) + "px"
121
- : "0"
122
- : "0"
123
- };
99
+ const railEntity = variables[`rail${rail}_entity`];
100
+ const isStationRail = variables.station_rail == rail;
101
+ const terminateAt = variables.terminate_at.split(' ');
102
+
103
+ const railStyle = {
104
+ 'display': railEntity == null ? 'none' : 'block',
105
+ 'position': 'absolute',
106
+ 'left': `${rail * 12}px`,
107
+ 'top': '0',
108
+ 'width': '0',
109
+ 'height': '100%',
110
+ 'box-shadow': '0px 0px 0px 2px rgba(var(--rgb-primary-background-color), 0.5)',
111
+ 'border-left': '8px solid var(--rail-color)',
112
+ 'border-radius': '0',
113
+ '--rail-color': 'transparent',
114
+ };
115
+
116
+ if (isStationRail) {
117
+ if (terminateAt.includes('top')) {
118
+ Object.assign(railStyle, {
119
+ 'top': terminateAt.includes('terminal') ? '50%' : 'calc(50% - 4px)',
120
+ });
124
121
}
125
- `
126
- extra_styles = extra_styles + style
127
- }
128
- var stationStyle = `
129
- #station {
130
- --station-color:
131
- ${ entity != null
132
- ? entity.attributes[variables.condition_attribute] !== undefined
133
- ? entity.attributes[variables.condition_attribute] >= variables.condition_threshold
134
- ? variables.mid_color
135
- : variables.on_color
136
- : variables.off_color
137
- : 'transparent'
122
+
123
+ if (terminateAt.includes('terminal')) {
124
+ Object.assign(railStyle, {
125
+ 'height': '50%',
126
+ });
127
+ } else {
128
+ if (terminateAt.includes('top')) {
129
+ Object.assign(railStyle, {
130
+ 'border-top': '8px solid var(--rail-color)',
131
+ 'border-radius': `${6 * (5 - rail)}px 0 0 0`,
132
+ 'width': `${6 + (4 - rail) * 12}px`,
133
+ 'height': 'calc(50% + 4px)',
134
+ });
135
+ } else if (terminateAt.includes('bottom')) {
136
+ Object.assign(railStyle, {
137
+ 'border-bottom': '8px solid var(--rail-color)',
138
+ 'border-radius': `0 0 0 ${6 * (5 - rail)}px`,
139
+ 'width': `${6 + (4 - rail) * 12}px`,
140
+ 'height': 'calc(50% - 4px)',
141
+ });
138
142
}
139
143
}
140
- `
141
- return extra_styles + stationStyle
144
+ }
145
+
146
+ if (railEntity != null) {
147
+ railStyle['--rail-color'] = getColor(states[railEntity]);
148
+ }
149
+
150
+ extraStyles += buildStyle(`rail${rail}`, railStyle);
151
+ }
152
+
153
+ extraStyles += buildStyle('station', {
154
+ '--station-color': entity ? getColor(entity) : 'transparent',
155
+ });
156
+
157
+ return extraStyles;
142
158
]]]
143
159
tap_action :
144
160
action : none
@@ -225,62 +241,56 @@ rail_interchange_row:
225
241
- background : var(--station-color)
226
242
extra_styles : |
227
243
[[[
228
- var extra_styles = ''
244
+ function getColor(entity) {
245
+ const attr = entity.attributes[variables.condition_attribute];
246
+ if (typeof attr == 'undefined' || attr == null) {
247
+ return variables.off_color;
248
+ } else if (attr >= variables.condition_threshold) {
249
+ return variables.mid_color;
250
+ } else {
251
+ return variables.on_color;
252
+ }
253
+ }
254
+
255
+ function buildStyle(id, styleObject) {
256
+ const styles = Object.keys(styleObject).map((key) => `${key}:${styleObject[key]}`).join(';');
257
+ return `#${id}{${styles}}`;
258
+ }
259
+
260
+ let extraStyles = '';
261
+
229
262
for (let rail = 0; rail <= 4; rail++) {
230
- var style = `
231
- #rail${ rail }_top {
232
- display: ${ (variables['rail' + rail + '_top_entity'] == null) ? 'none' : 'block' };
233
- position: absolute;
234
- top: 0;
235
- left: ${ rail * 12 }px;
236
- width: 0;
237
- height: 50%;
238
- --rail-color:
239
- ${ variables['rail' + rail + '_top_entity'] != null
240
- ? states[variables['rail' + rail + '_top_entity']].attributes[variables.condition_attribute] != undefined
241
- ? states[variables['rail' + rail + '_top_entity']].attributes[variables.condition_attribute] >= variables.condition_threshold
242
- ? variables.mid_color
243
- : variables.on_color
244
- : variables.off_color
245
- : 'transparent'
246
- };
247
- border-left: 8px solid var(--rail-color);
248
- }
249
- #rail${ rail }_bottom {
250
- display: ${ (variables['rail' + rail + '_bottom_entity'] == null) ? 'none' : 'block' };
251
- position: absolute;
252
- top: 50%;
253
- left: ${ rail * 12 }px;
254
- width: 0;
255
- height: 50%;
256
- --rail-color:
257
- ${ variables['rail' + rail + '_bottom_entity'] != null
258
- ? states[variables['rail' + rail + '_bottom_entity']].attributes[variables.condition_attribute] != undefined
259
- ? states[variables['rail' + rail + '_bottom_entity']].attributes[variables.condition_attribute] >= variables.condition_threshold
260
- ? variables.mid_color
261
- : variables.on_color
262
- : variables.off_color
263
- : 'transparent'
264
- };
265
- border-left: 8px solid var(--rail-color);
266
- }
267
- `
268
- extra_styles = extra_styles + style
263
+ const railTopEntity = variables[`rail${rail}_top_entity`];
264
+ const railBottomEntity = variables[`rail${rail}_bottom_entity`];
265
+
266
+ extraStyles += buildStyle(`rail${rail}_top`, {
267
+ 'display': railTopEntity == null ? 'none' : 'block',
268
+ 'position': 'absolute',
269
+ 'top': '0',
270
+ 'left': `${rail * 12}px`,
271
+ 'width': '0',
272
+ 'height': '50%',
273
+ 'border-left': '8px solid var(--rail-color)',
274
+ '--rail-color': railTopEntity ? getColor(states[railTopEntity]) : 'transparent',
275
+ });
276
+
277
+ extraStyles += buildStyle(`rail${rail}_bottom`, {
278
+ 'display': railBottomEntity == null ? 'none' : 'block',
279
+ 'position': 'absolute',
280
+ 'top': '50%',
281
+ 'left': `${rail * 12}px`,
282
+ 'width': '0',
283
+ 'height': '50%',
284
+ 'border-left': '8px solid var(--rail-color)',
285
+ '--rail-color': railBottomEntity ? getColor(states[railBottomEntity]) : 'transparent',
286
+ });
269
287
}
270
- var stationStyle = `
271
- #station {
272
- --station-color:
273
- ${ entity != null
274
- ? entity.attributes[variables.condition_attribute] != undefined
275
- ? entity.attributes[variables.condition_attribute] >= variables.condition_threshold
276
- ? variables.mid_color
277
- : variables.on_color
278
- : variables.off_color
279
- : 'transparent'
280
- }
281
- }
282
- `
283
- return extra_styles + stationStyle
288
+
289
+ extraStyles += buildStyle('station', {
290
+ '--station-color': entity ? getColor(entity) : 'transparent',
291
+ });
292
+
293
+ return extraStyles;
284
294
]]]
285
295
286
296
0 commit comments