-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
261 lines (220 loc) · 6.69 KB
/
script.js
File metadata and controls
261 lines (220 loc) · 6.69 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
let isSmallerThan802 = false;
let isSmallerThan802Old = false;
/**
* the init-function in body onload
*/
async function init() {
includeHTML();
mobileGreeting();
}
/**
* Includes HTML-Files into container containing "w3-include-html"-attribute
*/
async function includeHTML() {
let includeElements = document.querySelectorAll("[w3-include-html]");
for (let i = 0; i < includeElements.length; i++) {
const element = includeElements[i];
file = element.getAttribute("w3-include-html");
let resp = await fetch(file);
if (resp.ok) {
element.innerHTML = await resp.text();
} else {
element.innerHTML = "Page not found";
}
}
showInitials();
setIsSmallerThan802()
addResizeEventListener();
runFunctionsOnBreakpoint();
}
/**
* Adds an event listener to the window's resize event. When the window is resized,
* it checks if the window's inner width is less than or equal to 802. If it is,
* it sets the isSmallerThan802 variable to true. Otherwise, it sets it to false.
* If the value of isSmallerThan802 has changed since the last resize event,
* it updates isSmallerThan802Old with the new value and calls the runFunctionsOnBreakpoint function.
*
* @return {void} This function does not return anything.
*/
function addResizeEventListener(){
window.addEventListener('resize', () => {
setIsSmallerThan802();
if (isSmallerThan802 !== isSmallerThan802Old){
isSmallerThan802Old = isSmallerThan802;
runFunctionsOnBreakpoint();
}
});
}
/**
* Runs specific functions based on the current breakpoint.
*/
function runFunctionsOnBreakpoint() {
if(isSmallerThan802){
renderMobileNavigation()
setActiveNavButton();
}
else{
renderStandardNavigation();
}
}
/**
* Renders the navigation for mobile devices by clearing the contents of the
* navigation containers and populating them with the navigation HTML.
*
* @return {void} This function does not return a value.
*/
function renderMobileNavigation(){
let container = document.getElementById('navigation-container');
let mobileContainer = document.getElementById('navigation-container-mobile');
container.innerHTML = "";
mobileContainer.innerHTML = renderNavigationHTML();
}
/**
* Renders the navigation for standard devices by populating the navigation container and mobile container with appropriate content.
*
* @return {void} This function does not return a value.
*/
function renderStandardNavigation(){
let container = document.getElementById('navigation-container');
let mobileContainer = document.getElementById('navigation-container-mobile');
container.innerHTML = renderNavigationHTML();
mobileContainer.innerHTML = "";
}
/**
* Sets the value of the isSmallerThan802 variable based on the window's inner width.
*
* @return {void} This function does not return anything.
*/
function setIsSmallerThan802(){
if (window.innerWidth <= 801) {
isSmallerThan802 = true;
} else {
isSmallerThan802 = false;
}
}
/**
*
* @param {string} id id eines divs
* @returns div with given id
*/
function getDiv(id) {
let content = document.getElementById(id);
return content;
}
/**
* rendering the board-page,
* calling renderCategories to render all available tasks to each category
*/
function renderBoard() {
let content = getDiv("main");
content.innerHTML = renderBoardHTML();
renderCategories();
}
/**
* Opens the help page by switching to the 'help.html' page.
*/
function openHelp() {
switchPage('help.html');
}
function checkIfUserIsRemembered() {
if (!sessionStorage.getItem('currentUser')) {
if (localStorage.getItem('rememberedUser') != null) {
sessionStorage.setItem('currentUser', localStorage.getItem('rememberedUser'));
if (window.location.href == 'index.html') switchPage('summary.html');
}
}
}
/**
* Logs out the current user by removing the 'currentUser' item from the local storage and redirecting to the login page.
*/
function logout() {
localStorage.removeItem('rememberedUser');
sessionStorage.removeItem('currentUser');
switchPage('index.html');
}
/**
* Displays the initials of the current user in the 'userInitials' element. If no user is logged in, displays 'G' for guest instead.
*/
function showInitials() {
checkIfUserIsRemembered();
try {
let userAsString = sessionStorage.getItem('currentUser');
let userInitialsElement = document.getElementById('userInitials');
if (userInitialsElement) {
if (userAsString) {
let userName = JSON.parse(userAsString).username;
let userInitials = getInitials(userName);
userInitialsElement.innerHTML = userInitials;
} else {
userInitialsElement.innerHTML = 'G';
}
}
} catch (error) {
console.error('Error while retrieving user data from localStorage:', error);
// Handle the error accordingly, such as setting a default value or displaying an error message.
let userInitialsElement = document.getElementById('userInitials');
if (userInitialsElement) {
userInitialsElement.innerHTML = 'G';
}
}
}
/**
* Function to handle opening the small menu based on screen width.
*/
function openSmallMenu() {
let screenWidth = window.innerWidth;
let smallMenu = getDiv("smallMenu");
let smallMenuMobile = getDiv("smallMenuMobile");
if (screenWidth <= 801) {
smallMenu.classList.remove("d-none");
smallMenuMobile.classList.toggle("d-none");
} else {
smallMenuMobile.classList.add("d-none");
smallMenu.classList.toggle("d-none");
}
}
/**
* Updates the window location pathname to the new URL.
*
* @param {string} newUrl - The new URL to navigate to
*/
function switchPage(newUrl) {
window.location.href = newUrl;
}
/**
* Opens a new tab with the specified URL.
*
* @param {string} newUrl - The URL to open in the new tab
*/
function switchPageNewTab(newUrl) {
window.open(newUrl, '_blank');
}
/**
* Sets the active navigation button based on the current location pathname.
*/
function setActiveNavButton() {
const navLinks = ["summary", "addTask", "board", "contacts"];
const activeNavLink = document.querySelector(".nav-btn.active");
// Remove active class from any previously active nav link
if (activeNavLink) {
activeNavLink.classList.remove("active");
}
navLinks.forEach((link) => {
if (location.pathname.includes(link)) {
document.getElementById(link).classList.add("active");
if (isSmallerThan802) document.getElementById(link).querySelector("img").src = `./assets/img/icon-${link}-marked.png`;
else document.getElementById(link).querySelector("img").src = `./assets/img/icon-${link}.png`;
}
});
}
/**
* Closes the current window if the previous URL includes 'index', otherwise navigates back to the previous page.
*/
function goBack() {
const previousURL = document.referrer;
if (previousURL.includes('index') || previousURL.includes('signUp')) {
window.close()
}else{
window.history.go(-1);
}
}