92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
// assets/js/app.js
|
|
import { initTabs } from './ui-tabs.js';
|
|
import { initLists } from './ui-list.js';
|
|
import { initCreate } from './ui-create.js';
|
|
import { initEditor } from './ui-editor.js';
|
|
import { initUserPanel } from './ui-user.js';
|
|
import { mountLogoutButton, ensureFloatingLogout } from './ui-auth.js';
|
|
import { apiAction } from './api.js';
|
|
|
|
|
|
/**
|
|
* Zeigt die App erst, wenn Auth validiert ist.
|
|
* Wichtig: KEIN finally → nur im Erfolgsfall UI freigeben (verhindert Flashing für Gäste).
|
|
*/
|
|
async function ensureAuthenticated() {
|
|
try {
|
|
const me = await apiAction('auth.me', { method: 'GET' });
|
|
if (!me?.ok || !me?.user) {
|
|
window.location.href = '/login.php';
|
|
return false;
|
|
}
|
|
window.__currentUser = me.user;
|
|
// ✅ nur für eingeloggte Nutzer: UI freigebensss
|
|
document.documentElement.classList.remove('auth-pending');
|
|
const appRoot = document.getElementById('app');
|
|
if (appRoot && appRoot.hasAttribute('hidden')) appRoot.removeAttribute('hidden');
|
|
return true;
|
|
} catch {
|
|
// apiAction leitet bei 401 ohnehin um
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function initAppFeatures() {
|
|
installGlobalModalGuards();
|
|
initTabs();
|
|
initLists();
|
|
initCreate();
|
|
initEditor();
|
|
initUserPanel();
|
|
|
|
// Logout-Buttons
|
|
mountLogoutButton('#btn-logout', { redirect: '/login.php' });
|
|
ensureFloatingLogout({ redirect: '/login.php' });
|
|
}
|
|
|
|
function installGlobalModalGuards() {
|
|
if (window.__modalGuardsInstalled) return;
|
|
window.__modalGuardsInstalled = true;
|
|
|
|
document.addEventListener('click', (evt) => {
|
|
const dlg = evt.target && evt.target.tagName === 'DIALOG'
|
|
? evt.target
|
|
: (evt.target && evt.target.closest ? evt.target.closest('dialog') : null);
|
|
if (!dlg) return;
|
|
if (evt.target === dlg) {
|
|
evt.preventDefault();
|
|
evt.stopPropagation();
|
|
}
|
|
}, true);
|
|
|
|
document.addEventListener('cancel', (evt) => {
|
|
const dlg = evt.target;
|
|
if (dlg && dlg.tagName === 'DIALOG') {
|
|
evt.preventDefault();
|
|
evt.stopPropagation();
|
|
}
|
|
}, true);
|
|
}
|
|
|
|
// Sync-Nachrichten aus dem Editor-Iframe (unverändert, aber mit credentials)
|
|
async function handleEditorMessages(ev) {
|
|
const msg = ev.data || {};
|
|
if (msg.source !== 'email-editor' || msg.type !== 'save') return;
|
|
|
|
try {
|
|
return;
|
|
} catch (e) {
|
|
console.error('refs sync skipped', e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
const ok = await ensureAuthenticated();
|
|
if (!ok) return; // Gast → Redirect, UI bleibt verborgen
|
|
initAppFeatures(); // Eingeloggt → App initialisieren
|
|
});
|
|
|
|
window.addEventListener('message', handleEditorMessages);
|