-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (195 loc) · 7.97 KB
/
script.js
File metadata and controls
229 lines (195 loc) · 7.97 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
// Firebase initialization (commented out for now)
// import { app } from "./firebase-Config";
// console.log("Firebase Initialized:", app);
const bar = document.getElementById('bar');
const close = document.getElementById('close');
const nav = document.getElementById('navbar');
if(bar){
bar.addEventListener('click' , () => {
nav.classList.add('active');
})
}
if(close){
close.addEventListener('click' , () => {
nav.classList.remove('active');
})
}
// Product Search Functionality
function searchProducts() {
console.log("Search Products called");
const searchInput = document.getElementById('productSearch');
if (!searchInput) {
console.error("Search input not found");
return;
}
const searchTerm = searchInput.value.toLowerCase().trim();
console.log("Searching for:", searchTerm);
filterProducts();
}
function filterProducts() {
console.log("Filter Products called");
const searchInput = document.getElementById('productSearch');
const brandFilter = document.getElementById('brandFilter');
const priceFilter = document.getElementById('priceFilter');
const searchResults = document.getElementById('searchResults');
const resultsCount = document.getElementById('resultsCount');
if (!searchInput) {
console.error('Search input not found');
return;
}
const searchTerm = searchInput.value.toLowerCase().trim();
const selectedBrand = brandFilter ? brandFilter.value : '';
const selectedPriceRange = priceFilter ? priceFilter.value : '';
console.log('Filtering with:', {searchTerm, selectedBrand, selectedPriceRange});
// Get all product containers - use a more specific selector
const products = document.querySelectorAll('.pro-container .pro');
let visibleCount = 0;
let hasFilters = searchTerm || selectedBrand || selectedPriceRange;
console.log('Found products:', products.length);
products.forEach((product, index) => {
console.log(`Processing product ${index}`);
// Get product details
const brandElement = product.querySelector('.des span');
const nameElement = product.querySelector('.des h5');
const priceElement = product.querySelector('.des h4');
if (!brandElement || !nameElement || !priceElement) {
console.log(`Missing elements in product ${index}`);
return;
}
const brand = brandElement.textContent.toLowerCase().trim();
const productName = nameElement.textContent.toLowerCase().trim();
const priceText = priceElement.textContent;
const price = parseInt(priceText.replace(/[^\d]/g, ''));
console.log(`Product ${index}:`, {brand, productName, price});
let showProduct = true;
// Search term filter - more comprehensive matching
if (searchTerm) {
const searchWords = searchTerm.split(' ');
const matchFound = searchWords.some(word =>
productName.includes(word) ||
brand.includes(word) ||
(word === 'shirt' && (productName.includes('shirt') || productName.includes('tee'))) ||
(word === 'tee' && productName.includes('tee')) ||
(word === 'shoes' && productName.includes('shoes'))
);
if (!matchFound) {
showProduct = false;
console.log(`Product ${index} hidden - no search match`);
}
}
// Brand filter
if (selectedBrand && brandElement.textContent.trim() !== selectedBrand) {
showProduct = false;
console.log(`Product ${index} hidden - brand filter`);
}
// Price filter
if (selectedPriceRange) {
if (selectedPriceRange === '0-2000' && (price < 0 || price > 2000)) {
showProduct = false;
} else if (selectedPriceRange === '2000-5000' && (price < 2000 || price > 5000)) {
showProduct = false;
} else if (selectedPriceRange === '5000-10000' && (price < 5000 || price > 10000)) {
showProduct = false;
} else if (selectedPriceRange === '10000+' && price < 10000) {
showProduct = false;
}
}
// Show/hide product with multiple methods to ensure it works
if (showProduct) {
product.style.display = 'block';
product.style.visibility = 'visible';
product.classList.remove('product-hidden');
visibleCount++;
console.log(`Product ${index} shown`);
} else {
product.style.display = 'none';
product.style.visibility = 'hidden';
product.classList.add('product-hidden');
console.log(`Product ${index} hidden`);
}
});
console.log('Total visible products:', visibleCount);
// Show/hide search results info
if (searchResults) {
if (hasFilters) {
searchResults.style.display = 'flex';
if (visibleCount === 0) {
if (resultsCount) {
resultsCount.innerHTML = '<i class="fas fa-search"></i> No products found. Try different search terms.';
}
showNoResults();
} else {
if (resultsCount) {
resultsCount.textContent = `Found ${visibleCount} product${visibleCount !== 1 ? 's' : ''}`;
}
hideNoResults();
}
} else {
searchResults.style.display = 'none';
hideNoResults();
}
}
}
function clearSearch() {
console.log("Clear search called");
const searchInput = document.getElementById('productSearch');
const brandFilter = document.getElementById('brandFilter');
const priceFilter = document.getElementById('priceFilter');
const searchResults = document.getElementById('searchResults');
const products = document.querySelectorAll('.pro-container .pro');
console.log("Clearing search for", products.length, "products");
// Clear all inputs
if (searchInput) searchInput.value = '';
if (brandFilter) brandFilter.value = '';
if (priceFilter) priceFilter.value = '';
// Show all products
products.forEach((product, index) => {
product.style.display = 'block';
product.style.visibility = 'visible';
product.classList.remove('product-hidden');
console.log(`Product ${index} restored`);
});
// Hide search results
if (searchResults) {
searchResults.style.display = 'none';
}
hideNoResults();
console.log("Search cleared successfully");
}
function showNoResults() {
const proContainer = document.querySelector('#product1 .pro-container');
let noResultsDiv = document.querySelector('.no-results');
if (!noResultsDiv) {
noResultsDiv = document.createElement('div');
noResultsDiv.className = 'no-results';
noResultsDiv.innerHTML = `
<i class="fas fa-search"></i>
<h3>No products found</h3>
<p>Try adjusting your search criteria or browse our categories</p>
`;
proContainer.appendChild(noResultsDiv);
} else {
noResultsDiv.style.display = 'block';
}
}
function hideNoResults() {
const noResultsDiv = document.querySelector('.no-results');
if (noResultsDiv) {
noResultsDiv.style.display = 'none';
}
}
// Add enter key support for search
document.addEventListener('DOMContentLoaded', function() {
const searchInput = document.getElementById('productSearch');
if (searchInput) {
searchInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
searchProducts();
}
});
}
});
// Make functions globally available
window.searchProducts = searchProducts;
window.filterProducts = filterProducts;
window.clearSearch = clearSearch;