-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (173 loc) · 6.98 KB
/
Copy pathscript.js
File metadata and controls
221 lines (173 loc) · 6.98 KB
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
//console.dir(document); //shows all the properties, methods inside the <!DOCTYPE html>
/*
* var = global variable declaration
* let = accessible only to its function scope
* const = fixed variable value
* localStorage - saves data indefinitely until explicitly deleted
*/
//GLOBAL VARIABLES DECLARATION
const html_body = document.querySelector('body');
const main_header = document.querySelector('#main-header');
const navbar = document.querySelector('nav');
const main_section = document.querySelector('section');
const main_footer = document.querySelector('#main-footer');
const footerEnd = main_footer.querySelector('.footer-end');
/*****/
//FUNCTIONS
//retain theme on load
document.addEventListener("DOMContentLoaded",function(){
const setMode = localStorage.getItem("mode"); //for UI theme
const i_switchMode = localStorage.getItem("iconSwitch"); //for toggle icon state
//update changes accordingly
toggleMode(setMode);
//retain icon state
let toggleIcon = navbar.querySelector("#toggle-icon i");
if(i_switchMode === "on"){
//switch icon to 'on'
toggleIcon.classList.add("fa-toggle-on");
toggleIcon.classList.remove("fa-toggle-off");
}else{
//switch icon to 'off'
toggleIcon.classList.add("fa-toggle-off");
toggleIcon.classList.remove("fa-toggle-on");
}
});
//toggle theme icon
navbar.querySelector("#toggle-icon").addEventListener("click",function(event){
event.preventDefault(); //prevent page reload
//check which element was clicked
let i_icon = event.target;
//if <a> or <li> was clicked, find the <i> inside it
if (i_icon.tagName.toLowerCase() !== 'i') {
i_icon = event.target.querySelector('i');
}
//if <i> was clicked or found, change its class name
if (i_icon.tagName.toLowerCase() == 'i'){
i_icon.classList.toggle("fa-toggle-off");
i_icon.classList.toggle("fa-toggle-on");
}
let mode = i_icon.classList.contains("fa-toggle-on") ? "dark" : "light";
let iconSwitch = i_icon.classList.contains("fa-toggle-on") ? "on" : "off";
//update changes accordingly
toggleMode(mode);
//save toggle mode for UI theme
localStorage.setItem("mode",mode);
//save toggle icon state
localStorage.setItem("iconSwitch",iconSwitch);
});
//mode is funciton's parameter
function toggleMode(mode){
//mode = toggle
const arrTags = [html_body,main_header,navbar,main_footer,footerEnd];
let arrColor = ["white","black"];
let brdrColor = "2px solid black";
if(mode==="dark"){
arrColor = ["black","white"];
brdrColor = "2px solid red";
}else{
arrColor = ["white","black"];
brdrColor = "2px solid black";
}
for(let i=0; i<arrTags.length; i++){
arrTags[i].style.backgroundColor = arrColor[0];
arrTags[i].style.color = arrColor[1];
if(arrTags[i]===navbar || arrTags[i]===footerEnd){
arrTags[i].style.borderBlock = brdrColor;
}
if(arrTags[i]===main_footer){
arrTags[i].style.borderTop = brdrColor;
}
if(mode!="dark"){
if(arrTags[i]===main_header || arrTags[i]===navbar || arrTags[i]===main_footer || arrTags[i]===footerEnd){
arrTags[i].style.backgroundColor = "#FAF9F6";
arrTags[i].style.color = arrTags[1];
if(arrTags[i]===main_footer){
arrTags[i].style.borderTop = "none";
}
}
}
}
//selects all a in the navbar
const navlist = navbar.querySelectorAll('a');
toggleMode_1(mode,navlist);
//tricky, added to ensure functional and uniformed theme
if(!(window.location.toString().includes("playground") || (window.location.toString().includes("cpp")))){
//selects all img and button in main_section
let descImg = main_section.querySelectorAll('img');
let btn = main_section.querySelectorAll('button');
let arrImgs = ["img/cpp.png","img/r.png","img/sql.png"];
let imgBorder = "none";
let imgRadius = "none";
let btnBgColor = "white";
if(mode==="dark"){
arrImgs = ["img/cpp_dark1.png","img/r_dark1.png","img/sql_dark1.png"];
imgBorder = "2px solid red";
imgRadius = "50%";
btnBgColor = "transparent";
}else{
arrImgs = ["img/cpp.png","img/r.png","img/sql.png"];
imgBorder = "none";
imgRadius = "none";
btnBgColor = "white";
}
for(let i=0; i<arrImgs.length; i++){
descImg[i].src = arrImgs[i];
descImg[i].style.border = imgBorder;
descImg[i].style.borderRadius = imgRadius;
}
for(let i=0; i<btn.length; i++){
btn[i].style.backgroundColor = btnBgColor;
}
}
//selects all a in the main_footer
const footerlist = main_footer.querySelectorAll('a');
toggleMode_1(mode,footerlist);
}
function toggleMode_1(mode,element){
txtColor = "black";
bgColor = "transparent";
if(mode==="dark"){
txtColor = "white";
bgColor = "transparent";
}else{
txtColor = "black";
bgColor = "transparent";
}
for(let i=0; i<element.length; i++){
element[i].style.color = txtColor;
element[i].style.backgroundColor = bgColor;
//hover effect
element[i].addEventListener('mouseenter', function(event) {
if (mode==="dark"){
txtColor = "red";
bgColor = "black";
}else{
txtColor = "white";
bgColor = "#36454F";
}
event.target.style.color = txtColor;
event.target.style.backgroundColor = bgColor;
});
element[i].addEventListener('mouseleave', function(event) {
if (mode==="dark"){
txtColor = "white";
bgColor = "black";
}else{
txtColor = "black";
bgColor = "#FAF9F6";
}
event.target.style.color = txtColor;
event.target.style.backgroundColor = bgColor;
});
}
}
const main_sect_button = main_section.querySelectorAll(".learn-more-button");
main_sect_button.forEach(sect_button=>{
sect_button.addEventListener("click",()=>{
if(sect_button.id === "btn-cpp"){
document.location="cpp.html";
}else{
alert("A work in progress.");
}
});
});