Skip to content

Commit a0941e8

Browse files
committed
Make choice handling more generic
1 parent 8135055 commit a0941e8

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

firmware/data/web_server/index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,23 @@ <h3>Sleep after</h3>
105105
<div class="tabcontent" id="tabBacklight">
106106
<h2>Backlight configuration</h2>
107107
<div class="field-container">
108-
<label for="ssid">Backlight type:</label>
108+
<label for="backlightType">Backlight type:</label>
109109
<div class="choice-container">
110-
<div class="choice" onclick="selectBacklightChoice(event, 'radioChoiceOff')">
111-
<input id="radioChoiceOff" type="radio" name="backlightType" />
112-
<label for="radioChoiceOff">Off</label>
110+
<div class="choice" onclick="selectBacklightChoice(event)" name="backlightTypeChoice" value="0">
111+
<input id="backlightTypeOff" type="radio"/>
112+
<label for="backlightTypeOff">Off</label>
113113
</div>
114-
<div class="choice" onclick="selectBacklightChoice(event, 'radioChoiceOn')">
115-
<input id="radioChoiceOn" type="radio" value="1" />
116-
<label for="radioChoiceOn">On</label>
114+
<div class="choice" onclick="selectBacklightChoice(event)" name="backlightTypeChoice" value="1">
115+
<input id="backlightTypeOn" type="radio"/>
116+
<label for="backlightTypeOn">On</label>
117117
</div>
118-
<div class="choice" onclick="selectBacklightChoice(event, 'radioChoiceFade')">
119-
<input id="radioChoiceFade" type="radio" value="2" />
120-
<label for="radioChoiceFade">Fade</label>
118+
<div class="choice" onclick="selectBacklightChoice(event)" name="backlightTypeChoice" value="2">
119+
<input id="backlightTypeFade" type="radio"/>
120+
<label for="backlightTypeFade">Fade</label>
121121
</div>
122-
<div class="choice" onclick="selectBacklightChoice(event, 'radioChoicePulse')">
123-
<input id="radioChoicePulse" type="radio" value="3" />
124-
<label for="radioChoicePulse">Pulse</label>
122+
<div class="choice" onclick="selectBacklightChoice(event)" name="backlightTypeChoice" value="3">
123+
<input id="backlightTypePulse" type="radio"/>
124+
<label for="backlightTypePulse">Pulse</label>
125125
</div>
126126
</div>
127127
</div>

firmware/data/web_server/server.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ function getLedInfo() {
260260
})
261261
.then(data => {
262262
ledInfo = new LedInfo.Builder().fromJson(data);
263-
choices = document.getElementsByClassName("choice");
264-
choices[ledInfo.state].className += " active";
263+
selectRadioChoiceByName("backlightTypeChoice", ledInfo.state);
265264
var hsv = RGBtoHSV(ledInfo.r, ledInfo.g, ledInfo.b);
266265
document.getElementById("sliderHue").value = hsv.h * 360;
267266
document.getElementById("sliderSaturation").value = hsv.s * 100;
@@ -363,14 +362,29 @@ function setWifiInfo() {
363362
});
364363
}
365364

366-
function selectBacklightChoice(evt, choiceName) {
367-
var i, choiceContent, choices;
368-
choices = document.getElementsByClassName("choice");
365+
function selectRadioChoiceByName(name, value) {
366+
let choices = document.getElementsByName(name);
369367
for (i = 0; i < choices.length; i++) {
368+
if(choices[i].getAttribute("value") == value) {
369+
choices[i].className += " active";
370+
break;
371+
}
372+
}
373+
}
374+
375+
function selectRadioChoiceByEvent(evt) {
376+
var i, choices;
377+
choices = document.getElementsByName(evt.currentTarget.getAttribute("name"));
378+
for (i = 0; i < choices.length; i++) {
379+
370380
choices[i].className = choices[i].className.replace(" active", "");
371381
}
372382
evt.currentTarget.className += " active";
373-
ledInfo.state = document.getElementById(choiceName).value;
383+
return evt.currentTarget.getAttribute("value");
384+
}
385+
386+
function selectBacklightChoice(evt) {
387+
ledInfo.state = selectRadioChoiceByEvent(evt);
374388
}
375389

376390
function updateSleepBefore() {

0 commit comments

Comments
 (0)