# app/components/Admin/FileUpload/index.coffee # Добавление стилей компонента if globalThis.stylFns and globalThis.stylFns['app/components/Admin/FileUpload/index.styl'] styleElement = document.createElement('style') styleElement.type = 'text/css' styleElement.textContent = globalThis.stylFns['app/components/Admin/FileUpload/index.styl'] document.head.appendChild(styleElement) else log '⚠️ Стили FileUpload не найдены' module.exports = name: 'file-upload' props: accept: type: String default: '.csv,.xlsx,.xls' multiple: type: Boolean default: false maxSize: type: Number default: 10485760 # 10MB data: -> { isDragging: false selectedFiles: [] uploadProgress: 0 isUploading: false } methods: onDragOver: (event) -> event.preventDefault() @isDragging = true onDragLeave: (event) -> event.preventDefault() @isDragging = false onDrop: (event) -> event.preventDefault() @isDragging = false files = Array.from(event.dataTransfer.files) @handleFiles(files) onFileSelect: (event) -> files = Array.from(event.target.files) @handleFiles(files) handleFiles: (files) -> validFiles = files.filter (file) => if file.size > @maxSize @$emit('error', 'Файл '+file.name+' слишком большой. Максимальный размер: '+(@maxSize / 1048576)+'MB') return false if @accept and !@isFileTypeAccepted(file) @$emit('error', 'Файл '+file.name+' имеет недопустимый тип') return false return true if @multiple @selectedFiles = [...@selectedFiles, ...validFiles] else @selectedFiles = validFiles.slice(0, 1) @$emit('select', @selectedFiles) isFileTypeAccepted: (file) -> acceptTypes = @accept.split(',').map (type) -> type.trim() return acceptTypes.some (type) => if type.startsWith('.') return file.name.toLowerCase().endsWith(type.toLowerCase()) else return file.type.match(type) removeFile: (index) -> @selectedFiles.splice(index, 1) @$emit('select', @selectedFiles) uploadFiles: -> if @selectedFiles.length == 0 @$emit('error', 'Нет файлов для загрузки') return @isUploading = true @uploadProgress = 0 # Имитация загрузки с использованием Promise вместо async/await uploadSimulation = => interval = setInterval (=> @uploadProgress += 10 if @uploadProgress >= 100 clearInterval(interval) @isUploading = false @$emit('upload', @selectedFiles) @selectedFiles = [] ), 100 uploadSimulation() getFileIcon: (file) -> if file.type.includes('spreadsheet') or file.name.includes('.csv') return '📊' else if file.type.includes('image') return '🖼️' else return '📄' render: (new Function '_ctx', '_cache', globalThis.renderFns['app/components/Admin/FileUpload/index.pug'])()