Started adding advanced search

This commit is contained in:
Jason
2026-04-28 15:09:56 -05:00
parent fcaa15bf6e
commit b8c477a59c
5 changed files with 665 additions and 0 deletions
+163
View File
@@ -732,3 +732,166 @@ body {
margin: 5px 0;
color: #666;
}
/* Search Bar Component */
.search-container {
padding: 20px 30px;
background-color: #ffffff;
border-bottom: 2px solid #e0e0e0;
}
.search-wrapper {
max-width: 800px;
margin: 0 auto;
}
.search-input-wrapper {
position: relative;
margin-bottom: 15px;
}
.search-input {
width: 100%;
padding: 12px 16px;
font-size: 16px;
border: 2px solid #333;
border-radius: 8px;
outline: none;
transition: all 0.3s ease;
background-color: white;
}
.search-input:focus {
border-color: #6a4c93;
box-shadow: 0 0 0 3px rgba(106, 76, 147, 0.1);
}
.search-dropdown {
position: absolute;
top: 100%;
left: 0;
right: 0;
background-color: white;
border: 2px solid #333;
border-top: none;
border-radius: 0 0 8px 8px;
max-height: 300px;
overflow-y: auto;
display: none;
z-index: 1000;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.search-dropdown-item {
padding: 12px 16px;
cursor: pointer;
border-bottom: 1px solid #e0e0e0;
transition: background-color 0.2s ease;
}
.search-dropdown-item:last-child {
border-bottom: none;
}
.search-dropdown-item:hover {
background-color: #f5f5f5;
}
.search-dropdown-item.selected {
background-color: #e8e0f0;
font-weight: bold;
}
.search-dropdown-item-title {
font-weight: bold;
color: #333;
margin-bottom: 4px;
}
.search-dropdown-item-code {
font-size: 12px;
color: #666;
}
.search-buttons {
display: flex;
gap: 10px;
justify-content: center;
}
.search-btn {
padding: 10px 20px;
font-size: 14px;
font-weight: bold;
border: 2px solid #333;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
background-color: white;
color: #333;
}
.search-btn:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}
.search-btn:active:not(:disabled) {
transform: translateY(0);
}
.search-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.view-btn {
background-color: #6a4c93;
color: white;
border-color: #6a4c93;
}
.view-btn:hover:not(:disabled) {
background-color: #563d7a;
border-color: #563d7a;
}
.search-btn-action {
background-color: #4CAF50;
color: white;
border-color: #4CAF50;
}
.search-btn-action:hover:not(:disabled) {
background-color: #45a049;
border-color: #45a049;
}
.advanced-btn {
background-color: #2196F3;
color: white;
border-color: #2196F3;
}
.advanced-btn:hover:not(:disabled) {
background-color: #0b7dda;
border-color: #0b7dda;
}
/* Dropdown scrollbar */
.search-dropdown::-webkit-scrollbar {
width: 8px;
}
.search-dropdown::-webkit-scrollbar-track {
background: #f1f1f1;
}
.search-dropdown::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
.search-dropdown::-webkit-scrollbar-thumb:hover {
background: #555;
}
+318
View File
@@ -1445,3 +1445,321 @@ function navigateToHistory(index) {
// Start the quiz when page loads
window.addEventListener('DOMContentLoaded', init);
// ===== SEARCH BAR FUNCTIONALITY =====
// Search state variables
let searchMatches = [];
let selectedSearchIndex = -1;
// Toggle search bar visibility based on current screen
function toggleSearchBar() {
const searchContainer = document.getElementById('searchContainer');
const currentKey = history.length > 0 ? history[history.length - 1] : 'start';
if (currentKey === 'start') {
searchContainer.style.display = 'block';
} else {
searchContainer.style.display = 'none';
// Clear search when hiding
clearSearch();
}
}
// Clear search state
function clearSearch() {
const searchInput = document.getElementById('searchInput');
const searchDropdown = document.getElementById('searchDropdown');
const viewBtn = document.getElementById('viewBtn');
if (searchInput) searchInput.value = '';
if (searchDropdown) {
searchDropdown.style.display = 'none';
searchDropdown.innerHTML = '';
}
if (viewBtn) viewBtn.disabled = true;
searchMatches = [];
selectedSearchIndex = -1;
}
// Filter products by search term
function filterProductsBySearch(searchTerm) {
if (!searchTerm || searchTerm.trim().length < 2) {
return [];
}
const term = searchTerm.toLowerCase().trim();
return productData.filter(product => {
const description = (product.description || '').toLowerCase();
const productCode = (product.productCode || '').toLowerCase();
const category = (product.category || '').toLowerCase();
const baseType = (product.baseType || '').toLowerCase();
const subTypeWindow = (product.subType?.window || '').toLowerCase();
const subTypeDoor = (product.subType?.door || '').toLowerCase();
return description.includes(term) ||
productCode.includes(term) ||
category.includes(term) ||
baseType.includes(term) ||
subTypeWindow.includes(term) ||
subTypeDoor.includes(term);
});
}
// Update search dropdown with matches
function updateSearchDropdown(matches) {
const dropdown = document.getElementById('searchDropdown');
const viewBtn = document.getElementById('viewBtn');
if (!matches || matches.length === 0) {
dropdown.style.display = 'none';
dropdown.innerHTML = '';
viewBtn.disabled = true;
selectedSearchIndex = -1;
return;
}
// Show only first 10 matches
const displayMatches = matches.slice(0, 10);
dropdown.innerHTML = displayMatches.map((product, index) => {
const prodCode = product.productCode || product.id;
const desc = product.description || 'No description';
const isSelected = index === selectedSearchIndex ? 'selected' : '';
return `
<div class="search-dropdown-item ${isSelected}"
data-index="${index}"
data-product-code="${prodCode}"
onclick="selectSearchItem(${index})">
<div class="search-dropdown-item-title">${desc}</div>
<div class="search-dropdown-item-code">${prodCode}</div>
</div>
`;
}).join('');
if (matches.length > 10) {
dropdown.innerHTML += `
<div class="search-dropdown-item" style="font-style: italic; color: #999; cursor: pointer;"
onclick="handleSearchResults()">
+ ${matches.length - 10} more results... (click to view all)
</div>
`;
}
dropdown.style.display = 'block';
}
// Select a search item
function selectSearchItem(index, closeDropdown = true) {
selectedSearchIndex = index;
const viewBtn = document.getElementById('viewBtn');
const searchInput = document.getElementById('searchInput');
const searchDropdown = document.getElementById('searchDropdown');
viewBtn.disabled = false;
// Update search input with selected product description
if (searchMatches[index]) {
const selectedProduct = searchMatches[index];
const desc = selectedProduct.description || selectedProduct.DESCRIPTION || 'No description';
searchInput.value = desc;
}
// Hide dropdown after selection (unless using keyboard navigation)
if (closeDropdown) {
searchDropdown.style.display = 'none';
}
// Update visual selection
const items = document.querySelectorAll('.search-dropdown-item');
items.forEach((item, i) => {
if (i === index) {
item.classList.add('selected');
} else {
item.classList.remove('selected');
}
});
}
// Handle View button click
function handleViewProduct() {
if (selectedSearchIndex >= 0 && selectedSearchIndex < searchMatches.length) {
const product = searchMatches[selectedSearchIndex];
const prodCode = product.productCode || product.id;
// Clear search and hide dropdown
clearSearch();
// Navigate to product
showProductByCode(prodCode);
}
}
// Handle Search button click (show results grid)
function handleSearchResults() {
const searchInput = document.getElementById('searchInput');
const searchTerm = searchInput.value.trim();
if (!searchTerm || searchTerm.length < 2) {
alert('Please enter at least 2 characters to search.');
return;
}
const matches = filterProductsBySearch(searchTerm);
if (matches.length === 0) {
alert('No products found matching "' + searchTerm + '".');
return;
}
// Store search results temporarily
searchMatches = matches;
// Clear search UI
clearSearch();
// Display results in grid format (similar to showFilteredProducts)
showSearchResultsGrid(matches, searchTerm);
}
// Show search results in a grid
function showSearchResultsGrid(matches, searchTerm) {
const contentDiv = document.getElementById('content');
let html = `
<div class="result-container">
<div class="result-title">Found ${matches.length} Product${matches.length > 1 ? 's' : ''}</div>
<div class="result-details">
<p style="margin-bottom: 20px;">Search results for: <strong>"${searchTerm}"</strong></p>
</div>
<div class="products-grid">
`;
matches.forEach(product => {
const prodCode = product.productCode || product.id;
const desc = product.description || product.DESCRIPTION || 'No description';
const materials = product.materials ? product.materials.join(', ') : 'N/A';
const colors = product.colors ? product.colors.join(', ') : 'N/A';
const baseType = product.baseType || 'N/A';
const subType = product.subType?.window || product.subType?.door || 'N/A';
html += `
<div class="product-card" onclick="showProductByCode('${prodCode}')">
<h3>${desc}</h3>
<p style="color: #999; font-size: 0.95em; margin-top: 5px;">${prodCode}</p>
<div style="font-size: 0.9em; color: #666; margin-top: 10px;">
<strong>Type:</strong> ${baseType}${subType !== 'N/A' ? ' - ' + subType : ''}<br>
<strong>Materials:</strong> ${materials}<br>
<strong>Colors:</strong> ${colors}
</div>
</div>
`;
});
html += `
</div>
<div class="result-actions">
<button class="back-button" onclick="startOver()">← Back to Start</button>
<button class="back-button" onclick="shareCurrentPage()">🔗 Share</button>
</div>
</div>
`;
contentDiv.innerHTML = html;
// Update history to track search results
history.push('search-results');
updateBreadcrumb();
}
// Handle Advanced Search button click
function handleAdvancedSearch() {
// Navigate to advanced search page
window.location.href = '/quiz/advanced-search';
}
// Set up search input event listeners
function initSearchBar() {
const searchInput = document.getElementById('searchInput');
const searchDropdown = document.getElementById('searchDropdown');
if (!searchInput) return;
// Input event for real-time filtering
searchInput.addEventListener('input', function(e) {
const searchTerm = e.target.value.trim();
if (searchTerm.length < 2) {
searchMatches = [];
updateSearchDropdown([]);
return;
}
searchMatches = filterProductsBySearch(searchTerm);
updateSearchDropdown(searchMatches);
// Reset selection
selectedSearchIndex = -1;
document.getElementById('viewBtn').disabled = true;
});
// Keyboard navigation (arrow keys and enter)
searchInput.addEventListener('keydown', function(e) {
if (searchMatches.length === 0) return;
if (e.key === 'ArrowDown') {
e.preventDefault();
selectedSearchIndex = Math.min(selectedSearchIndex + 1, searchMatches.length - 1);
updateSearchDropdown(searchMatches);
selectSearchItem(selectedSearchIndex, false); // Keep dropdown open
} else if (e.key === 'ArrowUp') {
e.preventDefault();
selectedSearchIndex = Math.max(selectedSearchIndex - 1, -1);
if (selectedSearchIndex >= 0) {
updateSearchDropdown(searchMatches);
selectSearchItem(selectedSearchIndex, false); // Keep dropdown open
} else {
document.getElementById('viewBtn').disabled = true;
}
} else if (e.key === 'Enter') {
e.preventDefault();
if (selectedSearchIndex >= 0) {
handleViewProduct();
} else {
handleSearchResults();
}
} else if (e.key === 'Escape') {
clearSearch();
}
});
// Click outside to close dropdown
document.addEventListener('click', function(e) {
if (!searchInput.contains(e.target) && !searchDropdown.contains(e.target)) {
searchDropdown.style.display = 'none';
}
});
}
// Override loadContent to toggle search bar
const originalLoadContent = loadContent;
loadContent = function(key) {
originalLoadContent(key);
toggleSearchBar();
};
// Override startOver to show search bar
const originalStartOver = startOver;
startOver = function() {
originalStartOver();
toggleSearchBar();
};
// Initialize search bar when page loads
window.addEventListener('DOMContentLoaded', function() {
initSearchBar();
toggleSearchBar();
});