This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/components/ImageUploader.vue

95 lines
2.7 KiB
Vue
Raw Normal View History

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},
small: {type: Boolean}
2021-01-06 06:21:20 -08:00
},
data() {
return {
uploading: false,
drag: false,
errorMessage: '',
}
},
methods: {
async filesChange(fieldName, fileList) {
if (!fileList.length) {
return;
}
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 {
const ids = await this.$axios.$post('/images/upload', formData, {
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>