From 0a60570aaf54138b88a115eaa30bd1b6f8422097 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 19 Jul 2018 19:02:09 +0200 Subject: Add offline support --- appcache.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ pointless/viewer/init.js | 9 +++++++- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 appcache.js diff --git a/appcache.js b/appcache.js new file mode 100644 index 0000000..87d5e80 --- /dev/null +++ b/appcache.js @@ -0,0 +1,60 @@ +/* + * appcache.js + * Part of Pointless Viewer, a Beamer presentation viewer + * Copyright 2018 Pacien TRAN-GIRARD + * License: GNU GPL v3 + */ + +"use strict"; + +class AppCache { + constructor() { + this.cacheName = "cache"; + this.filesToCache = [ + "viewer.html", + "appcache.js", + + "pointless/pdfjs/pdf.js", + "pointless/pdfjs/pdf.worker.js", + + "pointless/viewer/viewer.css", + "pointless/viewer/init.js", + "pointless/viewer/viewer.js", + "pointless/viewer/presentation.js", + "pointless/viewer/stage.js", + "pointless/viewer/screen.js", + "pointless/viewer/timer.js" + ]; + + var appCache = this; + self.addEventListener("install", function(event) { + event.waitUntil(appCache._onInstall()); + }); + + self.addEventListener("fetch", function(event) { + event.respondWith(appCache._onFetch(event.request)); + }); + } + + _onInstall() { + var self = this; + return caches.open(this.cacheName).then(function(cache) { + return cache.addAll(self.filesToCache); + }); + } + + _onFetch(request) { + return navigator.onLine ? this._fetchUpdate(request) : caches.match(request); + } + + _fetchUpdate(request) { + return caches.open(this.cacheName).then(function(cache) { + return fetch(request).then(function(response) { + cache.put(request, response.clone()); + return response + }); + }); + } +} + +var appCache = new AppCache(); diff --git a/pointless/viewer/init.js b/pointless/viewer/init.js index 9454a23..9ba881c 100644 --- a/pointless/viewer/init.js +++ b/pointless/viewer/init.js @@ -20,9 +20,16 @@ function isController() { return window.opener == null || window.opener.location.href != window.location.href; } +function initCache() { + if (!navigator.serviceWorker) return; + navigator.serviceWorker.register("appcache.js"); +} + function init() { + initCache(); + var viewer = new Viewer(); - + if ("file" in params) viewer.load(params["file"]); } -- cgit v1.2.3