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

289 lines
7.6 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import classNames from 'classnames';
import uniqBy from 'lodash/uniqBy';
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';
import Dragger from './Dragger';
import UploadList from './UploadList';
import { UploadProps } from './interface';
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
2018-04-13 16:19:50 +08:00
2019-01-12 11:33:27 +08:00
export { UploadProps };
2018-04-13 16:19:50 +08:00
export default {
name: 'AUpload',
mixins: [BaseMixin],
2019-02-01 17:23:00 +08:00
inheritAttrs: false,
Dragger: Dragger,
2018-04-13 16:19:50 +08:00
props: initDefaultProps(UploadProps, {
prefixCls: 'ant-upload',
type: 'select',
multiple: false,
action: '',
data: {},
accept: '',
beforeUpload: T,
showUploadList: true,
listType: 'text', // or pictrue
disabled: false,
supportServerRender: true,
}),
// recentUploadStatus: boolean | PromiseLike<any>;
2019-01-12 11:33:27 +08:00
data() {
this.progressTimer = null;
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) {
this.sFileList = val;
2018-04-13 16:19:50 +08:00
},
},
2019-02-01 17:23:00 +08:00
beforeDestroy() {
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();
const fileIndex = nextFileList.findIndex(({ 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
}
2018-04-13 16:19:50 +08:00
this.onChange({
file: targetItem,
fileList: nextFileList,
2019-01-12 11:33:27 +08:00
});
2018-04-13 16:19:50 +08:00
// fix ie progress
if (!window.FormData) {
2019-01-12 11:33:27 +08:00
this.autoUpdateProgress(0, targetItem);
2018-04-13 16:19:50 +08:00
}
},
2019-01-12 11:33:27 +08:00
autoUpdateProgress(_, file) {
const getPercent = genPercentAdd();
let curPercent = 0;
this.clearProgressTimer();
2018-04-13 16:19:50 +08:00
this.progressTimer = setInterval(() => {
2019-01-12 11:33:27 +08:00
curPercent = getPercent(curPercent);
this.onProgress(
{
percent: curPercent * 100,
},
file,
);
}, 200);
2018-04-13 16:19:50 +08:00
},
2019-01-12 11:33:27 +08:00
onSuccess(response, file) {
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;
2018-04-13 16:19:50 +08:00
this.onChange({
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;
2018-04-13 16:19:50 +08:00
this.onChange({
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';
2018-04-13 16:19:50 +08:00
this.onChange({
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
handleRemove(file) {
const { remove } = getOptionProps(this);
Promise.resolve(typeof remove === 'function' ? remove(file) : remove).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
}
2019-01-12 11:33:27 +08:00
const removedFileList = removeFileItem(file, this.sFileList);
2018-04-13 16:19:50 +08:00
if (removedFileList) {
this.onChange({
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) {
this.$refs.uploadRef.abort(file);
file.status = 'removed'; // eslint-disable-line
this.handleRemove(file);
2018-04-13 16:19:50 +08:00
},
2019-01-12 11:33:27 +08:00
onChange(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
}
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) {
2018-04-13 16:19:50 +08:00
if (!this.beforeUpload) {
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
const result = this.beforeUpload(file, fileList);
2018-04-13 16:19:50 +08:00
if (result === false) {
this.onChange({
file,
2019-01-12 11:33:27 +08:00
fileList: uniqBy(this.sFileList.concat(fileList.map(fileToObject)), item => item.uid),
});
return false;
2018-04-13 16:19:50 +08:00
} else 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
},
2019-01-12 11:33:27 +08:00
renderUploadList(locale) {
const { showUploadList = {}, listType } = getOptionProps(this);
const { showRemoveIcon, showPreviewIcon } = showUploadList;
2018-04-13 16:19:50 +08:00
const uploadListProps = {
props: {
listType,
items: this.sFileList,
showRemoveIcon,
showPreviewIcon,
locale: { ...locale, ...this.$props.locale },
},
on: {
remove: this.handleManualRemove,
},
2019-01-12 11:33:27 +08:00
};
2018-07-03 10:22:03 +08:00
if (this.$listeners.preview) {
2019-01-12 11:33:27 +08:00
uploadListProps.on.preview = this.$listeners.preview;
2018-07-03 10:22:03 +08:00
}
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() {
const { prefixCls = '', showUploadList, listType, type, disabled } = getOptionProps(this);
2018-04-13 16:19:50 +08:00
const vcUploadProps = {
props: {
...this.$props,
beforeUpload: this.reBeforeUpload,
},
on: {
// ...this.$listeners,
start: this.onStart,
error: this.onError,
progress: this.onProgress,
success: this.onSuccess,
},
ref: 'uploadRef',
class: `${prefixCls}-btn`,
2019-01-01 22:30:06 +08:00
attrs: this.$attrs,
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}
2019-01-12 11:33:27 +08:00
scopedSlots={{ default: this.renderUploadList }}
/>
) : null;
2018-04-13 16:19:50 +08:00
2019-01-12 11:33:27 +08:00
const children = this.$slots.default;
2018-04-13 16:19:50 +08: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 11:33:27 +08:00
});
2018-04-13 16:19:50 +08:00
return (
<span>
<div
class={dragCls}
onDrop={this.onFileDrop}
onDragover={this.onFileDrop}
onDragleave={this.onFileDrop}
>
<VcUpload {...vcUploadProps}>
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
});
2018-04-13 16:19:50 +08:00
const uploadButton = (
<div class={uploadButtonCls} style={{ display: children ? '' : '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 (
<span>
{uploadList}
{uploadButton}
</span>
2019-01-12 11:33:27 +08:00
);
2018-04-13 16:19:50 +08:00
}
return (
<span>
{uploadButton}
{uploadList}
</span>
2019-01-12 11:33:27 +08:00
);
2018-04-13 16:19:50 +08:00
},
2019-01-12 11:33:27 +08:00
};