Files
ti/static/offline.js

111 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

// citti offline support: register the SW, and make mutations survive no-net.
// window.cittiFetch(url, opts) tries the network; if it fails (offline), it
// queues the POST in localStorage, resolves optimistically, and replays the
// queue in order when the connection returns.
(function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').catch(function (e) {
console.warn('[citti] SW registration failed', e);
});
}
var QKEY = 'citti_queue_v1';
function getQ() { try { return JSON.parse(localStorage.getItem(QKEY) || '[]'); } catch (e) { return []; } }
function setQ(q) { try { localStorage.setItem(QKEY, JSON.stringify(q)); } catch (e) {} }
window.cittiFetch = function (url, opts) {
opts = opts || {};
var method = (opts.method || 'GET').toUpperCase();
return fetch(url, opts).then(function (r) {
if (!r.ok) throw new Error('status ' + r.status);
return r;
}).catch(function () {
if (method !== 'GET') {
var q = getQ();
q.push({
url: url, method: method,
headers: opts.headers || {},
body: typeof opts.body === 'string' ? opts.body : null
});
setQ(q);
render();
}
// optimistic: let the UI proceed as if saved
return new Response(JSON.stringify({ ok: true, queued: true }),
{ status: 200, headers: { 'Content-Type': 'application/json' } });
});
};
var flushing = false;
function flush() {
if (flushing || !navigator.onLine) { render(); return; }
var q = getQ();
if (!q.length) { render(); return; }
flushing = true;
var next = q[0];
fetch(next.url, { method: next.method, headers: next.headers, body: next.body })
.then(function (r) {
if (!r.ok) throw new Error('status ' + r.status);
var qq = getQ(); qq.shift(); setQ(qq);
flushing = false; render();
if (getQ().length) flush();
})
.catch(function () { flushing = false; render(); });
}
function render() {
var n = getQ().length;
var el = document.getElementById('citti-sync');
if (!el) {
el = document.createElement('div');
el.id = 'citti-sync';
el.style.cssText = 'position:fixed;left:50%;transform:translateX(-50%);bottom:14px;z-index:9999;'
+ 'padding:8px 16px;border-radius:20px;font:600 13px/1 system-ui,-apple-system,sans-serif;'
+ 'color:#fff;box-shadow:0 2px 10px rgba(0,0,0,.25);transition:opacity .3s;pointer-events:none';
document.body.appendChild(el);
}
var offline = !navigator.onLine;
if (offline && n) { el.textContent = '📴 Offline · ' + n + ' gemmes når du er online'; el.style.background = '#92580a'; el.style.opacity = '1'; }
else if (offline) { el.textContent = '📴 Offline'; el.style.background = '#5a5550'; el.style.opacity = '1'; }
else if (n) { el.textContent = '↻ Synkroniserer ' + n + '…'; el.style.background = '#2d6a4f'; el.style.opacity = '1'; }
else { el.style.opacity = '0'; }
}
// ---- Install ("Add to home screen") prompt ----
var deferredPrompt = null;
function showInstall() {
if (!document.body) { document.addEventListener('DOMContentLoaded', showInstall); return; }
var b = document.getElementById('citti-install');
if (!b) {
b = document.createElement('button');
b.id = 'citti-install';
b.type = 'button';
b.textContent = '⬇ Installér app';
b.style.cssText = 'position:fixed;left:14px;bottom:14px;z-index:9999;padding:9px 15px;'
+ 'border:none;border-radius:20px;background:#1a1814;color:#fff;cursor:pointer;'
+ 'font:600 13px/1 system-ui,-apple-system,sans-serif;box-shadow:0 2px 10px rgba(0,0,0,.25)';
b.addEventListener('click', function () {
if (!deferredPrompt) return;
deferredPrompt.prompt();
deferredPrompt.userChoice.finally(function () { deferredPrompt = null; b.remove(); });
});
document.body.appendChild(b);
}
}
window.addEventListener('beforeinstallprompt', function (e) {
e.preventDefault(); // keep the event so we can trigger it from our button
deferredPrompt = e;
showInstall();
});
window.addEventListener('appinstalled', function () {
deferredPrompt = null;
var b = document.getElementById('citti-install');
if (b) b.remove();
});
window.addEventListener('online', function () { render(); flush(); });
window.addEventListener('offline', render);
if (document.readyState !== 'loading') { render(); flush(); }
else document.addEventListener('DOMContentLoaded', function () { render(); flush(); });
})();