-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearches.js
171 lines (141 loc) · 6.83 KB
/
searches.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
document.addEventListener("DOMContentLoaded", function () {
//SEARCH BAR------------------------------------------------------------------------------
const searchInput = document.querySelector(".searchBar input");
// Listen for the 'keydown' event
searchInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
// Get input + remove extra spaces
const query = searchInput.value.trim();
if (query) {
// Open the searches page and pass the query as a parameter
window.location.href = `searches.html?q=${encodeURIComponent(query)}`;
}
}
});//-------------------------------------------------------------------------------------
//CATEGORY STRIP -------------------------------------------------------------------------
// Correct selector for categories links
const categoryLinks = document.querySelectorAll(".categories ul li a");
categoryLinks.forEach((link) => {
link.addEventListener("click", function (event) {
event.preventDefault(); // Prevent default link behavior
const linkText = link.textContent.trim(); // Get the text content of the link
// Trigger the appropriate function based on link text
switch (linkText) {
case "Rachu":
window.location.href = "searches.html?q=" + encodeURIComponent("Rachu");
break;
case "Tego":
window.location.href = "searches.html?q=" + encodeURIComponent("Tego");
break;
case "Wonju":
window.location.href = "searches.html?q=" + encodeURIComponent("Wonju");
break;
case "Kira":
window.location.href = "searches.html?q=" + encodeURIComponent("Kira");
break;
case "Accessories":
window.location.href = "searches.html?q=" + encodeURIComponent("Accessories");
break;
default:
console.warn("No matching function for:", linkText);
}
});
});//------------------------------------------------------------------------------------
//FOOTER CATEGORIES-----------------------------------------------------------------------
// GO TO SEARCHES PAGE + SET SEARCHBAR = Rachu
function redirectToSearchWithRachu() {
window.location.href = "searches.html?q=" + encodeURIComponent("Rachu");
}
// GO TO SEARCHES PAGE + SET SEARCHBAR = Wonju
function redirectToSearchWithWonju() {
window.location.href = "searches.html?q=" + encodeURIComponent("Wonju");
}
// GO TO SEARCHES PAGE + SET SEARCHBAR = Tego
function redirectToSearchWithTego() {
window.location.href = "searches.html?q=" + encodeURIComponent("Tego");
}
// GO TO SEARCHES PAGE + SET SEARCHBAR = Kira
function redirectToSearchWithKira() {
window.location.href = "searches.html?q=" + encodeURIComponent("Kira");
}
// GO TO SEARCHES PAGE + SET SEARCHBAR = Accessories
function redirectToSearchWithAccessories() {
window.location.href = "searches.html?q=" + encodeURIComponent("Accessories");
}
// Correct selector for footer links
const footerLinks = document.querySelectorAll(".footer-section ul li a");
footerLinks.forEach((link) => {
link.addEventListener("click", function (event) {
event.preventDefault(); // Prevent default link behavior
const linkText = link.textContent.trim(); // Get the text content of the link
// Trigger the appropriate function based on link text
switch (linkText) {
case "Shop for Rachu":
redirectToSearchWithRachu();
break;
case "Shop for Wonju":
redirectToSearchWithWonju();
break;
case "Shop for Tego":
redirectToSearchWithTego();
break;
case "Shop for Kira":
redirectToSearchWithKira();
break;
case "Shop for Accessories":
redirectToSearchWithAccessories();
break;
default:
console.warn("No matching function for:", linkText);
}
});
});//-------------------------------------------------------------------------------------
// CHANGING PRODUCTS BASED ON ADDRESS + SEARCH BAR INPUT ---------------------------------
const searchBar = document.querySelector(".searchBar input[type='text']"); // The search bar
const rachuImages = document.querySelectorAll("[id^='rachuImg']"); // Select all elements with IDs starting with "rachuImg"
const searchTerm = document.querySelector(".searchTermBox2 h1"); // The <h1> inside .searchTermBox2
// Function to determine the image URL based on input value
const getData = (value) => {
switch (value.toLowerCase()) {
case "rachu":
return { imageURL: "resources/kira1.png", text: "RACHU" };
case "wonju":
return { imageURL: "resources/kira2.png", text: "WONJU" };
case "tego":
return { imageURL: "resources/kira3.png", text: "TEGO" };
case "kira":
return { imageURL: "resources/kira4.png", text: "KIRA" };
case "accessories":
return { imageURL: "resources/kira5.png", text: "ACCESSORIES" };
default:
return { imageURL: "", text: "UNKNOWN" };
}
};
// Function to update images and text dynamically
const updatePageContent = (value) => {
const { imageURL, text } = getData(value); // Get the data for the value
// Update images
if (imageURL) {
rachuImages.forEach((rachuImage) => {
rachuImage.style.backgroundImage = `url('${imageURL}')`;
});
}
// Update the h1 text
if (searchTerm && text) {
searchTerm.textContent = text;
}
};
// Check the URL's query parameter for the `q` value
const params = new URLSearchParams(window.location.search);
const queryValue = params.get("q");
if (queryValue) {
updatePageContent(queryValue); // Update page content based on the query parameter
}
// Add an event listener for search bar input
if (searchBar) {
searchBar.addEventListener("input", (event) => {
const searchValue = event.target.value.trim(); // Get the trimmed search value
updatePageContent(searchValue); // Update page content dynamically
});
}//-------------------------------------------------------------------------------------
});