diff options
author | pacien | 2018-07-19 11:55:02 +0200 |
---|---|---|
committer | pacien | 2018-07-19 11:55:02 +0200 |
commit | 8931cc9418be09206de86f6081b61d591ce8e862 (patch) | |
tree | d2dcf8eb0202a11aa4a1e47729e29543e1795f50 /pointless | |
parent | 5c30b8700e16323b3af7d27369818c7a02fdefee (diff) | |
download | beamer-viewer-8931cc9418be09206de86f6081b61d591ce8e862.tar.gz |
Handle file drag and drop
Diffstat (limited to 'pointless')
-rw-r--r-- | pointless/viewer/init.js | 31 | ||||
-rw-r--r-- | pointless/viewer/viewer.css | 3 | ||||
-rw-r--r-- | pointless/viewer/viewer.js | 50 |
3 files changed, 56 insertions, 28 deletions
diff --git a/pointless/viewer/init.js b/pointless/viewer/init.js new file mode 100644 index 0000000..9454a23 --- /dev/null +++ b/pointless/viewer/init.js | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * init.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 | |||
10 | var params = function() { | ||
11 | var queryDict = {}; | ||
12 | location.search.substr(1).split("&").forEach(function(item) { | ||
13 | var pair = item.split("="); | ||
14 | queryDict[pair[0]] = pair[1]; | ||
15 | }); | ||
16 | return queryDict; | ||
17 | }(); | ||
18 | |||
19 | function isController() { | ||
20 | return window.opener == null || window.opener.location.href != window.location.href; | ||
21 | } | ||
22 | |||
23 | function init() { | ||
24 | var viewer = new Viewer(); | ||
25 | |||
26 | if ("file" in params) | ||
27 | viewer.load(params["file"]); | ||
28 | } | ||
29 | |||
30 | if (isController()) | ||
31 | init(); | ||
diff --git a/pointless/viewer/viewer.css b/pointless/viewer/viewer.css index bea73e6..be4f876 100644 --- a/pointless/viewer/viewer.css +++ b/pointless/viewer/viewer.css | |||
@@ -5,11 +5,12 @@ | |||
5 | * License: GNU GPL v3 | 5 | * License: GNU GPL v3 |
6 | */ | 6 | */ |
7 | 7 | ||
8 | body { | 8 | html, body { |
9 | background-color: black; | 9 | background-color: black; |
10 | color: white; | 10 | color: white; |
11 | font-family: sans-serif; | 11 | font-family: sans-serif; |
12 | overflow: hidden; | 12 | overflow: hidden; |
13 | height: 100%; | ||
13 | } | 14 | } |
14 | 15 | ||
15 | #fileInput { | 16 | #fileInput { |
diff --git a/pointless/viewer/viewer.js b/pointless/viewer/viewer.js index 1c651fc..78035ee 100644 --- a/pointless/viewer/viewer.js +++ b/pointless/viewer/viewer.js | |||
@@ -7,50 +7,46 @@ | |||
7 | 7 | ||
8 | "use strict"; | 8 | "use strict"; |
9 | 9 | ||
10 | var params = function() { | ||
11 | var queryDict = {}; | ||
12 | location.search.substr(1).split("&").forEach(function(item) { | ||
13 | var pair = item.split("="); | ||
14 | queryDict[pair[0]] = pair[1]; | ||
15 | }); | ||
16 | return queryDict; | ||
17 | }(); | ||
18 | |||
19 | function isController() { | ||
20 | return window.opener == null || window.opener.location.href != window.location.href; | ||
21 | } | ||
22 | |||
23 | class Viewer { | 10 | class Viewer { |
24 | constructor() { | 11 | constructor() { |
25 | this.fileInput = document.getElementById("fileInput"); | 12 | this.fileInput = document.getElementById("fileInput"); |
26 | this.fileInput.style.display = "block"; | 13 | this.fileInput.style.display = "block"; |
27 | 14 | this._listenForInput(); | |
28 | var self = this; | ||
29 | fileInput.addEventListener("change", function(event) { | ||
30 | var callback = function(file) { self._load(file) }; | ||
31 | self._readFile(event.target.files[0], callback); | ||
32 | }); | ||
33 | |||
34 | if ("file" in params) | ||
35 | this._load(params["file"]); | ||
36 | } | 15 | } |
37 | 16 | ||
38 | _load(source) { | 17 | load(source) { |
39 | this.fileInput.style.display = "none"; | 18 | this.fileInput.style.display = "none"; |
40 | pdfjsLib.getDocument(source).then(function(pdf) { | 19 | pdfjsLib.getDocument(source).then(function(pdf) { |
41 | var presentation = new Presentation(pdf); | 20 | var presentation = new Presentation(pdf); |
42 | }); | 21 | }); |
43 | } | 22 | } |
44 | 23 | ||
45 | _readFile(file, callback) { | 24 | _readFile(file) { |
46 | var fileReader = new FileReader(); | 25 | var fileReader = new FileReader(); |
26 | var self = this; | ||
47 | fileReader.onload = function() { | 27 | fileReader.onload = function() { |
48 | var byteArray = new Uint8Array(this.result); | 28 | var byteArray = new Uint8Array(this.result); |
49 | callback(byteArray); | 29 | self.load(byteArray); |
50 | } | 30 | } |
51 | 31 | ||
52 | fileReader.readAsArrayBuffer(file); | 32 | fileReader.readAsArrayBuffer(file); |
53 | } | 33 | } |
54 | } | ||
55 | 34 | ||
56 | if (isController()) new Viewer(); | 35 | _listenForInput() { |
36 | var self = this; | ||
37 | fileInput.addEventListener("change", function(event) { | ||
38 | self._readFile(event.target.files[0]); | ||
39 | }); | ||
40 | |||
41 | document.body.addEventListener("drop", function(event) { | ||
42 | event.preventDefault(); | ||
43 | event.stopPropagation(); | ||
44 | self._readFile(event.dataTransfer.files[0]); | ||
45 | }); | ||
46 | |||
47 | document.body.addEventListener("dragover", function(event) { | ||
48 | event.preventDefault(); | ||
49 | event.stopPropagation(); | ||
50 | }); | ||
51 | } | ||
52 | } | ||