From 966ffecbfca05a4794e82d111375815592609a0e Mon Sep 17 00:00:00 2001 From: luxowner Date: Wed, 29 Oct 2025 20:09:08 +0100 Subject: [PATCH] Upload files to "assets/js" --- assets/js/main.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 assets/js/main.js diff --git a/assets/js/main.js b/assets/js/main.js new file mode 100644 index 0000000..73859e2 --- /dev/null +++ b/assets/js/main.js @@ -0,0 +1,127 @@ +(function() { + // Functions + // ========================================================================= + /** + * Adds event listeners to change active stylesheet and restore previously + * activated stylesheet on reload. + * + * @example + * + * This link: + * Bar + * Activates an existing matched (href + group match): + * + * Generates a new if needed (no href + group match): + * + * Disables elements that match group but not href + * + * Ignores elements that do not match href and group + * + */ + function initStyleSwitcher() { + var SESSION_STORAGE_KEY = 'activeStylesheetHref'; + var SESSION_VAL_SEPARATOR = '||'; + + var isInitialized = false; + + function createLinkedStylesheet(styleHref, styleGroup) { + var activeElm = document.createElement('link'); + + activeElm.setAttribute('rel', 'stylesheet'); + activeElm.setAttribute('href', styleHref); + activeElm.setAttribute('data-style-group', styleGroup || ''); + + document.head.appendChild(activeElm); + + activeElm.addEventListener('load', function linkOnLoad() { + activeElm.removeEventListener('load', linkOnLoad); + handleSwitch(styleHref, styleGroup); + }); + + return activeElm; + } + + function handleSwitch(styleHref, styleGroup) { + var activeElm = styleGroup + ? document.querySelector('link[href*="' + styleHref +'"][data-style-group="' + styleGroup + '"]') + : document.querySelector('link[href*="' + styleHref +'"]'); + + if (!activeElm) { + activeElm = createLinkedStylesheet(styleHref, styleGroup); + + // Stylesheet will call this function after loading is complete + return; + } + + // Remove "alternate" keyword and media attribute + activeElm.setAttribute('rel', (activeElm.rel || '').replace(/\s*alternate/g, '').trim()); + activeElm.removeAttribute('media'); + + // Force enable stylesheet (required for some browsers) + activeElm.disabled = true; + activeElm.disabled = false; + + // Store active style sheet + sessionStorage.setItem(SESSION_STORAGE_KEY, styleGroup ? styleHref + SESSION_VAL_SEPARATOR + styleGroup : styleHref); + + var inactiveElms = styleGroup + ? document.querySelectorAll('link:not([href*="' + styleHref +'"])[data-style-group="' + styleGroup + '"]') + : document.querySelectorAll('link:not([href*="' + styleHref +'"])'); + + // Disable other elms + for (var i = 0; i < inactiveElms.length; i++) { + var elm = inactiveElms[i]; + + elm.disabled = true; + + // Fix for browsersync and alternate stylesheet updates. Will + // cause FOUC when switching stylesheets during development, but + // required to properly apply style updates when alternate + // stylesheets are enabled. + if (window.browsersyncObserver) { + var linkRel = elm.getAttribute('rel') || ''; + var linkRelAlt = linkRel.indexOf('alternate') > -1 ? linkRel : (linkRel + ' alternate').trim(); + + elm.setAttribute('rel', linkRelAlt); + } + } + + // CSS custom property ponyfill + if ((window.$docsify || {}).themeable) { + window.$docsify.themeable.util.cssVars(); + } + } + + // Event listeners + if (!isInitialized) { + isInitialized = true; + + // Restore active stylesheet + document.addEventListener('DOMContentLoaded', function() { + var storedData = sessionStorage.getItem(SESSION_STORAGE_KEY) || ''; + var storedVals = storedData.split(SESSION_VAL_SEPARATOR); + var styleHref = storedVals[0] || ''; + var styleGroup = storedVals[1] || ''; + + if (styleHref) { + handleSwitch(styleHref, styleGroup); + } + }); + + // Update active stylesheet + document.addEventListener('click', function(evt) { + var styleHref = evt.target.getAttribute('data-style-href'); + var styleGroup = evt.target.getAttribute('data-style-group'); + + if (styleHref) { + handleSwitch(styleHref, styleGroup); + evt.preventDefault(); + } + }); + } + } + + // Main + // ========================================================================= + initStyleSwitcher(); +})(); \ No newline at end of file