From 1f4724edbaaddccf46aa3157abb0fa9fb71eece2 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 26 Oct 2023 20:40:25 +0200 Subject: init: simplify loading file path from hash --- beamer/viewer/init.js | 18 ------------------ beamer/viewer/viewer.js | 20 +++++++++++++++++++- 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 @@ "use strict"; -const params = function() { - const queryDict = {}; - location.hash.substr(1).split("&").forEach(item => { - const pair = item.split("="); - queryDict[pair[0]] = pair[1]; - }); - return queryDict; -}(); - function isController() { return window.opener == null || window.opener.location.href != window.location.href; } @@ -41,16 +32,7 @@ function initCache() { function init() { initCache(); - const viewer = new Viewer(); - - if ("file" in params) - viewer.loadFromUrl(params["file"]); -} - -function load(file) { - location.hash = "file=" + file; - location.reload(); } 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 { this.fileInput.files = dummy.files; } - loadFromUrl(url) { + _loadFromUrl(url) { this._setFileName(url); this._setPresentation(url); } @@ -94,5 +94,23 @@ class Viewer { event.preventDefault(); event.stopPropagation(); }); + + window.addEventListener('load', () => this._on_hash_change()); + window.addEventListener('hashchange', () => this._on_hash_change()); + } + + _on_hash_change() { + const params = this._hash_params(); + if ("file" in params) + this._loadFromUrl(params["file"]); + } + + _hash_params() { + const queryDict = {}; + location.hash.substr(1).split("&").forEach(item => { + const pair = item.split("="); + queryDict[pair[0]] = pair[1]; + }); + return queryDict; } } diff --git a/index.html b/index.html index 2f261ff..3df5997 100644 --- a/index.html +++ b/index.html @@ -73,7 +73,7 @@
Select a PDF file to open, or try with - demo slides. + demo slides.
-- cgit v1.2.3