Skip to content

Commit 180a1fa

Browse files
committed
Added skin switching functionality to a separate skins.html that is included in the head.html
- docRot and cookie handling is moded to the new skin hanling part that is always presented this way in each page very early Signed-off-by: Hofi <hofione@gmail.com>
1 parent c02ee98 commit 180a1fa

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed

_includes/head.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
<link href="{% if site.atom_feed.path %}{{ site.atom_feed.path }}{% else %}{{ '/feed.xml' | relative_url }}{% endif %}" type="application/atom+xml" rel="alternate" title="{{ site.title }} Feed">
77
{% endunless %}
88

9-
<!-- https://t.co/dKP3o1e -->
109
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1110

12-
<script>
13-
document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/g, '') + ' js ';
14-
</script>
11+
<script>document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/g, '') + ' js ';</script>
12+
13+
{% include skins.html %}
1514

16-
<!-- For all browsers -->
17-
<link rel="stylesheet" href="{{ '/assets/css/main.css' | relative_url }}">
1815
<link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
1916
<noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css"></noscript>
2017

_includes/skins.html

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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>

_layouts/base.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
<!-- layout: compress -->
23
---
34

45
<!doctype html>
@@ -15,6 +16,8 @@
1516
</head>
1617

1718
<body class="layout--{{ page.layout | default: layout.layout }}{% if page.classes or layout.classes %}{{ page.classes | default: layout.classes | join: ' ' | prepend: ' ' }}{% endif %}">
18-
{{ content }}
19+
<div id="full-page-container" class="full-page-container hidden">
20+
{{ content }}
21+
</div>
1922
</body>
2023
</html>

0 commit comments

Comments
 (0)