feat: add PWA and offline mode support with local caching and action safeguards
This commit is contained in:
parent
039b5ac483
commit
128a5f7553
15 changed files with 589 additions and 0 deletions
|
|
@ -25,6 +25,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
|
||||
// Exclude / Restore button
|
||||
if (target.classList.contains('btn-exclude') || target.classList.contains('btn-disinclude')) {
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
alert('Çevrimdışı moddasınız. Ödev gizleme/geri alma işlemi için internet bağlantısı gereklidir.');
|
||||
return;
|
||||
}
|
||||
const assignmentId = target.dataset.id;
|
||||
const card = target.closest('.assignment-card');
|
||||
if (!card) return;
|
||||
|
|
@ -78,6 +82,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
const target = e.target;
|
||||
if (!target.classList.contains('assignment-toggle')) return;
|
||||
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
target.checked = !target.checked;
|
||||
alert('Çevrimdışı moddasınız. Ödev durumu internet bağlantısı olmadan değiştirilemez.');
|
||||
return;
|
||||
}
|
||||
|
||||
const assignmentId = target.dataset.id;
|
||||
const isChecked = target.checked;
|
||||
const card = target.closest('.assignment-card');
|
||||
|
|
@ -303,6 +313,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
// Sync actions
|
||||
if (btnSyncRecent) {
|
||||
btnSyncRecent.addEventListener('click', async () => {
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
showAlert('Çevrimdışı moddasınız. Senkronizasyon için internet bağlantısı gereklidir.', 'danger');
|
||||
return;
|
||||
}
|
||||
btnSyncRecent.disabled = true;
|
||||
btnSyncRecent.textContent = 'Syncing...';
|
||||
showAlert('Fetching recent assignments...', 'info');
|
||||
|
|
@ -327,6 +341,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
|
||||
if (btnSyncAll) {
|
||||
btnSyncAll.addEventListener('click', async () => {
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
showAlert('Çevrimdışı moddasınız. Senkronizasyon için internet bağlantısı gereklidir.', 'danger');
|
||||
return;
|
||||
}
|
||||
btnSyncAll.disabled = true;
|
||||
btnSyncAll.textContent = 'Syncing all...';
|
||||
showAlert('Syncing full history from portal...', 'info');
|
||||
|
|
|
|||
|
|
@ -96,6 +96,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
// Sync Exams Action
|
||||
// ----------------------------------------------------
|
||||
async function handleSync() {
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
showAlert('Çevrimdışı moddasınız. Sınav senkronizasyonu için internet bağlantısı gereklidir.', 'danger');
|
||||
return;
|
||||
}
|
||||
|
||||
const triggerBtn = btnSyncExams || btnEmptySync;
|
||||
if (triggerBtn) {
|
||||
triggerBtn.disabled = true;
|
||||
|
|
|
|||
|
|
@ -123,6 +123,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
e.preventDefault();
|
||||
hideFeedback();
|
||||
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
showFeedback('Çevrimdışı moddasınız. Yeni özel ders başvurusu yapmak için internet bağlantısı gereklidir.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const ogrenciDonemId = document.getElementById('ogrenciDonemId')?.value || '';
|
||||
const okulId = document.getElementById('okulId')?.value || '';
|
||||
const sinifId = document.getElementById('sinifId')?.value || '';
|
||||
|
|
|
|||
209
public/js/pwa.js
Normal file
209
public/js/pwa.js
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
/**
|
||||
* BeKultur - PWA & Offline State Manager
|
||||
*/
|
||||
(function () {
|
||||
let isOffline = !navigator.onLine;
|
||||
|
||||
// 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() {
|
||||
const banner = getOrCreateBanner();
|
||||
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>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function showOnlineBanner() {
|
||||
const banner = getOrCreateBanner();
|
||||
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>
|
||||
</div>
|
||||
`;
|
||||
setTimeout(() => {
|
||||
banner.className = 'offline-banner hidden';
|
||||
}, 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 = 'Offline mode: Homework status cannot be changed without connection.';
|
||||
} 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 = 'Offline mode: Homework cannot be excluded without connection.';
|
||||
} else {
|
||||
btn.disabled = false;
|
||||
btn.title = btn.textContent.trim() === 'Restore' ? 'Restore homework' : 'Exclude homework';
|
||||
}
|
||||
});
|
||||
|
||||
// --- B. Private Lesson Request Form ---
|
||||
const lessonForm = document.getElementById('privateLessonForm');
|
||||
const btnSubmitLesson = document.getElementById('btnSubmitLessonRequest');
|
||||
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 = '⚡ <strong>Çevrimdışı mod:</strong> Yeni özel ders başvurusu yapmak için internet bağlantısı gereklidir.';
|
||||
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. 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 = 'Offline mode: Sync requires internet connection.';
|
||||
btn.textContent = `${btn.dataset.onlineLabel} (Offline)`;
|
||||
} 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('Çevrimdışı moddasınız. Ödev durumu internet bağlantısı olmadan değiştirilemez.');
|
||||
}
|
||||
},
|
||||
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', () => {
|
||||
if (!navigator.onLine) {
|
||||
applyOfflineState(true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (!navigator.onLine) {
|
||||
applyOfflineState(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Export helper globally
|
||||
window.BeKulturPWA = {
|
||||
isOffline: () => isOffline,
|
||||
refreshState: () => applyOfflineState(!navigator.onLine)
|
||||
};
|
||||
})();
|
||||
|
|
@ -74,6 +74,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
// 4. Sync Syllabus
|
||||
if (btnSyncSyllabus) {
|
||||
btnSyncSyllabus.addEventListener('click', async () => {
|
||||
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
|
||||
showAlert('Çevrimdışı moddasınız. Senkronizasyon için internet bağlantısı gereklidir.', 'danger');
|
||||
return;
|
||||
}
|
||||
btnSyncSyllabus.disabled = true;
|
||||
btnSyncSyllabus.textContent = 'Syncing...';
|
||||
showAlert('Fetching weekly timetable across all 5 days...', 'info');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue