| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- # Добавление стилей компонента
- if globalThis.stylFns and globalThis.stylFns['app/components/Admin/MediaUpload/index.styl']
- styleElement = document.createElement('style')
- styleElement.type = 'text/css'
- styleElement.textContent = globalThis.stylFns['app/components/Admin/MediaUpload/index.styl']
- document.head.appendChild(styleElement)
- else
- log '⚠️ Стили MediaUpload не найдены'
- module.exports =
- name: 'media-upload'
- props:
- currentImage:
- type: String
- default: ''
- previewAlt:
- type: String
- default: 'Превью изображения'
- maxSize:
- type: Number
- default: 10485760 # 10MB
- data: ->
- {
- isDragging: false
- selectedFile: null
- uploading: false
- uploadProgress: 0
- }
- 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)
- openFileDialog: ->
- @$refs.fileInput?.click()
- onFileSelect: (event) ->
- files = Array.from(event.target.files)
- @handleFiles(files)
- # Сброс input для возможности выбора того же файла снова
- event.target.value = ''
- handleFiles: (files) ->
- if files.length == 0
- return
- file = files[0]
-
- # Валидация типа файла
- if not file.type.startsWith('image/')
- @$emit('error', 'Выберите файл изображения')
- return
-
- # Валидация размера
- if file.size > @maxSize
- @$emit('error', 'Файл слишком большой. Максимальный размер: ' + (@maxSize / 1048576) + 'MB')
- return
-
- @selectedFile = file
-
- # Создание preview
- if URL and URL.createObjectURL
- previewUrl = URL.createObjectURL(file)
- @$emit('image-selected', previewUrl)
-
- # Начало загрузки
- @startUpload(file)
- startUpload: (file) ->
- @uploading = true
- @uploadProgress = 0
-
- # Имитация загрузки файла
- # В реальном приложении здесь будет загрузка на сервер
- interval = setInterval (=>
- @uploadProgress += 10
- if @uploadProgress >= 100
- clearInterval(interval)
- @uploading = false
- @$emit('upload-complete', {
- file: file
- url: URL.createObjectURL(file)
- name: file.name
- size: file.size
- type: file.type
- })
- ), 100
- removeImage: ->
- @selectedFile = null
- @uploadProgress = 0
- @$emit('image-removed')
-
- if @currentImage and URL and URL.revokeObjectURL
- URL.revokeObjectURL(@currentImage)
- formatFileSize: (bytes) ->
- if bytes == 0
- return '0 Bytes'
- k = 1024
- sizes = ['Bytes', 'KB', 'MB', 'GB']
- i = Math.floor(Math.log(bytes) / Math.log(k))
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
- beforeUnmount: ->
- # Очистка URL объектов при уничтожении компонента
- if @currentImage and URL and URL.revokeObjectURL
- URL.revokeObjectURL(@currentImage)
- render: (new Function '_ctx', '_cache', globalThis.renderFns['app/components/Admin/MediaUpload/index.pug'])()
|