From 61adebd4abccda1e4a9c7b768ad2f6ace8dfa5ed Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Sun, 9 Aug 2020 01:19:41 +0200 Subject: [PATCH] fix cw opening on first load --- script/single.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/script/single.js b/script/single.js index 8062c4f..cd5364b 100644 --- a/script/single.js +++ b/script/single.js @@ -8,7 +8,7 @@ let mainlink; let opened = new Set; function openCW(id, caption, focusLink) { - opened.add(id); + if (id) opened.add(id); mainfig.removeChild(caption); mainlink.tabIndex = 0; if (focusLink) mainlink.focus(); @@ -22,7 +22,7 @@ function addCWListeners(id, caption) { } } -function setImage(id, src, href, cw) { +function setImage(id, src, width, height, href, cw, firstLoad) { let caption = document.getElementById('cw'); let cwText = `cw: ${cw}`; @@ -46,14 +46,16 @@ function setImage(id, src, href, cw) { mainlink.href = href; } -function activateButton(button, doPush = true) { +function activateButton(button, doPush = true, firstLoad = false) { setImage(button.id, button.value, - button.dataset.link, button.dataset.warning); + button.dataset.width, button.dataset.height, + button.dataset.link, button.dataset.warning, + firstLoad); if (doPush) history.pushState(null, '', '#' + button.id); } -function useFragment() { +function useFragment(firstLoad = false) { let button = document.querySelector('#alts input'); let frag = document.location.hash.replace(/^#/, ''); @@ -62,10 +64,15 @@ function useFragment() { if (button0) button = button0; } + let id; + if (button) { + id = button.id; button.checked = true; - activateButton(button, false); + activateButton(button, false, firstLoad); } + + if (firstLoad) addCWListeners(id, document.getElementById('cw')); } function setup() { @@ -77,10 +84,8 @@ function setup() { button.onchange = e => { if (button.checked) activateButton(button); }; } - addCWListeners(document.getElementById('cw')); - window.addEventListener('popstate', useFragment); - useFragment(); + useFragment(true); } window.addEventListener('DOMContentLoaded', setup);