From 8931cc9418be09206de86f6081b61d591ce8e862 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 19 Jul 2018 11:55:02 +0200 Subject: Handle file drag and drop --- pointless/viewer/init.js | 31 ++++++++++++++++++++++++++++ pointless/viewer/viewer.css | 3 ++- pointless/viewer/viewer.js | 50 +++++++++++++++++++++------------------------ 3 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 pointless/viewer/init.js (limited to 'pointless/viewer') 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 @@ +/* + * init.js + * Part of Pointless Viewer, a Beamer presentation viewer + * Copyright 2018 Pacien TRAN-GIRARD + * License: GNU GPL v3 + */ + +"use strict"; + +var params = function() { + var queryDict = {}; + location.search.substr(1).split("&").forEach(function(item) { + var pair = item.split("="); + queryDict[pair[0]] = pair[1]; + }); + return queryDict; +}(); + +function isController() { + return window.opener == null || window.opener.location.href != window.location.href; +} + +function init() { + var viewer = new Viewer(); + + if ("file" in params) + viewer.load(params["file"]); +} + +if (isController()) + 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 @@ * License: GNU GPL v3 */ -body { +html, body { background-color: black; color: white; font-family: sans-serif; overflow: hidden; + height: 100%; } #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 @@ "use strict"; -var params = function() { - var queryDict = {}; - location.search.substr(1).split("&").forEach(function(item) { - var pair = item.split("="); - queryDict[pair[0]] = pair[1]; - }); - return queryDict; -}(); - -function isController() { - return window.opener == null || window.opener.location.href != window.location.href; -} - class Viewer { constructor() { this.fileInput = document.getElementById("fileInput"); this.fileInput.style.display = "block"; - - var self = this; - fileInput.addEventListener("change", function(event) { - var callback = function(file) { self._load(file) }; - self._readFile(event.target.files[0], callback); - }); - - if ("file" in params) - this._load(params["file"]); + this._listenForInput(); } - _load(source) { + load(source) { this.fileInput.style.display = "none"; pdfjsLib.getDocument(source).then(function(pdf) { var presentation = new Presentation(pdf); }); } - _readFile(file, callback) { + _readFile(file) { var fileReader = new FileReader(); + var self = this; fileReader.onload = function() { var byteArray = new Uint8Array(this.result); - callback(byteArray); + self.load(byteArray); } fileReader.readAsArrayBuffer(file); } -} -if (isController()) new Viewer(); + _listenForInput() { + var self = this; + fileInput.addEventListener("change", function(event) { + self._readFile(event.target.files[0]); + }); + + document.body.addEventListener("drop", function(event) { + event.preventDefault(); + event.stopPropagation(); + self._readFile(event.dataTransfer.files[0]); + }); + + document.body.addEventListener("dragover", function(event) { + event.preventDefault(); + event.stopPropagation(); + }); + } +} -- cgit v1.2.3