import BaseMixin from '../_util/BaseMixin'
import { getOptionProps, initDefaultProps } from '../_util/props-util'
import getTransitionProps from '../_util/getTransitionProps'
import Icon from '../icon'
import Tooltip from '../tooltip'
import Progress from '../progress'
import classNames from 'classnames'
import { UploadListProps } from './interface'
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
const previewFile = (file, callback) => {
const reader = new window.FileReader()
reader.onloadend = () => callback(reader.result)
reader.readAsDataURL(file)
}
const isImageUrl = (url) => {
return /^data:image\//.test(url) || /\.(webp|svg|png|gif|jpg|jpeg)$/.test(url)
}
export default {
name: 'AUploadList',
mixins: [BaseMixin],
props: initDefaultProps(UploadListProps, {
listType: 'text', // or picture
progressAttr: {
strokeWidth: 2,
showInfo: false,
},
prefixCls: 'ant-upload',
showRemoveIcon: true,
showPreviewIcon: true,
}),
updated () {
this.$nextTick(() => {
if (this.listType !== 'picture' && this.listType !== 'picture-card') {
return
}
(this.items || []).forEach(file => {
if (typeof document === 'undefined' ||
typeof window === 'undefined' ||
!window.FileReader || !window.File ||
!(file.originFileObj instanceof window.File) ||
file.thumbUrl !== undefined) {
return
}
/*eslint-disable */
file.thumbUrl = '';
/*eslint -enable */
previewFile(file.originFileObj, (previewDataUrl) => {
/*eslint-disable */
file.thumbUrl = previewDataUrl;
/*eslint -enable todo */
// this.forceUpdate()
})
})
})
},
methods: {
handleClose (file) {
this.$emit('remove', file)
},
handlePreview (file, e) {
const { preview } = this.$listeners
if(!preview) {
return
}
e.preventDefault()
return this.$emit('preview', file)
},
},
render () {
const { prefixCls, items = [], listType, showPreviewIcon, showRemoveIcon, locale } = getOptionProps(this)
const list = items.map(file => {
let progress
let icon =