feat: upgrade to SQLite session & data store with zero-data-loss migration, dark theme, and Turkish i18n

This commit is contained in:
tyuXX 2026-11-09 20:54:26 +00:00
parent 1af973d001
commit 56274f479b
27 changed files with 4222 additions and 1360 deletions

View file

@ -1,10 +1,98 @@
/**
* BeKultur - PWA & Offline State Manager
* 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', () => {
@ -50,8 +138,8 @@
banner.className = 'offline-banner active';
banner.innerHTML = `
<div class="offline-banner-content">
<span class="offline-badge-pill">⚡ Offline</span>
<span class="offline-banner-text">Showing locally cached data. Actions requiring connection are paused.</span>
<span class="offline-badge-pill">${t('pwa.offlineBadge')}</span>
<span class="offline-banner-text">${t('pwa.offlineText')}</span>
</div>
`;
}
@ -65,8 +153,8 @@
banner.className = 'offline-banner online';
banner.innerHTML = `
<div class="offline-banner-content">
<span class="online-badge-pill">✓ Online</span>
<span class="offline-banner-text">Connection restored. Live features re-enabled.</span>
<span class="online-badge-pill">${t('pwa.onlineBadge')}</span>
<span class="offline-banner-text">${t('pwa.onlineText')}</span>
</div>
`;
bannerTimeout = setTimeout(() => {
@ -93,7 +181,7 @@
if (offline) {
checkbox.disabled = true;
checkbox.title = 'Offline mode: Homework status cannot be changed without connection.';
checkbox.title = t('pwa.offlineCheckboxTitle');
} else {
// Only re-enable if not excluded
checkbox.disabled = isExcluded;
@ -105,16 +193,17 @@
excludeButtons.forEach((btn) => {
if (offline) {
btn.disabled = true;
btn.title = 'Offline mode: Homework cannot be excluded without connection.';
btn.title = t('pwa.offlineExcludeTitle');
} else {
btn.disabled = false;
btn.title = btn.textContent.trim() === 'Restore' ? 'Restore homework' : 'Exclude homework';
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 btnSubmitLesson = document.getElementById('btnSubmitRequest');
const formSelects = document.querySelectorAll('#privateLessonForm select, #privateLessonForm button[type="submit"]');
let noticeEl = document.getElementById('privateLessonOfflineNotice');
@ -123,7 +212,7 @@
noticeEl.id = 'privateLessonOfflineNotice';
noticeEl.className = 'alert alert-warning hidden';
noticeEl.style.marginBottom = '12px';
noticeEl.innerHTML = '⚡ <strong>Çevrimdışı mod:</strong> Yeni özel ders başvurusu yapmak için internet bağlantısı gereklidir.';
noticeEl.innerHTML = t('pwa.offlinePrivateNotice');
lessonForm.prepend(noticeEl);
}
@ -145,7 +234,7 @@
stationButtons.forEach((btn) => {
if (offline) {
btn.classList.add('station-btn-offline');
btn.title = 'Offline mode: Station selection requires connection.';
btn.title = t('pwa.offlineStationTitle');
} else {
btn.classList.remove('station-btn-offline');
btn.title = '';
@ -172,8 +261,8 @@
}
btn.disabled = true;
btn.classList.add('btn-offline-disabled');
btn.title = 'Offline mode: Sync requires internet connection.';
btn.textContent = `${btn.dataset.onlineLabel} (Offline)`;
btn.title = t('pwa.offlineSyncTitle');
btn.textContent = `${btn.dataset.onlineLabel} ${t('pwa.offlineSuffix')}`;
} else {
btn.disabled = false;
btn.classList.remove('btn-offline-disabled');
@ -195,7 +284,7 @@
if (target.classList && target.classList.contains('assignment-toggle')) {
e.preventDefault();
e.stopPropagation();
alert('Çevrimdışı moddasınız. Ödev durumu internet bağlantısı olmadan değiştirilemez.');
alert(t('dashboard.offlineToggleAlert'));
}
},
true
@ -213,11 +302,13 @@
// 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);
}
@ -226,6 +317,7 @@
// Export helper globally
window.BeKulturPWA = {
isOffline: () => isOffline,
refreshState: () => applyOfflineState(!navigator.onLine)
refreshState: () => applyOfflineState(!navigator.onLine),
getTheme: () => getResolvedScheme()
};
})();