78 lines
3.4 KiB
JavaScript
78 lines
3.4 KiB
JavaScript
/* ============================================================
|
||
Black Sheep Bluff – globales Frontend-Script (auf jeder Seite)
|
||
- Lucide-Icons rendern
|
||
- Spenden-Link (config-gesteuert)
|
||
- Bild-Fallbacks
|
||
- Auth-aware Navigation (Login/Profil)
|
||
- Cookie-Banner
|
||
Exponiert window.BSB.{ renderIcons, onUser }.
|
||
============================================================ */
|
||
window.BSB = (function () {
|
||
function renderIcons() { if (window.lucide) lucide.createIcons(); }
|
||
|
||
// Bild-Fallbacks: fehlende Bilder durch passende Icons ersetzen
|
||
window.bsbImgFail = function (img) {
|
||
const i = document.createElement('i');
|
||
i.setAttribute('data-lucide', img.dataset.icon || 'image');
|
||
img.replaceWith(i);
|
||
renderIcons();
|
||
};
|
||
window.bsbGalleryFail = function (img) {
|
||
const f = img.closest('figure');
|
||
if (f) { f.classList.add('empty'); f.innerHTML = '<i data-lucide="image"></i><span>Bild hier ablegen</span>'; renderIcons(); }
|
||
};
|
||
|
||
// Spenden-Link (config-gesteuert)
|
||
(function () {
|
||
const u = (window.BSB_CONFIG && window.BSB_CONFIG.donationUrl) || '';
|
||
if (u) { const d = document.getElementById('footDonate'); if (d) { d.href = u; d.classList.remove('hidden'); } }
|
||
})();
|
||
|
||
// Admin-Impersonation: Banner „Zurück zum Admin"
|
||
function bsbImpBar(on){
|
||
if(!on || document.getElementById('imp-bar')) return;
|
||
const b=document.createElement('div'); b.id='imp-bar';
|
||
b.style.cssText='position:fixed;top:0;left:0;right:0;z-index:5000;background:#a86f00;color:#fff;font:600 13px/1.4 Inter,system-ui,sans-serif;padding:8px 14px;display:flex;align-items:center;justify-content:center;gap:12px;flex-wrap:wrap';
|
||
b.innerHTML='👁 Admin-Modus – du bist als anderer User angemeldet <button type="button" style="background:#fff;color:#a86f00;border:none;border-radius:6px;padding:5px 12px;font-weight:700;cursor:pointer">Zurück zum Admin</button>';
|
||
b.querySelector('button').onclick=async()=>{ try{ await fetch('/api/auth/stop_impersonating',{method:'POST'}); }catch(e){} location.href='/admin'; };
|
||
document.body.appendChild(b); document.body.style.paddingTop=(b.offsetHeight||38)+'px';
|
||
}
|
||
|
||
// ── Auth-aware Navigation + User-Broadcast ──
|
||
let userResolved = false, currentUser = null;
|
||
const userCbs = [];
|
||
function onUser(cb) { if (userResolved) cb(currentUser); else userCbs.push(cb); }
|
||
(async () => {
|
||
try {
|
||
const r = await fetch('/api/auth/me');
|
||
const d = await r.json();
|
||
currentUser = (d && d.user) ? d.user : null;
|
||
bsbImpBar(d && d.impersonating);
|
||
} catch (e) { currentUser = null; }
|
||
userResolved = true;
|
||
if (currentUser) {
|
||
const p = document.getElementById('navProfile'); if (p) p.classList.remove('hidden');
|
||
const l = document.getElementById('navLogin'); if (l) l.classList.add('hidden');
|
||
}
|
||
userCbs.forEach(cb => cb(currentUser));
|
||
})();
|
||
|
||
// ── Cookie-Banner ──
|
||
(function () {
|
||
const KEY = 'bsb_cookie_consent';
|
||
const banner = document.getElementById('cookieBanner');
|
||
if (!banner) return;
|
||
function show() { requestAnimationFrame(() => banner.classList.add('show')); }
|
||
function hide() { banner.classList.remove('show'); }
|
||
window.Cookie = {
|
||
accept(level) {
|
||
try { localStorage.setItem(KEY, JSON.stringify({ level, ts: Date.now() })); } catch (e) {}
|
||
hide();
|
||
}
|
||
};
|
||
if (!localStorage.getItem(KEY)) setTimeout(show, 600);
|
||
})();
|
||
|
||
renderIcons();
|
||
return { renderIcons, onUser };
|
||
})();
|