Spaces:
Running
Running
Download script.js from Dabelu/droneslibres-ai: direct link, hf CLI and curl.
- Browser
- Download file 7.53 kB
-
https://huggingface.co/spaces/Dabelu/droneslibres-ai/resolve/main/script.js
- Command line
-
hf download hf://spaces/Dabelu/droneslibres-ai/script.js
-
curl -L -o script.js https://huggingface.co/spaces/Dabelu/droneslibres-ai/resolve/main/script.js
7.53 kB
| // Main JavaScript for DronesLibres.ai | |
| // Shopping Cart Logic | |
| class ShoppingCart { | |
| constructor() { | |
| this.items = JSON.parse(localStorage.getItem('droneCart')) || []; | |
| this.updateCartUI(); | |
| } | |
| addItem(id, name, price, quantity = 1) { | |
| const existingItem = this.items.find(item => item.id === id); | |
| if (existingItem) { | |
| existingItem.quantity += quantity; | |
| } else { | |
| this.items.push({ id, name, price, quantity }); | |
| } | |
| this.save(); | |
| this.updateCartUI(); | |
| this.showNotification(`✓ ${name} agregado al carro`); | |
| } | |
| removeItem(id) { | |
| this.items = this.items.filter(item => item.id !== id); | |
| this.save(); | |
| this.updateCartUI(); | |
| } | |
| updateQuantity(id, quantity) { | |
| const item = this.items.find(item => item.id === id); | |
| if (item) { | |
| item.quantity = quantity; | |
| if (item.quantity <= 0) { | |
| this.removeItem(id); | |
| } else { | |
| this.save(); | |
| this.updateCartUI(); | |
| } | |
| } | |
| } | |
| getTotal() { | |
| return this.items.reduce((total, item) => total + (item.price * item.quantity), 0); | |
| } | |
| getItemCount() { | |
| return this.items.reduce((count, item) => count + item.quantity, 0); | |
| } | |
| save() { | |
| localStorage.setItem('droneCart', JSON.stringify(this.items)); | |
| } | |
| updateCartUI() { | |
| const cartCount = document.getElementById('cart-count'); | |
| const cartTotal = document.getElementById('cart-total'); | |
| const cartItems = document.getElementById('cart-items'); | |
| if (cartCount) cartCount.textContent = this.getItemCount(); | |
| if (cartTotal) cartTotal.textContent = `$${this.getTotal().toLocaleString('es-ES')}`; | |
| // Update cart items in sidebar if exists | |
| if (cartItems && document.querySelector('drone-cart')) { | |
| cartItems.innerHTML = this.items.map(item => ` | |
| <div class="flex items-center justify-between p-4 border-b"> | |
| <div> | |
| <h4 class="font-semibold">${item.name}</h4> | |
| <p class="text-gray-600">$${item.price.toLocaleString('es-ES')} x ${item.quantity}</p> | |
| </div> | |
| <div class="flex items-center gap-2"> | |
| <button class="decrease-item px-2 py-1 bg-gray-200 rounded" data-id="${item.id}">-</button> | |
| <span>${item.quantity}</span> | |
| <button class="increase-item px-2 py-1 bg-gray-200 rounded" data-id="${item.id}">+</button> | |
| <button class="remove-item ml-4 text-red-500" data-id="${item.id}"> | |
| <i data-feather="trash-2" class="w-4 h-4"></i> | |
| </button> | |
| </div> | |
| </div> | |
| `).join(''); | |
| feather.replace({ 'data-feather': 'trash-2' }); | |
| } | |
| } | |
| 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 translate-x-0 transition-transform duration-300'; | |
| notification.textContent = message; | |
| document.body.appendChild(notification); | |
| // Remove after 3 seconds | |
| setTimeout(() => { | |
| notification.style.transform = 'translateX(100%)'; | |
| setTimeout(() => notification.remove(), 300); | |
| }, 3000); | |
| } | |
| } | |
| // Initialize cart | |
| const cart = new ShoppingCart(); | |
| // DOM Content Loaded | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Add to cart buttons | |
| document.addEventListener('click', function(e) { | |
| if (e.target.classList.contains('add-to-cart')) { | |
| const button = e.target; | |
| const id = button.dataset.id; | |
| const name = button.dataset.name; | |
| const price = parseInt(button.dataset.price); | |
| cart.addItem(id, name, price, 1); | |
| } | |
| // Cart sidebar interactions | |
| if (e.target.classList.contains('decrease-item') || e.target.closest('.decrease-item')) { | |
| const button = e.target.classList.contains('decrease-item') ? e.target : e.target.closest('.decrease-item'); | |
| const id = button.dataset.id; | |
| const item = cart.items.find(item => item.id === id); | |
| if (item) cart.updateQuantity(id, item.quantity - 1); | |
| } | |
| if (e.target.classList.contains('increase-item') || e.target.closest('.increase-item')) { | |
| const button = e.target.classList.contains('increase-item') ? e.target : e.target.closest('.increase-item'); | |
| const id = button.dataset.id; | |
| const item = cart.items.find(item => item.id === id); | |
| if (item) cart.updateQuantity(id, item.quantity + 1); | |
| } | |
| if (e.target.classList.contains('remove-item') || e.target.closest('.remove-item')) { | |
| const button = e.target.classList.contains('remove-item') ? e.target : e.target.closest('.remove-item'); | |
| const id = button.dataset.id; | |
| cart.removeItem(id); | |
| } | |
| }); | |
| // Toggle cart sidebar | |
| document.addEventListener('click', function(e) { | |
| if (e.target.closest('#cart-toggle')) { | |
| const cartSidebar = document.querySelector('drone-cart'); | |
| if (cartSidebar) { | |
| const isHidden = cartSidebar.style.right === '-400px' || !cartSidebar.style.right; | |
| cartSidebar.style.right = isHidden ? '0' : '-400px'; | |
| // Update overlay | |
| const overlay = document.getElementById('cart-overlay'); | |
| if (overlay) { | |
| overlay.classList.toggle('hidden', !isHidden); | |
| overlay.classList.toggle('opacity-0', !isHidden); | |
| } | |
| } | |
| } | |
| }); | |
| // Close cart when clicking overlay | |
| document.addEventListener('click', function(e) { | |
| if (e.target.id === 'cart-overlay') { | |
| const cartSidebar = document.querySelector('drone-cart'); | |
| const overlay = document.getElementById('cart-overlay'); | |
| if (cartSidebar) cartSidebar.style.right = '-400px'; | |
| if (overlay) { | |
| overlay.classList.add('hidden', 'opacity-0'); | |
| } | |
| } | |
| }); | |
| // Initialize Feather Icons | |
| if (typeof feather !== 'undefined') { | |
| feather.replace(); | |
| } | |
| }); | |
| // API Integration for drone parts (example) | |
| async function fetchDroneParts(category) { | |
| try { | |
| // This would be replaced with actual API endpoint | |
| const response = await fetch(`https://api.example.com/drone-parts?category=${category}`); | |
| const data = await response.json(); | |
| return data; | |
| } catch (error) { | |
| console.error('Error fetching drone parts:', error); | |
| return []; | |
| } | |
| } | |
| // Form validation for customizer | |
| function validateCustomizerForm() { | |
| const frames = document.querySelectorAll('input[name="frame"]:checked'); | |
| const motors = document.querySelectorAll('input[name="motor"]:checked'); | |
| if (frames.length === 0) { | |
| alert('Por favor selecciona un frame.'); | |
| return false; | |
| } | |
| if (motors.length === 0) { | |
| alert('Por favor selecciona motores.'); | |
| return false; | |
| } | |
| return true; | |
| } |