2019-01-12 11:33:27 +08:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-01-19 16:58:38 +08:00
|
|
|
import { getOptionProps, initDefaultProps, getListeners } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
2019-03-28 19:51:27 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 19:45:13 +08:00
|
|
|
import { previewImage, isImageUrl } from './utils';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Icon from '../icon';
|
|
|
|
import Tooltip from '../tooltip';
|
|
|
|
import Progress from '../progress';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { UploadListProps } from './interface';
|
2018-04-13 16:19:50 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AUploadList',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: initDefaultProps(UploadListProps, {
|
|
|
|
listType: 'text', // or picture
|
|
|
|
progressAttr: {
|
|
|
|
strokeWidth: 2,
|
|
|
|
showInfo: false,
|
|
|
|
},
|
|
|
|
showRemoveIcon: true,
|
2020-03-07 19:45:13 +08:00
|
|
|
showDownloadIcon: false,
|
2018-04-13 16:19:50 +08:00
|
|
|
showPreviewIcon: true,
|
2020-03-07 19:45:13 +08:00
|
|
|
previewFile: previewImage,
|
2018-04-13 16:19:50 +08:00
|
|
|
}),
|
2019-03-28 19:51:27 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-28 19:51:27 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-04-13 16:19:50 +08:00
|
|
|
this.$nextTick(() => {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { listType, items, previewFile } = this.$props;
|
|
|
|
if (listType !== 'picture' && listType !== 'picture-card') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-04-13 16:19:50 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
(items || []).forEach(file => {
|
2019-01-12 11:33:27 +08:00
|
|
|
if (
|
|
|
|
typeof document === 'undefined' ||
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
!window.FileReader ||
|
|
|
|
!window.File ||
|
2020-03-07 19:45:13 +08:00
|
|
|
!(file.originFileObj instanceof File || file.originFileObj instanceof Blob) ||
|
2019-01-12 11:33:27 +08:00
|
|
|
file.thumbUrl !== undefined
|
|
|
|
) {
|
|
|
|
return;
|
2018-04-13 16:19:50 +08:00
|
|
|
}
|
|
|
|
/*eslint-disable */
|
|
|
|
file.thumbUrl = '';
|
2020-03-07 19:45:13 +08:00
|
|
|
if (previewFile) {
|
|
|
|
previewFile(file.originFileObj).then(previewDataUrl => {
|
|
|
|
// Need append '' to avoid dead loop
|
|
|
|
file.thumbUrl = previewDataUrl || '';
|
|
|
|
this.$forceUpdate();
|
|
|
|
});
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
});
|
2018-04-13 16:19:50 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handlePreview(file, e) {
|
2020-01-19 16:58:38 +08:00
|
|
|
const { preview } = getListeners(this);
|
2019-01-12 11:33:27 +08:00
|
|
|
if (!preview) {
|
|
|
|
return;
|
2018-07-03 10:22:03 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
e.preventDefault();
|
|
|
|
return this.$emit('preview', file);
|
2018-04-13 16:19:50 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
handleDownload(file) {
|
|
|
|
const { download } = getListeners(this);
|
|
|
|
if (typeof download === 'function') {
|
|
|
|
download(file);
|
|
|
|
} else if (file.url) {
|
|
|
|
window.open(file.url);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleClose(file) {
|
|
|
|
this.$emit('remove', file);
|
|
|
|
},
|
2018-04-13 16:19:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
const {
|
2019-03-28 19:51:27 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2019-01-12 11:33:27 +08:00
|
|
|
items = [],
|
|
|
|
listType,
|
|
|
|
showPreviewIcon,
|
|
|
|
showRemoveIcon,
|
2020-03-07 19:45:13 +08:00
|
|
|
showDownloadIcon,
|
2019-01-12 11:33:27 +08:00
|
|
|
locale,
|
2020-03-07 19:45:13 +08:00
|
|
|
progressAttr,
|
2019-01-12 11:33:27 +08:00
|
|
|
} = getOptionProps(this);
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-28 19:51:27 +08:00
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
|
|
|
|
2018-04-13 16:19:50 +08:00
|
|
|
const list = items.map(file => {
|
2019-01-12 11:33:27 +08:00
|
|
|
let progress;
|
|
|
|
let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />;
|
2018-04-13 16:19:50 +08:00
|
|
|
|
|
|
|
if (listType === 'picture' || listType === 'picture-card') {
|
|
|
|
if (listType === 'picture-card' && file.status === 'uploading') {
|
2019-01-12 11:33:27 +08:00
|
|
|
icon = <div class={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
|
2018-04-13 16:19:50 +08:00
|
|
|
} else if (!file.thumbUrl && !file.url) {
|
2019-01-12 11:33:27 +08:00
|
|
|
icon = <Icon class={`${prefixCls}-list-item-thumbnail`} type="picture" theme="twoTone" />;
|
2018-04-13 16:19:50 +08:00
|
|
|
} else {
|
2018-09-05 21:28:54 +08:00
|
|
|
const thumbnail = isImageUrl(file) ? (
|
2020-03-07 19:45:13 +08:00
|
|
|
<img
|
|
|
|
src={file.thumbUrl || file.url}
|
|
|
|
alt={file.name}
|
|
|
|
class={`${prefixCls}-list-item-image`}
|
|
|
|
/>
|
2018-04-13 16:19:50 +08:00
|
|
|
) : (
|
2018-12-19 21:43:09 +08:00
|
|
|
<Icon type="file" class={`${prefixCls}-list-item-icon`} theme="twoTone" />
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-13 16:19:50 +08:00
|
|
|
icon = (
|
|
|
|
<a
|
|
|
|
class={`${prefixCls}-list-item-thumbnail`}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
href={file.url || file.thumbUrl}
|
2019-01-12 11:33:27 +08:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2018-04-13 16:19:50 +08:00
|
|
|
>
|
|
|
|
{thumbnail}
|
|
|
|
</a>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-13 16:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.status === 'uploading') {
|
|
|
|
const progressProps = {
|
|
|
|
props: {
|
2020-03-07 19:45:13 +08:00
|
|
|
...progressAttr,
|
2018-04-13 16:19:50 +08:00
|
|
|
type: 'line',
|
|
|
|
percent: file.percent,
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
};
|
2018-04-13 16:19:50 +08:00
|
|
|
// show loading icon if upload progress listener is disabled
|
2019-01-12 11:33:27 +08:00
|
|
|
const loadingProgress = 'percent' in file ? <Progress {...progressProps} /> : null;
|
2018-04-13 16:19:50 +08:00
|
|
|
|
|
|
|
progress = (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={`${prefixCls}-list-item-progress`} key="progress">
|
2018-04-13 16:19:50 +08:00
|
|
|
{loadingProgress}
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-13 16:19:50 +08:00
|
|
|
}
|
|
|
|
const infoUploadingClass = classNames({
|
|
|
|
[`${prefixCls}-list-item`]: true,
|
|
|
|
[`${prefixCls}-list-item-${file.status}`]: true,
|
2020-03-07 19:45:13 +08:00
|
|
|
[`${prefixCls}-list-item-list-type-${listType}`]: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
const linkProps =
|
|
|
|
typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
const removeIcon = showRemoveIcon ? (
|
|
|
|
<Icon type="delete" title={locale.removeFile} onClick={() => this.handleClose(file)} />
|
|
|
|
) : null;
|
|
|
|
const downloadIcon =
|
|
|
|
showDownloadIcon && file.status === 'done' ? (
|
|
|
|
<Icon
|
|
|
|
type="download"
|
|
|
|
title={locale.downloadFile}
|
|
|
|
onClick={() => this.handleDownload(file)}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
const downloadOrDelete = listType !== 'picture-card' && (
|
2018-04-13 16:19:50 +08:00
|
|
|
<span
|
2020-03-07 19:45:13 +08:00
|
|
|
key="download-delete"
|
|
|
|
class={`${prefixCls}-list-item-card-actions ${listType === 'picture' ? 'picture' : ''}`}
|
2018-04-13 16:19:50 +08:00
|
|
|
>
|
2020-03-07 19:45:13 +08:00
|
|
|
{downloadIcon && <a title={locale.downloadFile}>{downloadIcon}</a>}
|
|
|
|
{removeIcon && <a title={locale.removeFile}>{removeIcon}</a>}
|
2018-04-13 16:19:50 +08:00
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2020-03-07 19:45:13 +08:00
|
|
|
const listItemNameClass = classNames({
|
|
|
|
[`${prefixCls}-list-item-name`]: true,
|
|
|
|
[`${prefixCls}-list-item-name-icon-count-${
|
|
|
|
[downloadIcon, removeIcon].filter(x => x).length
|
|
|
|
}`]: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const preview = file.url
|
|
|
|
? [
|
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
class={listItemNameClass}
|
|
|
|
title={file.name}
|
|
|
|
{...linkProps}
|
|
|
|
href={file.url}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</a>,
|
|
|
|
downloadOrDelete,
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
<span
|
|
|
|
key="view"
|
|
|
|
class={`${prefixCls}-list-item-name`}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
title={file.name}
|
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</span>,
|
|
|
|
downloadOrDelete,
|
|
|
|
];
|
2019-01-12 11:33:27 +08:00
|
|
|
const style =
|
|
|
|
file.url || file.thumbUrl
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
pointerEvents: 'none',
|
|
|
|
opacity: 0.5,
|
|
|
|
};
|
2018-04-13 16:19:50 +08:00
|
|
|
const previewIcon = showPreviewIcon ? (
|
|
|
|
<a
|
|
|
|
href={file.url || file.thumbUrl}
|
2019-01-12 11:33:27 +08:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2018-04-13 16:19:50 +08:00
|
|
|
style={style}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
title={locale.previewFile}
|
|
|
|
>
|
2019-01-12 11:33:27 +08:00
|
|
|
<Icon type="eye-o" />
|
2018-04-13 16:19:50 +08:00
|
|
|
</a>
|
2019-01-12 11:33:27 +08:00
|
|
|
) : null;
|
2020-03-07 19:45:13 +08:00
|
|
|
const actions = listType === 'picture-card' && file.status !== 'uploading' && (
|
|
|
|
<span class={`${prefixCls}-list-item-actions`}>
|
|
|
|
{previewIcon}
|
|
|
|
{file.status === 'done' && downloadIcon}
|
|
|
|
{removeIcon}
|
|
|
|
</span>
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
let message;
|
2018-04-13 16:19:50 +08:00
|
|
|
if (file.response && typeof file.response === 'string') {
|
2019-01-12 11:33:27 +08:00
|
|
|
message = file.response;
|
2018-04-13 16:19:50 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
message = (file.error && file.error.statusText) || locale.uploadError;
|
2018-04-13 16:19:50 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
const iconAndPreview = (
|
|
|
|
<span>
|
|
|
|
{icon}
|
|
|
|
{preview}
|
|
|
|
</span>
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
const transitionProps = getTransitionProps('fade');
|
2020-03-07 19:45:13 +08:00
|
|
|
const dom = (
|
2018-04-13 16:19:50 +08:00
|
|
|
<div class={infoUploadingClass} key={file.uid}>
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={`${prefixCls}-list-item-info`}>{iconAndPreview}</div>
|
2018-04-13 16:19:50 +08:00
|
|
|
{actions}
|
2019-01-12 11:33:27 +08:00
|
|
|
<transition {...transitionProps}>{progress}</transition>
|
2018-04-13 16:19:50 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2020-03-07 19:45:13 +08:00
|
|
|
const listContainerNameClass = classNames({
|
|
|
|
[`${prefixCls}-list-picture-card-container`]: listType === 'picture-card',
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div key={file.uid} class={listContainerNameClass}>
|
|
|
|
{file.status === 'error' ? <Tooltip title={message}>{dom}</Tooltip> : <span>{dom}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-13 16:19:50 +08:00
|
|
|
const listClassNames = classNames({
|
|
|
|
[`${prefixCls}-list`]: true,
|
|
|
|
[`${prefixCls}-list-${listType}`]: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
const animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
|
|
|
|
const transitionGroupProps = getTransitionProps(`${prefixCls}-${animationDirection}`);
|
2018-04-13 16:19:50 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<transition-group {...transitionGroupProps} tag="div" class={listClassNames}>
|
2018-04-13 16:19:50 +08:00
|
|
|
{list}
|
|
|
|
</transition-group>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-13 16:19:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|