|
2 | 2 | <!-- Dynamic loading of the user selected skin stylesheet -->
|
3 | 3 | {% endcomment %}
|
4 | 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 }}"> |
| 5 | +<link rel="stylesheet" id="skinedStylesheet" href="{{ '/assets/css/main_oi-light.css' | relative_url }}"> |
7 | 6 |
|
8 | 7 | <style>
|
9 | 8 | #full-page-container.full-page-container {
|
|
23 | 22 |
|
24 | 23 | <script async="false">
|
25 | 24 |
|
26 |
| - const docRoot = '{{ site.baseurl }}'; |
27 |
| - const darkSkin = 'midnight'; |
28 |
| - const lightSkin = 'default'; |
29 |
| - const searchEnabled = {% if site.search == true %} true {% else %} false {% endif %}; |
30 |
| - const searchFromMastHead = {% if site.search_from_masthead == true %} true {% else %} false {% endif %}; |
| 25 | + (function () { |
| 26 | + const defaultDarkSkin = 'oi-dark'; |
| 27 | + const defaultLightSkin = 'oi-light'; |
31 | 28 |
|
32 |
| - function docPrefix() { |
33 |
| - return (docRoot != '' ? docRoot + '/' : ''); |
34 |
| - } |
| 29 | + const selectedSkinStateKey = 'skin-state'; |
| 30 | + const selectedSkinBackgroundColorKey = 'skin-background-color' |
| 31 | + const lightSkinKey = 'skin-light'; |
| 32 | + const darkSkinKey = 'skin-dark'; |
| 33 | + |
| 34 | + const darkModeStateOn = 'dark'; |
| 35 | + const darkModeStateOff = 'light'; |
| 36 | + |
| 37 | + //selectedLightSkin = 'default'; |
| 38 | + //selectedDarkSkin = 'default'; |
| 39 | + |
| 40 | + function setSkinModeState(state) { |
| 41 | + const stylesheet = document.getElementById('skinedStylesheet'); |
| 42 | + const skinName = getSkinForState(state); |
35 | 43 |
|
36 |
| - function setCookie(name, value, days) { |
37 |
| - var expires = ""; |
38 |
| - if (days) { |
39 |
| - var date = new Date(); |
40 |
| - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); |
41 |
| - expires = "; expires=" + date.toUTCString(); |
| 44 | + // TODO: It seems it is not always triggered on a href value change once it is loaded/cached |
| 45 | + // We might have to force save the new color somehow in a different way as well |
| 46 | + // (Maybe a delayed saveColorChanges from window.addEventListener("load", ...) bellow ?!?! |
| 47 | + stylesheet.onload = saveColorChanges; |
| 48 | + stylesheet.href = docPrefix() + '/assets/css/main' + (skinName == 'default' ? '' : '_' + skinName) + '.css'; |
| 49 | + |
| 50 | + setCookie(selectedSkinStateKey, state); |
42 | 51 | }
|
43 |
| - document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Strict"; |
44 |
| - } |
45 | 52 |
|
46 |
| - function getCookie(name, defaultValue = null) { |
47 |
| - var nameEQ = name + "="; |
48 |
| - var ca = document.cookie.split(';'); |
49 |
| - for (var i = 0; i < ca.length; i++) { |
50 |
| - var c = ca[i]; |
51 |
| - while (c.charAt(0) == ' ') { |
52 |
| - c = c.substring(1, c.length); |
53 |
| - } |
54 |
| - if (c.indexOf(nameEQ) == 0) { |
55 |
| - return c.substring(nameEQ.length, c.length); |
56 |
| - } |
| 53 | + function getLightSkin() { |
| 54 | + return getCookie(lightSkinKey, defaultLightSkin, true); |
57 | 55 | }
|
58 |
| - return defaultValue; |
59 |
| - } |
60 | 56 |
|
61 |
| - (function () { |
62 |
| - function setSkin(skin) { |
63 |
| - const stylesheet = document.getElementById('skinedStylesheet'); |
64 |
| - //stylesheet.onload = saveChanges; |
65 |
| - stylesheet.href = docPrefix() + '/assets/css/main' + (skin == 'default' ? '' : '_' + skin) + '.css'; |
66 |
| - setCookie('skin', skin, 365 * 100); |
| 57 | + function getDarkSkin() { |
| 58 | + return getCookie(darkSkinKey, defaultDarkSkin, true); |
67 | 59 | }
|
68 | 60 |
|
| 61 | + function getSkinModeState() { |
| 62 | + return getCookie(selectedSkinStateKey, darkModeStateOff, true); |
| 63 | + } |
| 64 | + |
| 65 | + function getSkinForState(state) { |
| 66 | + return state == darkModeStateOn ? getDarkSkin() : getLightSkin(); |
| 67 | + } |
| 68 | + |
| 69 | + function getSelectedSkin() { |
| 70 | + return getSkinForState(getSkinModeState()); |
| 71 | + } |
| 72 | + |
69 | 73 | function toggleSkin(event) {
|
70 |
| - if (getCookie('skin', 'default') == darkSkin) { |
| 74 | + const currentSkinState = getSkinModeState(); |
| 75 | + if (currentSkinState == darkModeStateOff) { |
71 | 76 | event.target.classList.remove('fa-toggle-on');
|
72 | 77 | event.target.classList.add('fa-toggle-off');
|
73 |
| - setSkin(lightSkin); |
74 | 78 | }
|
75 | 79 | else {
|
76 | 80 | event.target.classList.remove('fa-toggle-off');
|
77 | 81 | event.target.classList.add('fa-toggle-on');
|
78 |
| - setSkin(darkSkin); |
79 | 82 | }
|
| 83 | + setSkinModeState(currentSkinState == darkModeStateOff ? darkModeStateOn : darkModeStateOff); |
80 | 84 | event.currentTarget.blur();
|
81 | 85 | }
|
82 | 86 |
|
| 87 | + function saveColorChanges() { |
| 88 | + const htmlStyle = window.getComputedStyle(document.documentElement); |
| 89 | + const backgroundColor = htmlStyle.backgroundColor; |
| 90 | + setCookie(selectedSkinBackgroundColorKey, backgroundColor); |
| 91 | + }; |
| 92 | + |
83 | 93 | function toggleIcon(target, off) {
|
84 | 94 | if (off) {
|
85 | 95 | target.classList.remove('fa-toggle-on');
|
|
92 | 102 | }
|
93 | 103 |
|
94 | 104 | function toggleSkin(event) {
|
95 |
| - var off = getCookie('skin', 'default') == darkSkin; |
| 105 | + var off = getSkinModeState() == darkModeStateOff; |
96 | 106 |
|
97 |
| - if (off) |
98 |
| - setSkin(lightSkin); |
99 |
| - else |
100 |
| - setSkin(darkSkin); |
| 107 | + setSkinModeState(off ? darkModeStateOn : darkModeStateOff); |
101 | 108 | toggleIcon(event.target, off);
|
102 | 109 |
|
103 | 110 | event.currentTarget.blur();
|
|
107 | 114 | 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)
|
108 | 115 | */
|
109 | 116 | window.addEventListener("load", function () {
|
110 |
| - function saveChanges() { |
111 |
| - const htmlStyle = window.getComputedStyle(document.documentElement); |
112 |
| - const backgroundColor = htmlStyle.backgroundColor; |
113 |
| - setCookie('skin-background-color', backgroundColor, 365 * 100); |
114 |
| - }; |
115 |
| - |
116 | 117 | var container = document.getElementById("full-page-container");
|
117 | 118 | if (container)
|
118 | 119 | container.classList.remove('hidden');
|
119 | 120 |
|
120 | 121 | // Why this is not working?!?!
|
121 | 122 | //document.body.style.removeProperty("backgroundColor");
|
122 | 123 | document.body.style.backgroundColor = "";
|
123 |
| - if (storedSkin !== 'default') |
124 |
| - toggleIcon($('#skin-button').find('.masthead_button_icon')[0], false); |
125 |
| - |
| 124 | + |
| 125 | + toggleIcon($('#skin-button').find('.masthead_button_icon')[0], getSkinModeState()); |
126 | 126 | $("#skin-button").on("click", toggleSkin);
|
127 | 127 |
|
128 |
| - saveChanges(); |
| 128 | + // TODO: See, setSkinModeState - stylesheet.onload |
| 129 | + // setTimeout(function () { |
| 130 | + // saveColorChanges(); |
| 131 | + //}, 100); |
129 | 132 | });
|
130 | 133 |
|
131 | 134 | document.addEventListener("DOMContentLoaded", function () {
|
132 |
| - const skinBackgroundColor = getCookie('skin-background-color'); |
| 135 | + const skinBackgroundColor = getCookie(selectedSkinBackgroundColorKey); |
133 | 136 | if (skinBackgroundColor)
|
134 | 137 | document.body.style.backgroundColor = skinBackgroundColor;
|
135 | 138 | });
|
136 | 139 |
|
137 |
| - const storedSkin = getCookie('skin', 'default'); |
138 |
| - if (storedSkin !== 'default') |
139 |
| - setSkin(storedSkin); |
| 140 | + // These are just for always saving a default to the cookiestore if missing |
| 141 | + getLightSkin(); |
| 142 | + getDarkSkin(); |
| 143 | + |
| 144 | + setSkinModeState(getSkinModeState()); |
140 | 145 |
|
141 | 146 | })();
|
142 | 147 | </script>
|
0 commit comments