// QR Code Generator Application class QRGenerator { constructor() { this.currentType = 'url'; this.currentQRUrl = null; this.init(); } init() { this.bindEvents(); this.updateSizeDisplay(); this.setActiveTab('url'); } bindEvents() { // Tab switching document.querySelectorAll('.tab-btn').forEach(btn => { btn.addEventListener('click', (e) => { const type = e.currentTarget.dataset.type; this.setActiveTab(type); }); }); // Size slider const sizeSlider = document.getElementById('qr-size'); sizeSlider.addEventListener('input', () => { this.updateSizeDisplay(); }); // Generate button document.getElementById('generate-btn').addEventListener('click', () => { this.generateQRCode(); }); // Download button document.getElementById('download-btn').addEventListener('click', () => { this.downloadQRCode(); }); // Real-time input changes this.bindInputEvents(); // Color changes - removed auto-generation // Users need to click "Generate QR Code" to apply color changes } bindInputEvents() { // Clear QR code when input changes, but don't auto-generate // URL input document.getElementById('url-input').addEventListener('input', () => { this.clearPreview(); }); // Text input document.getElementById('text-input').addEventListener('input', () => { this.clearPreview(); }); // Email inputs document.getElementById('email-input').addEventListener('input', () => { this.clearPreview(); }); document.getElementById('email-subject').addEventListener('input', () => { this.clearPreview(); }); document.getElementById('email-body').addEventListener('input', () => { this.clearPreview(); }); // Phone input document.getElementById('phone-input').addEventListener('input', () => { this.clearPreview(); }); // WiFi inputs document.getElementById('wifi-ssid').addEventListener('input', () => { this.clearPreview(); }); document.getElementById('wifi-password').addEventListener('input', () => { this.clearPreview(); }); document.getElementById('wifi-security').addEventListener('change', () => { this.clearPreview(); }); document.getElementById('wifi-hidden').addEventListener('change', () => { this.clearPreview(); }); // SMS inputs document.getElementById('sms-number').addEventListener('input', () => { this.clearPreview(); }); document.getElementById('sms-message').addEventListener('input', () => { this.clearPreview(); }); } setActiveTab(type) { this.currentType = type; // Update tab buttons document.querySelectorAll('.tab-btn').forEach(btn => { btn.classList.remove('active'); }); document.querySelector(`[data-type="${type}"]`).classList.add('active'); // Update content forms document.querySelectorAll('.content-form').forEach(form => { form.classList.remove('active'); }); document.getElementById(`${type}-form`).classList.add('active'); // Clear current QR code this.clearPreview(); } updateSizeDisplay() { const size = document.getElementById('qr-size').value; document.querySelector('.size-value').textContent = `${size}px`; } autoGenerate() { const content = this.getContentData(); if (content && content.trim()) { this.generateQRCode(); } else { this.clearPreview(); } } getContentData() { switch (this.currentType) { case 'url': return document.getElementById('url-input').value; case 'text': return document.getElementById('text-input').value; case 'email': const email = document.getElementById('email-input').value; const subject = document.getElementById('email-subject').value; const body = document.getElementById('email-body').value; if (!email) return ''; let mailto = `mailto:${email}`; const params = []; if (subject) params.push(`subject=${encodeURIComponent(subject)}`); if (body) params.push(`body=${encodeURIComponent(body)}`); if (params.length > 0) mailto += `?${params.join('&')}`; return mailto; case 'phone': const phone = document.getElementById('phone-input').value; return phone ? `tel:${phone}` : ''; case 'wifi': const ssid = document.getElementById('wifi-ssid').value; const password = document.getElementById('wifi-password').value; const security = document.getElementById('wifi-security').value; const hidden = document.getElementById('wifi-hidden').checked; if (!ssid) return ''; return `WIFI:T:${security};S:${ssid};P:${password};H:${hidden ? 'true' : 'false'};;`; case 'sms': const smsNumber = document.getElementById('sms-number').value; const smsMessage = document.getElementById('sms-message').value; if (!smsNumber) return ''; let sms = `sms:${smsNumber}`; if (smsMessage) sms += `?body=${encodeURIComponent(smsMessage)}`; return sms; default: return ''; } } async generateQRCode() { const content = this.getContentData(); if (!content || !content.trim()) { this.showToast('Please enter content to generate QR code', 'error'); return; } this.showLoading(true); try { const qrUrl = this.buildQRUrl(content); await this.displayQRCode(qrUrl); this.currentQRUrl = qrUrl; // Enable download button document.getElementById('download-btn').disabled = false; this.showToast('QR code generated successfully!', 'success'); } catch (error) { console.error('Error generating QR code:', error); this.showToast('Failed to generate QR code. Please try again.', 'error'); } finally { this.showLoading(false); } } buildQRUrl(content) { const size = document.getElementById('qr-size').value; const fgColor = document.getElementById('fg-color').value.replace('#', ''); const bgColor = document.getElementById('bg-color').value.replace('#', ''); const errorCorrection = document.getElementById('error-correction').value; const format = document.getElementById('output-format').value; const params = new URLSearchParams({ cht: 'qr', chs: `${size}x${size}`, chl: content, choe: 'UTF-8', chld: `${errorCorrection}|2`, icqrf: fgColor, icqrb: bgColor }); if (format === 'svg') { params.append('chof', '.svg'); } return `https://image-charts.com/chart?${params.toString()}`; } async displayQRCode(url) { return new Promise((resolve, reject) => { const img = new Image(); img.onload = () => { const preview = document.getElementById('qr-preview'); preview.innerHTML = ''; preview.appendChild(img); preview.classList.add('has-qr'); resolve(); }; img.onerror = () => { reject(new Error('Failed to load QR code image')); }; img.src = url; img.alt = 'Generated QR Code'; img.style.maxWidth = '100%'; img.style.height = 'auto'; }); } clearPreview() { const preview = document.getElementById('qr-preview'); preview.innerHTML = `
Enter content to generate QR code