-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.js
142 lines (115 loc) · 3.58 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
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
/* ******************************* Theme code ******************************** */
const DARK_THEME_LOCAL_ITEM = "darkTheme";
let isLocalDarkMode = localStorage.getItem(DARK_THEME_LOCAL_ITEM) === "true";
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
const lightIconElem = document.getElementById("lightIcon");
const darkIconElem = document.getElementById("darkIcon");
if (darkThemeMq.matches || isLocalDarkMode) {
setTheme("dark");
} else {
setTheme("light");
}
function setTheme(theme) {
document.documentElement.setAttribute("data-bs-theme", theme);
if (theme === "dark") {
// Theme switch icon
lightIconElem.style.display = "inline";
darkIconElem.style.display = "none";
localStorage.setItem(DARK_THEME_LOCAL_ITEM, "true");
isLocalDarkMode = true;
} else {
lightIconElem.style.display = "none";
darkIconElem.style.display = "inline";
localStorage.setItem(DARK_THEME_LOCAL_ITEM, "false");
isLocalDarkMode = false;
}
switchImagesByTheme();
}
// Theme change event listener
darkThemeMq.addEventListener("change", e => {
if (e.matches) {
setTheme("dark");
} else {
setTheme("light");
}
});
// Swich images by theme
function switchImagesByTheme() {
// To load images after page redraw
setTimeout(() => {
// Internal Id can be same between components
let githubLogo = document.getElementById("githubLogo");
if (!githubLogo) {
return;
}
if (darkThemeMq.matches || isLocalDarkMode) {
githubLogo.setAttribute("src", "assets/github-mark-theme_dark.svg");
} else {
githubLogo.setAttribute("src", "assets/github-mark.svg");
}
}, 0);
}
/* ******************************* 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;
switchImagesByTheme();
overlayLoaderOff();
},
error: function (response) {
errorToast.showToast();
}
});
}
function isHrefUrlSame(url) {
const urlAfterHref = window.location.hash.substring(2);
if (url === "./" + urlAfterHref + ".html") {
return true;
}
return false;
}
/* ******************************* Router ******************************** */
function redirectByHref() {
const hash = window.location.hash;
// Load main component if not any
if (!hash) {
getComponent("./components/main.html");
return;
}
if (!hash.includes("#/components")) {
getComponent("./components/main.html");
return;
}
const url = hash.substring(2) + ".html";
getComponent(url);
}
// When the page is loaded
redirectByHref();
// Router listner back forward
addEventListener('popstate', (state)=>{
// console.log(state.currentTarget.location);
redirectByHref();
});
/* ******************************* Overlay ******************************** */
function overlayLoaderOn() {
document.getElementById("overlay").style.display = "block";
}
function overlayLoaderOff() {
document.getElementById("overlay").style.display = "none";
}