Ripristinato per intero lo stato del commit 24391c4, che sull'iPhone
con l'icona installata si vedeva correttamente. Le modifiche dei commit
successivi restano nella storia per essere riprese una per volta.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
161 lines
6.6 KiB
JavaScript
161 lines
6.6 KiB
JavaScript
/**
|
|
* RPIGroup Play - installazione come app (PWA) e registrazione del service worker
|
|
*
|
|
* - Chrome/Edge (Android e computer): intercetta beforeinstallprompt e mostra i pulsanti [data-pwa-install]
|
|
* (sidebar, topbar) e, su telefono, il banner in basso.
|
|
* - iOS: niente prompt; i pulsanti aprono le istruzioni passo passo.
|
|
* - App già installata (standalone): niente inviti.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var BASE = window.BASE_PATH || '';
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', function () {
|
|
navigator.serviceWorker.register(BASE + '/sw.js').catch(function (error) {
|
|
console.error('[PWA] Registrazione Service Worker fallita:', error);
|
|
});
|
|
});
|
|
}
|
|
|
|
var isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true;
|
|
var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
|
|
|
window.deferredPrompt = null;
|
|
|
|
function $(sel) { return document.querySelector(sel); }
|
|
function $$(sel) { return Array.prototype.slice.call(document.querySelectorAll(sel)); }
|
|
|
|
function isBannerDismissed() {
|
|
try {
|
|
var t = localStorage.getItem('pwa-banner-dismissed');
|
|
return t ? (Date.now() - parseInt(t, 10)) < 7 * 24 * 60 * 60 * 1000 : false;
|
|
} catch (e) { return false; }
|
|
}
|
|
|
|
function showBanner() {
|
|
var banner = $('#pwa-install-banner');
|
|
if (!banner || !isMobile || isBannerDismissed()) return;
|
|
banner.hidden = false;
|
|
setTimeout(function () { banner.classList.add('show'); }, 100);
|
|
}
|
|
|
|
function dismissBanner() {
|
|
try { localStorage.setItem('pwa-banner-dismissed', String(Date.now())); } catch (e) { /* niente archivio */ }
|
|
hideBanner();
|
|
}
|
|
|
|
function hideBanner() {
|
|
var banner = $('#pwa-install-banner');
|
|
if (!banner) return;
|
|
banner.classList.remove('show');
|
|
setTimeout(function () { banner.hidden = true; }, 400);
|
|
}
|
|
|
|
function showButtons(show) {
|
|
$$('[data-pwa-install]').forEach(function (b) { b.hidden = !show; });
|
|
}
|
|
|
|
function icon(name) {
|
|
var paths = {
|
|
share: '<path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"/><path d="m16 6-4-4-4 4M12 2v13"/>',
|
|
plus: '<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 12h8M12 8v8"/>',
|
|
more: '<circle cx="12" cy="12" r="1"/><circle cx="12" cy="5" r="1"/><circle cx="12" cy="19" r="1"/>',
|
|
download: '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5M12 15V3"/>'
|
|
};
|
|
return '<span class="pwa-inline-icon"><svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' + paths[name] + '</svg></span>';
|
|
}
|
|
|
|
function step(n, text) {
|
|
return '<div class="pwa-step"><div class="pwa-step-num">' + n + '</div><div class="pwa-step-text">' + text + '</div></div>';
|
|
}
|
|
|
|
function showInstructionsModal() {
|
|
var modal = $('#pwa-instructions-modal');
|
|
var box = $('#pwa-instructions-content');
|
|
if (!modal || !box) return;
|
|
|
|
if (isIOS) {
|
|
box.innerHTML =
|
|
step(1, "Tocca l'icona <strong>Condividi</strong> " + icon('share') + " nella barra di Safari.") +
|
|
step(2, "Scorri l'elenco e scegli <strong>Aggiungi alla schermata Home</strong> " + icon('plus') + ".") +
|
|
step(3, "Tocca <strong>Aggiungi</strong> in alto a destra: l'icona compare sul telefono.");
|
|
} else if (isMobile) {
|
|
box.innerHTML =
|
|
step(1, "Apri il menu del browser con i <strong>tre puntini</strong> " + icon('more') + " in alto a destra.") +
|
|
step(2, "Scegli <strong>Installa app</strong> oppure <strong>Aggiungi a schermata Home</strong>.") +
|
|
step(3, "Conferma: RPIGroup Play si apre a schermo intero come un'app.");
|
|
} else {
|
|
box.innerHTML =
|
|
step(1, "Nella barra degli indirizzi del browser cerca l'icona <strong>Installa</strong> " + icon('download') + ".") +
|
|
step(2, "Oppure apri il menu del browser e scegli <strong>Installa RPIGroup Play</strong>.") +
|
|
step(3, "L'app si apre in una finestra sua, con la sua icona nel sistema.");
|
|
}
|
|
|
|
modal.hidden = false;
|
|
setTimeout(function () { modal.classList.add('show'); }, 10);
|
|
}
|
|
|
|
function closeModal() {
|
|
var modal = $('#pwa-instructions-modal');
|
|
if (!modal) return;
|
|
modal.classList.remove('show');
|
|
setTimeout(function () { modal.hidden = true; }, 300);
|
|
}
|
|
|
|
function triggerInstall(e) {
|
|
if (e) e.preventDefault();
|
|
if (window.deferredPrompt) {
|
|
window.deferredPrompt.prompt();
|
|
window.deferredPrompt.userChoice.then(function (choice) {
|
|
if (choice.outcome === 'accepted') dismissBanner();
|
|
window.deferredPrompt = null;
|
|
});
|
|
} else {
|
|
showInstructionsModal();
|
|
}
|
|
}
|
|
|
|
function initPwaUI() {
|
|
if (isStandalone) return;
|
|
|
|
// Su iOS i pulsanti compaiono subito (non esiste beforeinstallprompt)
|
|
if (isIOS) {
|
|
showButtons(true);
|
|
showBanner();
|
|
}
|
|
|
|
document.addEventListener('click', function (e) {
|
|
if (e.target.closest('[data-pwa-install], #pwa-banner-install-btn')) triggerInstall(e);
|
|
});
|
|
|
|
var closeBtn = $('#pwa-banner-close-btn');
|
|
if (closeBtn) closeBtn.addEventListener('click', function (e) { e.preventDefault(); dismissBanner(); });
|
|
|
|
var modal = $('#pwa-instructions-modal');
|
|
var modalClose = $('#pwa-modal-close-btn');
|
|
if (modalClose) modalClose.addEventListener('click', closeModal);
|
|
if (modal) modal.addEventListener('click', function (e) { if (e.target === modal) closeModal(); });
|
|
document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeModal(); });
|
|
}
|
|
|
|
window.addEventListener('beforeinstallprompt', function (e) {
|
|
e.preventDefault();
|
|
if (isStandalone) return;
|
|
window.deferredPrompt = e;
|
|
showButtons(true);
|
|
showBanner();
|
|
});
|
|
|
|
window.addEventListener('appinstalled', function () {
|
|
window.deferredPrompt = null;
|
|
showButtons(false);
|
|
hideBanner();
|
|
});
|
|
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initPwaUI);
|
|
else initPwaUI();
|
|
})();
|