Skip to content

Commit e2f82c5

Browse files
committed
/setup
1 parent a322c0a commit e2f82c5

File tree

7 files changed

+573
-546
lines changed

7 files changed

+573
-546
lines changed

built-in-webpages/setup/app.js

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ var options = {};
1717
var configFile;
1818
var lastBox;
1919

20-
// Simple JQuery-like selector
20+
// Element selector shorthands
2121
var $ = function(el) {
2222
return document.getElementById(el);
2323
};
2424

25+
function hide(id) {
26+
$(id).classList.add('hide');
27+
}
28+
29+
function show(id) {
30+
$(id).classList.remove('hide');
31+
}
32+
2533
function newEl(element, attribute) {
2634
var el = document.createElement(element);
2735
if (typeof(attribute) === 'object') {
@@ -34,8 +42,7 @@ function newEl(element, attribute) {
3442

3543
function getParameters() {
3644
var logo;
37-
$('loader').classList.remove('hide');
38-
45+
show('loader');
3946
// Fetch actual status and config info
4047
fetch(esp + "getStatus")
4148
.then(res => res.json())
@@ -76,7 +83,7 @@ function getParameters() {
7683

7784
options = data;
7885
createOptionsBox(options);
79-
$('loader').classList.add('hide');
86+
hide('loader');
8087
});
8188
});
8289
}
@@ -195,8 +202,8 @@ async function createOptionsBox(raw) {
195202
$('gateway').value = raw.gateway;
196203
$('subnet').value = raw.subnet;
197204
if ($('no-dhcp').checked){
198-
$('conf-wifi').classList.remove('hide');
199-
$('save-wifi').classList.remove('hide');
205+
show('conf-wifi');
206+
show('save-wifi');
200207
}
201208
}
202209

@@ -317,7 +324,7 @@ function saveParameters() {
317324
type: 'application/json'
318325
});
319326
var formData = new FormData();
320-
formData.append("data", myblob, configFile);
327+
formData.append("data", myblob, '/' + configFile);
321328

322329
// POST data using the Fetch API
323330
fetch('/edit', {
@@ -328,7 +335,7 @@ function saveParameters() {
328335
// Handle the server response
329336
.then(response => response.text())
330337
.then(text => {
331-
openModal('Save options','<br><b>"' + configFile +'"</b> saved successfully on flash memory!<br><br>');
338+
openModal('Save options','<br><b>"/' + configFile +'"</b> saved successfully on flash memory!<br><br>');
332339
});
333340
}
334341

@@ -337,23 +344,23 @@ function showHidePassword() {
337344
var inp = $("password");
338345
if (inp.type === "password") {
339346
inp.type = "text";
340-
$('show-pass').classList.remove("hide");
341-
$('hide-pass').classList.add("hide");
347+
show('show-pass');
348+
hide('hide-pass');
342349
}
343350
else {
344351
inp.type = "password";
345-
$('show-pass').classList.add("hide");
346-
$('hide-pass').classList.remove("hide");
352+
hide('show-pass');
353+
show('hide-pass');
347354
}
348355
}
349356

350357
function getWiFiList() {
351-
$('loader').classList.remove('hide');
358+
show('loader');
352359
fetch(esp + "scan")
353360
.then(response => response.json())
354361
.then(data => {
355362
listWifi(data);
356-
$('loader').classList.add('hide');
363+
hide('loader');
357364
});
358365
}
359366

@@ -393,7 +400,7 @@ function listWifi(obj) {
393400
// Add row to list
394401
list.appendChild(row);
395402
});
396-
$("wifi-table").classList.remove("hide");
403+
show('wifi-table');
397404
}
398405

399406
function doConnection(e, f) {
@@ -418,7 +425,7 @@ function doConnection(e, f) {
418425
redirect: 'follow'
419426
};
420427

421-
$('loader').classList.remove('hide');
428+
show('loader');
422429
var s;
423430
fetch('/connect', requestOptions)
424431
.then(function(res) {
@@ -427,28 +434,25 @@ function doConnection(e, f) {
427434
})
428435
.then(function(data) {
429436
if (s === 200) {
430-
if (data.includes("already")) {
437+
if (data.includes("already"))
431438
openModal('Connect to WiFi', data, () => {doConnection(e, true)});
432-
$('loader').classList.add('hide');
433-
}
434439
else
435440
openModal('Connect to WiFi', data, restartESP);
436441
}
437442
else
438443
openModal('Error!', data);
439-
$('loader').classList.add('hide');
444+
445+
hide('loader');
440446
})
441447
.catch((error) => {
442448
openModal('Connect to WiFi', error);
443-
$('loader').classList.add('hide');
449+
hide('loader');
444450
});
445451
}
446452

447453

448454
function switchPage(el) {
449-
console.log(el);
450-
$('top-nav').classList.remove('responsive');
451-
455+
$('top-nav').classList.remove('resp');
452456
// Menu items
453457
document.querySelectorAll("a").forEach(item => {
454458
item.classList.remove('active');
@@ -459,7 +463,7 @@ function switchPage(el) {
459463
document.querySelectorAll(".opt-box").forEach(e => {
460464
e.classList.add('hide');
461465
});
462-
$(el.target.getAttribute("data-box")).classList.remove('hide');
466+
show(el.target.getAttribute("data-box"));
463467

464468
if(el.target.id != 'set-wifi') {
465469
var fragment = document.createDocumentFragment();
@@ -473,18 +477,18 @@ function switchPage(el) {
473477
box.insertBefore(el, $('btn-hr'));
474478
});
475479

476-
$('btn-box').classList.remove('hide');
477-
$('btn-hr').classList.remove('hide');
480+
show('btn-box');
481+
show('btn-hr');
478482
}
479483
else {
480-
$('btn-box').classList.add('hide');
481-
$('btn-hr').classList.add('hide');
484+
hide('btn-box');
485+
hide('btn-hr');
482486
}
483487
}
484488

485489

486490
function showMenu() {
487-
$('top-nav').classList.add('responsive');
491+
$('top-nav').classList.add('resp');
488492
}
489493

490494
function openModal(title, msg, fn, args) {
@@ -494,10 +498,10 @@ function openModal(title, msg, fn, args) {
494498
$('main-box').style.filter = "blur(3px)";
495499
if (typeof fn != 'undefined') {
496500
closeCb = fn;
497-
$('ok-modal').classList.remove('hide');
501+
show('ok-modal');
498502
}
499503
else
500-
$('ok-modal').classList.add('hide');
504+
hide('ok-modal');
501505
}
502506

503507
function closeModal(do_cb) {
@@ -526,18 +530,18 @@ function handleSubmit() {
526530
var update = $('update-log');
527531
var loader = $('loader');
528532
var prg = $('progress-wrap');
529-
loader.classList.remove('hide');
530-
prg.classList.remove('hide');
531-
prg.classList.add('active')
533+
show('loader');
534+
show('progress-wrap');
535+
$('progress-wrap').classList.add('active');
532536
update.innerHTML = 'Update in progress';
533537

534538
let formData = new FormData();
535539
formData.set('update', fileElement.files[0]);
536540
var req = new XMLHttpRequest();
537541
req.open('POST', '/update?size=' + fileElement.files[0].size);
538542
req.onload = function(d) {
539-
loader.classList.add('hide');
540-
prg.classList.remove('active');
543+
hide('loader');
544+
$('progress-wrap').classList.remove('active');
541545
update.innerHTML = (req.status != 200) ? `Error ${req.status}: ${req.statusText}` : req.response;
542546
};
543547
req.upload.addEventListener('progress', (p) => {
@@ -551,12 +555,12 @@ function handleSubmit() {
551555
}
552556

553557
async function uploadFolder(e) {
554-
let listing = document.getElementById('listing');
558+
let list = $('listing');
555559
for (let file of Array.from(e.target.files)) {
556560
let path = file.webkitRelativePath;
557-
let item = document.createElement('li');
561+
let item = newEl('li');
558562
item.textContent = path;
559-
listing.appendChild(item);
563+
list.appendChild(item);
560564
// Save each file in the ESP flash
561565
var reader = new FileReader();
562566
reader.onload = function(event) {
@@ -602,14 +606,12 @@ $('file-input').addEventListener('change', () => {
602606
});
603607

604608
$('no-dhcp').addEventListener('change', function() {
605-
let el = $('conf-wifi');
606-
let btn = $('save-wifi');
607609
if (this.checked) {
608-
el.classList.remove('hide');
609-
btn.classList.remove('hide');
610+
show('conf-wifi');
611+
show('save-wifi');
610612
}
611613
else {
612-
el.classList.add('hide');
614+
hide('conf-wifi');
613615
}
614616
});
615617
window.addEventListener('load', getParameters);

built-in-webpages/setup/setup.min.htm

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)