2019-05-22 14:12:11 +02:00
|
|
|
<template>
|
2019-06-17 17:15:27 +02:00
|
|
|
<div class="root">
|
2019-10-12 19:23:32 +02:00
|
|
|
<figure class="image" v-if="imageSrc">
|
|
|
|
<img :src="imageSrc" />
|
|
|
|
</figure>
|
|
|
|
<figure class="image is-128x128" v-else>
|
|
|
|
<div class="image-placeholder">
|
|
|
|
<span class="has-text-centered">{{ textFallback }}</span>
|
|
|
|
</div>
|
2019-06-17 17:15:27 +02:00
|
|
|
</figure>
|
|
|
|
|
2019-10-12 19:23:32 +02:00
|
|
|
<b-upload @input="onFileChanged" :accept="accept">
|
2019-06-17 17:15:27 +02:00
|
|
|
<a class="button is-primary">
|
|
|
|
<b-icon icon="upload"></b-icon>
|
2019-09-12 11:34:01 +02:00
|
|
|
<span>{{ $t('Click to upload') }}</span>
|
2019-06-17 17:15:27 +02:00
|
|
|
</a>
|
|
|
|
</b-upload>
|
|
|
|
</div>
|
2019-05-22 14:12:11 +02:00
|
|
|
</template>
|
|
|
|
|
2019-10-12 19:23:32 +02:00
|
|
|
<style scoped lang="scss">
|
2019-06-17 17:15:27 +02:00
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
2019-10-12 19:23:32 +02:00
|
|
|
figure.image {
|
2019-06-17 17:15:27 +02:00
|
|
|
margin-right: 30px;
|
2019-10-12 19:23:32 +02:00
|
|
|
max-height: 200px;
|
|
|
|
max-width: 200px;
|
2019-06-17 17:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.image-placeholder {
|
|
|
|
background-color: grey;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
border-radius: 100%;
|
2019-10-12 19:23:32 +02:00
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
span {
|
|
|
|
flex: 1;
|
|
|
|
color: #eee;
|
|
|
|
}
|
2019-06-17 17:15:27 +02:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
<script lang="ts">
|
2019-10-12 19:23:32 +02:00
|
|
|
import { Component, Model, Prop, Vue, Watch } from 'vue-property-decorator';
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
@Component
|
|
|
|
export default class PictureUpload extends Vue {
|
2019-06-17 17:15:27 +02:00
|
|
|
@Model('change', { type: File }) readonly pictureFile!: File;
|
2019-10-12 19:23:32 +02:00
|
|
|
@Prop({ type: String, required: false, default: 'image/png,image/jpeg' }) accept;
|
|
|
|
// @ts-ignore
|
|
|
|
@Prop({ type: String, required: false, default() { return this.$t('Avatar'); } }) textFallback!: string;
|
2019-06-17 17:15:27 +02:00
|
|
|
|
|
|
|
imageSrc: string | null = null;
|
|
|
|
|
2019-09-09 11:21:42 +02:00
|
|
|
mounted() {
|
|
|
|
this.updatePreview(this.pictureFile);
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:15:27 +02:00
|
|
|
@Watch('pictureFile')
|
2019-09-09 11:21:42 +02:00
|
|
|
onPictureFileChanged(val: File) {
|
2019-06-17 17:15:27 +02:00
|
|
|
this.updatePreview(val);
|
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
onFileChanged(file: File) {
|
2019-06-17 17:15:27 +02:00
|
|
|
this.$emit('change', file);
|
|
|
|
|
|
|
|
this.updatePreview(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
private updatePreview(file?: File) {
|
|
|
|
if (file) {
|
|
|
|
this.imageSrc = URL.createObjectURL(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.imageSrc = null;
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|