Files
led-controller/web/static/help.js
T
jimmyandClaude Opus 5.5 bfeeb958df feat: main's import/export, zones, audio API and web UI
Profiles, presets and sequences export and import as main's bundles.
Zones carry group_ids, preset_group_ids, sequence_ids, content_kind and
brightness; profiles seed rainbow as colour_cycle, and deleting a
profile also deletes its sequences. Groups, zones and sequences written
by older versions are migrated at boot.

/api/audio (status, event stream, start/stop, reset, anchor-bar,
devices) maps main's audio detector onto the mic's beat detector. Audio
start/stop picks whether mic beats or the BPM clock time sequences; the
clock's beats also go to the drivers while the mic is off.
/ws now greets from a post-handshake callback (httpd does not call the
handler for the upgrade).

The web UI is main's, plus v2's outputs editor, list search and
hostname/static IP settings; the bridge, Wi-Fi AP and led-tool parts
are gone. API tests follow main's behaviour (119 pass on the board).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:34:22 +13:00

256 lines
9.1 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
// Help modal
const helpBtn = document.getElementById('help-btn');
const helpModal = document.getElementById('help-modal');
const helpCloseBtn = document.getElementById('help-close-btn');
const mainMenuBtn = document.getElementById('main-menu-btn');
const mainMenuDropdown = document.getElementById('main-menu-dropdown');
if (helpBtn && helpModal) {
const openHelp = () => {
helpModal.classList.add('active');
switchHelpTab('overview');
};
helpBtn.addEventListener('click', openHelp);
}
if (helpCloseBtn && helpModal) {
helpCloseBtn.addEventListener('click', () => {
helpModal.classList.remove('active');
});
}
const helpTabButtons = document.querySelectorAll('[data-help-tab]');
const helpTabPanels = document.querySelectorAll('[data-help-panel]');
function switchHelpTab(tabId) {
if (!tabId) tabId = 'overview';
for (const btn of helpTabButtons) {
const on = btn.getAttribute('data-help-tab') === tabId;
btn.classList.toggle('active', on);
btn.setAttribute('aria-selected', on ? 'true' : 'false');
}
for (const panel of helpTabPanels) {
const on = panel.getAttribute('data-help-panel') === tabId;
panel.classList.toggle('active', on);
panel.hidden = !on;
}
}
for (const btn of helpTabButtons) {
btn.addEventListener('click', () => {
switchHelpTab(btn.getAttribute('data-help-tab'));
});
}
// Mobile main menu: forward clicks to existing header buttons
if (mainMenuBtn && mainMenuDropdown) {
mainMenuBtn.addEventListener('click', () => {
mainMenuDropdown.classList.toggle('open');
const zonesMenuDropdown = document.getElementById('zones-menu-dropdown');
const zonesMenuBtn = document.getElementById('zones-menu-btn');
if (zonesMenuDropdown) zonesMenuDropdown.classList.remove('open');
if (zonesMenuBtn) zonesMenuBtn.setAttribute('aria-expanded', 'false');
});
mainMenuDropdown.addEventListener('click', (event) => {
const target = event.target;
if (target && target.matches('button[data-target]')) {
const id = target.getAttribute('data-target');
const realBtn = document.getElementById(id);
if (realBtn) {
realBtn.click();
}
mainMenuDropdown.classList.remove('open');
}
});
}
// Settings modal wiring (reusing existing settings endpoints).
const settingsButton = document.getElementById('settings-btn');
const settingsModal = document.getElementById('settings-modal');
const settingsCloseButton = document.getElementById('settings-close-btn');
const showSettingsMessage = (text, type = 'success') => {
const messageEl = document.getElementById('settings-message');
if (!messageEl) return;
messageEl.textContent = text;
messageEl.className = `message ${type} show`;
setTimeout(() => {
messageEl.classList.remove('show');
}, 5000);
};
async function loadDeviceSettings() {
try {
const response = await fetch('/settings');
const data = await response.json();
const nameInput = document.getElementById('device-name-input');
if (nameInput && data && typeof data === 'object') {
nameInput.value = data.device_name || 'led-controller';
}
const chInput = document.getElementById('wifi-channel-input');
if (chInput && data && typeof data === 'object') {
const ch = data.wifi_channel;
chInput.value =
ch !== undefined && ch !== null && ch !== '' ? String(ch) : '6';
}
const hostInput = document.getElementById('hostname-input');
if (hostInput && staticIpsList && data && typeof data === 'object') {
hostInput.value = data.hostname || '';
staticIpsList.innerHTML = '';
(Array.isArray(data.static_ips) ? data.static_ips : []).forEach(addStaticIpRow);
}
} catch (error) {
console.error('Error loading device settings:', error);
}
}
const staticIpsList = document.getElementById('static-ips-list');
const STATIC_IP_FIELDS = [
['ip', 'IP address', '192.168.1.50'],
['netmask', 'Netmask', '255.255.255.0'],
['gw', 'Gateway', '192.168.1.1'],
['dns', 'DNS (optional)', 'gateway'],
];
function addStaticIpRow(entry = {}) {
if (!staticIpsList) return;
const row = document.createElement('div');
row.className = 'static-ip-row';
row.style.cssText = 'display:flex;flex-wrap:wrap;gap:0.5rem;margin:0.5rem 0;align-items:center;';
STATIC_IP_FIELDS.forEach(([key, label, placeholder]) => {
const input = document.createElement('input');
input.type = 'text';
input.dataset.key = key;
input.placeholder = `${label} (${placeholder})`;
input.title = label;
input.value = entry[key] || '';
input.style.cssText = 'flex:1 1 9rem;min-width:0;';
row.appendChild(input);
});
const remove = document.createElement('button');
remove.type = 'button';
remove.className = 'btn btn-secondary';
remove.textContent = 'Remove';
remove.addEventListener('click', () => row.remove());
row.appendChild(remove);
staticIpsList.appendChild(row);
}
const IPV4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
const validIp = (v) => {
const m = IPV4.exec(v);
return !!m && m.slice(1).every((o) => Number(o) <= 255);
};
function readStaticIps() {
const out = [];
for (const row of staticIpsList.querySelectorAll('.static-ip-row')) {
const entry = {};
row.querySelectorAll('input').forEach((i) => {
const v = i.value.trim();
if (v) entry[i.dataset.key] = v;
});
if (!Object.keys(entry).length) continue;
if (!entry.ip) throw new Error('Every static IP needs an IP address');
for (const [k, v] of Object.entries(entry)) {
if (!validIp(v)) throw new Error(`"${v}" is not a valid ${k} address`);
}
out.push(entry);
}
return out;
}
const addIpButton = document.getElementById('static-ip-add-btn');
if (addIpButton) addIpButton.addEventListener('click', () => addStaticIpRow());
if (settingsButton && settingsModal) {
settingsButton.addEventListener('click', () => {
settingsModal.classList.add('active');
loadDeviceSettings();
});
}
if (settingsCloseButton && settingsModal) {
settingsCloseButton.addEventListener('click', () => {
settingsModal.classList.remove('active');
});
}
const deviceForm = document.getElementById('device-form');
if (deviceForm) {
deviceForm.addEventListener('submit', async (e) => {
e.preventDefault();
const nameInput = document.getElementById('device-name-input');
const deviceName = nameInput ? nameInput.value.trim() : '';
if (!deviceName) {
showSettingsMessage('Device name is required', 'error');
return;
}
const chRaw = document.getElementById('wifi-channel-input')
? document.getElementById('wifi-channel-input').value
: '6';
const wifiChannel = parseInt(chRaw, 10);
if (Number.isNaN(wifiChannel) || wifiChannel < 1 || wifiChannel > 11) {
showSettingsMessage('WiFi channel must be between 1 and 11', 'error');
return;
}
try {
const response = await fetch('/settings/settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
device_name: deviceName,
wifi_channel: wifiChannel,
}),
});
const result = await response.json();
if (response.ok) {
showSettingsMessage(
'Device settings saved. They will apply on next restart where relevant.',
'success',
);
} else {
showSettingsMessage(`Error: ${result.error || 'Failed to save device name'}`, 'error');
}
} catch (error) {
showSettingsMessage(`Error: ${error.message}`, 'error');
}
});
}
const networkForm = document.getElementById('network-form');
if (networkForm) {
networkForm.addEventListener('submit', async (e) => {
e.preventDefault();
const hostname = document.getElementById('hostname-input').value.trim();
if (hostname && !/^[A-Za-z0-9-]+$/.test(hostname)) {
showSettingsMessage('Hostname may only contain letters, digits and hyphens', 'error');
return;
}
let staticIps;
try {
staticIps = readStaticIps();
} catch (error) {
showSettingsMessage(error.message, 'error');
return;
}
try {
const response = await fetch('/settings/settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hostname, static_ips: staticIps }),
});
const result = await response.json();
if (response.ok) {
showSettingsMessage('Network settings saved. Restart the controller to apply them.', 'success');
} else {
showSettingsMessage(`Error: ${result.error || 'Failed to save network settings'}`, 'error');
}
} catch (error) {
showSettingsMessage(`Error: ${error.message}`, 'error');
}
});
}
});