323 lines
9.4 KiB
JavaScript
323 lines
9.4 KiB
JavaScript
/**
|
|
* BeKultur - PWA, Theme & Offline State Manager
|
|
*/
|
|
(function () {
|
|
let isOffline = !navigator.onLine;
|
|
let bannerTimeout = null;
|
|
|
|
function t(key, params) {
|
|
if (typeof window.BeKulturT === 'function') {
|
|
return window.BeKulturT(key, params);
|
|
}
|
|
return key;
|
|
}
|
|
|
|
// ----------------------------------------------------
|
|
// 0. Dark / Light Theme Manager (System-Aware + Override)
|
|
// ----------------------------------------------------
|
|
function getSystemScheme() {
|
|
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
? 'dark'
|
|
: 'light';
|
|
}
|
|
|
|
function getStoredScheme() {
|
|
try {
|
|
const val = localStorage.getItem('color-scheme');
|
|
if (val === 'dark' || val === 'light') return val;
|
|
} catch (e) {}
|
|
return null;
|
|
}
|
|
|
|
function getResolvedScheme() {
|
|
return getStoredScheme() || getSystemScheme();
|
|
}
|
|
|
|
function applyThemeScheme(storedOverride) {
|
|
const meta = document.querySelector('meta[name="color-scheme"]');
|
|
const resolved = storedOverride === 'dark' || storedOverride === 'light'
|
|
? storedOverride
|
|
: getSystemScheme();
|
|
|
|
if (meta) {
|
|
meta.content = storedOverride === 'dark' || storedOverride === 'light'
|
|
? storedOverride
|
|
: 'light dark';
|
|
}
|
|
document.documentElement.setAttribute('data-theme', resolved);
|
|
}
|
|
|
|
function initThemeToggle() {
|
|
// Ensure initial attribute is synced
|
|
applyThemeScheme(getStoredScheme());
|
|
|
|
const toggleBtn = document.getElementById('themeToggleBtn');
|
|
if (toggleBtn) {
|
|
toggleBtn.addEventListener('click', () => {
|
|
const currentRendered = getResolvedScheme();
|
|
const target = currentRendered === 'dark' ? 'light' : 'dark';
|
|
const sys = getSystemScheme();
|
|
|
|
try {
|
|
if (target === sys) {
|
|
// Revert to system default when target matches system preference
|
|
localStorage.removeItem('color-scheme');
|
|
applyThemeScheme(null);
|
|
} else {
|
|
localStorage.setItem('color-scheme', target);
|
|
applyThemeScheme(target);
|
|
}
|
|
} catch (e) {
|
|
applyThemeScheme(target);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Listen for OS theme changes when following system default
|
|
if (window.matchMedia) {
|
|
const mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
if (mql.addEventListener) {
|
|
mql.addEventListener('change', () => {
|
|
if (!getStoredScheme()) {
|
|
applyThemeScheme(null);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Keep tabs in sync
|
|
window.addEventListener('storage', (e) => {
|
|
if (e.key === 'color-scheme') {
|
|
applyThemeScheme(getStoredScheme());
|
|
}
|
|
});
|
|
}
|
|
|
|
// 1. Service Worker Registration
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker
|
|
.register('/sw.js', { scope: '/' })
|
|
.then((reg) => {
|
|
// If updated service worker found
|
|
reg.addEventListener('updatefound', () => {
|
|
const installing = reg.installing;
|
|
if (installing) {
|
|
installing.addEventListener('statechange', () => {
|
|
if (installing.state === 'installed' && navigator.serviceWorker.controller) {
|
|
console.log('New BeKultur version available.');
|
|
}
|
|
});
|
|
}
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.warn('ServiceWorker registration failed:', err);
|
|
});
|
|
});
|
|
}
|
|
|
|
// 2. Offline Banner Component
|
|
function getOrCreateBanner() {
|
|
let banner = document.getElementById('offlineBanner');
|
|
if (!banner) {
|
|
banner = document.createElement('div');
|
|
banner.id = 'offlineBanner';
|
|
banner.className = 'offline-banner hidden';
|
|
document.body.prepend(banner);
|
|
}
|
|
return banner;
|
|
}
|
|
|
|
function showOfflineBanner() {
|
|
if (bannerTimeout) {
|
|
clearTimeout(bannerTimeout);
|
|
bannerTimeout = null;
|
|
}
|
|
const banner = getOrCreateBanner();
|
|
banner.className = 'offline-banner active';
|
|
banner.innerHTML = `
|
|
<div class="offline-banner-content">
|
|
<span class="offline-badge-pill">${t('pwa.offlineBadge')}</span>
|
|
<span class="offline-banner-text">${t('pwa.offlineText')}</span>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
function showOnlineBanner() {
|
|
if (bannerTimeout) {
|
|
clearTimeout(bannerTimeout);
|
|
bannerTimeout = null;
|
|
}
|
|
const banner = getOrCreateBanner();
|
|
banner.className = 'offline-banner online';
|
|
banner.innerHTML = `
|
|
<div class="offline-banner-content">
|
|
<span class="online-badge-pill">${t('pwa.onlineBadge')}</span>
|
|
<span class="offline-banner-text">${t('pwa.onlineText')}</span>
|
|
</div>
|
|
`;
|
|
bannerTimeout = setTimeout(() => {
|
|
banner.className = 'offline-banner hidden';
|
|
bannerTimeout = null;
|
|
}, 2800);
|
|
}
|
|
|
|
// 3. Apply Offline Safeguards to UI
|
|
function applyOfflineState(offline) {
|
|
isOffline = offline;
|
|
|
|
if (offline) {
|
|
showOfflineBanner();
|
|
} else {
|
|
showOnlineBanner();
|
|
}
|
|
|
|
// --- A. Homework Checkboxes & Exclude Buttons ---
|
|
const assignmentToggles = document.querySelectorAll('.assignment-toggle');
|
|
assignmentToggles.forEach((checkbox) => {
|
|
const card = checkbox.closest('.assignment-card');
|
|
const isExcluded = card && card.dataset.excluded === 'true';
|
|
|
|
if (offline) {
|
|
checkbox.disabled = true;
|
|
checkbox.title = t('pwa.offlineCheckboxTitle');
|
|
} else {
|
|
// Only re-enable if not excluded
|
|
checkbox.disabled = isExcluded;
|
|
checkbox.title = '';
|
|
}
|
|
});
|
|
|
|
const excludeButtons = document.querySelectorAll('.btn-exclude, .btn-disinclude');
|
|
excludeButtons.forEach((btn) => {
|
|
if (offline) {
|
|
btn.disabled = true;
|
|
btn.title = t('pwa.offlineExcludeTitle');
|
|
} else {
|
|
btn.disabled = false;
|
|
const card = btn.closest('.assignment-card');
|
|
const isExcl = card && card.dataset.excluded === 'true';
|
|
btn.title = isExcl ? t('dashboard.btnRestoreTitle') : t('dashboard.btnExcludeTitle');
|
|
}
|
|
});
|
|
|
|
// --- B. Private Lesson Request Form ---
|
|
const lessonForm = document.getElementById('privateLessonForm');
|
|
const formSelects = document.querySelectorAll('#privateLessonForm select, #privateLessonForm button[type="submit"]');
|
|
|
|
let noticeEl = document.getElementById('privateLessonOfflineNotice');
|
|
if (!noticeEl && lessonForm) {
|
|
noticeEl = document.createElement('div');
|
|
noticeEl.id = 'privateLessonOfflineNotice';
|
|
noticeEl.className = 'alert alert-warning hidden';
|
|
noticeEl.style.marginBottom = '12px';
|
|
noticeEl.innerHTML = t('pwa.offlinePrivateNotice');
|
|
lessonForm.prepend(noticeEl);
|
|
}
|
|
|
|
if (formSelects.length > 0) {
|
|
formSelects.forEach((el) => {
|
|
el.disabled = offline;
|
|
});
|
|
if (noticeEl) {
|
|
if (offline) {
|
|
noticeEl.classList.remove('hidden');
|
|
} else {
|
|
noticeEl.classList.add('hidden');
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- C. Station Buttons in Syllabus ---
|
|
const stationButtons = document.querySelectorAll('.station-btn');
|
|
stationButtons.forEach((btn) => {
|
|
if (offline) {
|
|
btn.classList.add('station-btn-offline');
|
|
btn.title = t('pwa.offlineStationTitle');
|
|
} else {
|
|
btn.classList.remove('station-btn-offline');
|
|
btn.title = '';
|
|
}
|
|
});
|
|
|
|
// --- D. Sync Buttons Across All Pages ---
|
|
const syncButtonSelectors = [
|
|
'#btnSyncRecent',
|
|
'#btnSyncAll',
|
|
'#btnSyncSyllabus',
|
|
'#btnSyncPrivateLessons',
|
|
'#btnSyncExams',
|
|
'#btnEmptySync'
|
|
];
|
|
|
|
syncButtonSelectors.forEach((sel) => {
|
|
const btn = document.querySelector(sel);
|
|
if (!btn) return;
|
|
|
|
if (offline) {
|
|
if (!btn.dataset.onlineLabel) {
|
|
btn.dataset.onlineLabel = btn.textContent.trim();
|
|
}
|
|
btn.disabled = true;
|
|
btn.classList.add('btn-offline-disabled');
|
|
btn.title = t('pwa.offlineSyncTitle');
|
|
btn.textContent = `${btn.dataset.onlineLabel} ${t('pwa.offlineSuffix')}`;
|
|
} else {
|
|
btn.disabled = false;
|
|
btn.classList.remove('btn-offline-disabled');
|
|
btn.title = '';
|
|
if (btn.dataset.onlineLabel) {
|
|
btn.textContent = btn.dataset.onlineLabel;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// 4. Click interceptor on homework checkboxes when offline
|
|
document.addEventListener(
|
|
'click',
|
|
(e) => {
|
|
if (!isOffline) return;
|
|
|
|
const target = e.target;
|
|
if (target.classList && target.classList.contains('assignment-toggle')) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
alert(t('dashboard.offlineToggleAlert'));
|
|
}
|
|
},
|
|
true
|
|
);
|
|
|
|
// 5. Connection Listeners
|
|
window.addEventListener('online', () => {
|
|
applyOfflineState(false);
|
|
});
|
|
|
|
window.addEventListener('offline', () => {
|
|
applyOfflineState(true);
|
|
});
|
|
|
|
// 6. Initial Application on DOM Ready
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
initThemeToggle();
|
|
if (!navigator.onLine) {
|
|
applyOfflineState(true);
|
|
}
|
|
});
|
|
} else {
|
|
initThemeToggle();
|
|
if (!navigator.onLine) {
|
|
applyOfflineState(true);
|
|
}
|
|
}
|
|
|
|
// Export helper globally
|
|
window.BeKulturPWA = {
|
|
isOffline: () => isOffline,
|
|
refreshState: () => applyOfflineState(!navigator.onLine),
|
|
getTheme: () => getResolvedScheme()
|
|
};
|
|
})();
|