Prevent "darkMode"-class duplication
This commit is contained in:
70
js/main.js
70
js/main.js
@@ -3,32 +3,41 @@ var state = false;
|
|||||||
var debug = "false";
|
var debug = "false";
|
||||||
if (getCookie("darkmode") == null) {
|
if (getCookie("darkmode") == null) {
|
||||||
applyDarkmode = false;
|
applyDarkmode = false;
|
||||||
if(debug == "true") console.log('Cookie "darkmode" empty');
|
if (debug == "true") console.log('Cookie "darkmode" empty');
|
||||||
} else {
|
} else {
|
||||||
applyDarkmode = getCookie("darkmode");
|
applyDarkmode = getCookie("darkmode");
|
||||||
if(debug == "true") console.log("Cookie darkmode:" + applyDarkmode);
|
if (debug == "true") console.log("Cookie darkmode:" + applyDarkmode);
|
||||||
}
|
}
|
||||||
var isDark = false;
|
var isDark = false;
|
||||||
|
|
||||||
function setdarkMode(state) {
|
function setdarkMode(state) {
|
||||||
if(debug == "true") console.log(
|
if (debug == "true")
|
||||||
'function "setdarkMode" was initiated with value "state" as ' + state
|
console.log(
|
||||||
);
|
'function "setdarkMode" was initiated with value "state" as ' + state
|
||||||
|
);
|
||||||
var elements = document.getElementsByClassName("couldDark");
|
var elements = document.getElementsByClassName("couldDark");
|
||||||
len = elements !== null ? elements.length : 0;
|
len = elements !== null ? elements.length : 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
if (state == true) {
|
if (state == true) {
|
||||||
for (i; i < len; i++) {
|
for (i; i < len; i++) {
|
||||||
elements[i].classList.add("darkMode");
|
if (elements[i].classList.contains("darkMode") == false) {
|
||||||
|
elements[i].classList.add("darkMode");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/darkmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
document.getElementById("pfostendynamics").src = "https://pfostendynamics.com/img/PfostenDynamicsDark.png";
|
"https://pfostendynamics.com/img/darkmode.png";
|
||||||
|
document.getElementById("pfostendynamics").src =
|
||||||
|
"https://pfostendynamics.com/img/PfostenDynamicsDark.png";
|
||||||
} else if (state == false) {
|
} else if (state == false) {
|
||||||
for (i; i < len; i++) {
|
for (i; i < len; i++) {
|
||||||
elements[i].classList.remove("darkMode");
|
if (elements[i].classList.contains("darkMode") == true) {
|
||||||
|
elements[i].classList.remove("darkMode");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/lightmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
document.getElementById("pfostendynamics").src = "https://pfostendynamics.com/img/PfostenDynamics.png";
|
"https://pfostendynamics.com/img/lightmode.png";
|
||||||
|
document.getElementById("pfostendynamics").src =
|
||||||
|
"https://pfostendynamics.com/img/PfostenDynamics.png";
|
||||||
}
|
}
|
||||||
state = state.toString();
|
state = state.toString();
|
||||||
setCookie("darkmode", state, 999);
|
setCookie("darkmode", state, 999);
|
||||||
@@ -36,9 +45,10 @@ function setdarkMode(state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setNavbar(state) {
|
function setNavbar(state) {
|
||||||
if(debug == "true") console.log(
|
if (debug == "true")
|
||||||
'function "setNavbar" was initiated with value "state" as ' + state
|
console.log(
|
||||||
);
|
'function "setNavbar" was initiated with value "state" as ' + state
|
||||||
|
);
|
||||||
if (state) {
|
if (state) {
|
||||||
document.getElementById("navbar").classList.remove("navbar-light");
|
document.getElementById("navbar").classList.remove("navbar-light");
|
||||||
document.getElementById("navbar").classList.remove("bg-light");
|
document.getElementById("navbar").classList.remove("bg-light");
|
||||||
@@ -55,7 +65,7 @@ function setNavbar(state) {
|
|||||||
|
|
||||||
function darkMode() {
|
function darkMode() {
|
||||||
applyDarkmode = !applyDarkmode;
|
applyDarkmode = !applyDarkmode;
|
||||||
if(debug == "true") console.log("applyDarkmode:" + applyDarkmode);
|
if (debug == "true") console.log("applyDarkmode:" + applyDarkmode);
|
||||||
setdarkMode(applyDarkmode);
|
setdarkMode(applyDarkmode);
|
||||||
setNavbar(applyDarkmode);
|
setNavbar(applyDarkmode);
|
||||||
return;
|
return;
|
||||||
@@ -65,11 +75,15 @@ function checkDarkmode() {
|
|||||||
var tmp = getCookie("darkmode");
|
var tmp = getCookie("darkmode");
|
||||||
if (tmp == "false") {
|
if (tmp == "false") {
|
||||||
isDark = false;
|
isDark = false;
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/lightmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
document.getElementById("pfostendynamics").src = "https://pfostendynamics.com/img/PfostenDynamics.png";
|
"https://pfostendynamics.com/img/lightmode.png";
|
||||||
|
document.getElementById("pfostendynamics").src =
|
||||||
|
"https://pfostendynamics.com/img/PfostenDynamics.png";
|
||||||
} else if (tmp == "true") {
|
} else if (tmp == "true") {
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/darkmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
document.getElementById("pfostendynamics").src = "https://pfostendynamics.com/img/PfostenDynamicsDark.png";
|
"https://pfostendynamics.com/img/darkmode.png";
|
||||||
|
document.getElementById("pfostendynamics").src =
|
||||||
|
"https://pfostendynamics.com/img/PfostenDynamicsDark.png";
|
||||||
isDark = true;
|
isDark = true;
|
||||||
}
|
}
|
||||||
applyDarkmode = isDark;
|
applyDarkmode = isDark;
|
||||||
@@ -80,14 +94,20 @@ function checkDarkmode() {
|
|||||||
i = 0;
|
i = 0;
|
||||||
if (state == true) {
|
if (state == true) {
|
||||||
for (i; i < len; i++) {
|
for (i; i < len; i++) {
|
||||||
elements[i].className += " darkMode";
|
if (elements[i].classList.contains("darkMode") == false) {
|
||||||
|
elements[i].className += " darkMode";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/darkmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
|
"https://pfostendynamics.com/img/darkmode.png";
|
||||||
} else if (state == false) {
|
} else if (state == false) {
|
||||||
for (i; i < len; i++) {
|
for (i; i < len; i++) {
|
||||||
elements[i].classList.remove("darkMode");
|
if (elements[i].classList.contains("darkMode") == true) {
|
||||||
|
elements[i].classList.remove("darkMode");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
document.getElementById("darkModeImg").src = "https://pfostendynamics.com/img/lightmode.png";
|
document.getElementById("darkModeImg").src =
|
||||||
|
"https://pfostendynamics.com/img/lightmode.png";
|
||||||
}
|
}
|
||||||
setNavbar(isDark);
|
setNavbar(isDark);
|
||||||
return isDark;
|
return isDark;
|
||||||
@@ -95,11 +115,11 @@ function checkDarkmode() {
|
|||||||
|
|
||||||
function load() {
|
function load() {
|
||||||
debug = getCookie("debug");
|
debug = getCookie("debug");
|
||||||
if(getCookie("darkmode") == "") {
|
if (getCookie("darkmode") == "") {
|
||||||
setCookie("darkmode", false, 999);
|
setCookie("darkmode", false, 999);
|
||||||
}
|
}
|
||||||
checkDarkmode();
|
checkDarkmode();
|
||||||
if(location.hostname == "localhost") givePHP();
|
if (location.hostname == "localhost") givePHP();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Cookie scripts*/
|
/*Cookie scripts*/
|
||||||
|
Reference in New Issue
Block a user