/* * Gemeinsamer Hinweis für Code 99: Daten wurden geändert, Seite neu laden. * * Dispatcher- und Mobil-Views rufen window.showReloadNotice(text) aus ihrer * bestehenden WebSocket-Verbindung auf. Admin-Seiten erhalten über den * optionalen Watcher eine eigene Verbindung, damit auch dort Änderungen * anderer Administratoren sichtbar werden. */ (function () { 'use strict'; const HINT = 'Die Webseite muss neu geladen werden, damit die Änderungen sichtbar werden.'; let dialog = null; let adminSocket = null; let adminRetryTimer = null; let adminStopped = false; function ensureDialog() { if (dialog) return dialog; dialog = document.createElement('dialog'); dialog.id = 'reloadNoticeModal'; dialog.className = 'reload-notice-dialog'; dialog.setAttribute('aria-labelledby', 'reloadNoticeTitle'); const article = document.createElement('article'); const header = document.createElement('header'); const title = document.createElement('h3'); title.id = 'reloadNoticeTitle'; title.textContent = 'Daten geändert'; header.appendChild(title); const body = document.createElement('div'); body.className = 'reload-notice-body'; const text = document.createElement('p'); text.id = 'reloadNoticeText'; text.textContent = 'Die Daten wurden geändert.'; const hint = document.createElement('p'); hint.className = 'reload-notice-hint'; hint.textContent = HINT; body.append(text, hint); const footer = document.createElement('footer'); const laterButton = document.createElement('button'); laterButton.type = 'button'; laterButton.className = 'secondary'; laterButton.textContent = 'Später'; laterButton.addEventListener('click', closeReloadNotice); const restartButton = document.createElement('button'); restartButton.type = 'button'; restartButton.textContent = 'Neustarten'; restartButton.addEventListener('click', () => window.location.reload()); footer.append(laterButton, restartButton); article.append(header, body, footer); dialog.appendChild(article); document.body.appendChild(dialog); // Escape entspricht "Später": Dialog schließen, keine weitere Aktion. dialog.addEventListener('cancel', event => { event.preventDefault(); closeReloadNotice(); }); return dialog; } function closeReloadNotice() { if (dialog && dialog.open) dialog.close(); } function showReloadNotice(text) { const currentDialog = ensureDialog(); const textElement = currentDialog.querySelector('#reloadNoticeText'); if (textElement) { textElement.textContent = text && String(text).trim() ? String(text).trim() : 'Die Daten wurden geändert.'; } if (!currentDialog.open) { if (typeof currentDialog.showModal === 'function') { currentDialog.showModal(); } else { currentDialog.setAttribute('open', ''); } } } function appUrl(path) { if (typeof window.CMBNET_APP_URL === 'function') { return window.CMBNET_APP_URL(path); } const base = (window.CMBNET_BASE_URL || '').replace(/\/+$/, ''); const clean = String(path == null ? '' : path).replace(/^\/+/, ''); return clean ? `${base}/${clean}` : (base || '/'); } function scheduleAdminReconnect() { if (adminStopped || adminRetryTimer) return; adminRetryTimer = setTimeout(() => { adminRetryTimer = null; connectAdminWatcher(); }, 3000); } async function connectAdminWatcher() { if (adminStopped) return; const tenant = window.CMBNET_TENANT || ''; if (!tenant) return; try { const response = await fetch(appUrl(`${tenant}/api/auth/ws-token`), { credentials: 'same-origin' }); if (!response.ok) throw new Error(`HTTP ${response.status}`); const data = await response.json(); if (!data.token) throw new Error('Kein WebSocket-Token'); const wsUrl = `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://` + `${window.location.host}${appUrl(`${tenant}/ws`)}`; const ws = new WebSocket(wsUrl); adminSocket = ws; ws.addEventListener('open', () => { ws.send(JSON.stringify({ code: 0, msg: data.token, session_id: window.CMBNET_CLIENT_SESSION_ID || '' })); }); ws.addEventListener('message', event => { let message; try { message = JSON.parse(event.data); } catch (error) { return; } if (Number(message.code) === 1) { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({code: 2, msg: 'pong'})); } return; } if (Number(message.code) !== 99) return; const text = typeof message.msg === 'string' ? message.msg : ''; showReloadNotice(text); }); ws.addEventListener('close', () => { if (adminSocket === ws) adminSocket = null; scheduleAdminReconnect(); }); ws.addEventListener('error', () => { if (adminSocket === ws) adminSocket = null; scheduleAdminReconnect(); }); } catch (error) { scheduleAdminReconnect(); } } window.showReloadNotice = showReloadNotice; window.closeReloadNotice = closeReloadNotice; document.addEventListener('DOMContentLoaded', () => { ensureDialog(); if (window.CMBNET_ADMIN_RELOAD_WATCH) { connectAdminWatcher(); } }); window.addEventListener('beforeunload', () => { adminStopped = true; if (adminRetryTimer) clearTimeout(adminRetryTimer); if (adminSocket) adminSocket.close(); }); })();