(function () { const root = document.getElementById("custom-pwa-admin-root"); if (!root) { return; } const settingsGetUrl = root.dataset.settingsGetUrl || ""; const settingsSaveUrl = root.dataset.settingsSaveUrl || ""; const vapidSubjectInput = document.getElementById("custom-pwa-vapid-subject"); const vapidPublicKeyInput = document.getElementById("custom-pwa-vapid-public-key"); const vapidPrivateKeyInput = document.getElementById("custom-pwa-vapid-private-key"); const clearVapidPrivateKeyInput = document.getElementById("custom-pwa-clear-vapid-private-key"); const emailFallbackInput = document.getElementById("custom-pwa-email-fallback"); const smsCriticalFallbackInput = document.getElementById("custom-pwa-sms-critical-fallback"); const privateKeyStatusEl = document.getElementById("custom-pwa-private-key-status"); const feedbackEl = document.getElementById("custom-pwa-admin-feedback"); const saveButton = document.getElementById("custom-pwa-save-settings"); const refreshButton = document.getElementById("custom-pwa-refresh-settings"); function setFeedback(message, isError) { if (!feedbackEl) { return; } feedbackEl.textContent = message || ""; feedbackEl.style.color = isError ? "#b3312d" : ""; } function setPrivateKeyStatus(configured) { if (!privateKeyStatusEl) { return; } privateKeyStatusEl.textContent = configured ? "configured" : "not configured"; } async function fetchJson(url, options) { const response = await fetch(url, { credentials: "same-origin", headers: { requesttoken: (window.OC && window.OC.requestToken) || "", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", ...(options && options.headers ? options.headers : {}), }, ...(options || {}), }); const text = await response.text(); let payload = null; try { payload = text ? JSON.parse(text) : null; } catch (_error) { payload = null; } if (!response.ok) { const message = payload && (payload.error || payload.message); throw new Error(message || ("Request failed (" + response.status + ")")); } return payload; } function readForm() { return { vapidSubject: (vapidSubjectInput && vapidSubjectInput.value ? vapidSubjectInput.value : "").trim(), vapidPublicKey: (vapidPublicKeyInput && vapidPublicKeyInput.value ? vapidPublicKeyInput.value : "").trim(), vapidPrivateKey: vapidPrivateKeyInput && vapidPrivateKeyInput.value ? vapidPrivateKeyInput.value : "", clearVapidPrivateKey: !!(clearVapidPrivateKeyInput && clearVapidPrivateKeyInput.checked), emailFallbackEnabled: !!(emailFallbackInput && emailFallbackInput.checked), smsCriticalFallbackEnabled: !!(smsCriticalFallbackInput && smsCriticalFallbackInput.checked), }; } function writeForm(data) { if (!data || typeof data !== "object") { return; } if (vapidSubjectInput) { vapidSubjectInput.value = data.vapidSubject || ""; } if (vapidPublicKeyInput) { vapidPublicKeyInput.value = data.vapidPublicKey || ""; } if (vapidPrivateKeyInput) { vapidPrivateKeyInput.value = ""; } if (clearVapidPrivateKeyInput) { clearVapidPrivateKeyInput.checked = false; } if (emailFallbackInput) { emailFallbackInput.checked = !!data.emailFallbackEnabled; } if (smsCriticalFallbackInput) { smsCriticalFallbackInput.checked = !!data.smsCriticalFallbackEnabled; } setPrivateKeyStatus(!!data.vapidPrivateKeyConfigured); } function encodeForm(data) { const params = new URLSearchParams(); Object.keys(data || {}).forEach((key) => { const value = data[key]; if (value === undefined || value === null) { return; } params.set(key, String(value)); }); return params.toString(); } async function refreshSettings() { if (!settingsGetUrl) { return; } setFeedback("Refreshing settings...", false); const payload = await fetchJson(settingsGetUrl, { method: "GET", headers: {} }); writeForm((payload && payload.data) || {}); setFeedback("CustomPWA settings refreshed.", false); } async function saveSettings() { if (!settingsSaveUrl) { return; } if (saveButton) { saveButton.disabled = true; } setFeedback("Saving CustomPWA settings...", false); try { const form = readForm(); const payload = await fetchJson(settingsSaveUrl, { method: "POST", body: encodeForm(form), }); writeForm((payload && payload.data) || {}); setFeedback("CustomPWA settings saved.", false); } catch (error) { setFeedback(error && error.message ? error.message : "Failed to save settings", true); } finally { if (saveButton) { saveButton.disabled = false; } } } if (saveButton) { saveButton.addEventListener("click", function () { saveSettings(); }); } if (refreshButton) { refreshButton.addEventListener("click", function () { refreshSettings().catch(function (error) { setFeedback(error && error.message ? error.message : "Failed to refresh settings", true); }); }); } })();