/** * Enhanced Cart and Wishlist Management System * Amazon/eBay-style product display with images and details */ class ShoppingManager { constructor() { this.cart = this.loadFromStorage("skyart_cart") || []; this.wishlist = this.loadFromStorage("skyart_wishlist") || []; this.init(); } init() { this.updateAllBadges(); this.setupEventListeners(); this.renderCart(); this.renderWishlist(); } loadFromStorage(key) { try { const data = localStorage.getItem(key); return data ? JSON.parse(data) : null; } catch (e) { console.error("Error loading from storage:", e); return null; } } saveToStorage(key, data) { try { localStorage.setItem(key, JSON.stringify(data)); } catch (e) { console.error("Error saving to storage:", e); } } setupEventListeners() { // Cart toggle const cartToggle = document.getElementById("cartToggle"); const cartPanel = document.getElementById("cartPanel"); const cartClose = document.getElementById("cartClose"); if (cartToggle) { cartToggle.addEventListener("click", (e) => { e.stopPropagation(); cartPanel?.classList.toggle("active"); document.getElementById("wishlistPanel")?.classList.remove("active"); }); } if (cartClose) { cartClose.addEventListener("click", (e) => { e.stopPropagation(); cartPanel?.classList.remove("active"); }); } // Wishlist toggle const wishlistToggle = document.getElementById("wishlistToggle"); const wishlistPanel = document.getElementById("wishlistPanel"); const wishlistClose = document.getElementById("wishlistClose"); if (wishlistToggle) { wishlistToggle.addEventListener("click", (e) => { e.stopPropagation(); wishlistPanel?.classList.toggle("active"); cartPanel?.classList.remove("active"); }); } if (wishlistClose) { wishlistClose.addEventListener("click", (e) => { e.stopPropagation(); wishlistPanel?.classList.remove("active"); }); } // Mobile menu const mobileToggle = document.getElementById("mobileMenuToggle"); const mobileMenu = document.getElementById("mobileMenu"); const mobileClose = document.getElementById("mobileMenuClose"); if (mobileToggle) { mobileToggle.addEventListener("click", () => { mobileMenu?.classList.toggle("active"); document.body.style.overflow = mobileMenu?.classList.contains("active") ? "hidden" : ""; }); } if (mobileClose) { mobileClose.addEventListener("click", () => { mobileMenu?.classList.remove("active"); document.body.style.overflow = ""; }); } // Close dropdowns on outside click document.addEventListener("click", (e) => { if (!e.target.closest(".cart-dropdown-wrapper")) { cartPanel?.classList.remove("active"); } if (!e.target.closest(".wishlist-dropdown-wrapper")) { wishlistPanel?.classList.remove("active"); } }); } // Add to Cart addToCart(product, quantity = 1) { const existingItem = this.cart.find((item) => item.id === product.id); if (existingItem) { existingItem.quantity += quantity; } else { this.cart.push({ id: product.id, name: product.name, price: parseFloat(product.price), imageurl: product.imageurl, quantity: quantity, addedAt: new Date().toISOString(), }); } this.saveToStorage("skyart_cart", this.cart); this.updateAllBadges(); this.renderCart(); this.showNotification(`${product.name} added to cart!`, "success"); } // Remove from Cart removeFromCart(productId) { this.cart = this.cart.filter((item) => item.id !== productId); this.saveToStorage("skyart_cart", this.cart); this.updateAllBadges(); this.renderCart(); this.showNotification("Item removed from cart", "info"); } // Update Cart Quantity updateCartQuantity(productId, quantity) { const item = this.cart.find((item) => item.id === productId); if (item) { if (quantity <= 0) { this.removeFromCart(productId); } else { item.quantity = quantity; this.saveToStorage("skyart_cart", this.cart); this.updateAllBadges(); this.renderCart(); } } } // Add to Wishlist addToWishlist(product) { const exists = this.wishlist.find((item) => item.id === product.id); if (!exists) { this.wishlist.push({ id: product.id, name: product.name, price: parseFloat(product.price), imageurl: product.imageurl, addedAt: new Date().toISOString(), }); this.saveToStorage("skyart_wishlist", this.wishlist); this.updateAllBadges(); this.renderWishlist(); this.showNotification(`${product.name} added to wishlist!`, "success"); } else { this.showNotification("Already in wishlist", "info"); } } // Remove from Wishlist removeFromWishlist(productId) { this.wishlist = this.wishlist.filter((item) => item.id !== productId); this.saveToStorage("skyart_wishlist", this.wishlist); this.updateAllBadges(); this.renderWishlist(); this.showNotification("Item removed from wishlist", "info"); } // Move from Wishlist to Cart moveToCart(productId) { const item = this.wishlist.find((item) => item.id === productId); if (item) { this.addToCart(item, 1); this.removeFromWishlist(productId); } } // Update All Badges updateAllBadges() { const cartCount = this.cart.reduce((sum, item) => sum + item.quantity, 0); const wishlistCount = this.wishlist.length; const cartBadge = document.getElementById("cartCount"); const wishlistBadge = document.getElementById("wishlistCount"); if (cartBadge) { cartBadge.textContent = cartCount; cartBadge.style.display = cartCount > 0 ? "flex" : "none"; } if (wishlistBadge) { wishlistBadge.textContent = wishlistCount; wishlistBadge.style.display = wishlistCount > 0 ? "flex" : "none"; } } // Render Cart renderCart() { const cartContent = document.getElementById("cartContent"); const cartSubtotal = document.getElementById("cartSubtotal"); if (!cartContent) return; if (this.cart.length === 0) { cartContent.innerHTML = '

Your cart is empty

'; if (cartSubtotal) cartSubtotal.textContent = "$0.00"; return; } const subtotal = this.cart.reduce( (sum, item) => sum + item.price * item.quantity, 0 ); cartContent.innerHTML = this.cart .map( (item) => `
${item.name}

${item.name}

$${item.price.toFixed(2)}

${item.quantity}

$${(item.price * item.quantity).toFixed( 2 )}

` ) .join(""); if (cartSubtotal) { cartSubtotal.textContent = `$${subtotal.toFixed(2)}`; } } // Render Wishlist renderWishlist() { const wishlistContent = document.getElementById("wishlistContent"); if (!wishlistContent) return; if (this.wishlist.length === 0) { wishlistContent.innerHTML = '

Your wishlist is empty

'; return; } wishlistContent.innerHTML = this.wishlist .map( (item) => `
${item.name}

${item.name}

$${item.price.toFixed(2)}

` ) .join(""); } // Show Notification showNotification(message, type = "info") { const notification = document.createElement("div"); notification.className = `notification notification-${type}`; notification.innerHTML = ` ${message} `; document.body.appendChild(notification); setTimeout(() => notification.classList.add("show"), 10); setTimeout(() => { notification.classList.remove("show"); setTimeout(() => notification.remove(), 300); }, 3000); } // Get Cart Total getCartTotal() { return this.cart.reduce((sum, item) => sum + item.price * item.quantity, 0); } // Get Cart Count getCartCount() { return this.cart.reduce((sum, item) => sum + item.quantity, 0); } // Clear Cart clearCart() { this.cart = []; this.saveToStorage("skyart_cart", this.cart); this.updateAllBadges(); this.renderCart(); } } // Initialize Shopping Manager const shoppingManager = new ShoppingManager(); // Make it globally available window.shoppingManager = shoppingManager; // Navigation active state document.addEventListener("DOMContentLoaded", () => { const currentPage = window.location.pathname.split("/").pop() || "home.html"; document.querySelectorAll(".nav-link, .mobile-link").forEach((link) => { const linkPage = link.getAttribute("href")?.split("/").pop(); if (linkPage === currentPage) { link.classList.add("active"); } }); });