-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (85 loc) · 3.35 KB
/
script.js
File metadata and controls
105 lines (85 loc) · 3.35 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
// Simple sidebar open/close toggle for mobile
const sidebar = document.querySelector('.sidebar');
let sidebarOpen = false;
function toggleSidebar() {
sidebarOpen = !sidebarOpen;
if (sidebarOpen) {
sidebar.classList.add('open');
} else {
sidebar.classList.remove('open');
}
}
// Optional console log to confirm JS is loaded
console.log('DB Yale site script loaded!');
//Actual filter
document.addEventListener("DOMContentLoaded", function () {
const checkboxes = document.querySelectorAll('input[name="category"]');
const contentItems = document.querySelectorAll('.event-list li, .media-list li');
function filterContent() {
const selectedCategories = Array.from(checkboxes)
.filter(cb => cb.checked)
.map(cb => cb.value);
contentItems.forEach(item => {
const itemCategories = item.getAttribute("data-category").split(" ");
const isVisible = selectedCategories.some(category => itemCategories.includes(category));
item.style.display = isVisible ? "" : "none";
});
}
// Apply filter when checkboxes change
checkboxes.forEach(cb => cb.addEventListener("change", filterContent));
// Run filter on page load
filterContent();
});
// search bar function
document.addEventListener("DOMContentLoaded", function () {
const searchInput = document.getElementById("sidebarSearch");
const searchButton = document.getElementById("searchButton");
if (!searchInput || !searchButton) {
console.error("Search input or button not found!");
return;
}
const allSections = document.querySelectorAll("h2, h3, p, li"); // What the search looks for on the page
function searchContent() {
const filter = searchInput.value.trim().toLowerCase();
let found = false;
allSections.forEach((section) => {
const text = section.textContent.toLowerCase();
if (text.includes(filter) && !found) {
section.scrollIntoView({ behavior: "smooth", block: "center" });
section.style.backgroundColor = "yellow"; // Highlight
setTimeout(() => section.style.backgroundColor = "transparent", 2000);
found = true;
}
});
return found;
}
function searchAndRedirect() {
const filter = searchInput.value.trim().toLowerCase();
const pageMap = {
"about": "about.html",
"collaborate": "collaborate.html",
"syllabus": "syllabus.html",
"news": "news.html",
"gallery": "gallery.html",
"board": "meettheboard.html",
"events": "news.html",
"mission": "about.html"
};
for (const keyword in pageMap) {
if (filter.includes(keyword)) {
window.location.href = pageMap[keyword];
return;
}
}
if (!searchContent()) {
alert("No matching results found.");
}
}
searchButton.addEventListener("click", searchAndRedirect);
searchInput.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
event.preventDefault();
searchAndRedirect();
}
});
});