droneslibres-ai / components /customizer.js
Dabelu's picture
I want to make a website to sell FPV Drones both for civilian, comercial, agricultural and military use. I want sections where customers can choose their own parts for maximum customization. Must be made in Spanish.
6fd26a4 verified
Raw History Blame Contribute Delete
17.9 kB
class DroneCustomizer extends HTMLElement {
constructor() {
super();
this.selectedParts = {
frame: null,
motor: null,
controller: null,
camera: null,
battery: null,
propeller: null
};
this.totalPrice = 0;
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
this.addEventListeners();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
width: 100%;
font-family: 'Inter', sans-serif;
}
.customizer-container {
background: white;
border-radius: 1.5rem;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.component-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
.component-card {
border: 2px solid #e5e7eb;
border-radius: 1rem;
padding: 1.5rem;
cursor: pointer;
transition: all 0.3s ease;
background: white;
}
.component-card:hover {
transform: translateY(-4px);
box-shadow: 0 10px 30px rgba(59, 130, 246, 0.15);
}
.component-card.selected {
border-color: #3b82f6;
background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
}
.component-image {
height: 160px;
border-radius: 0.75rem;
overflow: hidden;
margin-bottom: 1rem;
background: #f8fafc;
}
.price-tag {
background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
color: white;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-weight: 600;
font-size: 0.875rem;
}
.specs-list {
list-style: none;
padding: 0;
}
.specs-list li {
display: flex;
justify-content: space-between;
padding: 0.25rem 0;
border-bottom: 1px solid #f1f5f9;
}
.summary-card {
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
border-radius: 1rem;
padding: 2rem;
position: sticky;
top: 2rem;
}
.total-price {
font-size: 2.5rem;
font-weight: 800;
background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.add-to-cart-btn {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
color: white;
padding: 1rem 2rem;
border-radius: 0.75rem;
font-weight: 600;
transition: all 0.3s ease;
border: none;
width: 100%;
cursor: pointer;
}
.add-to-cart-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(16, 185, 129, 0.3);
}
.add-to-cart-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.component-category {
font-size: 1.5rem;
font-weight: 700;
color: #1e293b;
margin-bottom: 1.5rem;
padding-bottom: 0.5rem;
border-bottom: 3px solid #3b82f6;
display: inline-block;
}
@media (max-width: 768px) {
.component-grid {
grid-template-columns: 1fr;
}
}
</style>
<div class="customizer-container">
<div class="grid lg:grid-cols-3 gap-8 p-8">
<!-- Component Selector -->
<div class="lg:col-span-2 space-y-12">
<!-- Frame -->
<div>
<h3 class="component-category">
<i data-feather="box" class="inline mr-2"></i> Frame
</h3>
<div class="component-grid">
${this.renderComponentCards('frame', [
{ id: 'frame-1', name: 'X-Light Carbon', price: 299, specs: ['5" size', 'Carbon fiber', '120g', '3K weave'], img: 'http://static.photos/technology/320x240/501' },
{ id: 'frame-2', name: 'Titan Armor', price: 459, specs: ['6" size', 'Armored', '210g', 'Crash resistant'], img: 'http://static.photos/technology/320x240/502' },
{ id: 'frame-3', name: 'Stealth Pro', price: 699, specs: ['7" size', 'Military grade', '180g', 'Low radar profile'], img: 'http://static.photos/technology/320x240/503' }
])}
</div>
</div>
<!-- Motors -->
<div>
<h3 class="component-category">
<i data-feather="zap" class="inline mr-2"></i> Motores
</h3>
<div class="component-grid">
${this.renderComponentCards('motor', [
{ id: 'motor-1', name: 'Vortex 2207', price: 189, specs: ['2450KV', '4S-6S', '35A max', 'Neodymium'], img: 'http://static.photos/industry/320x240/511' },
{ id: 'motor-2', name: 'Titan 2806', price: 289, specs: ['1750KV', '6S-8S', '45A max', 'Industrial'], img: 'http://static.photos/industry/320x240/512' },
{ id: 'motor-3', name: 'Cyclone 2306', price: 239, specs: ['1950KV', '4S-6S', '40A max', 'High torque'], img: 'http://static.photos/industry/320x240/513' }
])}
</div>
</div>
<!-- Flight Controller -->
<div>
<h3 class="component-category">
<i data-feather="cpu" class="inline mr-2"></i> Controlador de Vuelo
</h3>
<div class="component-grid">
${this.renderComponentCards('controller', [
{ id: 'fc-1', name: 'Betaflight F7', price: 129, specs: ['F7 processor', '8 PWM', 'Blackbox', 'OSD'], img: 'http://static.photos/technology/320x240/521' },
{ id: 'fc-2', name: 'iNAV Pro', price: 199, specs: ['Triple IMU', 'GPS ready', 'Autonomous', 'Return home'], img: 'http://static.photos/technology/320x240/522' },
{ id: 'fc-3', name: 'Ardupilot Military', price: 599, specs: ['Encrypted', 'Waypoints', 'Thermal cam', 'Secure comms'], img: 'http://static.photos/technology/320x240/523' }
])}
</div>
</div>
<!-- Camera -->
<div>
<h3 class="component-category">
<i data-feather="video" class="inline mr-2"></i> Cámara
</h3>
<div class="component-grid">
${this.renderComponentCards('camera', [
{ id: 'cam-1', name: 'Foxeer 4K', price: 159, specs: ['4K 60fps', 'Superview', 'Gyro flow', 'Low light'], img: 'http://static.photos/technology/320x240/531' },
{ id: 'cam-2', name: 'DJI O3 Air', price: 239, specs: ['4K 120fps', '10-bit', '50km range', 'HD low latency'], img: 'http://static.photos/technology/320x240/532' },
{ id: 'cam-3', name: 'Thermal FLIR', price: 1299, specs: ['Thermal 640x512', 'Zoom 30x', 'Night vision', 'Military spec'], img: 'http://static.photos/technology/320x240/533' }
])}
</div>
</div>
<!-- Battery -->
<div>
<h3 class="component-category">
<i data-feather="battery" class="inline mr-2"></i> Batería
</h3>
<div class="component-grid">
${this.renderComponentCards('battery', [
{ id: 'bat-1', name: 'CNHL 6S 1300mAh', price: 89, specs: ['1300mAh', '6S 100C', 'XT60', 'Lightweight'], img: 'http://static.photos/technology/320x240/541' },
{ id: 'bat-2', name: 'Tattu 6S 1800mAh', price: 129, specs: ['1800mAh', '6S 120C', 'XT90', 'Long flight'], img: 'http://static.photos/technology/320x240/542' },
{ id: 'bat-3', name: 'Military 8S 2500mAh', price: 299, specs: ['2500mAh', '8S 150C', 'Anti-EMP', 'Wide temp'], img: 'http://static.photos/technology/320x240/543' }
])}
</div>
</div>
<!-- Propellers -->
<div>
<h3 class="component-category">
<i data-feather="wind" class="inline mr-2"></i> Hélices
</h3>
<div class="component-grid">
${this.renderComponentCards('propeller', [
{ id: 'prop-1', name: 'Gemfan 51466', price: 29, specs: ['3-blade', '5.1" pitch', '66mm', 'Durable'], img: 'http://static.photos/abstract/320x240/551' },
{ id: 'prop-2', name: 'HQProp 6040', price: 39, specs: ['4-blade', '6" pitch', '40mm', 'Efficient'], img: 'http://static.photos/abstract/320x240/552' },
{ id: 'prop-3', name: 'Stealth Whisper', price: 59, specs: ['5-blade', 'Silent', 'Carbon', 'Military'], img: 'http://static.photos/abstract/320x240/553' }
])}
</div>
</div>
</div>
<!-- Summary Sidebar -->
<div class="summary-card h-fit">
<h3 class="text-2xl font-bold mb-6">Tu Configuración</h3>
<div class="space-y-4 mb-8">
${Object.entries(this.selectedParts).map(([category, part]) => `
<div class="flex justify-between items-center p-3 bg-white rounded-lg">
<span class="font-medium capitalize">${category}</span>
<span class="font-semibold">${part ? part.name : 'No seleccionado'}</span>
${part ? `<span class="font-bold text-primary">$${part.price}</span>` : ''}
</div>
`).join('')}
</div>
<div class="border-t pt-6">
<div class="flex justify-between items-center mb-2">
<span class="text-lg font-semibold">Subtotal:</span>
<span class="text-xl font-bold" id="subtotal">$0</span>
</div>
<div class="flex justify-between items-center mb-2">
<span class="text-lg font-semibold">Envío:</span>
<span class="text-xl font-bold">$99</span>
</div>
<div class="flex justify-between items-center mb-6 border-t pt-4">
<span class="text-2xl font-extrabold">Total:</span>
<span class="total-price" id="total-price">$0</span>
</div>
<button id="add-custom-to-cart" class="add-to-cart-btn" disabled>
<i data-feather="shopping-cart" class="inline mr-2"></i>
Agregar al Carro
</button>
<p class="text-sm text-gray-500 mt-4 text-center">
<i data-feather="shield" class="inline w-4 h-4 mr-1"></i>
2 años de garantía incluida
</p>
</div>
</div>
</div>
</div>
`;
if (typeof feather !== 'undefined') {
feather.replace();
}
this.updateSummary();
}
renderComponentCards(category, components) {
return components.map(component => `
<div class="component-card ${this.selectedParts[category]?.id === component.id ? 'selected' : ''}"
data-category="${category}"
data-id="${component.id}"
data-name="${component.name}"
data-price="${component.price}">
<div class="component-image">
<img src="${component.img}" alt="${component.name}" class="w-full h-full object-cover">
</div>
<div class="flex justify-between items-start mb-3">
<h4 class="font-bold text-lg">${component.name}</h4>
<span class="price-tag">$${component.price}</span>
</div>
<ul class="specs-list text-sm text-gray-600">
${component.specs.map(spec => `<li><span>${spec}</span></li>`).join('')}
</ul>
</div>
`).join('');
}
addEventListeners() {
this.shadowRoot.addEventListener('click', (e) => {
const card = e.target.closest('.component-card');
if (card) {
const category = card.dataset.category;
const id = card.dataset.id;
const name = card.dataset.name;
const price = parseInt(card.dataset.price);
this.selectComponent(category, { id, name, price });
this.updateSummary();
}
if (e.target.id === 'add-custom-to-cart' || e.target.closest('#add-custom-to-cart')) {
this.addCustomBuildToCart();
}
});
}
selectComponent(category, component) {
this.selectedParts[category] = component;
// Update UI
const cards = this.shadowRoot.querySelectorAll(`[data-category="${category}"]`);
cards.forEach(card => {
card.classList.remove('selected');
if (card.dataset.id === component.id) {
card.classList.add('selected');
}
});
}
updateSummary() {
// Calculate subtotal
this.totalPrice = Object.values(this.selectedParts)
.filter(part => part !== null)
.reduce((total, part) => total + part.price, 0);
const subtotalEl = this.shadowRoot.getElementById('subtotal');
const totalEl = this.shadowRoot.getElementById('total-price');
const cartBtn = this.shadowRoot.getElementById('add-custom-to-cart');
if (subtotalEl) subtotalEl.textContent = `$${this.totalPrice}`;
if (totalEl) totalEl.textContent = `$${this.totalPrice + 99}`;
// Enable/disable cart button
const allSelected = Object.values(this.selectedParts).every(part => part !== null);
if (cartBtn) {
cartBtn.disabled = !allSelected;
}
}
addCustomBuildToCart() {
if (!Object.values(this.selectedParts).every(part => part !== null)) {
alert('Por favor selecciona todos los componentes antes de agregar al carro.');
return;
}
// Generate a unique build name
const buildName = `Drone Personalizado: ${this.selectedParts.frame.name} + ${this.selectedParts.motor.name}`;
const totalPrice = this.totalPrice + 99;
// Add to cart
const event = new CustomEvent('add-custom-build', {
detail: {
id: `custom-${Date.now()}`,
name: buildName,
price: totalPrice,
parts: this.selectedParts
}
});
document.dispatchEvent(event);
// Show success message
alert(`✓ ${buildName} agregado al carro por $${totalPrice}`);
}
}
customElements.define('drone-customizer', DroneCustomizer);