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