-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (83 loc) · 3.41 KB
/
index.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
/* Darkmode.js Settings */
const options = {
bottom: "32px", // default: "32px"
right: "32px", // default: "32px"
left: "unset", // default: "unset"
time: "0.3s", // default: "0.3s"
mixColor: "#fff", // default: "#fff"
backgroundColor: "#fff", // default: "#fff"
buttonColorDark: "#100f2c", // default: "#100f2c"
buttonColorLight: "#fff", // default: "#fff"
saveInCookies: true, // default: true,
label: "<i class='fas fa-lg fa-moon text-white'></i>", // default: ""
autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
const darkmodeButton = document.body.getElementsByTagName("button")[0];
const dateSettingsButton = document.getElementById("date-settings");
/* Date settings (UTC) */
let utcYear = 2019,
utcMonth = 1,
utcDay = 1,
utcHour = 0,
utcMinute = 0,
utcSeconds = 0;
let countUpDate = new Date(utcYear, utcMonth - 1, utcDay, utcHour, utcMinute, utcSeconds);
/**
* Converts a single digit number to a two digit string
* @param {number} numberToConvert
*/
const numberToTwoDigitsString = (numberToConvert = null) => {
if (numberToConvert = null) {
return;
}
if (numberToConvert <= 9 && numberToConvert >= 0) return "0" + numberToConvert;
return "" + numberToConvert;
};
/**
* Updates the date information on the page
*/
const updateDateInformation = () => {
let dateNow = new Date();
let timeNow = dateNow.getTime();
let diffBetweenDates = timeNow - countUpDate.getTime();
let dateDefinitions = [];
dateDefinitions["years"] = Math.floor(diffBetweenDates / (1000 * 60 * 60 * 24 * 365));
dateDefinitions["days"] = Math.floor(diffBetweenDates % (1000 * 60 * 60 * 24 * 365) / (1000 * 60 * 60 * 24));
dateDefinitions["hours"] = Math.floor(diffBetweenDates % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
dateDefinitions["minutes"] = Math.floor(diffBetweenDates % (1000 * 60 * 60) / (1000 * 60));
dateDefinitions["seconds"] = Math.floor(diffBetweenDates % (1000 * 60) / (1000));
for (key in dateDefinitions) {
const element = document.getElementById(key);
element.querySelector("h1").innerText = dateDefinitions[key];
element.querySelector("p").innerText = (dateDefinitions[key] == 1 ? key.replace(/.$/, "") : key);
}
};
/**
* Starts updateDateInformation periodically
*/
const count = setInterval(() => {
updateDateInformation();
}, 1000);
updateDateInformation();
/** Changes the icon of the dark mode button */
const darkModeButtonChangeIcon = (element, classesToToggle) => {
if (element == null || classesToToggle == null) {
return;
}
classesToToggle.forEach(classToToggle => {
element.classList.toggle(classToToggle);
});
};
const darkmodeButtonClassList = ["text-warning", "text-white", "fa-moon", "fa-sun"];
/** Change icon if dark mode is already active */
if (document.body.classList.contains("darkmode--activated")) {
darkModeButtonChangeIcon(darkmodeButton.children[0], darkmodeButtonClassList);
darkModeButtonChangeIcon(dateSettingsButton, ["text-white"]);
}
/* Event listener for dark mode button */
darkmodeButton.addEventListener("click", () => {
darkModeButtonChangeIcon(darkmodeButton.children[0], darkmodeButtonClassList);
darkModeButtonChangeIcon(dateSettingsButton, ["text-white"]);
});