|
| 1 | +{% comment %} |
| 2 | +<!-- Dynamic loading of the user selected skin stylesheet --> |
| 3 | +{% endcomment %} |
| 4 | + |
| 5 | +{% comment %}<!-- NOTE: the 'default' skin is in the 'main.css', see the main.scss file for details -->{% endcomment %} |
| 6 | +<link rel="stylesheet" id="skinedStylesheet" href="{{ '/assets/css/main.css' | relative_url }}"> |
| 7 | + |
| 8 | +<style> |
| 9 | + #full-page-container.full-page-container { |
| 10 | + position: fixed; |
| 11 | + top: 0; |
| 12 | + left: 0; |
| 13 | + width: 100%; |
| 14 | + height: 100%; |
| 15 | + display: contents; |
| 16 | + } |
| 17 | + |
| 18 | + #full-page-container.hidden { |
| 19 | + visibility: hidden; |
| 20 | + display: none; |
| 21 | + } |
| 22 | +</style> |
| 23 | + |
| 24 | +<script async="false"> |
| 25 | + |
| 26 | + // FIXME: How to get the real base URL (without using Liquid and Front Matter) ?!?! |
| 27 | + const docRoot = ''; |
| 28 | + const darkSkin = 'midnight'; |
| 29 | + const lightSkin = 'default'; |
| 30 | + |
| 31 | + function docPrefix() { |
| 32 | + return (docRoot != '' ? docRoot + '/' : ''); |
| 33 | + } |
| 34 | + |
| 35 | + function setCookie(name, value, days) { |
| 36 | + var expires = ""; |
| 37 | + if (days) { |
| 38 | + var date = new Date(); |
| 39 | + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); |
| 40 | + expires = "; expires=" + date.toUTCString(); |
| 41 | + } |
| 42 | + document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Strict"; |
| 43 | + } |
| 44 | + |
| 45 | + function getCookie(name, defaultValue = null) { |
| 46 | + var nameEQ = name + "="; |
| 47 | + var ca = document.cookie.split(';'); |
| 48 | + for (var i = 0; i < ca.length; i++) { |
| 49 | + var c = ca[i]; |
| 50 | + while (c.charAt(0) == ' ') { |
| 51 | + c = c.substring(1, c.length); |
| 52 | + } |
| 53 | + if (c.indexOf(nameEQ) == 0) { |
| 54 | + return c.substring(nameEQ.length, c.length); |
| 55 | + } |
| 56 | + } |
| 57 | + return defaultValue; |
| 58 | + } |
| 59 | + |
| 60 | + function setSkin(skin) { |
| 61 | + const stylesheet = document.getElementById('skinedStylesheet'); |
| 62 | + //stylesheet.onload = saveChanges; |
| 63 | + stylesheet.href = docPrefix() + '/assets/css/main' + (skin == 'default' ? '' : '_' + skin) + '.css'; |
| 64 | + setCookie('skin', skin, 365 * 100); |
| 65 | + } |
| 66 | + |
| 67 | + function toggleSkin() { |
| 68 | + if (getCookie('skin', 'default') == darkSkin) |
| 69 | + setSkin(lightSkin); |
| 70 | + else |
| 71 | + setSkin(darkSkin); |
| 72 | + //location.reload(); |
| 73 | + } |
| 74 | + |
| 75 | + /* As of the dynamic skin stylsheet loading, it is better to wait for the finish of the whole page content load and rendering to avoid apperance of |
| 76 | + half rendered content parts (it is better to see an empty content even if it might appear in a different bacground color that the skin has) |
| 77 | + */ |
| 78 | + window.addEventListener("load", function () { |
| 79 | + function saveChanges() { |
| 80 | + const htmlStyle = window.getComputedStyle(document.documentElement); |
| 81 | + const backgroundColor = htmlStyle.backgroundColor; |
| 82 | + setCookie('skin-background-color', backgroundColor, 365 * 100); |
| 83 | + }; |
| 84 | + |
| 85 | + var container = document.getElementById("full-page-container"); |
| 86 | + if (container) |
| 87 | + container.classList.remove('hidden'); |
| 88 | + |
| 89 | + // Why this is not working?!?! |
| 90 | + //document.body.style.removeProperty("backgroundColor"); |
| 91 | + document.body.style.backgroundColor = ""; |
| 92 | + |
| 93 | + saveChanges(); |
| 94 | + |
| 95 | + $(".skin__toggle").on("click", toggleSkin); |
| 96 | + }); |
| 97 | + |
| 98 | + document.addEventListener("DOMContentLoaded", function () { |
| 99 | + const skinBackgroundColor = getCookie('skin-background-color'); |
| 100 | + if (skinBackgroundColor) |
| 101 | + document.body.style.backgroundColor = skinBackgroundColor; |
| 102 | + }); |
| 103 | + |
| 104 | + const storedSkin = getCookie('skin', 'default'); |
| 105 | + if (storedSkin !== 'default') |
| 106 | + setSkin(storedSkin); |
| 107 | + |
| 108 | +</script> |
0 commit comments