From 85629d0d33656d60235585068771db601e5f285f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 14 Sep 2023 11:03:22 +0300 Subject: [scripts/presentation] Improved logic to be more flexible and shortened ?a=y to ?a --- src/scripts/presentation.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src/scripts') diff --git a/src/scripts/presentation.js b/src/scripts/presentation.js index 77fa226..625b0ec 100644 --- a/src/scripts/presentation.js +++ b/src/scripts/presentation.js @@ -112,27 +112,39 @@ window.addEventListener('load', fixAbsoluteSlideScale); /* Show as article button */ function toggleArticleMode() { + var href = window.location.href; + var indqa = href.indexOf('?a'); + printBtn.classList.toggle('nodisplay'); presentBtn.classList.toggle('nodisplay'); // Toggle presentation.css document.styleSheets.item(3).disabled = !document.styleSheets.item(3).disabled; - // Add a=y to url when in article mode - var url = window.location.href.substring(0, window.location.href.indexOf('?')); - if (document.styleSheets.item(3).disabled) url += '?a=y'; - url += window.location.href.substr(window.location.href.indexOf('#')); + // Add "a" to url when in article mode + if (document.styleSheets.item(3).disabled && indqa == -1) { + var indquery = href.indexOf('?'); + if (indquery < 0) { + var indend = Math.max(href.lastIndexOf('/'), href.indexOf('.html') + 4); + href = href.slice(0, indend + 1) + '?a' + href.slice(indend + 1); + } + else { + href = href.slice(0, indquery + 1) + 'a' + href.slice(indquery + 1); + } + } + else if (!document.styleSheets.item(3).disabled && indqa > -1) { + href = href.slice(0, indqa) + href.slice(indqa + 2); + } - window.history.replaceState(history.state, document.title, url); + window.history.replaceState(history.state, document.title, href); // Fix message - if (articleBtn.innerText[0] === 'A') { + if (articleBtn.innerText[0] === 'A') articleBtn.innerText = 'Slide' + articleBtn.innerText.substr(7); - } else { + else articleBtn.innerText = 'Article' + articleBtn.innerText.substr(5); - } } articleBtn.addEventListener('click', toggleArticleMode); window.addEventListener('load', function() { - if (window.location.href.substr(window.location.href.indexOf('?'), 4) === '?a=y') + if (window.location.href.substr(window.location.href.indexOf('?'), 2) === '?a') toggleArticleMode(); }); -- cgit v1.3.1-23-gd82d