2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-09-02 14:44:16 +00:00
|
|
|
import uniqBy from 'lodash-es/uniqBy';
|
|
|
|
import findIndex from 'lodash-es/findIndex';
|
2019-01-12 03:33:27 +00:00
|
|
|
import VcUpload from '../vc-upload';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-10-22 06:52:37 +00:00
|
|
|
import { getOptionProps, hasProp, getSlot } from '../_util/props-util';
|
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2019-01-12 03:33:27 +00:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../locale-provider/default';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Dragger from './Dragger';
|
|
|
|
import UploadList from './UploadList';
|
|
|
|
import { UploadProps } from './interface';
|
|
|
|
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
|
2020-10-22 06:52:37 +00:00
|
|
|
import { defineComponent, inject } from 'vue';
|
2020-08-27 08:03:42 +00:00
|
|
|
import { getDataAndAria } from '../vc-tree/src/util';
|
2018-04-13 08:19:50 +00:00
|
|
|
|
2020-10-22 06:52:37 +00:00
|
|
|
export default defineComponent({
|
2018-04-13 08:19:50 +00:00
|
|
|
name: 'AUpload',
|
|
|
|
mixins: [BaseMixin],
|
2019-02-01 09:23:00 +00:00
|
|
|
inheritAttrs: false,
|
2020-03-18 06:52:02 +00:00
|
|
|
Dragger,
|
2018-04-13 08:19:50 +00:00
|
|
|
props: initDefaultProps(UploadProps, {
|
|
|
|
type: 'select',
|
|
|
|
multiple: false,
|
|
|
|
action: '',
|
|
|
|
data: {},
|
|
|
|
accept: '',
|
|
|
|
beforeUpload: T,
|
|
|
|
showUploadList: true,
|
|
|
|
listType: 'text', // or pictrue
|
|
|
|
disabled: false,
|
|
|
|
supportServerRender: true,
|
|
|
|
}),
|
2020-06-25 13:23:39 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-10-22 06:52:37 +00:00
|
|
|
upload: null,
|
|
|
|
progressTimer: null,
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-06-25 13:23:39 +00:00
|
|
|
};
|
2019-03-28 11:51:27 +00:00
|
|
|
},
|
2018-04-13 08:19:50 +00:00
|
|
|
// recentUploadStatus: boolean | PromiseLike<any>;
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-04-13 08:19:50 +00:00
|
|
|
return {
|
|
|
|
sFileList: this.fileList || this.defaultFileList || [],
|
|
|
|
dragState: 'drop',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
fileList(val) {
|
2019-07-07 12:57:39 +00:00
|
|
|
this.sFileList = val || [];
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-02-01 09:23:00 +00:00
|
|
|
this.clearProgressTimer();
|
|
|
|
},
|
2018-04-13 08:19:50 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onStart(file) {
|
|
|
|
const targetItem = fileToObject(file);
|
|
|
|
targetItem.status = 'uploading';
|
|
|
|
const nextFileList = this.sFileList.concat();
|
2019-03-28 11:51:27 +00:00
|
|
|
const fileIndex = findIndex(nextFileList, ({ uid }) => uid === targetItem.uid);
|
2018-12-05 10:31:58 +00:00
|
|
|
if (fileIndex === -1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
nextFileList.push(targetItem);
|
2018-12-05 10:31:58 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
nextFileList[fileIndex] = targetItem;
|
2018-12-05 10:31:58 +00:00
|
|
|
}
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
file: targetItem,
|
|
|
|
fileList: nextFileList,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
// fix ie progress
|
2021-04-14 02:38:42 +00:00
|
|
|
if (!window.File || (typeof process === 'object' && process.env.TEST_IE)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.autoUpdateProgress(0, targetItem);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
onSuccess(response, file, xhr) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.clearProgressTimer();
|
2018-04-13 08:19:50 +00:00
|
|
|
try {
|
|
|
|
if (typeof response === 'string') {
|
2019-01-12 03:33:27 +00:00
|
|
|
response = JSON.parse(response);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
/* do nothing */
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const fileList = this.sFileList;
|
|
|
|
const targetItem = getFileItem(file, fileList);
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.status = 'done';
|
|
|
|
targetItem.response = response;
|
2020-03-07 11:45:13 +00:00
|
|
|
targetItem.xhr = xhr;
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onProgress(e, file) {
|
|
|
|
const fileList = this.sFileList;
|
|
|
|
const targetItem = getFileItem(file, fileList);
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.percent = e.percent;
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
event: e,
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList: this.sFileList,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onError(error, response, file) {
|
|
|
|
this.clearProgressTimer();
|
|
|
|
const fileList = this.sFileList;
|
|
|
|
const targetItem = getFileItem(file, fileList);
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-04-03 07:43:18 +00:00
|
|
|
onReject(fileList) {
|
|
|
|
this.$emit('reject', fileList);
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
handleRemove(file) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { remove: onRemove } = this;
|
|
|
|
const { sFileList: fileList } = this.$data;
|
2019-03-28 11:51:27 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
2018-04-13 08:19:50 +00:00
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const removedFileList = removeFileItem(file, fileList);
|
|
|
|
|
2018-04-13 08:19:50 +00:00
|
|
|
if (removedFileList) {
|
2020-03-07 11:45:13 +00:00
|
|
|
file.status = 'removed'; // eslint-disable-line
|
|
|
|
|
|
|
|
if (this.upload) {
|
|
|
|
this.upload.abort(file);
|
|
|
|
}
|
|
|
|
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
file,
|
|
|
|
fileList: removedFileList,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
handleManualRemove(file) {
|
2019-05-28 03:37:38 +00:00
|
|
|
if (this.$refs.uploadRef) {
|
2020-10-22 06:52:37 +00:00
|
|
|
(this.$refs.uploadRef as any).abort(file);
|
2019-03-28 11:51:27 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.handleRemove(file);
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2020-08-04 15:00:11 +00:00
|
|
|
handleChange(info) {
|
2018-04-13 08:19:50 +00:00
|
|
|
if (!hasProp(this, 'fileList')) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ sFileList: info.fileList });
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2020-08-04 15:00:11 +00:00
|
|
|
this.$emit('update:fileList', info.fileList);
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$emit('change', info);
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onFileDrop(e) {
|
2018-04-13 08:19:50 +00:00
|
|
|
this.setState({
|
|
|
|
dragState: e.type,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
reBeforeUpload(file, fileList) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { beforeUpload } = this.$props;
|
|
|
|
const { sFileList: stateFileList } = this.$data;
|
|
|
|
if (!beforeUpload) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return true;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
const result = beforeUpload(file, fileList);
|
2018-04-13 08:19:50 +00:00
|
|
|
if (result === false) {
|
2020-08-04 15:00:11 +00:00
|
|
|
this.handleChange({
|
2018-04-13 08:19:50 +00:00
|
|
|
file,
|
2020-03-07 11:45:13 +00:00
|
|
|
fileList: uniqBy(stateFileList.concat(fileList.map(fileToObject)), item => item.uid),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
return false;
|
2019-03-28 11:51:27 +00:00
|
|
|
}
|
|
|
|
if (result && result.then) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return result;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return true;
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
clearProgressTimer() {
|
|
|
|
clearInterval(this.progressTimer);
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
autoUpdateProgress(_, file) {
|
|
|
|
const getPercent = genPercentAdd();
|
|
|
|
let curPercent = 0;
|
|
|
|
this.clearProgressTimer();
|
|
|
|
this.progressTimer = setInterval(() => {
|
|
|
|
curPercent = getPercent(curPercent);
|
|
|
|
this.onProgress(
|
|
|
|
{
|
|
|
|
percent: curPercent * 100,
|
|
|
|
},
|
|
|
|
file,
|
|
|
|
);
|
|
|
|
}, 200);
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
renderUploadList(locale) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const {
|
|
|
|
showUploadList = {},
|
|
|
|
listType,
|
|
|
|
previewFile,
|
|
|
|
disabled,
|
|
|
|
locale: propLocale,
|
|
|
|
} = getOptionProps(this);
|
|
|
|
const { showRemoveIcon, showPreviewIcon, showDownloadIcon } = showUploadList;
|
|
|
|
const { sFileList: fileList } = this.$data;
|
2020-08-04 15:00:11 +00:00
|
|
|
const { onDownload, onPreview } = this.$props;
|
2018-04-13 08:19:50 +00:00
|
|
|
const uploadListProps = {
|
2020-06-25 13:23:39 +00:00
|
|
|
listType,
|
|
|
|
items: fileList,
|
|
|
|
previewFile,
|
|
|
|
showRemoveIcon: !disabled && showRemoveIcon,
|
|
|
|
showPreviewIcon,
|
|
|
|
showDownloadIcon,
|
|
|
|
locale: { ...locale, ...propLocale },
|
|
|
|
onRemove: this.handleManualRemove,
|
|
|
|
onDownload,
|
|
|
|
onPreview,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return <UploadList {...uploadListProps} />;
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-05-28 03:37:38 +00:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
showUploadList,
|
|
|
|
listType,
|
|
|
|
type,
|
|
|
|
disabled,
|
|
|
|
} = getOptionProps(this);
|
2020-03-07 11:45:13 +00:00
|
|
|
const { sFileList: fileList, dragState } = this.$data;
|
2020-06-25 14:21:23 +00:00
|
|
|
const { class: className, style } = this.$attrs;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-28 11:51:27 +00:00
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
2018-04-13 08:19:50 +00:00
|
|
|
|
|
|
|
const vcUploadProps = {
|
2020-06-25 13:23:39 +00:00
|
|
|
...this.$props,
|
|
|
|
prefixCls,
|
|
|
|
beforeUpload: this.reBeforeUpload,
|
|
|
|
onStart: this.onStart,
|
|
|
|
onError: this.onError,
|
|
|
|
onProgress: this.onProgress,
|
|
|
|
onSuccess: this.onSuccess,
|
|
|
|
onReject: this.onReject,
|
2018-04-13 08:19:50 +00:00
|
|
|
ref: 'uploadRef',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-13 08:19:50 +00:00
|
|
|
|
|
|
|
const uploadList = showUploadList ? (
|
|
|
|
<LocaleReceiver
|
2019-01-12 03:33:27 +00:00
|
|
|
componentName="Upload"
|
2018-04-13 08:19:50 +00:00
|
|
|
defaultLocale={defaultLocale.Upload}
|
2020-06-25 13:23:39 +00:00
|
|
|
children={this.renderUploadList}
|
2019-01-12 03:33:27 +00:00
|
|
|
/>
|
|
|
|
) : null;
|
2018-04-13 08:19:50 +00:00
|
|
|
|
2020-06-25 13:23:39 +00:00
|
|
|
const children = getSlot(this);
|
2018-04-13 08:19:50 +00:00
|
|
|
|
|
|
|
if (type === 'drag') {
|
|
|
|
const dragCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-drag`]: true,
|
2020-10-22 06:52:37 +00:00
|
|
|
[`${prefixCls}-drag-uploading`]: fileList.some((file: any) => file.status === 'uploading'),
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
2018-04-13 08:19:50 +00:00
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-13 08:19:50 +00:00
|
|
|
return (
|
2020-08-27 08:03:42 +00:00
|
|
|
<span class={className} {...getDataAndAria(this.$attrs)}>
|
2018-04-13 08:19:50 +00:00
|
|
|
<div
|
|
|
|
class={dragCls}
|
|
|
|
onDrop={this.onFileDrop}
|
|
|
|
onDragover={this.onFileDrop}
|
|
|
|
onDragleave={this.onFileDrop}
|
2020-06-25 14:21:23 +00:00
|
|
|
style={style}
|
2018-04-13 08:19:50 +00:00
|
|
|
>
|
2020-02-01 11:32:39 +00:00
|
|
|
<VcUpload {...vcUploadProps} class={`${prefixCls}-btn`}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<div class={`${prefixCls}-drag-container`}>{children}</div>
|
2018-04-13 08:19:50 +00:00
|
|
|
</VcUpload>
|
|
|
|
</div>
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uploadButtonCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-select`]: true,
|
|
|
|
[`${prefixCls}-select-${listType}`]: true,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2019-03-28 11:51:27 +00:00
|
|
|
|
|
|
|
// Remove id to avoid open by label when trigger is hidden
|
|
|
|
// https://github.com/ant-design/ant-design/issues/14298
|
2020-09-23 06:06:42 +00:00
|
|
|
if (!children.length || disabled) {
|
2020-06-25 14:21:23 +00:00
|
|
|
delete vcUploadProps.id;
|
2019-03-28 11:51:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 08:19:50 +00:00
|
|
|
const uploadButton = (
|
2020-09-23 02:33:02 +00:00
|
|
|
<div class={uploadButtonCls} style={children.length ? undefined : { display: 'none' }}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<VcUpload {...vcUploadProps}>{children}</VcUpload>
|
2018-04-13 08:19:50 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-13 08:19:50 +00:00
|
|
|
|
|
|
|
if (listType === 'picture-card') {
|
|
|
|
return (
|
2020-08-27 08:03:42 +00:00
|
|
|
<span class={classNames(`${prefixCls}-picture-card-wrapper`, className)}>
|
2018-04-13 08:19:50 +00:00
|
|
|
{uploadList}
|
|
|
|
{uploadButton}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
|
|
|
return (
|
2020-08-27 08:03:42 +00:00
|
|
|
<span class={className}>
|
2018-04-13 08:19:50 +00:00
|
|
|
{uploadButton}
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2020-10-22 06:52:37 +00:00
|
|
|
});
|