-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
360 lines (338 loc) · 12.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use strict';
//Our sole export. Public methods and attributes must be added and modified here.
let vif = {};
let themes = {true:'dark', false:'light'}
let activeNotifs = {};
let cookies = {};
let duplicateToggleNotifCount = 0;
let source = ""
let held = false
let notifEndAnimDelay = 500
let notifEndAnim = null
let toggle = null
let added = false
let LIGHT_BACKGROUND = "#ffffff"
let LIGHT_FOREGROUND = "#36393f"
let DARK_BACKGROUND = "#36393f"
let DARK_FOREGROUND = "#ffffff"
//Make our light and dark constants public.
vif.LIGHT_BACKGROUND = LIGHT_BACKGROUND
vif.LIGHT_FOREGROUND = LIGHT_FOREGROUND
vif.DARK_BACKGROUND = DARK_BACKGROUND
vif.DARK_FOREGROUND = DARK_FOREGROUND
function getCookies(){
document.cookie.split(";").forEach(function(cookie){let intermediate=cookie.split("=");cookies[intermediate[0]]=intermediate[1];});
}
function setValueIfNotExists(object, key, value){
if (! object[key]){
object[key] = value
}
}
function showToggleNotif(){
getCookies();
if (duplicateToggleNotifCount >= 3){
return
}
//create local variable with current value of themes['toggle'] as it can change when spamming toggle
let currentTheme = themes[toggle]
//add "this will persist across sessions" if cookies are enabled
if (cookies['accepted']){
var crossSessionPersistence = "<div class=\"toast-subsection text-muted\" style=\"font-size:7.8pt\">This will persist across sessions.</div>"
} else {
var crossSessionPersistence = ""
}
if (activeNotifs['toggle-notif']){
duplicateToggleNotifCount += 1
setTimeout(function(){showNotif('toggle-notif', false, 2300); document.getElementById('toggle-set').innerHTML = "Switched to <b>" + currentTheme + " theme</b>. " + crossSessionPersistence; duplicateToggleNotifCount -= 1;}, (3100*duplicateToggleNotifCount)+(100*duplicateToggleNotifCount))
} else {
document.getElementById('toggle-set').innerHTML = "Switched to <b>"+ themes[toggle] + " theme</b>. " + crossSessionPersistence
showNotif('toggle-notif', false, 2300);
}
}
function toggleTheme(){
toggle = ! getCurrentTheme();
initTheming(false);
showToggleNotif();
}
function getCurrentTheme(){
// true == dark theme, false == light theme
if (toggle != null){
return toggle
} else {
console.log("toggled not set!"); //toggled not set yet? check cookies
}
if (cookies['theme']){
source = "cookie"
console.log("cookie is set, setting theme to " + cookies['theme']);
return cookies['theme'] == "dark" //true == dark, false == light
}
console.log("cookie not set!") //cookie not set? check system theme
source = "system"
try {
return window.matchMedia("(prefers-color-scheme:dark)").matches;
} catch (error){
//browser doesn't support this? default to dark theme
return true;
}
}
function swapComponents(OLD_THEME, NEW_THEME, componentName, inverted){
let newColor = "";
let newBackgroundColor = "";
if (NEW_THEME == "light"){
newColor = vif.LIGHT_FOREGROUND;
newBackgroundColor = vif.LIGHT_BACKGROUND;
} else {
newColor = vif.DARK_FOREGROUND;
newBackgroundColor = vif.DARK_BACKGROUND;
}
if (inverted){
let oldThemeCopy = OLD_THEME;
OLD_THEME = NEW_THEME;
NEW_THEME = oldThemeCopy;
}
let components = document.querySelectorAll("." + componentName + "-" + OLD_THEME);
let oldName = componentName + "-" + OLD_THEME;
let newName = componentName + "-" + NEW_THEME;
for (const each of components){
each.className = each.className.replace(oldName, newName);
each.style.color = newColor;
each.style.backgroundColor = newBackgroundColor;
}
}
function applyStylingForTag(OLD_THEME, NEW_THEME, target, inverted){
if (inverted){
let oldThemeCopy = OLD_THEME;
OLD_THEME = NEW_THEME;
NEW_THEME = oldThemeCopy;
}
let newColor = "";
let newBackgroundColor = "";
if (NEW_THEME == "light"){
newColor = vif.LIGHT_FOREGROUND;
newBackgroundColor = vif.LIGHT_BACKGROUND;
} else {
newColor = vif.DARK_FOREGROUND;
newBackgroundColor = vif.DARK_BACKGROUND;
}
for (const elem of document.querySelectorAll(target)){
//Don't theme navigation elements for now.
if (elem.className.includes("nav")){
continue
};
elem.style.color = newColor;
elem.style.backgroundColor = newBackgroundColor;
}
}
function applyStyling(foreground, background){
let NEW_THEME = "";
let OLD_THEME = "";
//Set our master background and foreground colors.
document.body.style.color = foreground;
document.body.style.backgroundColor = background;
if (background == vif.LIGHT_BACKGROUND){
console.log("Applying light theme styling.")
NEW_THEME = "light"; //light theme
OLD_THEME = "dark";
} else {
console.log("Applying dark theme styling.")
NEW_THEME = "dark";
OLD_THEME = "light";
}
applyStylingForTag(OLD_THEME, NEW_THEME, 'input', false);
//Swap out our Bootstrap components for ones that match the new theme.
swapComponents(OLD_THEME, NEW_THEME, 'btn-outline', true);
swapComponents(OLD_THEME, NEW_THEME, 'link', true);
//Then apply theme-specific styling to other elements.
applyStylingForTag(OLD_THEME, NEW_THEME, 'li', false);
applyStylingForTag(OLD_THEME, NEW_THEME, 'ul', false);
applyStylingForTag(OLD_THEME, NEW_THEME, 'ol', false);
//Modals are special.
//applyStylingForTag uses querySelectorAll so all we need to do is target a class instead of a tag
applyStylingForTag(OLD_THEME, NEW_THEME, '.modal-content', false)
//TODO: page-specific styling things? maybe pages could register callbacks
//Toast close buttons are special.
for (const elem of document.querySelectorAll('.btn-close')){
if (NEW_THEME == "dark" && (! elem.className.includes("btn-close-white"))){
elem.className += " btn-close-white";
} else {
elem.className = elem.className.replace(" btn-close-white", "");
}
}
//Toasts are special too.
for (const elem of document.querySelectorAll(".toast")){
elem.style.color = foreground;
elem.style.backgroundColor = background;
}
}
function applyLightStyling(){
console.log("switching to light theme");
if (cookies['accepted']){
document.cookie = "theme=light";
}
applyStyling(vif.LIGHT_FOREGROUND, vif.LIGHT_BACKGROUND);
}
function applyDarkStyling(){
console.log("switching to dark theme");
if (cookies['accepted']){
document.cookie = "theme=dark";
}
applyStyling(vif.DARK_FOREGROUND, vif.DARK_BACKGROUND)
}
function addCloseButton(name){
try{
if (added){
return
}
} catch(error){
added = true
}
if (('ontouchstart' in window)||(navigator.maxTouchPoints>0)||(navigator.msMaxTouchPoints>0)){
return
}
console.log("adding close button")
document.getElementById(name+'-close').style.transform = "scale(1, 1)";
document.getElementById(name+'-close').style.marginLeft = "-1em";
document.getElementById(name+'-close').style.left = "calc(30em + 0.3vmin)";
document.getElementById(name+'-main').style.marginRight = "calc(0.9em + 0.1vmin)";
added = true
}
function removeCloseButton(name){
if (! added){
return
}
console.log("removing close button")
document.getElementById(name+'-close').style.transform = "scale(0.01, 0.01)";
document.getElementById(name+'-main').style.marginRight = "0";
added = false
}
function getNotifRef(name){
return bootstrap.Toast.getOrCreateInstance(document.getElementById(name))
}
function hideNotif(name){
if (held){
console.log("Attempted to hide notif when it was held!")
return
}
console.log("Hiding notif '" + name + "'")
document.getElementById(name).style.marginBottom = "1.5%";
setTimeout(activeNotifs[name].hide(), 700);
console.log("hid notif");
document.getElementById(name).style.marginBottom = "0%";
activeNotifs[name] = null;
}
function holdNotif(name){
if (held){
console.log("Notif already held!")
return //event handler may have gone off twice
}
if (('ontouchstart' in window)||(navigator.maxTouchPoints>0)||(navigator.msMaxTouchPoints>0)){
held = false
hideNotif(name);
}
console.log('Holding notif')
clearTimeout(notifEndAnim);
held = true
}
function releaseNotif(name){
if (! held){
return
}
console.log("Releasing notif, hiding after " + String(notifEndAnimDelay) + "ms")
held = false;
notifEndAnim = setTimeout(function(){hideNotif(name)}, notifEndAnimDelay);
}
function showNotif(name, closable, delay){
console.log("Showing notif '" + name + "'")
held = false
if (! delay){
notifEndAnimDelay = 5400;
} else {
notifEndAnimDelay = delay;
}
if (activeNotifs[name]){
console.error("Attempted to show notif '" + name + "' while it was active!!")
return
}
setValueIfNotExists(activeNotifs, name, getNotifRef(name))
let notifRef = activeNotifs[name];
let notifElem = document.getElementById(name);
if (! notifElem || ! notifRef){
throw new Error('Invalid name passed to showNotif!')
}
if (closable){
//notification closable?? add the respective listeners...
//mouse events
notifElem.addEventListener("mouseover", function(){addCloseButton(name)});
notifElem.addEventListener("mouseover", function(){holdNotif(name)});
notifElem.addEventListener("mouseout", function(){removeCloseButton(name)});
notifElem.addEventListener("mouseout", function(){releaseNotif(name)});
//focus events e.g tab key
notifElem.addEventListener("focusin", function(){addCloseButton(name)});
notifElem.addEventListener("focusin", function(){holdNotif(name)});
notifElem.addEventListener("focusout", function(){removeCloseButton(name)});
notifElem.addEventListener("focusout", function(){releaseNotif(name)});
//touch events
notifElem.addEventListener("touchstart", function(){addCloseButton(name)});
notifElem.addEventListener("touchstart", function(){holdNotif(name)});
notifElem.addEventListener("touchend", function(){removeCloseButton(name)});
notifElem.addEventListener("touchend", function(){releaseNotif(name)});
}
notifRef.show();
setTimeout(function(){notifElem.style.marginBottom = "1%";}, 500);
notifEndAnim = setTimeout(function(){hideNotif(name)}, notifEndAnimDelay);
}
function showThemeNotif(theme){
console.log("Showing theme notif");
held = false
let themeNotif = document.getElementById("theme-notif");
document.getElementById("theme-set").style.fontSize = "calc(.69rem + 0.3vmin)";
if (source == "system"){
document.getElementById("theme-set").innerHTML = "Automatically enabled <b> " + theme + " theme</b> based on your system theme." + document.getElementById("theme-set").innerHTML;
} else {
document.getElementById("theme-set").innerHTML = "Automatically enabled <b> " + theme + " theme</b> based on your previous session." + document.getElementById("theme-set").innerHTML;
}
showNotif('theme-notif', true);
}
function initTheming(show){
document.getElementById('theme-button').addEventListener("click", toggleTheme);
let loading = document.getElementById("loading");
if (loading){
loading.remove();
}
getCookies();
console.log("initializing theming")
let theme = getCurrentTheme();
let themeToggle = document.getElementById('theme-toggle');
let themeButton = document.getElementById('theme-button');
if (theme == true){
if (toggle != true) {
toggle = true;
}
applyDarkStyling();
document.body.style.opacity = "1";
if (show){
getCookies();
showThemeNotif("dark");
}
themeToggle.innerHTML = "<i class=\"fas fa-sun fa-xs\"></i>";
themeToggle.title = "Switch to light theme.";
themeToggle.style.marginBottom = "0";
} else {
applyLightStyling();
document.body.style.opacity = "1";
if (show){
getCookies();
showThemeNotif("light");
}
themeToggle.innerHTML = "<i class=\"fas fa-moon fa-xs\"></i>";
themeToggle.title = "Switch to dark theme.";
themeToggle.style.marginBottom = "0";
}
//apply theme transitions
//TODO remove i hate this
if (typeof toggle !== 'undefined'){
document.body.style.transition = "background-color 0.6s ease-in-out, color 0.6s ease-in-out, transform 0.6s ease-in-out !important";
}
}
vif.initTheming = initTheming;
export default vif;