Files
led-controller/web/static/ui_confirm.js
T
jimmyandClaude Haiku 5.5 edf35e9c71 Channel 11 defaults, ESP-NOW driver config and rig/desktop updates
Moves the controller, bridge and led-driver default ESP-NOW channel to 11 and
updates the led-driver and led-tool submodule pointers. Includes the rest of the
working tree: web UI, desktop and sim-controller changes, API and bridge code,
graphify output, and the .gitea and fixture-driver folders.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-21 01:19:02 +13:00

63 lines
2.6 KiB
JavaScript

// In-page confirmation dialog: uiConfirm(message, { okLabel }) resolves true (OK) or false (Cancel).
// Used instead of window.confirm(), which some browsers and webviews block (it then returns false at
// once, so a Delete button silently does nothing). Enter confirms, Escape or a click outside cancels.
(function () {
let modal = null;
let pending = null; // the open dialog's resolve; a new one cancels it
function build() {
modal = document.createElement('div');
modal.className = 'modal ui-confirm-modal';
modal.setAttribute('role', 'alertdialog');
modal.setAttribute('aria-modal', 'true');
modal.innerHTML =
'<div class="modal-content">' +
'<p class="ui-confirm-message" id="ui-confirm-message"></p>' +
'<div class="modal-actions">' +
'<button type="button" class="btn btn-secondary" data-ui-confirm="cancel">Cancel</button>' +
'<button type="button" class="btn btn-danger" data-ui-confirm="ok">OK</button>' +
'</div></div>';
modal.setAttribute('aria-labelledby', 'ui-confirm-message');
modal.addEventListener('click', (e) => {
const which = e.target && e.target.dataset ? e.target.dataset.uiConfirm : undefined;
if (which === 'ok') close(true);
else if (which === 'cancel' || e.target === modal) close(false);
});
modal.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
e.preventDefault();
e.stopPropagation(); // not the modal underneath
close(false);
} else if (e.key === 'Enter') {
e.preventDefault();
close(true);
}
});
document.body.appendChild(modal);
}
function close(result) {
if (modal) modal.classList.remove('active');
const resolve = pending;
pending = null;
if (resolve) resolve(result);
}
window.uiConfirm = function (message, opts) {
if (!modal) build();
if (pending) close(false);
const o = opts || {};
modal.querySelector('.ui-confirm-message').textContent = String(message);
const ok = modal.querySelector('[data-ui-confirm="ok"]');
// "Delete group …?" gets a Delete button, unless the caller names one
const verb = /^(Delete|Remove|Clear|Reboot)\b/.exec(String(message));
ok.textContent = o.okLabel || (verb ? verb[1] : 'OK');
modal.classList.add('active');
ok.focus();
return new Promise((resolve) => {
pending = resolve;
});
};
})();