vers. 3.0.0 - nuova interfaccia app per mobile e desktop
Nuova UI disegnata sul tema RPIGroup Radio Theme dei siti del gruppo: guscio unico responsive (sidebar e player in basso da 900px, topbar e tab bar su telefono), player della radio persistente nel dock con now playing, HLS/MP3, volume e Media Session; pagine radio, tv, podcast (copertine e programma dal feed), player e pagine di testo rifatte; icone SVG inline al posto di Bootstrap, jQuery e Material Icons; font Bricolage Grotesque e Plus Jakarta Sans in locale; service worker v5, manifest con orientamento libero, icone PWA 192 e 384 rigenerate. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ee93d3b75
commit
24391c49ac
58 files changed
+5226
-16550
No files matched your search
+146
-222
@@ -1,236 +1,160 @@
|
||||
/**
|
||||
* PWA Install and Service Worker Helper
|
||||
* Powered by Antigravity AI
|
||||
* 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 () {
|
||||
const BASE_PATH = window.BASE_PATH || '';
|
||||
|
||||
// Register Service Worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${BASE_PATH}/sw.js`;
|
||||
navigator.serviceWorker.register(swUrl)
|
||||
.then((registration) => {
|
||||
console.log('[PWA] Service Worker registrato con successo:', registration.scope);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('[PWA] Registrazione Service Worker fallita:', error);
|
||||
'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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Detectors
|
||||
const isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true;
|
||||
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
||||
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
|
||||
// Se l'app viene aperta in modalità standalone (PWA) su desktop, reindirizza all'interno dell'app
|
||||
if (isStandalone && !isMobile) {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
if (!urlParams.has('app') || urlParams.get('app') !== 'true') {
|
||||
const currentPath = window.location.pathname;
|
||||
const pathWithoutBase = currentPath.replace(BASE_PATH, '').replace(/^\//, '');
|
||||
const redirectParam = pathWithoutBase ? '&redirect=' + encodeURIComponent(pathWithoutBase) : '';
|
||||
window.location.href = BASE_PATH + '/?app=true' + redirectParam;
|
||||
}
|
||||
}
|
||||
|
||||
// Global variables
|
||||
window.deferredPrompt = null;
|
||||
|
||||
// Check if banner was dismissed in the last 7 days
|
||||
function isBannerDismissed() {
|
||||
const dismissedTime = localStorage.getItem('pwa-banner-dismissed');
|
||||
if (!dismissedTime) return false;
|
||||
|
||||
const sevenDays = 7 * 24 * 60 * 60 * 1000;
|
||||
return (Date.now() - parseInt(dismissedTime, 10)) < sevenDays;
|
||||
}
|
||||
|
||||
// Dismiss banner
|
||||
function dismissBanner() {
|
||||
localStorage.setItem('pwa-banner-dismissed', Date.now().toString());
|
||||
const banner = document.getElementById('pwa-install-banner');
|
||||
if (banner) {
|
||||
banner.classList.remove('show');
|
||||
setTimeout(() => {
|
||||
banner.style.display = 'none';
|
||||
}, 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize UI components and listeners
|
||||
function initPwaUI() {
|
||||
// If already in standalone mode, hide all promotional components
|
||||
if (isStandalone) {
|
||||
console.log('[PWA] Applicazione avviata in modalità Standalone. Nascondo elementi PWA.');
|
||||
return;
|
||||
}
|
||||
|
||||
const headerBtn = document.getElementById('pwa-header-install-btn');
|
||||
const desktopBtn = document.getElementById('desktopPwaInstallBtn');
|
||||
const banner = document.getElementById('pwa-install-banner');
|
||||
const bannerInstallBtn = document.getElementById('pwa-banner-install-btn');
|
||||
const bannerCloseBtn = document.getElementById('pwa-banner-close-btn');
|
||||
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);
|
||||
|
||||
// On iOS, show the installation buttons immediately (as they don't support beforeinstallprompt)
|
||||
if (isIOS) {
|
||||
console.log('[PWA] Rilevato dispositivo iOS');
|
||||
if (headerBtn) headerBtn.style.display = 'inline-flex';
|
||||
if (desktopBtn) desktopBtn.style.display = 'inline-flex';
|
||||
|
||||
if (banner && !isBannerDismissed()) {
|
||||
banner.style.display = 'flex';
|
||||
setTimeout(() => banner.classList.add('show'), 100);
|
||||
}
|
||||
}
|
||||
|
||||
// Attach click listeners to all PWA installation buttons
|
||||
const triggerInstall = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (window.deferredPrompt) {
|
||||
// Show native install prompt (Android / Desktop Chrome / Edge)
|
||||
window.deferredPrompt.prompt();
|
||||
window.deferredPrompt.userChoice.then((choiceResult) => {
|
||||
if (choiceResult.outcome === 'accepted') {
|
||||
console.log('[PWA] Utente ha installato la PWA');
|
||||
dismissBanner();
|
||||
} else {
|
||||
console.log('[PWA] Utente ha annullato l\'installazione');
|
||||
}
|
||||
window.deferredPrompt = null;
|
||||
});
|
||||
} else {
|
||||
// Fallback for iOS Safari or other unsupported prompts
|
||||
showInstructionsModal();
|
||||
}
|
||||
};
|
||||
|
||||
if (headerBtn) headerBtn.addEventListener('click', triggerInstall);
|
||||
if (desktopBtn) desktopBtn.addEventListener('click', triggerInstall);
|
||||
if (bannerInstallBtn) bannerInstallBtn.addEventListener('click', triggerInstall);
|
||||
|
||||
if (bannerCloseBtn) {
|
||||
bannerCloseBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
dismissBanner();
|
||||
});
|
||||
}
|
||||
|
||||
// Modal Close logic
|
||||
const modal = document.getElementById('pwa-instructions-modal');
|
||||
const modalCloseBtnTop = document.getElementById('pwa-modal-close-btn');
|
||||
|
||||
const closeModal = () => {
|
||||
if (modal) {
|
||||
modal.classList.remove('show');
|
||||
setTimeout(() => {
|
||||
modal.style.display = 'none';
|
||||
}, 300);
|
||||
}
|
||||
};
|
||||
|
||||
if (modalCloseBtnTop) modalCloseBtnTop.addEventListener('click', closeModal);
|
||||
if (modal) {
|
||||
modal.addEventListener('click', (e) => {
|
||||
if (e.target === modal) closeModal();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Listen for beforeinstallprompt (Chrome / Android / Edge)
|
||||
window.addEventListener('beforeinstallprompt', (e) => {
|
||||
// Prevent Chrome 67 and earlier from automatically showing the prompt
|
||||
e.preventDefault();
|
||||
|
||||
// Se siamo su desktop, non salviamo il prompt e non mostriamo nessun banner promozionale PWA
|
||||
if (!isMobile) {
|
||||
console.log('[PWA] Installazione via browser intercettata su desktop. Ignorata per requisiti.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Stash the event so it can be triggered later.
|
||||
window.deferredPrompt = e;
|
||||
|
||||
console.log('[PWA] Evento beforeinstallprompt intercettato.');
|
||||
|
||||
// Show custom buttons and banner (if not dismissed)
|
||||
const headerBtn = document.getElementById('pwa-header-install-btn');
|
||||
const desktopBtn = document.getElementById('desktopPwaInstallBtn');
|
||||
const banner = document.getElementById('pwa-install-banner');
|
||||
|
||||
if (headerBtn) headerBtn.style.display = 'inline-flex';
|
||||
if (desktopBtn) desktopBtn.style.display = 'inline-flex';
|
||||
|
||||
if (banner && !isBannerDismissed()) {
|
||||
banner.style.display = 'flex';
|
||||
setTimeout(() => banner.classList.add('show'), 100);
|
||||
}
|
||||
});
|
||||
|
||||
// Track successful installation
|
||||
window.addEventListener('appinstalled', () => {
|
||||
console.log('[PWA] L\'applicazione è stata installata con successo.');
|
||||
window.deferredPrompt = null;
|
||||
|
||||
// Hide all promotional components
|
||||
const headerBtn = document.getElementById('pwa-header-install-btn');
|
||||
const desktopBtn = document.getElementById('desktopPwaInstallBtn');
|
||||
const banner = document.getElementById('pwa-install-banner');
|
||||
|
||||
if (headerBtn) headerBtn.style.display = 'none';
|
||||
if (desktopBtn) desktopBtn.style.display = 'none';
|
||||
if (banner) banner.style.display = 'none';
|
||||
});
|
||||
|
||||
// Show instructions modal (iOS / Fallback)
|
||||
function showInstructionsModal() {
|
||||
const modal = document.getElementById('pwa-instructions-modal');
|
||||
const content = document.getElementById('pwa-instructions-content');
|
||||
if (!modal || !content) return;
|
||||
function $(sel) { return document.querySelector(sel); }
|
||||
function $$(sel) { return Array.prototype.slice.call(document.querySelectorAll(sel)); }
|
||||
|
||||
if (isIOS) {
|
||||
content.innerHTML = `
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">1</div>
|
||||
<div class="pwa-step-text">Tocca l'icona di <strong>Condividi</strong> <span class="pwa-inline-icon"><i class="material-icons" style="font-size:18px; color:#f7b835;">share</i></span> (il quadrato con la freccia verso l'alto) nella barra in basso di Safari.</div>
|
||||
</div>
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">2</div>
|
||||
<div class="pwa-step-text">Scorri l'elenco delle opzioni verso il basso e seleziona <strong>Aggiungi alla schermata Home</strong> <span class="pwa-inline-icon"><i class="material-icons" style="font-size:18px; color:#f7b835;">add_box</i></span>.</div>
|
||||
</div>
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">3</div>
|
||||
<div class="pwa-step-text">Tocca <strong>Aggiungi</strong> nell'angolo in alto a destra per inserire l'icona sul tuo telefono.</div>
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
content.innerHTML = `
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">1</div>
|
||||
<div class="pwa-step-text">Apri il menu delle impostazioni del browser toccando i <strong>tre puntini</strong> <span class="pwa-inline-icon"><i class="material-icons" style="font-size:18px; color:#f7b835;">more_vert</i></span> in alto a destra.</div>
|
||||
</div>
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">2</div>
|
||||
<div class="pwa-step-text">Seleziona la voce <strong>Installa applicazione</strong> o <strong>Aggiungi a schermata Home</strong>.</div>
|
||||
</div>
|
||||
<div class="pwa-step">
|
||||
<div class="pwa-step-num">3</div>
|
||||
<div class="pwa-step-text">Conferma per scaricare la PWA sul tuo telefono.</div>
|
||||
</div>
|
||||
`;
|
||||
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; }
|
||||
}
|
||||
|
||||
modal.style.display = 'flex';
|
||||
setTimeout(() => modal.classList.add('show'), 10);
|
||||
}
|
||||
function showBanner() {
|
||||
var banner = $('#pwa-install-banner');
|
||||
if (!banner || !isMobile || isBannerDismissed()) return;
|
||||
banner.hidden = false;
|
||||
setTimeout(function () { banner.classList.add('show'); }, 100);
|
||||
}
|
||||
|
||||
// Run on DOM loaded
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initPwaUI);
|
||||
} else {
|
||||
initPwaUI();
|
||||
}
|
||||
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();
|
||||
})();
|
||||
Reference in new issue
Block a user