ant-design-vue/components/upload/Upload.jsx

319 lines
8.3 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import classNames from 'classnames';
import uniqBy from 'lodash/uniqBy';
2019-03-28 11:51:27 +00:00
import findIndex from 'lodash/findIndex';
2019-01-12 03:33:27 +00:00
import VcUpload from '../vc-upload';
import BaseMixin from '../_util/BaseMixin';
import { getOptionProps, initDefaultProps, hasProp } from '../_util/props-util';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
2019-03-28 11:51:27 +00:00
import { ConfigConsumerProps } 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';
2018-04-13 08:19:50 +00:00
2019-01-12 03:33:27 +00:00
export { UploadProps };
2018-04-13 08:19:50 +00:00
export default {
name: 'AUpload',
mixins: [BaseMixin],
2019-02-01 09:23:00 +00:00
inheritAttrs: false,
Dragger: 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,
}),
2019-03-28 11:51:27 +00:00
inject: {
configProvider: { default: () => ({}) },
},
2018-04-13 08:19:50 +00:00
// recentUploadStatus: boolean | PromiseLike<any>;
2019-01-12 03:33:27 +00:00
data() {
this.progressTimer = null;
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) {
this.sFileList = val || [];
2018-04-13 08:19:50 +00:00
},
},
2019-02-01 09:23:00 +00:00
beforeDestroy() {
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
}
2018-04-13 08:19:50 +00:00
this.onChange({
file: targetItem,
fileList: nextFileList,
2019-01-12 03:33:27 +00:00
});
2018-04-13 08:19:50 +00:00
// fix ie progress
if (!window.FormData) {
2019-01-12 03:33:27 +00:00
this.autoUpdateProgress(0, targetItem);
2018-04-13 08:19:50 +00:00
}
},
2019-01-12 03:33:27 +00:00
autoUpdateProgress(_, file) {
const getPercent = genPercentAdd();
let curPercent = 0;
this.clearProgressTimer();
2018-04-13 08:19:50 +00:00
this.progressTimer = setInterval(() => {
2019-01-12 03:33:27 +00:00
curPercent = getPercent(curPercent);
this.onProgress(
{
percent: curPercent * 100,
},
file,
);
}, 200);
2018-04-13 08:19:50 +00:00
},
2019-01-12 03:33:27 +00:00
onSuccess(response, file) {
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;
2018-04-13 08:19:50 +00:00
this.onChange({
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;
2018-04-13 08:19:50 +00:00
this.onChange({
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';
2018-04-13 08:19:50 +00:00
this.onChange({
file: { ...targetItem },
fileList,
2019-01-12 03:33:27 +00:00
});
2018-04-13 08:19:50 +00:00
},
onReject(fileList) {
this.$emit('reject', fileList);
},
2019-01-12 03:33:27 +00:00
handleRemove(file) {
const { remove } = getOptionProps(this);
2019-03-28 11:51:27 +00:00
const { status } = file;
file.status = 'removed'; // eslint-disable-line
Promise.resolve(typeof remove === 'function' ? remove(file) : remove).then(ret => {
2018-04-13 08:19:50 +00:00
// Prevent removing file
if (ret === false) {
2019-03-28 11:51:27 +00:00
file.status = status;
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
const removedFileList = removeFileItem(file, this.sFileList);
2018-04-13 08:19:50 +00:00
if (removedFileList) {
this.onChange({
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) {
2019-03-28 11:51:27 +00:00
this.$refs.uploadRef.abort(file);
}
2019-01-12 03:33:27 +00:00
this.handleRemove(file);
2018-04-13 08:19:50 +00:00
},
2019-01-12 03:33:27 +00:00
onChange(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
}
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) {
2018-04-13 08:19:50 +00:00
if (!this.beforeUpload) {
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
const result = this.beforeUpload(file, fileList);
2018-04-13 08:19:50 +00:00
if (result === false) {
this.onChange({
file,
2019-01-12 03:33:27 +00:00
fileList: uniqBy(this.sFileList.concat(fileList.map(fileToObject)), item => item.uid),
});
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
},
2019-01-12 03:33:27 +00:00
renderUploadList(locale) {
const { showUploadList = {}, listType } = getOptionProps(this);
const { showRemoveIcon, showPreviewIcon } = showUploadList;
2018-04-13 08:19:50 +00:00
const uploadListProps = {
props: {
listType,
items: this.sFileList,
showRemoveIcon,
showPreviewIcon,
locale: { ...locale, ...this.$props.locale },
},
on: {
remove: this.handleManualRemove,
},
2019-01-12 03:33:27 +00:00
};
2018-07-03 02:22:03 +00:00
if (this.$listeners.preview) {
2019-01-12 03:33:27 +00:00
uploadListProps.on.preview = this.$listeners.preview;
2018-07-03 02:22:03 +00:00
}
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);
2019-03-28 11:51:27 +00:00
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('upload', customizePrefixCls);
2018-04-13 08:19:50 +00:00
const vcUploadProps = {
props: {
...this.$props,
beforeUpload: this.reBeforeUpload,
},
on: {
// ...this.$listeners,
start: this.onStart,
error: this.onError,
progress: this.onProgress,
success: this.onSuccess,
reject: this.onReject,
2018-04-13 08:19:50 +00:00
},
ref: 'uploadRef',
class: `${prefixCls}-btn`,
2019-01-01 14:30:06 +00:00
attrs: this.$attrs,
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}
2019-01-12 03:33:27 +00:00
scopedSlots={{ default: this.renderUploadList }}
/>
) : null;
2018-04-13 08:19:50 +00:00
2019-01-12 03:33:27 +00:00
const children = this.$slots.default;
2018-04-13 08:19:50 +00:00
if (type === 'drag') {
const dragCls = classNames(prefixCls, {
[`${prefixCls}-drag`]: true,
[`${prefixCls}-drag-uploading`]: this.sFileList.some(file => file.status === 'uploading'),
[`${prefixCls}-drag-hover`]: this.dragState === 'dragover',
[`${prefixCls}-disabled`]: disabled,
2019-01-12 03:33:27 +00:00
});
2018-04-13 08:19:50 +00:00
return (
<span>
<div
class={dragCls}
onDrop={this.onFileDrop}
onDragover={this.onFileDrop}
onDragleave={this.onFileDrop}
>
<VcUpload {...vcUploadProps}>
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
if (!children) {
delete vcUploadProps.props.id;
}
2018-04-13 08:19:50 +00:00
const uploadButton = (
2019-03-28 11:51:27 +00:00
<div class={uploadButtonCls} style={children ? 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 (
<span>
{uploadList}
{uploadButton}
</span>
2019-01-12 03:33:27 +00:00
);
2018-04-13 08:19:50 +00:00
}
return (
<span>
{uploadButton}
{uploadList}
</span>
2019-01-12 03:33:27 +00:00
);
2018-04-13 08:19:50 +00:00
},
2019-01-12 03:33:27 +00:00
};