-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
270 lines (225 loc) · 8.46 KB
/
script.js
File metadata and controls
270 lines (225 loc) · 8.46 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
262
263
264
265
266
267
268
269
270
const DB_URL = "https://join-379-kanban-board-default-rtdb.europe-west1.firebasedatabase.app";
/**
* loads HTML content into elements with the 'include-html' attribute.
* replaces the element's inner HTML with the fetched file content,
* or shows 'page not found' if the fetch fails.
*
* @async
* @function
* @returns {Promise<void>} - a promise that resolves when all HTML includes are loaded.
*/
async function includeHTML() {
let includeElements = document.querySelectorAll('[include-html]');
let promises = Array.from(includeElements).map(async (element) => {
const file = element.getAttribute("include-html");
let resp = await fetch(file);
if (resp.ok) {
element.innerHTML = await resp.text();
initializeNavigation();
} else {
element.innerHTML = 'page not found';
}
});
await Promise.all(promises);
await setUserDataFromLocalStorage();
}
/**
* asynchronously retrieves user data from localStorage and updates the corresponding html elements.
* this function updates both desktop and mobile greeting elements based on user data.
*
* @async
* @function
* @returns {Promise<void>} - a promise that resolves when the user data has been set.
*/
async function setUserDataFromLocalStorage() {
const userData = getUserDataFromLocalStorage();
updateGreetingElements(userData);
setMenuVisibility(!!userData.userName);
}
/**
* retrieves user data from localStorage.
*
* @returns {Object} - an object containing userName, greetingTime, and userInitials.
*/
function getUserDataFromLocalStorage() {
return {
userName: localStorage.getItem('userName'),
greetingTime: localStorage.getItem('greetingTime'),
userInitials: localStorage.getItem('userInitials')
};
}
/**
* updates the greeting elements for both desktop and mobile.
*
* @param {Object} userData - an object containing userName, greetingTime, and userInitials.
*/
function updateGreetingElements(userData) {
const greetingTimeElement = document.getElementById('greeting-time');
const greetingUserNameElement = document.getElementById('greeting-username');
const userInitialsElement = document.getElementById('current-user-initials');
const greetingTimeMobileElement = document.getElementById('greeting-time-mobile');
const greetingUserNameMobileElement = document.getElementById('greeting-username-mobile');
setTextContent(greetingTimeElement, userData.greetingTime);
setTextContent(greetingUserNameElement, userData.userName);
setTextContent(userInitialsElement, userData.userInitials);
setTextContent(greetingTimeMobileElement, userData.greetingTime);
setTextContent(greetingUserNameMobileElement, userData.userName);
}
/**
* sets the text content of an element if the element exists.
* @param {HTMLElement} element - the target element.
* @param {string} text - the text to be set.
*/
function setTextContent(element, text) {
if (element) {
element.textContent = text;
}
}
/**
* adjusts the visibility of the aside element based on screen size and login status.
* @param {boolean} isLoggedIn - true if the user is logged in, false otherwise.
*/
function adjustMobileMenuVisibility(isLoggedIn) {
const asideElement = document.querySelector('aside');
if (!asideElement) return;
const isSmallScreen = window.innerWidth < 960;
if (!isLoggedIn && isSmallScreen) {
asideElement.classList.add('hidden');
} else {
asideElement.classList.remove('hidden');
}
}
/**
* sets visibility of menu elements based on user login status.
* @param {boolean} isLoggedIn - true if the user is logged in, false otherwise.
*/
function setMenuVisibility(isLoggedIn) {
const mainMenuElement = document.getElementById('main-menu');
const headerMenuElement = document.getElementById('header-menu');
toggleClass(mainMenuElement, 'show', 'hidden', isLoggedIn);
toggleClass(headerMenuElement, 'show', 'hidden', isLoggedIn);
adjustMobileMenuVisibility(isLoggedIn);
}
/**
* toggles classes on an element based on a condition.
* @param {HTMLElement} element - the target element.
* @param {string} addClass - the class to add if the condition is true.
* @param {string} removeClass - the class to remove if the condition is true.
* @param {boolean} condition - if true, adds `addClass` and removes `removeClass`; otherwise, does the opposite.
*/
function toggleClass(element, addClass, removeClass, condition) {
if (element) {
if (condition) {
element.classList.add(addClass);
element.classList.remove(removeClass);
} else {
element.classList.add(removeClass);
element.classList.remove(addClass);
}
}
}
/**
* event listener that adjusts the visibility of the aside element
* based on the user login status when the window is resized.
*/
window.addEventListener('resize', () => {
const isLoggedIn = !!localStorage.getItem('userName');
adjustMobileMenuVisibility(isLoggedIn);
});
/**
* toggles the visibility of the mobile menu by adding or removing the 'hidden' and 'show' classes.
* if clicked outside of the mobile menu, it ensures that 'hidden' is added and 'show' is removed.
*
* @function
* @returns {void}
*/
function toggleMobileMenu() {
const mobileMenu = document.getElementById('mobile-menu');
mobileMenu.classList.toggle('hidden');
mobileMenu.classList.toggle('show');
document.addEventListener('click', handleClickOutsideMobileMenu);
}
/**
* handles clicks outside the mobile menu to close it by ensuring the 'hidden' class is added and the 'show' class is removed.
*
* @function
* @param {Event} event - the click event.
* @returns {void}
*/
function handleClickOutsideMobileMenu(event) {
const mobileMenu = document.getElementById('mobile-menu');
const profileButton = document.querySelector('.profile-button');
if (!mobileMenu.contains(event.target) && !profileButton.contains(event.target)) {
mobileMenu.classList.add('hidden');
mobileMenu.classList.remove('show');
document.removeEventListener('click', handleClickOutsideMobileMenu);
}
}
/**
* logs the user out by clearing localStorage and redirecting to the log in site.
*/
async function logOut() {
localStorage.removeItem('userName');
localStorage.removeItem('userInitials');
localStorage.removeItem('greetingTime');
localStorage.removeItem('userEmail');
localStorage.removeItem('hasShownMobileGreeting');
localStorage.removeItem('checkboxStates');
window.location.replace('../index.html');
}
/**
* navigates the user back to the previous page in the browser history.
*/
function goToPreviousPage() {
if (document.referrer) {
window.history.back();
} else {
window.location.replace('../index.html');
}
}
/**
* adds the 'show' class to the main content element if it exists in the DOM.
*
* @function
*/
document.addEventListener('DOMContentLoaded', function () {
const mainContent = document.querySelector('.main-content');
if (!mainContent) return;
mainContent.classList.add('show');
});
/**
* Escapes HTML special characters in a string to their corresponding HTML entities.
*
* This function replaces the characters `&`, `<`, `>`, `"`, and `'`
* with their respective HTML entity codes to ensure the string can
* be safely embedded in an HTML context without introducing security
* vulnerabilities like XSS (Cross-Site Scripting).
*
* @param {string} unsafe - The input string containing potentially unsafe characters.
* @returns {string} A string with HTML special characters replaced by their entity codes.
*
* @example
* const unsafeString = '<script>alert("XSS");</script>';
* const safeString = escapeHtml(unsafeString);
* console.log(safeString);
*/
function escapeHtml(unsafe) {
return unsafe.replace(/[&<>"']/g, function (m) {
return `&#${m.charCodeAt(0)};`;
});
}
function extractPriority(priorityText) {
const validPriorities = ["low", "medium", "high"];
const match = validPriorities.find(priority => priorityText.toLowerCase().startsWith(priority));
return match || "default";
}
function extractPriorityDisplay(priorityText) {
const validPriorities = ["low", "medium", "high"];
const lowerText = priorityText.toLowerCase();
for (const priority of validPriorities) {
if (lowerText.startsWith(priority)) {
return priority.charAt(0).toUpperCase() + priority.slice(1);
}
}
return "Default";
}