export class FilePreview { constructor(options = {}) { this.options = { containerId: options.containerId || 'filePreviewModal', onClose: options.onClose || (() => {}), ...options }; this.modal = null; this.init(); } init() { // Create modal if it doesn't exist if (!document.getElementById(this.options.containerId)) { const modalHtml = ` `; document.body.insertAdjacentHTML('beforeend', modalHtml); } this.modal = new bootstrap.Modal(document.getElementById(this.options.containerId)); // Add event listener for modal close document.getElementById(this.options.containerId).addEventListener('hidden.bs.modal', () => { this.options.onClose(); }); } getFileIcon(filename) { const extension = filename.split('.').pop().toLowerCase(); const iconMap = { pdf: 'fa-file-pdf', doc: 'fa-file-word', docx: 'fa-file-word', xls: 'fa-file-excel', xlsx: 'fa-file-excel', ppt: 'fa-file-powerpoint', pptx: 'fa-file-powerpoint', txt: 'fa-file-alt', jpg: 'fa-file-image', jpeg: 'fa-file-image', png: 'fa-file-image', gif: 'fa-file-image', zip: 'fa-file-archive', rar: 'fa-file-archive', mp3: 'fa-file-audio', mp4: 'fa-file-video' }; return iconMap[extension] || 'fa-file'; } async previewFile(file) { const contentDiv = document.getElementById(`${this.options.containerId}Content`); const extension = file.name.split('.').pop().toLowerCase(); // Show loading spinner contentDiv.innerHTML = `
Loading...
`; try { // Handle different file types if (['jpg', 'jpeg', 'png', 'gif'].includes(extension)) { // Image preview contentDiv.innerHTML = ` ${file.name} `; } else if (['pdf'].includes(extension)) { // PDF preview contentDiv.innerHTML = ` `; } else if (['mp4', 'webm'].includes(extension)) { // Video preview contentDiv.innerHTML = ` `; } else if (['mp3', 'wav'].includes(extension)) { // Audio preview contentDiv.innerHTML = ` `; } else { // Default preview for other file types contentDiv.innerHTML = `
${file.name}

Preview not available for this file type.

Download
`; } } catch (error) { console.error('Error previewing file:', error); contentDiv.innerHTML = `
Error Loading Preview

Unable to load file preview. Please try downloading the file instead.

Download
`; } // Show the modal this.modal.show(); } close() { this.modal.hide(); } }