fix cw opening on first load

This commit is contained in:
Rhiannon Morris 2020-08-09 01:19:41 +02:00
parent a3b78d86e8
commit 61adebd4ab

View file

@ -8,7 +8,7 @@ let mainlink;
let opened = new Set; let opened = new Set;
function openCW(id, caption, focusLink) { function openCW(id, caption, focusLink) {
opened.add(id); if (id) opened.add(id);
mainfig.removeChild(caption); mainfig.removeChild(caption);
mainlink.tabIndex = 0; mainlink.tabIndex = 0;
if (focusLink) mainlink.focus(); 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 caption = document.getElementById('cw');
let cwText = `<span id=cw-text>cw: <b>${cw}</b></span>`; let cwText = `<span id=cw-text>cw: <b>${cw}</b></span>`;
@ -46,14 +46,16 @@ function setImage(id, src, href, cw) {
mainlink.href = href; mainlink.href = href;
} }
function activateButton(button, doPush = true) { function activateButton(button, doPush = true, firstLoad = false) {
setImage(button.id, button.value, 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); if (doPush) history.pushState(null, '', '#' + button.id);
} }
function useFragment() { function useFragment(firstLoad = false) {
let button = document.querySelector('#alts input'); let button = document.querySelector('#alts input');
let frag = document.location.hash.replace(/^#/, ''); let frag = document.location.hash.replace(/^#/, '');
@ -62,10 +64,15 @@ function useFragment() {
if (button0) button = button0; if (button0) button = button0;
} }
let id;
if (button) { if (button) {
id = button.id;
button.checked = true; button.checked = true;
activateButton(button, false); activateButton(button, false, firstLoad);
} }
if (firstLoad) addCWListeners(id, document.getElementById('cw'));
} }
function setup() { function setup() {
@ -77,10 +84,8 @@ function setup() {
button.onchange = e => { if (button.checked) activateButton(button); }; button.onchange = e => { if (button.checked) activateButton(button); };
} }
addCWListeners(document.getElementById('cw'));
window.addEventListener('popstate', useFragment); window.addEventListener('popstate', useFragment);
useFragment(); useFragment(true);
} }
window.addEventListener('DOMContentLoaded', setup); window.addEventListener('DOMContentLoaded', setup);