2021-01-06 06:21:20 -08:00
|
|
|
<template>
|
|
|
|
<div
|
2021-01-08 06:14:53 -08:00
|
|
|
:class="['uploader-container', form ? 'form-control p-2' : 'btn btn-outline-primary btn-sm', drag ? 'drag' : '']"
|
2021-01-06 06:21:20 -08:00
|
|
|
@dragover="drag=true" @dragleave="drag=false"
|
|
|
|
>
|
|
|
|
<input type="file"
|
|
|
|
:name="name + (multiple ? '[]' : '')"
|
|
|
|
:multiple="multiple"
|
|
|
|
:disabled="uploading"
|
|
|
|
@change="filesChange($event.target.name, $event.target.files)"
|
|
|
|
accept="image/*">
|
2021-01-08 06:14:53 -08:00
|
|
|
<p v-if="errorMessage" class="text-danger mb-0">
|
2021-01-06 06:21:20 -08:00
|
|
|
<Icon v="exclamation-circle"/>
|
|
|
|
<T>{{errorMessage}}</T>
|
|
|
|
</p>
|
2021-01-08 06:14:53 -08:00
|
|
|
<p v-else-if="uploading" class="mb-0">
|
2021-01-06 06:21:20 -08:00
|
|
|
<Spinner/>
|
|
|
|
</p>
|
2021-01-08 06:14:53 -08:00
|
|
|
<p v-else class="mb-0">
|
2021-01-06 06:21:20 -08:00
|
|
|
<Icon v="upload"/>
|
2021-01-08 06:14:53 -08:00
|
|
|
<T>images.upload.instruction{{small ? 'Short' : ''}}</T>
|
2021-01-06 06:21:20 -08:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
multiple: {type: Boolean},
|
|
|
|
name: {'default': 'images'},
|
2021-01-08 06:14:53 -08:00
|
|
|
form: {type: Boolean},
|
2021-08-24 06:22:51 -07:00
|
|
|
small: {type: Boolean},
|
|
|
|
sizes: {'default': 'all'},
|
2021-01-06 06:21:20 -08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
uploading: false,
|
|
|
|
drag: false,
|
|
|
|
errorMessage: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async filesChange(fieldName, fileList) {
|
|
|
|
if (!fileList.length) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-06 06:38:05 -08:00
|
|
|
this.drag = false;
|
2021-01-06 06:21:20 -08:00
|
|
|
const formData = new FormData();
|
|
|
|
for (let file of fileList) {
|
|
|
|
formData.append(fieldName, file, file.name);
|
|
|
|
}
|
|
|
|
await this.save(formData);
|
|
|
|
},
|
|
|
|
async save(formData) {
|
|
|
|
this.uploading = true;
|
|
|
|
this.errorMessage = '';
|
|
|
|
try {
|
2021-08-24 06:22:51 -07:00
|
|
|
const ids = await this.$axios.$post('/images/upload?sizes=' + this.sizes, formData, {
|
2021-01-06 06:21:20 -08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.$emit('uploaded', ids);
|
|
|
|
} catch {
|
|
|
|
this.errorMessage = 'error.generic';
|
|
|
|
}
|
|
|
|
this.uploading = false;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
@import "../assets/style";
|
|
|
|
|
|
|
|
.uploader-container {
|
|
|
|
position: relative;
|
|
|
|
cursor: pointer;
|
2021-01-08 06:14:53 -08:00
|
|
|
&.form-control {
|
|
|
|
&:hover, &.drag {
|
|
|
|
background: lighten($primary, 50%);
|
|
|
|
}
|
2021-01-06 06:21:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input[type="file"] {
|
|
|
|
opacity: 0;
|
|
|
|
position: absolute;
|
|
|
|
left: 0;
|
|
|
|
top: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
</style>
|