forked from charandeepsinghb/learn-code-contribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (84 loc) · 2.69 KB
/
script.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
/* ******************************* Theme code ******************************** */
const DARK_THEME_LOCAL_ITEM = "darkTheme";
const isLocalDarkMode = localStorage.getItem(DARK_THEME_LOCAL_ITEM);
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
const lightIconElem = document.getElementById("lightIcon");
const darkIconElem = document.getElementById("darkIcon");
if (darkThemeMq.matches || isLocalDarkMode === "true") {
setTheme("dark");
} else {
setTheme("light");
}
function setTheme(theme) {
document.documentElement.setAttribute("data-bs-theme", theme);
if (theme === "dark") {
lightIconElem.style.display = "inline";
darkIconElem.style.display = "none";
localStorage.setItem(DARK_THEME_LOCAL_ITEM, "true");
} else {
lightIconElem.style.display = "none";
darkIconElem.style.display = "inline";
localStorage.setItem(DARK_THEME_LOCAL_ITEM, "false");
}
}
darkThemeMq.addEventListener("change", e => {
if (e.matches) {
setTheme("dark");
} else {
setTheme("light");
}
});
/* ******************************* Toast notification ******************************** */
const errorToast = Toastify({
text: "Error occurred",
duration: 2000,
style: {
background: "#ff3333",
},
});
/* ******************************* Get component ******************************** */
const home = document.getElementById("home");
function getComponent(url) {
if (isHrefUrlSame(url)) {
return;
}
overlayLoaderOn();
$.ajax({
type: "GET",
url: url,
success: function (response) {
home.innerHTML = response;
overlayLoaderOff();
},
error: function (response) {
errorToast.showToast();
}
});
}
function isHrefUrlSame(url) {
const href = window.location.href;
const urlAfterHref = window.location.href.substring(href.indexOf('#') + 2);
if (url === "./" + urlAfterHref + ".html") {
return true;
}
return false;
}
/* ******************************* Router ******************************** */
function redirectByHref() {
const href = window.location.href;
if (!href.includes("#/")) {
getComponent("./components/main.html");
return;
}
const url = window.location.href.substring(href.indexOf('#') + 2) + ".html";
getComponent(url);
}
// When the page is loaded
redirectByHref();
/* ******************************* Overlay ******************************** */
function overlayLoaderOn() {
document.getElementById("overlay").style.display = "block";
}
function overlayLoaderOff() {
document.getElementById("overlay").style.display = "none";
}