Download script.js from derhuehnerbauer24/techgenie-bazaar: direct link, hf CLI and curl.
- Browser
- Download file 9.76 kB
-
https://huggingface.co/spaces/derhuehnerbauer24/techgenie-bazaar/resolve/main/script.js
- Command line
-
hf download hf://spaces/derhuehnerbauer24/techgenie-bazaar/script.js
-
curl -L -o script.js https://huggingface.co/spaces/derhuehnerbauer24/techgenie-bazaar/resolve/main/script.js
9.76 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize cart functionality | |
| initializeCart(); | |
| // Smooth scroll for anchor links | |
| document.querySelectorAll('a[href^="#"]').forEach(anchor => { | |
| anchor.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| const targetId = this.getAttribute('href'); | |
| if (targetId === '#') return; | |
| const targetElement = document.querySelector(targetId); | |
| if (targetElement) { | |
| targetElement.scrollIntoView({ | |
| behavior: 'smooth' | |
| }); | |
| } | |
| }); | |
| }); | |
| // Newsletter form submission | |
| const newsletterForm = document.querySelector('section.bg-gradient-to-r.from-gray-900.to-gray-800'); | |
| if (newsletterForm) { | |
| const input = newsletterForm.querySelector('input[type="email"]'); | |
| const button = newsletterForm.querySelector('button[type="submit"]'); | |
| if (button && input) { | |
| button.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| if (input.value && input.value.includes('@')) { | |
| alert('Vielen Dank für Ihre Anmeldung zum Newsletter!'); | |
| input.value = ''; | |
| } else { | |
| alert('Bitte geben Sie eine gültige E-Mail-Adresse ein.'); | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| function initializeCart() { | |
| // Initialize cart count from localStorage | |
| if (!localStorage.getItem('cartItems')) { | |
| localStorage.setItem('cartItems', JSON.stringify([])); | |
| } | |
| if (!localStorage.getItem('cartCount')) { | |
| localStorage.setItem('cartCount', '0'); | |
| } | |
| // Add to cart buttons | |
| document.addEventListener('click', function(e) { | |
| if (e.target.classList.contains('add-to-cart-btn') || e.target.closest('.add-to-cart-btn')) { | |
| e.preventDefault(); | |
| const button = e.target.classList.contains('add-to-cart-btn') ? e.target : e.target.closest('.add-to-cart-btn'); | |
| const product = button.getAttribute('data-product'); | |
| const price = parseFloat(button.getAttribute('data-price')); | |
| const image = button.getAttribute('data-image'); | |
| addToCart(product, price, image); | |
| } | |
| // Buy now buttons | |
| if (e.target.classList.contains('buy-now-btn') || e.target.closest('.buy-now-btn')) { | |
| e.preventDefault(); | |
| const button = e.target.classList.contains('buy-now-btn') ? e.target : e.target.closest('.buy-now-btn'); | |
| const product = button.getAttribute('data-product'); | |
| const price = parseFloat(button.getAttribute('data-price')); | |
| const image = button.getAttribute('data-image') || 'http://static.photos/technology/640x360/1'; | |
| addToCart(product, price, image); | |
| // Open cart after adding | |
| const cart = document.querySelector('custom-cart'); | |
| if (cart) cart.openCart(); | |
| } | |
| }); | |
| // Update cart count display | |
| updateCartCountDisplay(); | |
| } | |
| function addToCart(product, price, image) { | |
| let cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; | |
| let existingItem = cartItems.find(item => item.product === product); | |
| if (existingItem) { | |
| existingItem.quantity += 1; | |
| } else { | |
| cartItems.push({ | |
| product: product, | |
| price: price, | |
| image: image, | |
| quantity: 1 | |
| }); | |
| } | |
| localStorage.setItem('cartItems', JSON.stringify(cartItems)); | |
| // Update cart count | |
| let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0); | |
| localStorage.setItem('cartCount', totalCount.toString()); | |
| // Update cart count display | |
| updateCartCountDisplay(); | |
| // Trigger event for navbar to update | |
| window.dispatchEvent(new Event('cartUpdated')); | |
| // Update cart UI if open | |
| updateCartUI(); | |
| // Show confirmation | |
| showNotification(`${product} wurde zum Warenkorb hinzugefügt!`); | |
| } | |
| function updateCartCountDisplay() { | |
| const count = localStorage.getItem('cartCount') || '0'; | |
| // Update in navbar shadow DOM | |
| const navbar = document.querySelector('custom-navbar'); | |
| if (navbar) { | |
| const cartCountElement = navbar.shadowRoot.getElementById('cart-count'); | |
| const mobileCartCountElement = navbar.shadowRoot.getElementById('mobile-cart-count'); | |
| if (cartCountElement) cartCountElement.textContent = count; | |
| if (mobileCartCountElement) mobileCartCountElement.textContent = count; | |
| } | |
| } | |
| function updateCartUI() { | |
| const cart = document.querySelector('custom-cart'); | |
| if (!cart) return; | |
| const cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; | |
| const cartItemsContainer = cart.shadowRoot.querySelector('.cart-items'); | |
| const subtotalElement = cart.shadowRoot.querySelector('.subtotal span:last-child'); | |
| if (!cartItemsContainer) return; | |
| // Clear existing items | |
| cartItemsContainer.innerHTML = ''; | |
| let subtotal = 0; | |
| cartItems.forEach((item, index) => { | |
| const itemTotal = item.price * item.quantity; | |
| subtotal += itemTotal; | |
| const itemElement = document.createElement('div'); | |
| itemElement.className = 'cart-item'; | |
| itemElement.innerHTML = ` | |
| <img src="${item.image}" alt="${item.product}" class="cart-item-img"> | |
| <div class="cart-item-details"> | |
| <div class="cart-item-title">${item.product}</div> | |
| <div class="cart-item-price">€${item.price.toFixed(2)}</div> | |
| <div class="quantity-selector"> | |
| <button class="quantity-btn minus" data-index="${index}">−</button> | |
| <input type="number" value="${item.quantity}" min="1" class="quantity-input" data-index="${index}"> | |
| <button class="quantity-btn plus" data-index="${index}">+</button> | |
| </div> | |
| <div class="delete-btn" data-index="${index}">Löschen</div> | |
| </div> | |
| `; | |
| cartItemsContainer.appendChild(itemElement); | |
| }); | |
| if (subtotalElement) { | |
| subtotalElement.textContent = `€${subtotal.toFixed(2)}`; | |
| } | |
| // Add event listeners for quantity buttons | |
| cartItemsContainer.querySelectorAll('.quantity-btn').forEach(btn => { | |
| btn.addEventListener('click', function() { | |
| const index = parseInt(this.getAttribute('data-index')); | |
| updateCartItemQuantity(index, this.classList.contains('plus') ? 1 : -1); | |
| }); | |
| }); | |
| cartItemsContainer.querySelectorAll('.quantity-input').forEach(input => { | |
| input.addEventListener('change', function() { | |
| const index = parseInt(this.getAttribute('data-index')); | |
| const newQuantity = parseInt(this.value) || 1; | |
| updateCartItemQuantity(index, 0, newQuantity); | |
| }); | |
| }); | |
| cartItemsContainer.querySelectorAll('.delete-btn').forEach(btn => { | |
| btn.addEventListener('click', function() { | |
| const index = parseInt(this.getAttribute('data-index')); | |
| removeFromCart(index); | |
| }); | |
| }); | |
| } | |
| function updateCartItemQuantity(index, change, newQuantity = null) { | |
| let cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; | |
| if (index >= 0 && index < cartItems.length) { | |
| if (newQuantity !== null) { | |
| cartItems[index].quantity = Math.max(1, newQuantity); | |
| } else { | |
| cartItems[index].quantity = Math.max(1, cartItems[index].quantity + change); | |
| } | |
| localStorage.setItem('cartItems', JSON.stringify(cartItems)); | |
| // Update cart count | |
| let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0); | |
| localStorage.setItem('cartCount', totalCount.toString()); | |
| // Update displays | |
| updateCartCountDisplay(); | |
| updateCartUI(); | |
| window.dispatchEvent(new Event('cartUpdated')); | |
| } | |
| } | |
| function removeFromCart(index) { | |
| let cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; | |
| if (index >= 0 && index < cartItems.length) { | |
| cartItems.splice(index, 1); | |
| localStorage.setItem('cartItems', JSON.stringify(cartItems)); | |
| // Update cart count | |
| let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0); | |
| localStorage.setItem('cartCount', totalCount.toString()); | |
| // Update displays | |
| updateCartCountDisplay(); | |
| updateCartUI(); | |
| window.dispatchEvent(new Event('cartUpdated')); | |
| showNotification('Artikel wurde aus dem Warenkorb entfernt!'); | |
| } | |
| } | |
| function showNotification(message) { | |
| // Create notification element | |
| const notification = document.createElement('div'); | |
| notification.className = 'fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg z-50 transform transition-transform duration-300 translate-x-full'; | |
| notification.textContent = message; | |
| notification.id = 'cart-notification'; | |
| document.body.appendChild(notification); | |
| // Animate in | |
| setTimeout(() => { | |
| notification.classList.remove('translate-x-full'); | |
| notification.classList.add('translate-x-0'); | |
| }, 10); | |
| // Remove after 3 seconds | |
| setTimeout(() => { | |
| notification.classList.remove('translate-x-0'); | |
| notification.classList.add('translate-x-full'); | |
| setTimeout(() => { | |
| if (notification.parentNode) { | |
| notification.parentNode.removeChild(notification); | |
| } | |
| }, 300); | |
| }, 3000); | |
| } | |