rtredes's picture
Initial DeepSite commit
e727d96 verified
Raw History Blame Contribute Delete
8.39 kB
// Cart functionality
let cart = [];
let cartTotal = 0;
// Initialize
document.addEventListener('DOMContentLoaded', () => {
initializeFilters();
initializeNavbar();
});
// Filter functionality
function initializeFilters() {
const filterBtns = document.querySelectorAll('.filter-btn');
const products = document.querySelectorAll('.product-card');
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
// Update active state
filterBtns.forEach(b => {
b.classList.remove('bg-primary', 'text-white', 'border-primary');
b.classList.add('text-gray-700', 'border-gray-300');
});
btn.classList.remove('text-gray-700', 'border-gray-300');
btn.classList.add('bg-primary', 'text-white', 'border-primary');
// Filter products
const filter = btn.dataset.filter;
products.forEach(product => {
if (filter === 'all' || product.dataset.category === filter) {
product.style.display = 'block';
product.classList.add('fade-in');
} else {
product.style.display = 'none';
}
});
});
});
}
// Navbar scroll effect
function initializeNavbar() {
const navbar = document.getElementById('navbar');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 100) {
navbar.classList.add('shadow-md');
} else {
navbar.classList.remove('shadow-md');
}
lastScroll = currentScroll;
});
}
// Mobile menu toggle
function toggleMobileMenu() {
const menu = document.getElementById('mobile-menu');
menu.classList.toggle('hidden');
}
// Cart toggle
function toggleCart() {
const sidebar = document.getElementById('cart-sidebar');
const overlay = document.getElementById('cart-overlay');
if (sidebar.classList.contains('cart-closed')) {
sidebar.classList.remove('cart-closed');
sidebar.classList.add('cart-open');
overlay.classList.remove('hidden');
document.body.style.overflow = 'hidden';
} else {
sidebar.classList.remove('cart-open');
sidebar.classList.add('cart-closed');
overlay.classList.add('hidden');
document.body.style.overflow = 'auto';
}
}
// Add to cart
function addToCart(id, name, price, image) {
const existingItem = cart.find(item => item.id === id);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({
id: id,
name: name,
price: price,
image: image,
quantity: 1
});
}
updateCart();
showToast(`${name} added to cart!`);
// Open cart automatically on first add
if (cart.length === 1 && cart[0].quantity === 1) {
setTimeout(() => {
const sidebar = document.getElementById('cart-sidebar');
if (sidebar.classList.contains('cart-closed')) {
toggleCart();
}
}, 500);
}
}
// Remove from cart
function removeFromCart(id) {
cart = cart.filter(item => item.id !== id);
updateCart();
}
// Update quantity
function updateQuantity(id, change) {
const item = cart.find(item => item.id === id);
if (item) {
item.quantity += change;
if (item.quantity <= 0) {
removeFromCart(id);
} else {
updateCart();
}
}
}
// Update cart UI
function updateCart() {
const cartItems = document.getElementById('cart-items');
const cartCount = document.getElementById('cart-count');
const cartTotalEl = document.getElementById('cart-total');
// Update count
const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
if (totalItems > 0) {
cartCount.textContent = totalItems;
cartCount.classList.remove('hidden');
} else {
cartCount.classList.add('hidden');
}
// Calculate total
cartTotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
cartTotalEl.textContent = `$${cartTotal.toFixed(2)}`;
// Render items
if (cart.length === 0) {
cartItems.innerHTML = `
<div class="text-center text-gray-500 py-12">
<i data-lucide="shopping-bag" class="w-16 h-16 mx-auto mb-4 text-gray-300"></i>
<p>Your cart is empty</p>
<button onclick="toggleCart()" class="mt-4 text-primary font-semibold hover:underline">Continue Shopping</button>
</div>
`;
} else {
cartItems.innerHTML = cart.map(item => `
<div class="flex gap-4 bg-gray-50 p-4 rounded-xl">
<img src="${item.image}" alt="${item.name}" class="w-20 h-20 object-cover rounded-lg">
<div class="flex-1">
<h3 class="font-semibold text-gray-900 line-clamp-1">${item.name}</h3>
<p class="text-primary font-bold mt-1">$${item.price.toFixed(2)}</p>
<div class="flex items-center mt-2 space-x-2">
<button onclick="updateQuantity(${item.id}, -1)" class="w-6 h-6 rounded-full bg-white border border-gray-300 flex items-center justify-center hover:bg-gray-100">
<i data-lucide="minus" class="w-3 h-3"></i>
</button>
<span class="font-medium w-8 text-center">${item.quantity}</span>
<button onclick="updateQuantity(${item.id}, 1)" class="w-6 h-6 rounded-full bg-white border border-gray-300 flex items-center justify-center hover:bg-gray-100">
<i data-lucide="plus" class="w-3 h-3"></i>
</button>
<button onclick="removeFromCart(${item.id})" class="ml-auto text-red-500 hover:text-red-700">
<i data-lucide="trash-2" class="w-4 h-4"></i>
</button>
</div>
</div>
</div>
`).join('');
}
// Reinitialize Lucide icons
lucide.createIcons();
}
// Checkout
function checkout() {
if (cart.length === 0) {
showToast('Your cart is empty!', 'error');
return;
}
showToast('Proceeding to checkout...', 'success');
setTimeout(() => {
alert('Thank you for your order! This is a demo checkout. Total: $' + cartTotal.toFixed(2));
cart = [];
updateCart();
toggleCart();
}, 1000);
}
// Toast notification
function showToast(message, type = 'success') {
const container = document.getElementById('toast-container');
const toast = document.createElement('div');
const bgColor = type === 'error' ? 'bg-red-500' : 'bg-green-500';
const icon = type === 'error' ? 'alert-circle' : 'check-circle';
toast.className = `toast ${bgColor} text-white px-6 py-3 rounded-full shadow-lg flex items-center space-x-2 min-w-[250px]`;
toast.innerHTML = `
<i data-lucide="${icon}" class="w-5 h-5"></i>
<span class="font-medium">${message}</span>
`;
container.appendChild(toast);
lucide.createIcons();
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateX(100%)';
toast.style.transition = 'all 0.3s ease';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// Newsletter
function handleNewsletter(e) {
e.preventDefault();
const email = e.target.querySelector('input[type="email"]').value;
showToast('Thank you for subscribing! Check your email for 15% off.');
e.target.reset();
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// Close mobile menu if open
document.getElementById('mobile-menu').classList.add('hidden');
}
});
});