Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/assets/js/theme.js
1677 views
1
function gtnLocalSet(key, value){
2
// Work around for localStorage not being available in Safari private browsing
3
// https://stackoverflow.com/a/14555361/358804
4
try {
5
localStorage.setItem(key, value);
6
} catch (e) {
7
console.log(`localStorage not available, cannot set ${key} to ${value}`);
8
}
9
}
10
11
function gtnLocalGet(key){
12
// Work around for localStorage not being available in Safari private browsing
13
try {
14
return localStorage.getItem(key);
15
} catch (e) {
16
console.log(`localStorage not available, cannot get ${key}`);
17
return undefined;
18
}
19
}
20
21
function getTheme2(){
22
return JSON.parse(gtnLocalGet('theme2')) || {brightness: "auto", light_theme: "white", dark_theme: "night", contrast: "auto", theme: "default"};
23
}
24
25
function getThemePreference(){
26
// If there's a theme specified in the URL
27
params = (new URL(document.location)).searchParams;
28
paramTheme = params.get('theme')
29
// Immediately override our preferences with it
30
if(paramTheme){
31
setThemePreference(paramTheme, false);
32
}
33
return getTheme2().theme;
34
}
35
36
37
function setThemePreference(newValue, automatic) {
38
var tmpprefs = getTheme2();
39
tmpprefs['theme'] = newValue;
40
gtnLocalSet('theme2', JSON.stringify(tmpprefs));
41
42
// If this setTheme call was triggered "automatically", not by user choice, mark it as such
43
if(automatic === true){
44
gtnLocalSet('training-theme-automatic', 'true');
45
} else {
46
gtnLocalSet('training-theme-automatic', 'false');
47
}
48
}
49
50
function setTheme(theme, automatic){
51
let body_elem = document.getElementsByTagName("body")[0];
52
var old_classes = body_elem.getAttribute("class")
53
if (old_classes === undefined || old_classes === null){
54
old_classes = "";
55
}
56
var new_classes = old_classes.split(' ').filter(function(x){ return x.substring(0, 6) !== "theme-" })
57
new_classes.push('theme-' + theme)
58
59
body_elem.setAttribute("class", new_classes.join(' '))
60
61
setThemePreference(theme, automatic);
62
}
63
64
// If it's in the URL, or in a saved peference
65
training_theme_cookie = getThemePreference()
66
if(training_theme_cookie){
67
// Then restore the theme to what's in the URL/preference.
68
setTheme(training_theme_cookie, false);
69
var prefs = getTheme2();
70
prefs['theme'] = training_theme_cookie;
71
gtnLocalSet('theme2', JSON.stringify(prefs));
72
}
73
74
// Theme2
75
function processTheme2(){
76
// MediaQueryList
77
const darkModePreference = window.matchMedia("(prefers-color-scheme: dark)");
78
if (darkModePreference.matches) {
79
document.body.dataset.brightness = 'dark';
80
} else {
81
document.body.dataset.brightness = 'light';
82
}
83
84
// recommended method for newer browsers: specify event-type as first argument
85
darkModePreference.addEventListener("change", e => {
86
console.log(e.matches);
87
if (e.matches) {
88
document.body.dataset.brightness = 'dark';
89
} else {
90
document.body.dataset.brightness = 'light';
91
}
92
});
93
94
95
const contrastModePreferenceMore = window.matchMedia("(prefers-contrast: more)");
96
const contrastModePreferenceLess= window.matchMedia("(prefers-contrast: less)");
97
if (contrastModePreferenceMore.matches) {
98
document.body.dataset.contrast = 'high';
99
}
100
if (contrastModePreferenceLess.matches) {
101
document.body.dataset.contrast = 'low';
102
}
103
104
// recommended method for newer browsers: specify event-type as first argument
105
contrastModePreferenceMore.addEventListener("change", e => {
106
if (e.matches) {
107
document.body.dataset.contrast = 'high';
108
}
109
});
110
contrastModePreferenceLess.addEventListener("change", e => {
111
if (e.matches) {
112
document.body.dataset.contrast = 'low';
113
}
114
});
115
116
// Do this last so it overrides the above
117
if (gtnLocalGet('theme2') !== null){
118
var prefs = getTheme2();
119
console.log("prefs", prefs);
120
Object.keys(prefs).forEach(function(key) {
121
if (key === "brightness" && prefs[key] === "auto"){
122
return;
123
}
124
document.body.dataset[key] = prefs[key];
125
})
126
}
127
128
if(gtnLocalGet('fontMain') !== null){
129
document.body.dataset["font_main"] = gtnLocalGet("fontMain");
130
}
131
if(gtnLocalGet('fontCode') !== null){
132
document.body.dataset["font_code"] = gtnLocalGet("fontCode");
133
}
134
135
}
136
processTheme2();
137
138
139
140
// However we have some additional things:
141
var gtnThemeCurrentDate = new Date();
142
var gtnThemeCurrentMonth = gtnThemeCurrentDate.getMonth();
143
// If we had automatically set the theme in the past, or never automatically set one before
144
var gtnThemeIsAutomatic = (gtnLocalGet('training-theme-automatic') === "true" || gtnLocalGet('training-theme-automatic') === undefined)
145
// If the user had manually chosen a theme, then gtnThemeIsAutomatic === false
146
147
if(gtnThemeCurrentMonth === 1 && window.location.pathname === "/training-material/"){ // Just for February
148
// If the user hasn't manually chosen a theme
149
if(gtnThemeIsAutomatic || getThemePreference() === null || getThemePreference() === "undefined" || getThemePreference() === undefined){
150
setTheme("blm", true);
151
}
152
}
153
else if(gtnThemeCurrentMonth === 5 && window.location.pathname === "/training-material/"){ // Just for June
154
// If the user hasn't manually chosen a theme
155
if(gtnThemeIsAutomatic || getThemePreference() === null || getThemePreference() === "undefined" || getThemePreference() === undefined){
156
setTheme("progress", true);
157
}
158
}
159
else { // Not one of the "special" months
160
if (gtnThemeIsAutomatic){
161
// Then we mark it as automatic
162
gtnLocalSet('training-theme-automatic', 'true');
163
// And set the theme back to default
164
setTheme("default", true);
165
}
166
}
167
168
// URL overrides
169
// If there's a theme specified in the URL
170
var theme2Override = getTheme2();
171
var params = (new URL(document.location)).searchParams;
172
['brightness', 'light_theme', 'dark_theme', 'contrast', 'theme'].forEach(function(key) {
173
if(params.get(key)){
174
console.log(`Processing URL override for ${key}=${params.get(key)}`);
175
document.body.dataset[key] = params.get(key);
176
theme2Override[key] = params.get(key);
177
}
178
})
179
if(Object.keys(theme2Override).length > 0){
180
gtnLocalSet('theme2', JSON.stringify(theme2Override));
181
}
182
183