diff --git a/POC/static/navbar-burger.js b/POC/static/navbar-burger.js deleted file mode 100644 index 9b18a3e..0000000 --- a/POC/static/navbar-burger.js +++ /dev/null @@ -1,25 +0,0 @@ -document.addEventListener('DOMContentLoaded', () => { - - // Get all "navbar-burger" elements - const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); - - // Check if there are any navbar burgers - if ($navbarBurgers.length > 0) { - - // Add a click event on each of them - $navbarBurgers.forEach( el => { - el.addEventListener('click', () => { - - // Get the target from the "data-target" attribute - const target = el.dataset.target; - const $target = document.getElementById(target); - - // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" - el.classList.toggle('is-active'); - $target.classList.toggle('is-active'); - - }); - }); - } - -}); \ No newline at end of file diff --git a/POC/static/510logo.jpg b/PWA/public/images/510logo.jpg similarity index 100% rename from POC/static/510logo.jpg rename to PWA/public/images/510logo.jpg diff --git a/POC/static/ReliefBox-horizontal-nobackground.png b/PWA/public/images/ReliefBox-horizontal-nobackground.png similarity index 100% rename from POC/static/ReliefBox-horizontal-nobackground.png rename to PWA/public/images/ReliefBox-horizontal-nobackground.png diff --git a/POC/static/ReliefBox-horizontal.png b/PWA/public/images/ReliefBox-horizontal.png similarity index 100% rename from POC/static/ReliefBox-horizontal.png rename to PWA/public/images/ReliefBox-horizontal.png diff --git a/POC/static/ReliefBox.PNG b/PWA/public/images/ReliefBox.PNG similarity index 100% rename from POC/static/ReliefBox.PNG rename to PWA/public/images/ReliefBox.PNG diff --git a/PWA/public/images/icons/app-icon-192x192.png b/PWA/public/images/icons/app-icon-192x192.png new file mode 100644 index 0000000..bb21d10 Binary files /dev/null and b/PWA/public/images/icons/app-icon-192x192.png differ diff --git a/PWA/public/images/icons/app-icon-512x512.png b/PWA/public/images/icons/app-icon-512x512.png new file mode 100644 index 0000000..0b998b8 Binary files /dev/null and b/PWA/public/images/icons/app-icon-512x512.png differ diff --git a/PWA/public/images/icons/favicon-16x16.png b/PWA/public/images/icons/favicon-16x16.png new file mode 100644 index 0000000..b3184d2 Binary files /dev/null and b/PWA/public/images/icons/favicon-16x16.png differ diff --git a/PWA/public/images/icons/favicon-32x32.png b/PWA/public/images/icons/favicon-32x32.png new file mode 100644 index 0000000..3eafb1b Binary files /dev/null and b/PWA/public/images/icons/favicon-32x32.png differ diff --git a/PWA/public/src/sw.js b/PWA/public/src/sw.js new file mode 100644 index 0000000..9f02005 --- /dev/null +++ b/PWA/public/src/sw.js @@ -0,0 +1,47 @@ +import { FetchEventHandlers } from "./Services/FetchEventHandlers/FetchEventHandlers.js"; +import { CacheFilePathService } from "./Services/CacheFilePathService.js"; +import { ActiveSession } from "./SessionState/ActiveSession.js"; +import { Database } from "./Services/Database.js"; +var CACHE_STATIC_NAME = "static-v10"; +var CACHE_DYNAMIC_NAME = "dynamic-v2"; +self.addEventListener("install", function (event) { + console.log("[Service Worker] Installing Service Worker ...", event); + console.log(new CacheFilePathService().pathsOfFilesToCache()); + event.waitUntil(caches.open(CACHE_STATIC_NAME) + .then(function (cache) { + console.log("[Service Worker] Precaching App Shell"); + cache.addAll([ + "/", + "/favicon.ico", + "/manifest.json", + "/apple-touch-icon.png", + "/apple-touch-icon-precomposed.png" + ] + .concat(new CacheFilePathService().pathsOfFilesToCache())); + })); +}); +self.addEventListener("activate", function (event) { + console.log("[Service Worker] Activating Service Worker ....", event); + event.waitUntil(caches.keys().then(function (keyList) { + return Promise.all(keyList.map(function (key) { + if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) { + console.log("[Service Worker] Removing old cache.", key); + return caches.delete(key); + } + })); + })); + return self.clients.claim(); +}); +// Cache-only +self.addEventListener("fetch", function (event) { + let customHandlers = new FetchEventHandlers(new ActiveSession(new Database(indexedDB))); + console.log("Handling fetch request " + event.request.url); + if (customHandlers.canHandleEvent(event)) { + console.log("using custom event handler"); + event.respondWith(customHandlers.handleEvent(event)); + } + else { + console.log("using custom cache"); + event.respondWith(caches.match(event.request)); + } +}); diff --git a/PWA/public/sw.js b/PWA/public/sw.js index 77b7e45..81b6647 100644 --- a/PWA/public/sw.js +++ b/PWA/public/sw.js @@ -10,20 +10,25 @@ self.addEventListener("install", function (event) { event.waitUntil(caches.open(CACHE_STATIC_NAME) .then(function (cache) { console.log("[Service Worker] Precaching App Shell"); - cache.addAll([ - "/", - "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css", - "https://unpkg.com/@zxing/library@latest/umd/index.min.js", - "/favicon.ico", - "/manifest.json", - "/images/ReliefBox-horizontal-nobackground.png", - "/images/510logo.jpg", - "/images/ReliefBox-horizontal.png", - "/images/ReliefBox.PNG", - "/apple-touch-icon.png", - "/apple-touch-icon-precomposed.png" - ] - .concat(new CacheFilePathService().pathsOfFilesToCache())); + try { + cache.addAll([ + "/", + "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css", + "https://unpkg.com/@zxing/library@latest/umd/index.min.js", + "/favicon.ico", + "/manifest.json", + "/images/ReliefBox-horizontal-nobackground.png", + "/images/510logo.jpg", + "/images/ReliefBox-horizontal.png", + "/images/ReliefBox.PNG", + "/apple-touch-icon.png", + "/apple-touch-icon-precomposed.png" + ] + .concat(new CacheFilePathService().pathsOfFilesToCache())); + } + catch (error) { + console.error("Failed to cache one ore more files because of error " + error); + } })); }); self.addEventListener("activate", function (event) { diff --git a/PWA/src/Services/CacheFilePathService.ts b/PWA/src/Services/CacheFilePathService.ts index f929b53..b330f49 100644 --- a/PWA/src/Services/CacheFilePathService.ts +++ b/PWA/src/Services/CacheFilePathService.ts @@ -25,52 +25,67 @@ import { BeneficiaryEligilityService } from "./BeneficiaryEligilityService.js"; // Provides all the files that have to be cached for offline use export class CacheFilePathService { pathsOfFilesToCache(): string[] { - return [ - this.pagePaths(), - this.modelPaths(), - this.toplevelScriptsPaths(), - this.fetchEventHanderPaths(), - this.interfacesPaths(), - this.externalLibrariesPaths(), - this.servicePaths(), - this.sessionStatePaths() + return [ + this.imagesPaths(), + this.pagePaths(), + this.modelPaths(), + this.toplevelScriptsPaths(), + this.fetchEventHanderPaths(), + this.interfacesPaths(), + this.externalLibrariesPaths(), + this.servicePaths(), + this.sessionStatePaths(), ].reduce((previousArray, currentValue) => { - return previousArray.concat(currentValue) - }, []) + return previousArray.concat(currentValue); + }, []); } private pagePaths(): string[] { return [ - RouteEvents.template, - RouteEvents.home, - RouteEvents.distributionsHome, - RouteEvents.nameDistribution, - RouteEvents.listDistributions, - RouteEvents.deleteDistribution, - RouteEvents.uploadData, - RouteEvents.uploadDataError, - RouteEvents.chooseBenificiaryCodeInputMethodPage, - RouteEvents.codeInputUsingCamera, - RouteEvents.codeinputUsingTextField, - RouteEvents.codeInputNotFound, - ] - } + RouteEvents.template, + RouteEvents.home, + RouteEvents.distributionsHome, + RouteEvents.nameDistribution, + RouteEvents.listDistributions, + RouteEvents.deleteDistribution, + RouteEvents.uploadData, + RouteEvents.uploadDataError, + RouteEvents.chooseBenificiaryCodeInputMethodPage, + RouteEvents.codeInputUsingCamera, + RouteEvents.codeinputUsingTextField, + RouteEvents.codeInputNotFound, + ]; + } private toplevelScriptsPaths(): string[] { - return this.pathsForTypesInFolder("", [ + return this.pathsForTypesInFolder("", [ "app", "sw", "navbar-burger", - RouteEvents.name + RouteEvents.name, ]); } private externalLibrariesPaths(): string[] { return this.pathsForTypesInFolder("ExternalLibraries", [ "mustache", - "xlsx.full.min" + "xlsx.full.min", + ]).concat( + [ + "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css", + "https://unpkg.com/@zxing/library@latest/umd/index.min.js", + ] + ) + } + + private imagesPaths(): string[] { + return this.pathsForTypesInFolder("images", [ + "/images/ReliefBox-horizontal-nobackground.png", + "/images/510logo.jpg", + "/images/ReliefBox-horizontal.png", + "/images/ReliefBox.PNG", ]); - } + } private modelPaths(): string[] { return this.pathsForTypesInFolder("Models", [ @@ -78,61 +93,69 @@ export class CacheFilePathService { Distribution.name, DeleteDistributionPost.name, SelectDistributionPost.name, - BeneficiaryCodeInputMethodPost.name + BeneficiaryCodeInputMethodPost.name, ]); } private servicePaths(): string[] { return this.pathsForTypesInFolder("Services", [ - BenificiaryInfoService.name, - CacheFilePathService.name, - Database.name, - DeserialisationService.name, - FormParser.name, - BeneficiaryEligilityService.name - ]) + BenificiaryInfoService.name, + CacheFilePathService.name, + Database.name, + DeserialisationService.name, + FormParser.name, + BeneficiaryEligilityService.name, + ]); } private sessionStatePaths(): string[] { - return this.pathsForTypesInFolder("Services", [ - ActiveSession.name - ]) + return this.pathsForTypesInFolder("Services", [ActiveSession.name]); } private fetchEventHanderPaths(): string[] { return this.pathsForTypesInFolder("Services/FetchEventHandlers", [ - BeneficiaryDataUploadHandler.name, - CreateDistributionRequestHandler.name, - DeleteDistributionPostHandler.name, - FetchEventHandlers.name, - ListDistributionRequestHandler.name, - NameDistributionRequestHandler.name, - SelectDistributionRequestHandler.name, - UploadDataHandler.name, - ChooseBenificiaryCodeInputMethodPageHandler.name, - SelectBenificiaryCodeInputMethodHandler.name, - BeneficiaryCodePostHandler.name, - ]) + BeneficiaryDataUploadHandler.name, + CreateDistributionRequestHandler.name, + DeleteDistributionPostHandler.name, + FetchEventHandlers.name, + ListDistributionRequestHandler.name, + NameDistributionRequestHandler.name, + SelectDistributionRequestHandler.name, + UploadDataHandler.name, + ChooseBenificiaryCodeInputMethodPageHandler.name, + SelectBenificiaryCodeInputMethodHandler.name, + BeneficiaryCodePostHandler.name, + ]); } private interfacesPaths(): string[] { return this.pathsForTypesInFolder("Interfaces", [ - "FetchEvent", - "FetchEventHandler" - ]) + "FetchEvent", + "FetchEventHandler", + ]); } - private pathsForTypesInFolder(folder: string, typeNames: String[], extension: string = ".js"): string[] { - return typeNames.map((name) => this.pathForTypeInFolder(name, folder, extension)); + private pathsForTypesInFolder( + folder: string, + typeNames: String[], + extension: string = ".js" + ): string[] { + return typeNames.map((name) => + this.pathForTypeInFolder(name, folder, extension) + ); } - private pathForTypeInFolder(typeName: String, folder: string, extension: string = ".js"): string { - let path = "/" - if(folder.length > 0) { - path += folder + "/" + private pathForTypeInFolder( + typeName: String, + folder: string, + extension: string = ".js" + ): string { + let path = "/"; + if (folder.length > 0) { + path += folder + "/"; } - path += typeName + extension + path += typeName + extension; - return path + return path; } } diff --git a/PWA/src/sw.ts b/PWA/src/sw.ts index 267fd0e..c309bd0 100644 --- a/PWA/src/sw.ts +++ b/PWA/src/sw.ts @@ -15,20 +15,14 @@ self.addEventListener("install", function (event: any) { console.log("[Service Worker] Precaching App Shell"); cache.addAll([ "/", - "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css", - "https://unpkg.com/@zxing/library@latest/umd/index.min.js", "/favicon.ico", "/manifest.json", - "/images/ReliefBox-horizontal-nobackground.png", - "/images/510logo.jpg", - "/images/ReliefBox-horizontal.png", - "/images/ReliefBox.PNG", "/apple-touch-icon.png", "/apple-touch-icon-precomposed.png" ] .concat(new CacheFilePathService().pathsOfFilesToCache()) ); - }) + }) ); });