aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-07-19 19:02:09 +0200
committerpacien2018-07-19 19:02:09 +0200
commit0a60570aaf54138b88a115eaa30bd1b6f8422097 (patch)
tree81b12a465dd90043fb957f18d1feb2c2eb0413f1
parentfeed2b79f25899d4655b1391b2ff58f8147303af (diff)
downloadbeamer-viewer-0a60570aaf54138b88a115eaa30bd1b6f8422097.tar.gz
Add offline support
-rw-r--r--appcache.js60
-rw-r--r--pointless/viewer/init.js9
2 files changed, 68 insertions, 1 deletions
diff --git a/appcache.js b/appcache.js
new file mode 100644
index 0000000..87d5e80
--- /dev/null
+++ b/appcache.js
@@ -0,0 +1,60 @@
1/*
2 * appcache.js
3 * Part of Pointless Viewer, a Beamer presentation viewer
4 * Copyright 2018 Pacien TRAN-GIRARD
5 * License: GNU GPL v3
6 */
7
8"use strict";
9
10class AppCache {
11 constructor() {
12 this.cacheName = "cache";
13 this.filesToCache = [
14 "viewer.html",
15 "appcache.js",
16
17 "pointless/pdfjs/pdf.js",
18 "pointless/pdfjs/pdf.worker.js",
19
20 "pointless/viewer/viewer.css",
21 "pointless/viewer/init.js",
22 "pointless/viewer/viewer.js",
23 "pointless/viewer/presentation.js",
24 "pointless/viewer/stage.js",
25 "pointless/viewer/screen.js",
26 "pointless/viewer/timer.js"
27 ];
28
29 var appCache = this;
30 self.addEventListener("install", function(event) {
31 event.waitUntil(appCache._onInstall());
32 });
33
34 self.addEventListener("fetch", function(event) {
35 event.respondWith(appCache._onFetch(event.request));
36 });
37 }
38
39 _onInstall() {
40 var self = this;
41 return caches.open(this.cacheName).then(function(cache) {
42 return cache.addAll(self.filesToCache);
43 });
44 }
45
46 _onFetch(request) {
47 return navigator.onLine ? this._fetchUpdate(request) : caches.match(request);
48 }
49
50 _fetchUpdate(request) {
51 return caches.open(this.cacheName).then(function(cache) {
52 return fetch(request).then(function(response) {
53 cache.put(request, response.clone());
54 return response
55 });
56 });
57 }
58}
59
60var 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() {
20 return window.opener == null || window.opener.location.href != window.location.href; 20 return window.opener == null || window.opener.location.href != window.location.href;
21} 21}
22 22
23function initCache() {
24 if (!navigator.serviceWorker) return;
25 navigator.serviceWorker.register("appcache.js");
26}
27
23function init() { 28function init() {
29 initCache();
30
24 var viewer = new Viewer(); 31 var viewer = new Viewer();
25 32
26 if ("file" in params) 33 if ("file" in params)
27 viewer.load(params["file"]); 34 viewer.load(params["file"]);
28} 35}