diff --git a/static/js/switchcss.js b/static/js/switchcss.js index f6aec2a..628a0b0 100644 --- a/static/js/switchcss.js +++ b/static/js/switchcss.js @@ -1,44 +1,80 @@ -const THEME_KEY = "ZULMA_THEME"; +(function (switch_css) { + //Constants + const THEME_KEY = "ZULMA_THEME"; + const STOP_LINK_CSS_ID = "stop-blink"; -let theme = localStorage.getItem(THEME_KEY); -if (theme) { - changeTheme(theme); - window.addEventListener('DOMContentLoaded', () => { - document.querySelectorAll('#theme-select>option').forEach(element => { - if (element.value === theme) { - element.selected = 'selected'; + //Variables + let link = null; + let theme = localStorage.getItem(THEME_KEY); + + //Private Methods + function changeTheme(themeName, firstLoad) { + var fileref = document.createElement("link"); + fileref.rel = "stylesheet"; + fileref.type = "text/css"; + fileref.href = `/${themeName}.css`; + link = document.getElementsByTagName("head")[0].appendChild(fileref); + + link.addEventListener('load', onLinkLoad); + if (firstLoad) { + document.querySelectorAll('.stylesheet').forEach((el) => { + el.remove(); + }); + } + + saveTheme(themeName); + }; + + function onLinkLoad() { + link.removeEventListener('load', onLinkLoad); + document.querySelectorAll('.stylesheet').forEach((el) => { + el.remove(); + }); + link.className += 'stylesheet'; + addcss('body{visibility:visible;}'); + }; + + function saveTheme(themeName) { + localStorage.setItem(THEME_KEY, themeName); + }; + + function addcss(css) { + let el = document.getElementById(STOP_LINK_CSS_ID); + if (el) { + el.innerText = css; + } else { + var head = document.getElementsByTagName('head')[0]; + var style = document.createElement('style'); + style.id = STOP_LINK_CSS_ID; + style.setAttribute('type', 'text/css'); + if (style.styleSheet) { // IE + style.styleSheet.cssText = css; + } else { // the world + style.appendChild(document.createTextNode(css)); + } + head.insertBefore(style, head.firstChild); + } + }; + + //Public Methods + switch_css.init = function () { + if (theme && !document.getElementById(theme)) { + addcss('body{visibility:hidden;}') + changeTheme(theme, true); + window.addEventListener('DOMContentLoaded', () => { + document.querySelectorAll('#theme-select>option').forEach(element => { + if (element.value === theme) { + element.selected = 'selected'; + } + }); + }); + } + window.addEventListener('DOMContentLoaded', () => { + document.getElementById('theme-select').onchange = function () { + changeTheme(this.value); } }); - }); -} - -window.addEventListener('DOMContentLoaded', () => { - document.getElementById('theme-select').onchange = function () { - changeTheme(this.value); } -}); +}(switch_css = window.switch_css || {})); -function changeTheme(themeName) { - let alternates = []; - - document.querySelectorAll('link.stylesheet').forEach(element => { - if (element.id === themeName) { - element.disabled = false; - element.media = ''; - } - else { - alternates.push(element); - } - }); - - alternates.forEach(element => { - element.disabled = true; - element.media = 'none'; - }); - - saveTheme(themeName); -} - -function saveTheme(themeName) { - localStorage.setItem(THEME_KEY, themeName); -} \ No newline at end of file +switch_css.init(); \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index c2a44c2..583260d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,23 +16,20 @@ {% block title %}{{ config.title }}{% endblock title %} - {% block css %} - {{ index_macros::css() }} - {% endblock css %} + + {{ index_macros::css() }} + {% if config.extra.zulma_allow_theme_selection %} + + {% endif %} {% if config.generate_rss %} {% endif %} - {% block js %} {% if config.build_search_index %} {% endif %} - {% if config.extra.zulma_allow_theme_selection %} - - {% endif %} - {% endblock js %} {% block extra_head %} {% endblock extra_head %} @@ -79,9 +76,11 @@ {% endif %} - {{ index_macros::footer() }} {% endblock content %} + {% block footer %} + {{ index_macros::footer() }} + {% endblock footer %} diff --git a/templates/index_macros.html b/templates/index_macros.html index 8c0c90e..93cd69d 100644 --- a/templates/index_macros.html +++ b/templates/index_macros.html @@ -125,24 +125,10 @@ {% endmacro footer %} {% macro css() %} -{% if config.extra.zulma_allow_theme_selection %} -{% for theme in config.extra.zulma_themes %} {% if config.extra.zulma_theme %} -{% set default_theme = config.extra.zulma_theme %} + {% else %} -{% set default_theme = "default" %} -{% endif %} -{% if default_theme == theme %} - -{% else %} - -{% endif %} -{% endfor %} -{% elif config.extra.zulma_theme %} - -{% else %} - + {% endif %} {% endmacro css %} \ No newline at end of file diff --git a/templates/page.html b/templates/page.html index 5280173..68dd1ca 100644 --- a/templates/page.html +++ b/templates/page.html @@ -7,23 +7,25 @@ {{ super() }} {% endblock header %} -
-
-
-
-
-
- {{ post_macros::article_header(page = page) }} -
- {{ page.content | safe }} +
+
+
+
+
+
+
+ {{ post_macros::article_header(page = page) }} +
+ {{ page.content | safe }} +
+ {% block page_footer %} + {{ post_macros::post_footer(page=page) }} + {% endblock page_footer %}
- {% block page_footer %} - {{ post_macros::post_footer(page=page) }} - {% endblock page_footer %} -
-
+ +
- -
+ + {% endblock content %} \ No newline at end of file