// Shared utility functions function formatNumber(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } document.addEventListener('DOMContentLoaded', function() { const fileUpload = document.getElementById('file-upload'); const fileList = document.getElementById('file-list'); const submitBtn = document.getElementById('submit-btn'); const dropZone = document.querySelector('.border-dashed'); let files = []; // Handle file selection fileUpload.addEventListener('change', function(e) { handleFiles(e.target.files); }); // Drag and drop functionality dropZone.addEventListener('dragover', function(e) { e.preventDefault(); this.classList.add('border-blue-400', 'bg-blue-50'); }); dropZone.addEventListener('dragleave', function() { this.classList.remove('border-blue-400', 'bg-blue-50'); }); dropZone.addEventListener('drop', function(e) { e.preventDefault(); this.classList.remove('border-blue-400', 'bg-blue-50'); handleFiles(e.dataTransfer.files); }); // Submit button submitBtn.addEventListener('click', function() { if (files.length === 0) { alert('Please select at least one file to upload.'); return; } // Here you would typically send files to server alert(`Successfully uploaded ${files.length} file(s)!`); files = []; fileList.innerHTML = ''; }); function handleFiles(newFiles) { for (let i = 0; i < newFiles.length; i++) { files.push(newFiles[i]); createFileItem(newFiles[i]); } } function createFileItem(file) { const fileItem = document.createElement('div'); fileItem.className = 'file-item bg-gray-50 rounded-lg p-4 flex justify-between items-center'; const fileInfo = document.createElement('div'); fileInfo.className = 'flex items-center'; const fileIcon = document.createElement('i'); fileIcon.setAttribute('data-feather', getFileIcon(file.type)); fileIcon.className = 'w-5 h-5 text-gray-500 mr-3'; const fileName = document.createElement('span'); fileName.className = 'text-gray-700'; fileName.textContent = file.name; const fileSize = document.createElement('span'); fileSize.className = 'text-gray-500 text-sm ml-2'; fileSize.textContent = formatFileSize(file.size); const fileActions = document.createElement('div'); fileActions.className = 'file-actions flex items-center'; const removeBtn = document.createElement('button'); removeBtn.className = 'text-red-500 hover:text-red-700 transition-colors duration-300'; removeBtn.innerHTML = ''; removeBtn.addEventListener('click', function() { files = files.filter(f => f !== file); fileItem.remove(); }); fileInfo.appendChild(fileIcon); fileInfo.appendChild(fileName); fileInfo.appendChild(fileSize); fileActions.appendChild(removeBtn); fileItem.appendChild(fileInfo); fileItem.appendChild(fileActions); fileList.appendChild(fileItem); feather.replace(); } function getFileIcon(type) { if (type.match('image.*')) return 'image'; if (type.match('video.*')) return 'film'; if (type.match('audio.*')) return 'music'; if (type.match('application/pdf')) return 'file-text'; if (type.match('application/msword') || type.match('application/vnd.openxmlformats-officedocument.wordprocessingml.document')) return 'file-text'; if (type.match('application/vnd.ms-excel') || type.match('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) return 'file-text'; if (type.match('application/vnd.ms-powerpoint') || type.match('application/vnd.openxmlformats-officedocument.presentationml.presentation')) return 'file-text'; if (type.match('application/zip') || type.match('application/x-rar-compressed')) return 'archive'; return 'file'; } function formatFileSize(bytes) { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } });