46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
/**
|
|
* Fociz Service Worker
|
|
* Beheert desktop push notificaties en click events voor agenda-afspraken.
|
|
*/
|
|
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
// Behandel klik op een notificatie
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
const targetUrl = (event.notification.data && event.notification.data.url)
|
|
? event.notification.data.url
|
|
: '/planner.php';
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windowClients) => {
|
|
// Als er al een Fociz tabblad open staat, breng die naar voren
|
|
for (let i = 0; i < windowClients.length; i++) {
|
|
const client = windowClients[i];
|
|
if ('focus' in client) {
|
|
if (targetUrl.startsWith('http') && !targetUrl.includes(self.location.host)) {
|
|
// Externe meeting link (zoals Teams): open in nieuw tabblad
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(targetUrl);
|
|
}
|
|
} else {
|
|
client.navigate(targetUrl);
|
|
return client.focus();
|
|
}
|
|
}
|
|
}
|
|
// Geen tabblad open: open een nieuw venster
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(targetUrl);
|
|
}
|
|
})
|
|
);
|
|
});
|