This repository was archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
465 lines (371 loc) · 19.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Define the number of students to display per page
let studentsPerPage = 12;
// Store the current page
let currentPage = 1;
// Function to create a student card element
function createStudentCard(student) {
const card = document.createElement("div");
card.classList.add("student-card");
// Create a new container div for the image with a 16:9 aspect ratio
const imageContainer = document.createElement("div");
imageContainer.classList.add("student-image-container");
// Set the aspect ratio using padding-bottom
imageContainer.style.paddingBottom = "64.1%"; /* 750/1170 = 64.1% ------- 1170*750 size image */
const image = document.createElement("img");
image.classList.add("student-image");
// for GitHub image link (must add the repository name to link the images folder)
image.src = `https://nishatrhythm.github.io/CSE11/images/${student.id}.jpg`;
// for localhost image link
// image.src = `/images/${student.id}.jpg`;
image.alt = student.name;
// Append the image to the image container
imageContainer.appendChild(image);
const name = document.createElement("h2");
name.classList.add("student-name");
name.textContent = student.name;
const id = document.createElement("p");
id.classList.add("student-id");
id.textContent = `B1903050${student.id}`;
const infoTable = document.createElement("table");
infoTable.classList.add("student-info");
const infoRows = [
{ label: "School:", value: student.school },
{ label: "College:", value: student.college },
{ label: "Hometown:", value: student.hometown },
];
infoRows.forEach((infoRow) => {
const row = document.createElement("tr");
const labelCell = document.createElement("td");
labelCell.classList.add("label");
labelCell.textContent = infoRow.label;
const valueCell = document.createElement("td");
valueCell.textContent = infoRow.value;
row.appendChild(labelCell);
row.appendChild(valueCell);
infoTable.appendChild(row);
});
const socialLinks = document.createElement("div");
socialLinks.classList.add("social-links", "custom-social-links");
const socialIcons = ["facebook", "x", "linkedin", "github"];
socialIcons.forEach((icon) => {
let socialLink = student.socialLinks[icon];
if (icon === "linkedin") {
socialLink = `https://www.linkedin.com/in/${socialLink}`;
} else if (icon === "x") {
socialLink = `https://www.x.com/${socialLink}`;
} else {
socialLink = `https://www.${icon}.com/${socialLink}`;
}
const link = document.createElement("a");
link.href = socialLink;
link.innerHTML = `<i class="fa-brands ${icon === "x" ? "fa-x-twitter" : `fa-${icon}`}"></i>`;
link.target = "_blank"; // Open link in a new tab
socialLinks.appendChild(link);
});
// Append the image container, name, id, infoTable, and socialLinks to the card
card.appendChild(imageContainer);
card.appendChild(name);
card.appendChild(id);
card.appendChild(infoTable);
card.appendChild(socialLinks);
return card;
}
// Function to display a page of students
function displayStudents(students, page, cardsPerPage) {
const studentProfiles = document.querySelector(".student-profiles");
studentProfiles.innerHTML = "";
const startIndex = (page - 1) * cardsPerPage; // Use the cardsPerPage parameter here
const endIndex = startIndex + cardsPerPage; // Use the cardsPerPage parameter here
const studentsToDisplay = students.slice(startIndex, endIndex);
studentsToDisplay.forEach((student) => {
const card = createStudentCard(student);
studentProfiles.appendChild(card);
});
}
// Function to display the custom modal with a message and an icon
function showModal(message, alertType) {
const modal = document.getElementById('customModal');
const modalIcon = document.getElementById('modalIcon');
const modalMessage = document.getElementById('modalMessage');
const modalOkayBtn = document.getElementById('modalOkayBtn');
const body = document.querySelector('body');
// Set the modal content based on the alert type
switch (alertType) {
case 'success':
modalIcon.className = 'fa fa-check-circle';
modalIcon.style.color = 'green';
break;
case 'error':
modalIcon.className = 'fa fa-triangle-exclamation';
modalIcon.style.color = 'orange';
// modalMessage.classList.add('error-message');
break;
default:
modalIcon.className = 'fa fa-info-circle';
modalIcon.style.color = 'blue';
}
modalMessage.textContent = message;
// Display the modal in the vertical center of the screen
modal.style.display = 'flex';
modal.classList.add('show'); // Add the 'show' class for the animation
// Add the class to the body element to prevent scrolling
body.classList.add('modal-open');
// Disable right-clicking while the modal is open
document.addEventListener('contextmenu', preventContextMenu);
// Handle the "Cancel" button click to close the modal and re-enable scrolling
modalOkayBtn.addEventListener('click', () => {
closeModal();
});
// Function to close the modal and remove animations
function closeModal() {
modal.classList.remove('show'); // Remove the 'show' class to trigger the fade-out animation
setTimeout(() => {
modal.style.display = 'none'; // Hide the modal after the animation completes
// Remove the 'modal-open' class to re-enable scrolling
body.classList.remove('modal-open');
// Remove the contextmenu event listener
document.removeEventListener('contextmenu', preventContextMenu);
}, 300); // Adjust the timeout to match the animation duration
}
}
// Function to prevent the context menu (right-click) while the modal is open
function preventContextMenu(event) {
event.preventDefault();
}
// Function to fetch JSON data and generate student cards
async function fetchAndGenerateStudentCards() {
try {
const response = await fetch("students.json"); // Fetch the JSON file
const students = await response.json(); // Parse the JSON data
// Add this code inside the fetchAndGenerateStudentCards function
const sortingDropdown = document.getElementById("sorting");
sortingDropdown.addEventListener("change", () => {
// Get the selected sorting option
const selectedSorting = sortingDropdown.value;
// Sort the students based on the selected option
if (selectedSorting === "asc") {
students.sort((a, b) => (a.id - b.id)); // Sort in ascending order by student ID
} else if (selectedSorting === "desc") {
students.sort((a, b) => (b.id - a.id)); // Sort in descending order by student ID
} else if (selectedSorting === "random") {
students.sort(() => Math.random() - 0.5); // Shuffle the students randomly
}
// Re-display the students with the new sorting
currentPage = 1; // Reset to the first page
// Check if there is a search query
if (searchInput.value.trim() !== "") {
const filteredStudents = filterStudents(searchInput.value.trim().toLowerCase(), students);
displayStudents(filteredStudents, currentPage, studentsPerPage);
} else {
displayStudents(students, currentPage, studentsPerPage);
}
// Update pagination buttons and call highlightCardContent
updatePaginationButtons();
highlightCardContent(searchInput.value.trim().toLowerCase()); // Add this line
});
// Function to handle the "Next" button click
function nextPage() {
currentPage++;
displayStudents(students, currentPage);
updatePaginationButtons();
scrollToTop(); // Call the scrollToTop function
}
// Function to handle the "Previous" button click
function prevPage() {
currentPage--;
displayStudents(students, currentPage);
updatePaginationButtons();
scrollToTop(); // Call the scrollToTop function
}
// Function to scroll to the top of the website
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: "smooth" // You can use "auto" for instant scrolling
});
}
// Function to update the state of pagination buttons
function updatePaginationButtons() {
const prevButton = document.querySelector(".pagination-previous");
const nextButton = document.querySelector(".pagination-next");
const searchValue = searchInput.value.toLowerCase();
// Filter students based on search input
const filteredStudents = students.filter((student) => {
const searchData = `${student.name} B1903050${student.id} ${student.school} ${student.college} ${student.hometown}`.toLowerCase();
return searchData.includes(searchValue);
});
// Sort and update pagination based on the filtered students
const totalFilteredStudents = filteredStudents.length;
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === Math.ceil(totalFilteredStudents / studentsPerPage) || totalFilteredStudents <= studentsPerPage;
}
// Event listeners for pagination buttons
const prevButton = document.querySelector(".pagination-previous");
const nextButton = document.querySelector(".pagination-next");
prevButton.addEventListener("click", () => {
currentPage--;
displayStudents(students, currentPage, studentsPerPage); // Pass 'studentsPerPage' as the third argument
updatePaginationButtons();
scrollToTop();
// Call highlightCardContent again after displaying students
highlightCardContent(searchInput.value.trim().toLowerCase());
});
nextButton.addEventListener("click", () => {
currentPage++;
displayStudents(students, currentPage, studentsPerPage); // Pass 'studentsPerPage' as the third argument
updatePaginationButtons();
scrollToTop();
// Call highlightCardContent again after displaying students
highlightCardContent(searchInput.value.trim().toLowerCase());
});
// Add event listener to the cards per page dropdown
const cardsPerPageDropdown = document.getElementById("cards");
cardsPerPageDropdown.addEventListener("change", () => {
const selectedCardsPerPage = parseInt(cardsPerPageDropdown.value, 10);
currentPage = 1;
studentsPerPage = selectedCardsPerPage; // Update the 'studentsPerPage' variable
// Check if there is a search query
if (searchInput.value.trim() !== "") {
const filteredStudents = filterStudents(searchInput.value.trim().toLowerCase(), students);
displayStudents(filteredStudents, currentPage, studentsPerPage);
} else {
displayStudents(students, currentPage, studentsPerPage);
}
// Update pagination buttons and call highlightCardContent
updatePaginationButtons();
highlightCardContent(searchInput.value.trim().toLowerCase()); // Add this line
scrollToTop();
});
// Function to filter students based on search query (case-insensitive)
function filterStudents(query, students) {
query = query.toLowerCase();
return students.filter((student) => {
// Combine student data fields into a single string for searching
const searchData = `${student.name} B1903050${student.id} ${student.school} ${student.college} ${student.hometown}`.toLowerCase();
// Check if the query exactly matches the full student ID
if (searchData.includes(`b1903050${query}`)) {
return true;
}
// Check if the query is a partial match for the full student ID or other fields
return searchData.includes(query);
});
}
// Event listener for the search button
const searchButton = document.getElementById("searchButton");
searchButton.addEventListener("click", () => {
const searchInput = document.getElementById("searchInput");
const searchQuery = searchInput.value.trim(); // Get the search query and remove leading/trailing spaces
if (searchQuery !== "") {
// Filter students based on the search query
const filteredStudents = filterStudents(searchQuery, students);
if (filteredStudents.length > 0) {
// If there are matching students, display them
currentPage = 1; // Reset to the first page
displayStudents(filteredStudents, currentPage, studentsPerPage);
// Highlight the searched text
highlightCardContent(searchQuery);
updatePaginationButtons();
} else {
// Display a modal alert
showModal("Your query did not bring up any results.", 'error');
// Add an event listener to the modal "Okay" button to scroll to the input field
const modalOkayBtn = document.getElementById('modalOkayBtn');
modalOkayBtn.addEventListener('click', () => {
// Set the Search Input field border red after the delay
setTimeout(() => {
searchInput.focus(); // Focus on the Search Input field
searchInput.classList.add("red-border");
// Customize the #searchInput outline color
searchInput.style.outlineColor = "red";
scrollToElement(searchInput); // Scroll to the Search Input field
}, 200);
});
// Add an input event listener to remove the red border when the user starts typing again
searchInput.addEventListener('input', function () {
searchInput.classList.remove("red-border"); // Remove the red border
// Reset #searchInput outline color to its original state
searchInput.style.outlineColor = "#3498db";
searchInput.focus();
});
}
} else {
// If the search input is empty, add the "shaky" class for animation
searchContainer.classList.add("shaky");
// Add the red border class to the input element
searchInput.classList.add("red-border");
// Remove the "shaky" class and red border class after the animation duration
setTimeout(() => {
searchContainer.classList.remove("shaky");
searchInput.classList.remove("red-border");
}, 500); // Adjust the duration to match the animation duration in CSS
}
// Call the highlightCardContent function after displaying students
highlightCardContent(searchQuery);
});
// Event listener for real-time search
const searchInput = document.getElementById("searchInput");
searchInput.addEventListener("input", () => {
const searchQuery = searchInput.value.trim().toLowerCase();
// Filter students based on the search query
const filteredStudents = filterStudents(searchQuery, students);
// Display the filtered students
currentPage = 1; // Reset to the first page
displayStudents(filteredStudents, currentPage, studentsPerPage);
updatePaginationButtons();
});
// Event listener for real-time search
searchInput.addEventListener("input", () => {
const searchQuery = searchInput.value.trim().toLowerCase();
// Filter students based on the search query
const filteredStudents = filterStudents(searchQuery, students);
// Display the filtered students
currentPage = 1; // Reset to the first page
displayStudents(filteredStudents, currentPage, studentsPerPage);
updatePaginationButtons();
// Highlight search results in all card content
highlightCardContent(searchQuery);
});
// Call the function to display the initial page of students
displayStudents(students, currentPage, studentsPerPage);
updatePaginationButtons();
} catch (error) {
console.error("Error fetching or parsing JSON data:", error);
// Handle the error by disabling the buttons
const prevButton = document.querySelector(".pagination-previous");
const nextButton = document.querySelector(".pagination-next");
prevButton.disabled = true;
nextButton.disabled = true;
}
}
// Function to highlight search results in student card content
function highlightCardContent(searchQuery) {
const studentProfiles = document.querySelector(".student-profiles");
// Remove previous highlights
const highlightedText = studentProfiles.querySelectorAll(".highlight");
highlightedText.forEach((element) => {
element.classList.remove("highlight");
element.classList.remove("highlight-school"); // Remove the specific highlight classes
element.classList.remove("highlight-college"); // Remove the specific highlight classes
element.classList.remove("highlight-hometown"); // Remove the specific highlight classes
});
// Highlight new search results in all card content
if (searchQuery.trim() !== "") {
const searchRegex = new RegExp(searchQuery, "gi"); // "gi" for global and case-insensitive search
const cardContentElements = studentProfiles.querySelectorAll(".student-name, .student-id, .student-info td"); // Include td elements for School, College, and Hometown
cardContentElements.forEach((element) => {
const contentText = element.textContent;
const highlightedHTML = contentText.replace(searchRegex, (match) => `<span class="highlight">${match}</span>`);
element.innerHTML = highlightedHTML;
// Add specific CSS classes for School, College, and Hometown
if (element.parentElement.classList.contains("school")) {
element.classList.add("highlight-school");
} else if (element.parentElement.classList.contains("college")) {
element.classList.add("highlight-college");
} else if (element.parentElement.classList.contains("hometown")) {
element.classList.add("highlight-hometown");
}
});
}
}
// Call the function to fetch and generate student cards when the page loads
window.addEventListener("load", fetchAndGenerateStudentCards);