mirror of https://github.com/ElemeFE/element
add upload-dragger
parent
461e5edbfd
commit
4c5820373c
|
@ -46,6 +46,7 @@
|
||||||
"row": "./packages/row/index.js",
|
"row": "./packages/row/index.js",
|
||||||
"col": "./packages/col/index.js",
|
"col": "./packages/col/index.js",
|
||||||
"upload": "./packages/upload/index.js",
|
"upload": "./packages/upload/index.js",
|
||||||
|
"upload-dragger": "./packages/upload/index.js",
|
||||||
"progress": "./packages/progress/index.js",
|
"progress": "./packages/progress/index.js",
|
||||||
"spinner": "./packages/spinner/index.js",
|
"spinner": "./packages/spinner/index.js",
|
||||||
"message": "./packages/message/index.js",
|
"message": "./packages/message/index.js",
|
||||||
|
|
|
@ -12,7 +12,15 @@
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
|
fileList: [{
|
||||||
|
name: 'food.jpeg',
|
||||||
|
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
|
||||||
|
status: 'finished'
|
||||||
|
}, {
|
||||||
|
name: 'food2.jpeg',
|
||||||
|
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
|
||||||
|
status: 'finished'
|
||||||
|
}]
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
@ -78,7 +86,20 @@
|
||||||
```
|
```
|
||||||
:::
|
:::
|
||||||
|
|
||||||
### 拖拽上传
|
::: demo 通过 slot 你可以传入自定义的上传按钮类型和文字提示。
|
||||||
|
```html
|
||||||
|
<el-upload-dragger
|
||||||
|
action="//jsonplaceholder.typicode.com/posts/"
|
||||||
|
:on-preview="handlePreview"
|
||||||
|
:on-remove="handleRemove"
|
||||||
|
:file-list="fileList">
|
||||||
|
<el-button size="small" type="primary">点击上传</el-button>
|
||||||
|
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
|
||||||
|
</el-upload-dragger>
|
||||||
|
```
|
||||||
|
:::
|
||||||
|
|
||||||
|
<!-- ### 拖拽上传
|
||||||
|
|
||||||
可将文件拖入指定区域进行上传。
|
可将文件拖入指定区域进行上传。
|
||||||
|
|
||||||
|
@ -154,7 +175,7 @@
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
:::
|
::: -->
|
||||||
|
|
||||||
### Attribute
|
### Attribute
|
||||||
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import UploadDragger from '../upload/src/upload-dragger';
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
|
UploadDragger.install = function(Vue) {
|
||||||
|
Vue.component(UploadDragger.name, UploadDragger);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UploadDragger;
|
|
@ -0,0 +1,10 @@
|
||||||
|
import Upload from './src';
|
||||||
|
import UploadDragger from './src/upload-dragger';
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
|
Upload.install = function(Vue) {
|
||||||
|
Vue.component(Upload.name, Upload);
|
||||||
|
Vue.component(UploadDragger.name, UploadDragger);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { Upload, UploadDragger };
|
|
@ -34,6 +34,7 @@ export default {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'file'
|
default: 'file'
|
||||||
},
|
},
|
||||||
|
draggable: Boolean,
|
||||||
withCredentials: Boolean,
|
withCredentials: Boolean,
|
||||||
thumbnailMode: Boolean,
|
thumbnailMode: Boolean,
|
||||||
showUploadList: {
|
showUploadList: {
|
||||||
|
@ -80,7 +81,7 @@ export default {
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
_fileList: [],
|
uploadFiles: [],
|
||||||
dragOver: false,
|
dragOver: false,
|
||||||
draging: false,
|
draging: false,
|
||||||
tempIndex: 1
|
tempIndex: 1
|
||||||
|
@ -91,13 +92,16 @@ export default {
|
||||||
fileList: {
|
fileList: {
|
||||||
immediate: true,
|
immediate: true,
|
||||||
handler(fileList) {
|
handler(fileList) {
|
||||||
this._fileList = fileList.map(item => {
|
this.uploadFiles = fileList.map(item => {
|
||||||
item.status = 'finished';
|
if (!item.uid) {
|
||||||
item.percentage = 100;
|
item.uid = Date.now() + this.tempIndex++;
|
||||||
item.uid = Date.now() + this.tempIndex++;
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
uploadFiles(value, oldValue) {
|
||||||
|
// console.log(value, oldValue);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -120,11 +124,11 @@ export default {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._fileList.push(_file);
|
this.uploadFiles.push(_file);
|
||||||
},
|
},
|
||||||
handleProgress(ev, file) {
|
handleProgress(ev, file) {
|
||||||
var _file = this.getFile(file);
|
var _file = this.getFile(file);
|
||||||
this.onProgress(ev, _file, this._fileList);
|
this.onProgress(ev, _file, this.uploadFiles);
|
||||||
_file.percentage = ev.percent || 0;
|
_file.percentage = ev.percent || 0;
|
||||||
},
|
},
|
||||||
handleSuccess(res, file) {
|
handleSuccess(res, file) {
|
||||||
|
@ -134,7 +138,7 @@ export default {
|
||||||
_file.status = 'finished';
|
_file.status = 'finished';
|
||||||
_file.response = res;
|
_file.response = res;
|
||||||
|
|
||||||
this.onSuccess(res, _file, this._fileList);
|
this.onSuccess(res, _file, this.uploadFiles);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
_file.showProgress = false;
|
_file.showProgress = false;
|
||||||
|
@ -143,7 +147,7 @@ export default {
|
||||||
},
|
},
|
||||||
handleError(err, response, file) {
|
handleError(err, response, file) {
|
||||||
var _file = this.getFile(file);
|
var _file = this.getFile(file);
|
||||||
var fileList = this._fileList;
|
var fileList = this.uploadFiles;
|
||||||
|
|
||||||
_file.status = 'fail';
|
_file.status = 'fail';
|
||||||
|
|
||||||
|
@ -152,12 +156,12 @@ export default {
|
||||||
this.onError(err, response, file);
|
this.onError(err, response, file);
|
||||||
},
|
},
|
||||||
handleRemove(file) {
|
handleRemove(file) {
|
||||||
var fileList = this._fileList;
|
var fileList = this.uploadFiles;
|
||||||
fileList.splice(fileList.indexOf(file), 1);
|
fileList.splice(fileList.indexOf(file), 1);
|
||||||
this.onRemove(file, fileList);
|
this.onRemove(file, fileList);
|
||||||
},
|
},
|
||||||
getFile(file) {
|
getFile(file) {
|
||||||
var fileList = this._fileList;
|
var fileList = this.uploadFiles;
|
||||||
var target;
|
var target;
|
||||||
fileList.every(item => {
|
fileList.every(item => {
|
||||||
target = file.uid === item.uid ? item : null;
|
target = file.uid === item.uid ? item : null;
|
||||||
|
@ -171,17 +175,39 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
clearFiles() {
|
clearFiles() {
|
||||||
this._fileList = [];
|
this.uploadFiles = [];
|
||||||
|
},
|
||||||
|
initDraggable() {
|
||||||
|
const target = this.$el;
|
||||||
|
const _this = this;
|
||||||
|
target.addEventListener('dragover', event => {
|
||||||
|
event.preventDefault();
|
||||||
|
_this.draggable = true;
|
||||||
|
});
|
||||||
|
target.addEventListener('drop', event => {
|
||||||
|
event.preventDefault();
|
||||||
|
_this.draggable = false;
|
||||||
|
});
|
||||||
|
target.addEventListener('dragleave', event => {
|
||||||
|
event.preventDefault();
|
||||||
|
_this.draggable = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
if (this.draggable) {
|
||||||
|
this.initDraggable();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
render(h) {
|
render(h) {
|
||||||
var uploadList;
|
var uploadList;
|
||||||
|
|
||||||
if (this.showUploadList && !this.thumbnailMode && this.fileList.length) {
|
if (this.showUploadList && !this.thumbnailMode && this.uploadFiles.length) {
|
||||||
uploadList = (
|
uploadList = (
|
||||||
<UploadList
|
<UploadList
|
||||||
files={this.fileList}
|
files={this.uploadFiles}
|
||||||
on-remove={this.handleRemove}
|
on-remove={this.handleRemove}
|
||||||
on-preview={this.handlePreview}>
|
on-preview={this.handlePreview}>
|
||||||
</UploadList>
|
</UploadList>
|
||||||
|
@ -191,6 +217,7 @@ export default {
|
||||||
var props = {
|
var props = {
|
||||||
props: {
|
props: {
|
||||||
type: this.type,
|
type: this.type,
|
||||||
|
draggable: this.draggable,
|
||||||
action: this.action,
|
action: this.action,
|
||||||
multiple: this.multiple,
|
multiple: this.multiple,
|
||||||
'before-upload': this.beforeUpload,
|
'before-upload': this.beforeUpload,
|
||||||
|
@ -213,25 +240,23 @@ export default {
|
||||||
? <upload {...props}>{this.$slots.default}</upload>
|
? <upload {...props}>{this.$slots.default}</upload>
|
||||||
: <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
|
: <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
|
||||||
|
|
||||||
if (this.type === 'select') {
|
return (
|
||||||
return (
|
<div class="el-upload">
|
||||||
<div class="el-upload">
|
{uploadList}
|
||||||
{uploadList}
|
{uploadComponent}
|
||||||
{uploadComponent}
|
{this.$slots.tip}
|
||||||
{this.$slots.tip}
|
</div>
|
||||||
</div>
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.type === 'drag') {
|
// if (this.type === 'drag') {
|
||||||
return (
|
// return (
|
||||||
<div class="el-upload">
|
// <div class="el-upload">
|
||||||
{uploadComponent}
|
// {uploadComponent}
|
||||||
{this.$slots.tip}
|
// {this.$slots.tip}
|
||||||
{uploadList}
|
// {uploadList}
|
||||||
</div>
|
// </div>
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
<script>
|
||||||
|
import index from '.';
|
||||||
|
import Upload from './upload';
|
||||||
|
import IframeUpload from './iframe-upload';
|
||||||
|
import UploadList from './upload-list';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: 'ElUploadDragger',
|
||||||
|
extends: index,
|
||||||
|
components: {
|
||||||
|
UploadList,
|
||||||
|
Upload,
|
||||||
|
IframeUpload
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
draggable: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
render(h) {
|
||||||
|
var uploadList;
|
||||||
|
|
||||||
|
if (this.showUploadList && !this.thumbnailMode && this.uploadFiles.length) {
|
||||||
|
uploadList = (
|
||||||
|
<UploadList
|
||||||
|
files={this.uploadFiles}
|
||||||
|
on-remove={this.handleRemove}
|
||||||
|
on-preview={this.handlePreview}>
|
||||||
|
</UploadList>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var props = {
|
||||||
|
props: {
|
||||||
|
type: this.type,
|
||||||
|
draggable: this.draggable,
|
||||||
|
action: this.action,
|
||||||
|
multiple: this.multiple,
|
||||||
|
'before-upload': this.beforeUpload,
|
||||||
|
'with-credentials': this.withCredentials,
|
||||||
|
headers: this.headers,
|
||||||
|
name: this.name,
|
||||||
|
data: this.data,
|
||||||
|
accept: this.thumbnailMode ? 'image/gif, image/png, image/jpeg, image/bmp, image/webp' : this.accept,
|
||||||
|
'on-start': this.handleStart,
|
||||||
|
'on-progress': this.handleProgress,
|
||||||
|
'on-success': this.handleSuccess,
|
||||||
|
'on-error': this.handleError,
|
||||||
|
'on-preview': this.handlePreview,
|
||||||
|
'on-remove': this.handleRemove
|
||||||
|
},
|
||||||
|
ref: 'upload-inner'
|
||||||
|
};
|
||||||
|
|
||||||
|
var uploadComponent = (typeof FormData !== 'undefined' || this.$isServer)
|
||||||
|
? <upload {...props}>{this.$slots.default}</upload>
|
||||||
|
: <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
class={{
|
||||||
|
'el-upload-dragger': true,
|
||||||
|
'is-dragOver': this.dragOver
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{uploadComponent}
|
||||||
|
{this.$slots.tip}
|
||||||
|
{uploadList}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
|
@ -6,9 +6,7 @@
|
||||||
'is-showCover': showCover
|
'is-showCover': showCover
|
||||||
}"
|
}"
|
||||||
@click="handleClick"
|
@click="handleClick"
|
||||||
@drop.prevent="onDrop"
|
>
|
||||||
@dragover.prevent="dragOver = true"
|
|
||||||
@dragleave.prevent="dragOver = false">
|
|
||||||
<slot v-if="!showCover"></slot>
|
<slot v-if="!showCover"></slot>
|
||||||
<cover :image="lastestFile" :on-preview="onPreview" :on-remove="onRemove" v-else></cover>
|
<cover :image="lastestFile" :on-preview="onPreview" :on-remove="onRemove" v-else></cover>
|
||||||
<input class="el-upload__input" type="file" ref="input" @change="handleChange" :multiple="multiple" :accept="accept">
|
<input class="el-upload__input" type="file" ref="input" @change="handleChange" :multiple="multiple" :accept="accept">
|
||||||
|
|
|
@ -47,6 +47,7 @@ import Icon from '../packages/icon';
|
||||||
import Row from '../packages/row';
|
import Row from '../packages/row';
|
||||||
import Col from '../packages/col';
|
import Col from '../packages/col';
|
||||||
import Upload from '../packages/upload';
|
import Upload from '../packages/upload';
|
||||||
|
import UploadDragger from '../packages/upload-dragger';
|
||||||
import Progress from '../packages/progress';
|
import Progress from '../packages/progress';
|
||||||
import Spinner from '../packages/spinner';
|
import Spinner from '../packages/spinner';
|
||||||
import Message from '../packages/message';
|
import Message from '../packages/message';
|
||||||
|
@ -201,6 +202,7 @@ module.exports = {
|
||||||
Row,
|
Row,
|
||||||
Col,
|
Col,
|
||||||
Upload,
|
Upload,
|
||||||
|
UploadDragger,
|
||||||
Progress,
|
Progress,
|
||||||
Spinner,
|
Spinner,
|
||||||
Message,
|
Message,
|
||||||
|
|
Loading…
Reference in New Issue