feat: rework the network settings page

This commit is contained in:
Yurii committed 2026-12-03 09:25:37 +03:00
1 parent 29a5bc0f43
commit 6d8a28e82c
8 files changed
+452 -440

No files matched your search

+11 -5
View File
@@ -29,6 +29,8 @@
"save": "保存",
"saved": "已保存",
"refresh": "刷新",
"search": "搜索",
"close": "关闭",
"restore": "恢复",
"restored": "已恢复",
"backup": "备份",
@@ -154,23 +156,27 @@
"ethernet": "以太网",
"wifi": "Wi-Fi",
"availableNetworks": "可用网络",
"staSettings": "WiFi 设置",
"apSettings": "AP 设置"
"staSettings": "连接到 Wi-Fi",
"apSettings": "备用接入点"
},
"scan": {
"pos": "#",
"info": "Info",
"hiddenSsid": "请输入隐藏网络的名称",
"empty": "未找到网络",
"failed": "无法获取网络列表。",
"unavailable": "仅在 Wi-Fi 模式下可扫描网络。"
},
"ethernet": {
"unavailable": "此固件版本不支持以太网。",
"notConfigured": "配置全部六个 GPIO 引脚以使用以太网。",
"clk": "SPI 时钟 GPIO",
"clk": "SPI clock GPIO",
"mosi": "SPI MOSI GPIO",
"miso": "SPI MISO GPIO",
"cs": "SPI CS GPIO",
"irq": "中断 GPIO",
"rst": "复位 GPIO",
"irq": "Interrupt GPIO",
"rst": "Reset GPIO",
"note": "W5500 SPI 接线"
},
"wifi": {
+11 -5
View File
@@ -29,6 +29,8 @@
"save": "Save",
"saved": "Saved",
"refresh": "Refresh",
"search": "Search",
"close": "Close",
"restore": "Restore",
"restored": "Restored",
"backup": "Backup",
@@ -154,23 +156,27 @@
"ethernet": "Ethernet",
"wifi": "Wi-Fi",
"availableNetworks": "Available networks",
"staSettings": "WiFi settings",
"apSettings": "AP settings"
"staSettings": "Connect to Wi-Fi",
"apSettings": "Fallback access point"
},
"scan": {
"pos": "#",
"info": "Info",
"hiddenSsid": "Enter the hidden network name",
"empty": "No networks found",
"failed": "Could not retrieve the list of networks.",
"unavailable": "Network scanning is available only in Wi-Fi mode."
},
"ethernet": {
"unavailable": "This firmware build does not support Ethernet.",
"notConfigured": "Configure all six GPIOs to use Ethernet.",
"clk": "SPI clock GPIO",
"clk": "SPI CLK GPIO",
"mosi": "SPI MOSI GPIO",
"miso": "SPI MISO GPIO",
"cs": "SPI CS GPIO",
"irq": "Interrupt GPIO",
"rst": "Reset GPIO",
"irq": "IRQ GPIO",
"rst": "RST GPIO",
"note": "W5500 SPI wiring"
},
"wifi": {
+14 -8
View File
@@ -29,6 +29,8 @@
"save": "Salva",
"saved": "Salvato",
"refresh": "Ricarica",
"search": "Cerca",
"close": "Chiudi",
"restore": "Recupera",
"restored": "Recuperato",
"backup": "Backup",
@@ -154,23 +156,27 @@
"ethernet": "Ethernet",
"wifi": "Wi-Fi",
"availableNetworks": "Reti disponibili",
"staSettings": "Impostazioni WiFi",
"apSettings": "Impostazioni AP"
"staSettings": "Connessione al Wi-Fi",
"apSettings": "Punto di accesso di riserva"
},
"scan": {
"pos": "#",
"info": "Info",
"hiddenSsid": "Inserisci il nome della rete nascosta",
"empty": "Nessuna rete trovata",
"failed": "Impossibile recuperare la lista delle reti.",
"unavailable": "La ricerca delle reti è disponibile solo in modalità Wi-Fi."
},
"ethernet": {
"unavailable": "Questa versione del firmware non supporta Ethernet.",
"notConfigured": "Configura tutti e sei i GPIO per utilizzare Ethernet.",
"clk": "GPIO clock SPI",
"mosi": "GPIO MOSI SPI",
"miso": "GPIO MISO SPI",
"cs": "GPIO CS SPI",
"irq": "GPIO interrupt",
"rst": "GPIO reset",
"clk": "SPI clock GPIO",
"mosi": "SPI MOSI GPIO",
"miso": "SPI MISO GPIO",
"cs": "SPI CS GPIO",
"irq": "Interrupt GPIO",
"rst": "Reset GPIO",
"note": "Cablaggio SPI del W5500"
},
"wifi": {
+9 -3
View File
@@ -29,6 +29,8 @@
"save": "Opslaan",
"saved": "Opgeslagen",
"refresh": "Vernieuwen",
"search": "Zoeken",
"close": "Sluiten",
"restore": "Herstellen",
"restored": "Hersteld",
"backup": "Back-up",
@@ -154,18 +156,22 @@
"ethernet": "Ethernet",
"wifi": "Wi-Fi",
"availableNetworks": "Beschikbare netwerken",
"staSettings": "WiFi-instellingen",
"apSettings": "AP-instellingen"
"staSettings": "Verbinden met wifi",
"apSettings": "Reservetoegangspunt"
},
"scan": {
"pos": "#",
"info": "Info",
"hiddenSsid": "Voer de naam van het verborgen netwerk in",
"empty": "Geen netwerken gevonden",
"failed": "De lijst met netwerken kon niet worden opgehaald.",
"unavailable": "Netwerken zoeken is alleen beschikbaar in de Wi-Fi-modus."
},
"ethernet": {
"unavailable": "Deze firmwarebuild ondersteunt geen Ethernet.",
"notConfigured": "Configureer alle zes GPIO's om Ethernet te gebruiken.",
"clk": "SPI-klok GPIO",
"clk": "SPI clock GPIO",
"mosi": "SPI MOSI GPIO",
"miso": "SPI MISO GPIO",
"cs": "SPI CS GPIO",
+14 -8
View File
@@ -29,6 +29,8 @@
"save": "Сохранить",
"saved": "Сохранено",
"refresh": "Обновить",
"search": "Поиск",
"close": "Закрыть",
"restore": "Восстановить настройки",
"restored": "Восстановлено",
"backup": "Сохранить настройки",
@@ -154,23 +156,27 @@
"ethernet": "Ethernet",
"wifi": "Wi-Fi",
"availableNetworks": "Доступные сети",
"staSettings": "Настройки подключения",
"apSettings": "Настройки точки доступа"
"staSettings": "Подключение к Wi-Fi",
"apSettings": "Резервная точка доступа"
},
"scan": {
"pos": "#",
"info": "Инфо",
"hiddenSsid": "Введите имя скрытой сети",
"empty": "Сети не найдены",
"failed": "Не удалось получить список сетей.",
"unavailable": "Поиск сетей доступен только в режиме Wi-Fi."
},
"ethernet": {
"unavailable": "Эта сборка прошивки не поддерживает Ethernet.",
"notConfigured": "Для подключения через Ethernet настройте все шесть GPIO.",
"clk": "GPIO тактирования SPI",
"mosi": "GPIO MOSI SPI",
"miso": "GPIO MISO SPI",
"cs": "GPIO CS SPI",
"irq": "GPIO прерывания",
"rst": "GPIO сброса",
"clk": "SPI clock GPIO",
"mosi": "SPI MOSI GPIO",
"miso": "SPI MISO GPIO",
"cs": "SPI CS GPIO",
"irq": "Interrupt GPIO",
"rst": "Reset GPIO",
"note": "Подключение W5500 по SPI"
},
"wifi": {
+373 -237
View File
@@ -41,47 +41,128 @@
<div id="network-settings-busy" aria-busy="true"></div>
<form action="/api/network/settings" id="network-settings" class="hidden">
<label>
<span data-i18n>network.params.hostname</span>
<input type="text" name="hostname" maxlength="24" pattern="[A-Za-z0-9]+[A-Za-z0-9\-]+[A-Za-z0-9]+" required>
</label>
<div class="grid">
<label>
<span data-i18n>network.params.hostname</span>
<input type="text" name="hostname" maxlength="24" pattern="[A-Za-z0-9]+[A-Za-z0-9\-]+[A-Za-z0-9]+" required>
</label>
<label>
<span data-i18n>network.type.title</span>
<select name="type">
<option value="2" data-i18n>network.type.wifi</option>
<option value="3" hidden disabled data-i18n>network.type.ethernet</option>
</select>
</label>
<label>
<span data-i18n>network.type.title</span>
<select name="type">
<option value="2" data-i18n>network.type.wifi</option>
<option value="3" data-i18n>network.type.ethernet</option>
</select>
</label>
</div>
<label>
<input type="checkbox" name="use_dhcp" value="true">
<span data-i18n>network.params.dhcp</span>
</label>
<br />
<hr />
<h4 data-i18n>network.section.static</h4>
<label>
<span data-i18n>network.params.ip</span>
<input type="text" name="static_config[ip]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<details>
<summary><b data-i18n>network.section.static</b></summary>
<div>
<fieldset>
<label>
<input type="checkbox" name="use_dhcp" value="true">
<span data-i18n>network.params.dhcp</span>
</label>
</fieldset>
<label>
<span data-i18n>network.params.gateway</span>
<input type="text" name="static_config[gateway]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<div class="grid">
<label>
<span data-i18n>network.params.ip</span>
<input type="text" name="static_config[ip]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<label>
<span data-i18n>network.params.subnet</span>
<input type="text" name="static_config[subnet]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<label>
<span data-i18n>network.params.subnet</span>
<input type="text" name="static_config[subnet]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
</div>
<label>
<span data-i18n>network.params.dns</span>
<input type="text" name="static_config[dns]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<div class="grid">
<label>
<span data-i18n>network.params.gateway</span>
<input type="text" name="static_config[gateway]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
<label>
<span data-i18n>network.params.dns</span>
<input type="text" name="static_config[dns]" maxlength="16" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" required>
</label>
</div>
</div>
</details>
<hr />
<details class="ethernet">
<summary><b data-i18n>network.section.ethernet</b></summary>
<div>
<p class="unsupported hidden" data-i18n>network.ethernet.unavailable</p>
<fieldset class="supported">
<div class="grid">
<label>
<span data-i18n>network.ethernet.clk</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][clk]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.mosi</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][mosi]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.miso</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][miso]" min="0" max="254" step="1">
</label>
</div>
<div class="grid">
<label>
<span data-i18n>network.ethernet.cs</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][cs]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.irq</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][irq]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.rst</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][rst]" min="0" max="254" step="1">
</label>
</div>
</fieldset>
</div>
</details>
<hr />
<details>
<summary><b data-i18n>network.section.apSettings</b></summary>
<div>
<div class="grid">
<label>
<span data-i18n>network.wifi.ssid</span>
<input type="text" name="wifi[ap][ssid]" maxlength="32" required>
</label>
<label>
<span data-i18n>network.wifi.password</span>
<input type="text" name="wifi[ap][password]" maxlength="64" required>
</label>
</div>
<label>
<span data-i18n>network.wifi.channel</span>
<input type="number" inputmode="numeric" name="wifi[ap][channel]" min="1" max="12" step="1" required>
</label>
</div>
</details>
<br />
<button type="submit" data-i18n>button.save</button>
</form>
</div>
@@ -91,147 +172,65 @@
<article id="wifi-section">
<div>
<hgroup>
<h2 data-i18n>network.section.wifi</h2>
<h2 data-i18n>network.section.staSettings</h2>
<p></p>
</hgroup>
<hgroup>
<h3 data-i18n>network.section.availableNetworks</h3>
<p></p>
</hgroup>
<form action="/api/network/scan" id="network-scan">
<fieldset id="wifi-scan-controls" disabled>
<div style="max-height: 25rem;" class="overflow-auto">
<table id="networks" role="grid">
<thead>
<tr>
<th scope="col" data-i18n>network.scan.pos</th>
<th scope="col" data-i18n>network.wifi.ssid</th>
<th scope="col" data-i18n>network.scan.info</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div>
<form action="/api/network/settings" id="sta-settings" class="hidden">
<label for="sta-ssid" data-i18n>network.wifi.ssid</label>
<div role="group">
<input type="text" id="sta-ssid" name="wifi[sta][ssid]" maxlength="32" required>
<button type="button" class="scan" aria-haspopup="dialog" aria-controls="scan-dialog" data-i18n>button.search</button>
</div>
<p id="scan-unavailable" class="hidden" data-i18n>network.scan.unavailable</p>
<button type="submit" data-i18n>button.refresh</button>
</fieldset>
</form>
<div class="grid">
<label>
<span data-i18n>network.wifi.password</span>
<input type="password" name="wifi[sta][password]" maxlength="64">
</label>
<hr />
<div>
<hgroup>
<h3 data-i18n>network.section.staSettings</h3>
<p></p>
</hgroup>
<div id="sta-settings-busy" aria-busy="true"></div>
<form action="/api/network/settings" id="sta-settings" class="hidden">
<label>
<span data-i18n>network.wifi.ssid</span>
<input type="text" name="wifi[sta][ssid]" maxlength="32" required>
</label>
<label>
<span data-i18n>network.wifi.password</span>
<input type="password" name="wifi[sta][password]" maxlength="64" required>
</label>
<label>
<span data-i18n>network.wifi.channel</span>
<input type="number" inputmode="numeric" name="wifi[sta][channel]" min="0" max="12" step="1" required>
<small data-i18n>network.sta.channel.note</small>
</label>
<label>
<span data-i18n>network.wifi.channel</span>
<input type="number" inputmode="numeric" name="wifi[sta][channel]" min="0" max="12" step="1" required>
<small data-i18n>network.sta.channel.note</small>
</label>
</div>
<br />
<button type="submit" data-i18n>button.save</button>
</form>
</div>
<hr />
<div>
<hgroup>
<h3 data-i18n>network.section.apSettings</h3>
<p></p>
</hgroup>
<div id="ap-settings-busy" aria-busy="true"></div>
<form action="/api/network/settings" id="ap-settings" class="hidden">
<label>
<span data-i18n>network.wifi.ssid</span>
<input type="text" name="wifi[ap][ssid]" maxlength="32" required>
</label>
<label>
<span data-i18n>network.wifi.password</span>
<input type="text" name="wifi[ap][password]" maxlength="64" required>
</label>
<label>
<span data-i18n>network.wifi.channel</span>
<input type="number" inputmode="numeric" name="wifi[ap][channel]" min="1" max="12" step="1" required>
</label>
<button type="submit" data-i18n>button.save</button>
</form>
</div>
</div>
</article>
<article id="ethernet-section" class="hidden">
<div>
<hgroup>
<h3 data-i18n>network.section.ethernet</h3>
<p></p>
</hgroup>
<div id="ethernet-settings-busy" aria-busy="true"></div>
<form action="/api/network/settings" id="ethernet-settings" class="hidden">
<small id="ethernet-not-configured" class="hidden" data-i18n>network.ethernet.notConfigured</small>
<div class="grid">
<label>
<span data-i18n>network.ethernet.clk</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][clk]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.mosi</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][mosi]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.miso</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][miso]" min="0" max="254" step="1">
</label>
</div>
<div class="grid">
<label>
<span data-i18n>network.ethernet.cs</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][cs]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.irq</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][irq]" min="0" max="254" step="1">
</label>
<label>
<span data-i18n>network.ethernet.rst</span>
<input type="number" inputmode="numeric" name="ethernet[gpio][rst]" min="0" max="254" step="1">
</label>
</div>
<small data-i18n>network.ethernet.note</small>
<button type="submit" data-i18n>button.save</button>
</form>
</div>
</article>
</main>
<dialog id="scan-dialog">
<article>
<header>
<button type="button" aria-label="Close" rel="prev"></button>
<p><b data-i18n>network.section.availableNetworks</b></p>
</header>
<form action="/api/network/scan" id="network-scan">
<table>
<thead>
<tr>
<th scope="col" data-i18n>network.scan.pos</th>
<th scope="col" data-i18n>network.wifi.ssid</th>
<th scope="col" data-i18n>network.scan.info</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div role="status"></div>
</form>
<footer>
<button type="submit" class="refresh" form="network-scan" data-i18n>button.refresh</button>
</footer>
</article>
</dialog>
<footer class="container">
<small>
<b>Made by Laxilef</b>
@@ -245,100 +244,237 @@
<script src="/static/app.js?{BUILD_TIME}"></script>
<script>
const setupNetworkScan = () => {
const staForm = document.getElementById('sta-settings');
const ssidInput = staForm.querySelector("[name='wifi[sta][ssid]']");
const dialog = document.getElementById('scan-dialog');
const form = dialog.querySelector('form');
const closeButton = dialog.querySelector('[rel="prev"]');
const refreshButton = dialog.querySelector('.refresh');
const status = form.querySelector('[role="status"]');
const tbody = form.querySelector('tbody');
const authNames = [
'Open', 'WEP', 'WPA', 'WPA2', 'WPA/WPA2', 'WPA/WPA2 Enterprise',
'WPA3', 'WPA2/WPA3', 'WAPI', 'OWE', 'WPA3 Enterprise'
];
let scanController;
let retryTimer;
const selectNetwork = (network) => {
if (network.hidden) {
ssidInput.value = '';
ssidInput.placeholder = i18n('network.scan.hiddenSsid');
ssidInput.removeAttribute('aria-invalid');
} else {
ssidInput.value = network.ssid;
ssidInput.removeAttribute('placeholder');
ssidInput.dispatchEvent(new Event('change', { bubbles: true }));
}
dialog.close();
ssidInput.focus();
};
const addIcon = (cell, iconClass, label) => {
const icon = document.createElement('i');
icon.className = iconClass;
const tooltip = document.createElement('span');
tooltip.dataset.tooltip = label;
tooltip.setAttribute('role', 'img');
tooltip.setAttribute('aria-label', label);
tooltip.appendChild(icon);
cell.appendChild(tooltip);
};
const addNetworkRow = (network, index) => {
const row = tbody.insertRow();
row.className = 'network';
row.insertCell().textContent = '#' + (index + 1);
const nameButton = document.createElement('button');
nameButton.type = 'button';
nameButton.textContent = network.hidden ? network.bssid : network.ssid;
nameButton.classList.toggle('bssid', network.hidden);
row.insertCell().appendChild(nameButton);
const infoCell = row.insertCell();
const strength = Math.max(0, Math.ceil(network.signal_quality / 20) - 1);
addIcon(infoCell, 'icons-wifi-strength-' + strength, network.signal_quality + '%');
const authIcon = network.auth === 0 ? 'icons-unlocked' : 'icons-locked';
addIcon(infoCell, authIcon, authNames[network.auth] || 'unknown');
row.addEventListener('click', () => selectNetwork(network));
};
const scan = async (signal, attempt = 1) => {
if (signal.aborted || !dialog.open) {
return;
}
const maxAttempts = 6;
let retryDelay = 0;
let unavailable = false;
let message = 'network.scan.failed';
try {
const response = await fetch(form.action, {
cache: "no-cache",
credentials: "include",
signal
});
const networks = response.status === 200
? await response.json()
: null;
// A previous scan can finish after the dialog is closed and opened again.
if (signal.aborted || !dialog.open) {
return;
}
if (networks !== null) {
networks.forEach(addNetworkRow);
message = networks.length ? '' : 'network.scan.empty';
} else if (response.status === 409) {
unavailable = true;
message = 'network.scan.unavailable';
} else if ((response.status === 404 || response.status === 202) && attempt < maxAttempts) {
// The ESP returns 404 when it starts a scan and 202 while it is running.
retryDelay = 5000;
}
} catch {
if (signal.aborted || !dialog.open) {
return;
}
tbody.replaceChildren();
if (attempt < maxAttempts) {
retryDelay = 10000;
}
} finally {
if (!signal.aborted && dialog.open) {
if (retryDelay) {
retryTimer = setTimeout(() => scan(signal, attempt + 1), retryDelay);
} else {
status.textContent = message ? i18n(message) : '';
refreshButton.disabled = unavailable;
refreshButton.removeAttribute('aria-busy');
}
}
}
};
const stopScan = () => {
clearTimeout(retryTimer);
scanController?.abort();
};
const startScan = () => {
stopScan();
scanController = new AbortController();
tbody.replaceChildren();
status.textContent = '';
refreshButton.disabled = true;
refreshButton.setAttribute('aria-busy', 'true');
scan(scanController.signal);
};
staForm.querySelector('.scan').addEventListener('click', () => {
closeButton.setAttribute('aria-label', i18n('button.close'));
dialog.showModal();
document.documentElement.classList.add('modal-is-open');
startScan();
});
form.addEventListener('submit', (event) => {
event.preventDefault();
if (dialog.open && !refreshButton.disabled) {
startScan();
}
});
closeButton.addEventListener('click', () => dialog.close());
dialog.addEventListener('click', (event) => {
if (event.target === dialog) {
dialog.close();
}
});
dialog.addEventListener('close', () => {
if (!dialog.open) {
stopScan();
document.documentElement.classList.remove('modal-is-open');
}
});
};
document.addEventListener('DOMContentLoaded', async () => {
const lang = new Lang(document.getElementById('lang'));
await lang.build();
let networkType = 2;
const fillNetworkSettings = (settings) => {
setSelectValue("[name='type']", settings.type);
setInputValue("[name='hostname']", settings.hostname);
setCheckboxValue("[name='use_dhcp']", settings.use_dhcp);
for (const field of ['ip', 'gateway', 'subnet', 'dns']) {
setInputValue(`[name='static_config[${field}]']`, settings.static_config[field]);
}
const fillData = (data) => {
networkType = data.type;
setSelectValue("[name='type']", networkType);
if (settings.ethernet?.gpio) {
for (const pin of ['clk', 'mosi', 'miso', 'cs', 'irq', 'rst']) {
const value = settings.ethernet.gpio[pin];
setInputValue(`[name='ethernet[gpio][${pin}]']`, value < 255 ? value : '');
}
}
const canScan = networkType === 2;
document.getElementById('wifi-scan-controls').disabled = !canScan;
document.querySelector('#scan-unavailable').classList.toggle('hidden', canScan);
setInputValue("[name='hostname']", data.hostname);
setCheckboxValue("[name='use_dhcp']", data.use_dhcp);
setInputValue("[name='static_config[ip]']", data.static_config.ip);
setInputValue("[name='static_config[gateway]']", data.static_config.gateway);
setInputValue("[name='static_config[subnet]']", data.static_config.subnet);
setInputValue("[name='static_config[dns]']", data.static_config.dns);
setBusy('#network-settings-busy', '#network-settings', false);
const ethernet = data.ethernet || {};
const ethernetConfigured = !!ethernet.configured;
const ethernetGpio = ethernet.gpio || {};
const gpioValue = (value) => (value ?? 255) < 255 ? value : '';
setInputValue("[name='ethernet[gpio][clk]']", gpioValue(ethernetGpio.clk));
setInputValue("[name='ethernet[gpio][mosi]']", gpioValue(ethernetGpio.mosi));
setInputValue("[name='ethernet[gpio][miso]']", gpioValue(ethernetGpio.miso));
setInputValue("[name='ethernet[gpio][cs]']", gpioValue(ethernetGpio.cs));
setInputValue("[name='ethernet[gpio][irq]']", gpioValue(ethernetGpio.irq));
setInputValue("[name='ethernet[gpio][rst]']", gpioValue(ethernetGpio.rst));
document.getElementById('ethernet-not-configured')?.classList.toggle('hidden', ethernetConfigured);
setBusy('#ethernet-settings-busy', '#ethernet-settings', false);
setInputValue("[name='wifi[sta][ssid]']", data.wifi.sta.ssid);
setInputValue("[name='wifi[sta][password]']", data.wifi.sta.password);
setInputValue("[name='wifi[sta][channel]']", data.wifi.sta.channel);
setBusy('#sta-settings-busy', '#sta-settings', false);
setInputValue("[name='wifi[ap][ssid]']", data.wifi.ap.ssid);
setInputValue("[name='wifi[ap][password]']", data.wifi.ap.password);
setInputValue("[name='wifi[ap][channel]']", data.wifi.ap.channel);
setBusy('#ap-settings-busy', '#ap-settings', false);
setInputValue("[name='wifi[ap][ssid]']", settings.wifi.ap.ssid);
setInputValue("[name='wifi[ap][password]']", settings.wifi.ap.password);
setInputValue("[name='wifi[ap][channel]']", settings.wifi.ap.channel);
};
const applyCapabilities = (info) => {
// pages and firmware ship together: the section stays hidden unless the build says it can do ethernet
const supported = info?.features?.ethernet === true;
document.getElementById('ethernet-section')?.classList.toggle('hidden', !supported);
const ethernetOption = document.querySelector("[name='type'] option[value='3']");
ethernetOption.hidden = !supported;
ethernetOption.disabled = !supported;
return supported;
const fillStaSettings = (settings) => {
setInputValue("[name='wifi[sta][ssid]']", settings.wifi.sta.ssid);
setInputValue("[name='wifi[sta][password]']", settings.wifi.sta.password);
setInputValue("[name='wifi[sta][channel]']", settings.wifi.sta.channel);
};
try {
const [response, infoResponse] = await Promise.all([
fetch("/api/network/settings", {
cache: "no-cache",
credentials: "include"
}),
fetch("/api/info", {
cache: "no-cache",
credentials: "include"
})
]);
const response = await fetch("/api/network/settings", {
cache: "no-cache",
credentials: "include"
});
if (!response.ok) {
throw new Error('Response not valid');
}
const info = infoResponse.ok ? await infoResponse.json() : null;
const ethernetSupported = applyCapabilities(info);
const result = await response.json();
fillData(result);
setupForm('#network-settings', fillData, ['hostname', 'static_config.ip', 'static_config.gateway', 'static_config.subnet', 'static_config.dns']);
// Keep scans disabled if an earlier request finishes after the connection method changes.
setupNetworkScanForm('#network-scan', '#networks', networkType === 2);
if (ethernetSupported) {
setupForm('#ethernet-settings', fillData);
const settings = await response.json();
if (!settings.ethernet) {
show('.ethernet .unsupported');
const gpioFields = document.querySelector('.ethernet .supported');
gpioFields.classList.add('hidden');
gpioFields.disabled = true;
}
setupForm('#sta-settings', fillData, ['wifi.sta.ssid', 'wifi.sta.password']);
setupForm('#ap-settings', fillData, ['wifi.ap.ssid', 'wifi.ap.password']);
fillNetworkSettings(settings);
fillStaSettings(settings);
setupForm('#network-settings', fillNetworkSettings, [
'hostname',
'static_config.ip', 'static_config.gateway', 'static_config.subnet', 'static_config.dns',
'wifi.ap.ssid', 'wifi.ap.password'
]);
setupForm('#sta-settings', fillStaSettings, [
'wifi.sta.ssid', 'wifi.sta.password'
]);
setupNetworkScan();
setBusy('#network-settings-busy', '#network-settings, #sta-settings', false);
} catch (error) {
console.log(error);
-174
View File
@@ -92,180 +92,6 @@ const setupForm = (formSelector, onResultCallback = null, noCastItems = []) => {
});
}
const setupNetworkScanForm = (formSelector, tableSelector, autorun = true) => {
const form = document.querySelector(formSelector);
if (!form) {
console.error("form not found");
return;
}
const url = form.action;
let button = form.querySelector('button[type="submit"]');
let defaultText;
const onSubmitFn = async (event) => {
if (event) {
event.preventDefault();
}
if (button) {
defaultText = button.innerHTML;
button.innerHTML = i18n('button.wait');
button.setAttribute('disabled', true);
button.setAttribute('aria-busy', true);
}
let table = document.querySelector(tableSelector);
if (!table) {
console.error("table not found");
return;
}
const onSuccess = async (response) => {
let result = await response.json();
console.log('networks: ', result);
let tbody = table.querySelector('tbody');
if (!tbody) {
tbody = table.createTBody();
}
while (tbody.rows.length > 0) {
tbody.rows[0].remove();
}
for (let i = 0; i < result.length; i++) {
let row = tbody.insertRow(-1);
row.classList.add("network");
row.dataset.ssid = result[i].hidden ? '' : result[i].ssid;
row.insertCell().textContent = `#${i + 1}`;
const nameCell = row.insertCell();
if (result[i].hidden) {
const hiddenName = document.createElement('i');
hiddenName.textContent = result[i].bssid;
nameCell.appendChild(hiddenName);
} else {
nameCell.textContent = result[i].ssid;
}
nameCell.onclick = (event) => {
const input = document.querySelector("[name='wifi[sta][ssid]']");
const ssid = row.dataset.ssid;
if (!input || !ssid) {
return;
}
input.value = ssid;
input.focus();
};
// info cell
let infoCell = row.insertCell();
// signal quality
let signalQualityIcon = document.createElement("i");
if (result[i].signal_quality > 80) {
signalQualityIcon.classList.add('icons-wifi-strength-4');
} else if (result[i].signal_quality > 60) {
signalQualityIcon.classList.add('icons-wifi-strength-3');
} else if (result[i].signal_quality > 40) {
signalQualityIcon.classList.add('icons-wifi-strength-2');
} else if (result[i].signal_quality > 20) {
signalQualityIcon.classList.add('icons-wifi-strength-1');
} else {
signalQualityIcon.classList.add('icons-wifi-strength-0');
}
let signalQualityContainer = document.createElement("span");
signalQualityContainer.dataset.tooltip = `${result[i].signal_quality}%`;
signalQualityContainer.appendChild(signalQualityIcon);
infoCell.appendChild(signalQualityContainer);
// auth
const authList = {
0: "Open",
1: "WEP",
2: "WPA",
3: "WPA2",
4: "WPA/WPA2",
5: "WPA/WPA2 Enterprise",
6: "WPA3",
7: "WPA2/WPA3",
8: "WAPI",
9: "OWE",
10: "WPA3 Enterprise"
};
let authIcon = document.createElement("i");
if (result[i].auth == 0) {
authIcon.classList.add('icons-unlocked');
} else {
authIcon.classList.add('icons-locked');
}
let authContainer = document.createElement("span");
authContainer.dataset.tooltip = (result[i].auth in authList) ? authList[result[i].auth] : "unknown";
authContainer.appendChild(authIcon);
infoCell.appendChild(authContainer);
}
if (button) {
button.innerHTML = defaultText;
button.removeAttribute('disabled');
button.removeAttribute('aria-busy');
}
};
const onFailed = async (response) => {
table.classList.remove('hidden');
if (button) {
button.innerHTML = defaultText;
button.removeAttribute('disabled');
button.removeAttribute('aria-busy');
}
};
let attempts = 6;
let attemptFn = async () => {
attempts--;
try {
let response = await fetch(url, {
cache: "no-cache",
credentials: "include"
});
if (response.status == 200) {
await onSuccess(response);
} else if (attempts <= 0) {
await onFailed(response);
} else {
setTimeout(attemptFn, 5000);
}
} catch (err) {
if (attempts <= 0) {
onFailed(err);
} else {
setTimeout(attemptFn, 10000);
}
}
};
attemptFn();
};
form.addEventListener('submit', onSubmitFn);
if (autorun) {
onSubmitFn();
}
}
const setBusy = (busySelector, contentSelector, value, parent = undefined) => {
if (!value) {
hide(busySelector, parent);
+20
View File
@@ -111,11 +111,31 @@ button.failed {
border-color: var(--pico-form-element-invalid-border-color);
}
#scan-dialog header {
margin-bottom: 0.25rem;
}
#scan-dialog thead {
position: sticky;
top: calc(-1 * var(--pico-block-spacing-vertical));
z-index: 1;
}
tr.network:hover {
--pico-background-color: var(--pico-primary-focus);
cursor: pointer;
}
tr.network button {
all: unset;
outline: revert;
overflow-wrap: anywhere;
}
tr.network .bssid {
font-style: italic;
}
.primary {
border: 0.25rem solid var(--pico-form-element-invalid-border-color);
padding: 1rem;