|
136 | 136 | getLoc();
|
137 | 137 | loadJS(getURL('/settings/s.js?p=1'), false); // If we set async false, file is loaded and executed, then next statement is processed
|
138 | 138 | if (loc) d.Sf.action = getURL('/settings/wifi');
|
| 139 | + setTimeout(tE, 500); // wait for DOM to load before calling tE() |
139 | 140 | }
|
140 | 141 |
|
141 |
| - var remoteCount = 0; |
142 |
| - // Initialize empty list for remotes |
143 |
| - function resetRemotes() { |
144 |
| - d.getElementById('rmlist').innerHTML = ''; |
145 |
| - remoteCount = 0; |
146 |
| - updateRMArray(); |
| 142 | + var rC = 0; // remote count |
| 143 | + // toggle visibility of ESP-NOW remote list based on checkbox state |
| 144 | + function tE() { |
| 145 | + const e = d.querySelector('input[name="RE"]').checked; |
| 146 | + const l = gId('rlc'); // remote list container |
| 147 | + // keep the hidden input with MAC addresses, only toggle visibility of the list UI |
| 148 | + if (l) l.style.display = e ? 'block' : 'none'; |
147 | 149 | }
|
148 |
| - |
149 |
| - // Add a Remote MAC to the list |
150 |
| - function addRemote(id, mac) { |
151 |
| - if (!mac || mac === 'None') return; |
152 |
| - if (remoteCount >= 10) return; |
153 |
| - let inputs = d.getElementById('rmlist').getElementsByTagName('input'); |
| 150 | + // reset remotes: initialize empty list (called from xml.cpp) |
| 151 | + function rstR() { |
| 152 | + gId('rml').innerHTML = ''; // clear remote list |
| 153 | + rC = 0; |
| 154 | + uR(); // update remote list |
| 155 | + } |
| 156 | + // add remote MAC to the list |
| 157 | + function aR(id, mac) { |
| 158 | + if (!/^[0-9A-F]{12}$/i.test(mac)) return; // check for valid hex string |
| 159 | + if (rC >= 10) return; |
| 160 | + let inputs = gId('rml').getElementsByTagName('input'); |
154 | 161 | for (let i = 0; i < inputs.length; i++) {
|
155 | 162 | if (inputs[i].value === mac) return;
|
156 | 163 | }
|
157 |
| - let list = d.getElementById('rmlist'); |
158 |
| - let row = d.createElement('div'); |
159 |
| - let inp = d.createElement('input'); |
| 164 | + let list = gId('rml'); |
| 165 | + let row = cE('div'); |
| 166 | + let inp = cE('input'); |
160 | 167 | inp.type = 'text';
|
161 | 168 | inp.name = id;
|
162 | 169 | inp.value = mac;
|
163 | 170 | inp.maxLength = 12;
|
164 | 171 | inp.minLength = 12;
|
165 |
| - inp.onchange = updateRMArray; |
| 172 | + inp.onchange = uR; |
166 | 173 | row.appendChild(inp);
|
167 | 174 | let btn = d.createElement('button');
|
168 | 175 | btn.type = 'button';
|
169 | 176 | btn.className = 'btn btn-xs';
|
170 | 177 | btn.innerText = 'Remove';
|
171 | 178 | btn.onclick = function() {
|
172 |
| - removeRemote(row); |
| 179 | + rR(row); |
173 | 180 | };
|
174 | 181 | row.appendChild(btn);
|
175 | 182 | list.appendChild(row);
|
176 |
| - remoteCount++; |
177 |
| - updateRMArray(); |
| 183 | + rC++; |
| 184 | + uR(); |
178 | 185 | }
|
179 |
| - // Remove a remote from the list |
180 |
| - function removeRemote(element) { |
181 |
| - element.remove(); |
182 |
| - updateRMArray(); |
| 186 | + // remove a remote from the list |
| 187 | + function rR(e) { |
| 188 | + e.remove(); |
| 189 | + uR(); |
183 | 190 | }
|
184 |
| - // Update hidden input with JSON array of MACs |
185 |
| - function updateRMArray() { |
| 191 | + // update remote list |
| 192 | + function uR() { |
186 | 193 | let macs = [];
|
187 |
| - let inputs = d.getElementById('rmlist').getElementsByTagName('input'); |
| 194 | + let inputs = gId('rml').getElementsByTagName('input'); |
188 | 195 | for (let i = 0; i < inputs.length; i++) {
|
189 | 196 | if (inputs[i].value && inputs[i].value !== '') {
|
190 | 197 | macs.push(inputs[i].value);
|
191 | 198 | }
|
192 | 199 | }
|
193 |
| - d.getElementById('rmacs').value = JSON.stringify(macs); |
| 200 | + gId('rmacs').value = JSON.stringify(macs); |
194 | 201 | return true; // Allow form submission to continue
|
195 | 202 | }
|
196 | 203 | </script>
|
197 | 204 | <style>@import url("style.css");</style>
|
198 | 205 | </head>
|
199 | 206 | <body onload="S()">
|
200 |
| - <form id="form_s" name="Sf" method="post" onsubmit="return updateRMArray()"> |
| 207 | + <form id="form_s" name="Sf" method="post" onsubmit="return uR()"> |
201 | 208 | <div class="toprow">
|
202 | 209 | <div class="helpB"><button type="button" onclick="H('features/settings/#wifi-settings')">?</button></div>
|
203 | 210 | <button type="button" onclick="B()">Back</button><button type="submit">Save & Connect</button><hr>
|
@@ -258,13 +265,15 @@ <h3>ESP-NOW Wireless</h3>
|
258 | 265 | <i class="warn">This firmware build does not include ESP-NOW support.<br></i>
|
259 | 266 | </div>
|
260 | 267 | <div id="ESPNOW">
|
261 |
| - Enable ESP-NOW: <input type="checkbox" name="RE"><br> |
| 268 | + Enable ESP-NOW: <input type="checkbox" name="RE" onchange="tE()"><br> |
262 | 269 | <i>Listen for events over ESP-NOW<br>
|
263 | 270 | Keep disabled if not using a remote or wireless sync, increases power consumption.<br></i>
|
264 |
| - Last device seen: <span class="rlid">None</span> |
265 |
| - <button type="button" class="btn btn-xs" onclick="addRemote('RM'+remoteCount,this.parentElement.querySelector('.rlid').textContent)">Add</button><br> |
266 |
| - Linked MACs:<br> |
267 |
| - <div id="rmlist"> |
| 271 | + <div id="rlc"> |
| 272 | + Last device seen: <span class="rlid" id="ld">None</span> |
| 273 | + <button type="button" class="btn btn-xs" onclick="aR('RM'+rC,gId('ld').textContent)">Add</button><br> |
| 274 | + Linked MACs:<br> |
| 275 | + <div id="rml"> |
| 276 | + </div> |
268 | 277 | </div>
|
269 | 278 | <input type="hidden" name="RMA" id="rmacs">
|
270 | 279 | </div>
|
|
0 commit comments