2019-05-22 14:12:11 +02:00
|
|
|
<template>
|
2019-06-17 17:15:27 +02:00
|
|
|
<div class="root">
|
2020-11-20 18:34:13 +01:00
|
|
|
<figure class="image" v-if="imageSrc">
|
|
|
|
<img :src="imageSrc" />
|
2019-10-12 19:23:32 +02:00
|
|
|
</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>
|
|
|
|
|
2020-11-20 18:34:13 +01:00
|
|
|
<div class="action-buttons">
|
|
|
|
<b-field class="file is-primary">
|
|
|
|
<b-upload @input="onFileChanged" :accept="accept" class="file-label">
|
|
|
|
<span class="file-cta">
|
|
|
|
<b-icon class="file-icon" icon="upload" />
|
|
|
|
<span>{{ $t("Click to upload") }}</span>
|
|
|
|
</span>
|
|
|
|
</b-upload>
|
|
|
|
</b-field>
|
|
|
|
<b-button type="is-text" v-if="imageSrc" @click="removeOrClearPicture">
|
|
|
|
{{ $t("Clear") }}
|
|
|
|
</b-button>
|
|
|
|
</div>
|
2019-06-17 17:15:27 +02:00
|
|
|
</div>
|
2019-05-22 14:12:11 +02:00
|
|
|
</template>
|
|
|
|
|
2019-10-12 19:23:32 +02:00
|
|
|
<style scoped lang="scss">
|
2020-02-18 08:57:00 +01:00
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
figure.image {
|
|
|
|
margin-right: 30px;
|
|
|
|
max-height: 200px;
|
|
|
|
max-width: 200px;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.image-placeholder {
|
|
|
|
background-color: grey;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
border-radius: 100%;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
span {
|
|
|
|
flex: 1;
|
|
|
|
color: #eee;
|
2019-06-17 17:15:27 +02:00
|
|
|
}
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
2020-11-20 18:34:13 +01:00
|
|
|
|
|
|
|
.action-buttons {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
2019-06-17 17:15:27 +02:00
|
|
|
</style>
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
<script lang="ts">
|
2020-11-26 11:41:13 +01:00
|
|
|
import { IMedia } from "@/types/media.model";
|
2020-02-18 08:57:00 +01: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 {
|
2020-02-18 08:57:00 +01:00
|
|
|
@Model("change", { type: File }) readonly pictureFile!: File;
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
@Prop({ type: Object, required: false }) defaultImage!: IMedia;
|
2020-09-29 09:53:48 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Prop({ type: String, required: false, default: "image/gif,image/png,image/jpeg,image/webp" })
|
|
|
|
accept!: string;
|
|
|
|
|
|
|
|
@Prop({
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default() {
|
2020-09-29 09:53:48 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2020-02-18 08:57:00 +01:00
|
|
|
// @ts-ignore
|
|
|
|
return this.$t("Avatar");
|
|
|
|
},
|
|
|
|
})
|
|
|
|
textFallback!: string;
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2020-11-20 18:34:13 +01:00
|
|
|
imageSrc: string | null = this.defaultImage ? this.defaultImage.url : null;
|
|
|
|
|
|
|
|
file!: File | null;
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2020-09-29 09:53:48 +02:00
|
|
|
mounted(): void {
|
2020-11-20 18:34:13 +01:00
|
|
|
if (this.pictureFile) {
|
|
|
|
this.updatePreview(this.pictureFile);
|
|
|
|
}
|
2019-09-09 11:21:42 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Watch("pictureFile")
|
2020-09-29 09:53:48 +02:00
|
|
|
onPictureFileChanged(val: File): void {
|
2020-11-20 18:34:13 +01:00
|
|
|
console.log("onPictureFileChanged", val);
|
2019-06-17 17:15:27 +02:00
|
|
|
this.updatePreview(val);
|
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2020-11-20 18:34:13 +01:00
|
|
|
@Watch("defaultImage")
|
2020-11-26 11:41:13 +01:00
|
|
|
onDefaultImageChange(defaultImage: IMedia): void {
|
2020-11-20 18:34:13 +01:00
|
|
|
console.log("onDefaultImageChange", defaultImage);
|
|
|
|
this.imageSrc = defaultImage ? defaultImage.url : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFileChanged(file: File | null): void {
|
2020-02-18 08:57:00 +01:00
|
|
|
this.$emit("change", file);
|
2019-06-17 17:15:27 +02:00
|
|
|
|
|
|
|
this.updatePreview(file);
|
2020-11-20 18:34:13 +01:00
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeOrClearPicture(): Promise<void> {
|
|
|
|
this.onFileChanged(null);
|
2019-06-17 17:15:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:34:13 +01:00
|
|
|
private updatePreview(file?: File | null) {
|
2019-06-17 17:15:27 +02:00
|
|
|
if (file) {
|
|
|
|
this.imageSrc = URL.createObjectURL(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.imageSrc = null;
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|