// 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 = ''; 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; }); }; })();