diff options
author | pacien | 2023-10-26 20:40:25 +0200 |
---|---|---|
committer | pacien | 2023-10-26 20:40:25 +0200 |
commit | 1f4724edbaaddccf46aa3157abb0fa9fb71eece2 (patch) | |
tree | a8befbd0a905a4615247827c698af19d082af38a | |
parent | d166db41aa424e276fe4d9ac2b3274426296e06d (diff) | |
download | beamer-viewer-1f4724edbaaddccf46aa3157abb0fa9fb71eece2.tar.gz |
init: simplify loading file path from hash
-rw-r--r-- | beamer/viewer/init.js | 18 | ||||
-rw-r--r-- | beamer/viewer/viewer.js | 20 | ||||
-rw-r--r-- | index.html | 2 |
3 files changed, 20 insertions, 20 deletions
diff --git a/beamer/viewer/init.js b/beamer/viewer/init.js index d634c94..a6ea7be 100644 --- a/beamer/viewer/init.js +++ b/beamer/viewer/init.js | |||
@@ -18,15 +18,6 @@ | |||
18 | 18 | ||
19 | "use strict"; | 19 | "use strict"; |
20 | 20 | ||
21 | const params = function() { | ||
22 | const queryDict = {}; | ||
23 | location.hash.substr(1).split("&").forEach(item => { | ||
24 | const pair = item.split("="); | ||
25 | queryDict[pair[0]] = pair[1]; | ||
26 | }); | ||
27 | return queryDict; | ||
28 | }(); | ||
29 | |||
30 | function isController() { | 21 | function isController() { |
31 | return window.opener == null || window.opener.location.href != window.location.href; | 22 | return window.opener == null || window.opener.location.href != window.location.href; |
32 | } | 23 | } |
@@ -41,16 +32,7 @@ function initCache() { | |||
41 | 32 | ||
42 | function init() { | 33 | function init() { |
43 | initCache(); | 34 | initCache(); |
44 | |||
45 | const viewer = new Viewer(); | 35 | const viewer = new Viewer(); |
46 | |||
47 | if ("file" in params) | ||
48 | viewer.loadFromUrl(params["file"]); | ||
49 | } | ||
50 | |||
51 | function load(file) { | ||
52 | location.hash = "file=" + file; | ||
53 | location.reload(); | ||
54 | } | 36 | } |
55 | 37 | ||
56 | if (isController()) | 38 | if (isController()) |
diff --git a/beamer/viewer/viewer.js b/beamer/viewer/viewer.js index 21b369a..28c0187 100644 --- a/beamer/viewer/viewer.js +++ b/beamer/viewer/viewer.js | |||
@@ -66,7 +66,7 @@ class Viewer { | |||
66 | this.fileInput.files = dummy.files; | 66 | this.fileInput.files = dummy.files; |
67 | } | 67 | } |
68 | 68 | ||
69 | loadFromUrl(url) { | 69 | _loadFromUrl(url) { |
70 | this._setFileName(url); | 70 | this._setFileName(url); |
71 | this._setPresentation(url); | 71 | this._setPresentation(url); |
72 | } | 72 | } |
@@ -94,5 +94,23 @@ class Viewer { | |||
94 | event.preventDefault(); | 94 | event.preventDefault(); |
95 | event.stopPropagation(); | 95 | event.stopPropagation(); |
96 | }); | 96 | }); |
97 | |||
98 | window.addEventListener('load', () => this._on_hash_change()); | ||
99 | window.addEventListener('hashchange', () => this._on_hash_change()); | ||
100 | } | ||
101 | |||
102 | _on_hash_change() { | ||
103 | const params = this._hash_params(); | ||
104 | if ("file" in params) | ||
105 | this._loadFromUrl(params["file"]); | ||
106 | } | ||
107 | |||
108 | _hash_params() { | ||
109 | const queryDict = {}; | ||
110 | location.hash.substr(1).split("&").forEach(item => { | ||
111 | const pair = item.split("="); | ||
112 | queryDict[pair[0]] = pair[1]; | ||
113 | }); | ||
114 | return queryDict; | ||
97 | } | 115 | } |
98 | } | 116 | } |
@@ -73,7 +73,7 @@ | |||
73 | <p> | 73 | <p> |
74 | Select a PDF file to open, | 74 | Select a PDF file to open, |
75 | or try with | 75 | or try with |
76 | <a href="#file=sample/demo.pdf" onclick="load('sample/demo.pdf')">demo slides</a>. | 76 | <a href="#file=sample/demo.pdf">demo slides</a>. |
77 | </p> | 77 | </p> |
78 | 78 | ||
79 | <input id="fileInput" type="file" multiple="false" accept="application/pdf" disabled /> | 79 | <input id="fileInput" type="file" multiple="false" accept="application/pdf" disabled /> |