summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_WPN/frontend/sw.js
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-01-11 12:35:47 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-01-11 12:35:47 +0100
commit19985dbb8c0aa66dc4bf7905abc1148de909097d (patch)
tree2cd5a5d20d7e80fc2a51adf60d838d8a2c40999e /admin/survey/modules/mod_WPN/frontend/sw.js
download1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.gz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.bz2
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.lz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.xz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.zst
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.zip
Diffstat (limited to 'admin/survey/modules/mod_WPN/frontend/sw.js')
-rw-r--r--admin/survey/modules/mod_WPN/frontend/sw.js124
1 files changed, 124 insertions, 0 deletions
diff --git a/admin/survey/modules/mod_WPN/frontend/sw.js b/admin/survey/modules/mod_WPN/frontend/sw.js
new file mode 100644
index 0000000..f20d9ac
--- /dev/null
+++ b/admin/survey/modules/mod_WPN/frontend/sw.js
@@ -0,0 +1,124 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+var cacheName = 'CSv4';
+
+var cachedFiles = [
+ /*'/frontend/',
+ '/index.html',
+ '/manifest.json',
+ '/js/main.js',
+ '/js/production.css',
+ '/css/main.css'
+ '/frontend/css/normalize.min.css',
+ '/frontend/img/cassidy.jpg',
+ '/frontend/img/cramer.jpg',
+ '/frontend/img/duffy.jpg',
+ '/frontend/img/gabor.jpg',
+ '/frontend/img/aths.png',
+ '/frontend/img/share.png',
+ '/frontend/apple-touch-icon.png',
+ '/frontend/android-chrome-192x192.png'*/
+];
+
+self.addEventListener('install', function(evt){
+ console.log('Service Worker Install Event');
+ //Add the file to the cache
+ evt.waitUntil(
+ caches.open(cacheName).then(function(cache){
+ console.log('Caching Files');
+ return cache.addAll(cachedFiles);
+ }).then(function(){
+ return self.skipWaiting();
+ }).catch(function(err){
+ console.log('Cache Failed', err);
+ })
+ );
+});
+
+self.addEventListener('activate', function(evt){
+ console.log('Service Worker Activated');
+ evt.waitUntil(
+ caches.keys().then(function(keyList){
+ return Promise.all(keyList.map(function(key){
+ if(key !== cacheName){
+ console.log('Removing Old Cache', key);
+ return caches.delete(key)
+ }
+ }));
+ })
+ );
+ return self.clients.claim();
+});
+
+/*self.addEventListener('fetch', function(evt){
+ console.log('Fetch Event' + evt.request.url);
+ evt.respondWith(
+ caches.match(evt.request).then(function(response){
+ return response || fetch(evt.request);
+ })
+ );
+});*/
+
+function closeNotification(msg, evt){
+ console.log(msg, evt.notification.data);
+ evt.notification.close();
+}
+
+self.addEventListener('notificationclose', function(evt){
+ closeNotification('Notification Closed', evt);
+});
+
+self.addEventListener('notificationclick', function(evt){
+ if(evt.action !== 'close'){
+ evt.waitUntil(
+ self.clients.matchAll({type: 'window', includeUncontrolled: 'true'}).then(function(allClients){
+ console.log(allClients);
+ var matchingClient = null;
+ for(var i = 0; i<allClients.length; i++){
+ if(allClients[i].visibilityState === 'visible'){
+ matchingClient = allClients[i];
+ console.log('Navigating');
+ matchingClient.navigate(evt.notification.data.loc);
+ break;
+ }
+ }
+ if(matchingClient === null){
+ console.log('Opening');
+ self.clients.openWindow(evt.notification.data.loc);
+ }
+ })
+ );
+ }
+ closeNotification('Notification Clicked', evt);
+});
+
+self.addEventListener('pushsubscriptionchange', function(e) {
+ // remove the entry from DB
+ console.log(e);
+});
+
+self.addEventListener('push', function(evt){
+ console.log('Push Message Received');
+ if(evt.data){
+ console.log('Data Received');
+ var payload = JSON.parse(evt.data.text());
+ console.log(evt.data.text());
+ var options = {
+ body: payload.message,
+ icon: 'admin/survey/modules/mod_WPN/frontend/android-chrome-192x192.png',
+ data: {
+ timestamp: Date.now(),
+ loc: payload.link
+ },
+ actions: [
+ {action: 'go', title: 'Fill out'}
+ ]
+ };
+ evt.waitUntil(
+ self.registration.showNotification(payload.title, options)
+ );
+ }
+}); \ No newline at end of file