2019-05-22 14:12:11 +02:00
|
|
|
<template>
|
2019-06-17 17:15:27 +02:00
|
|
|
<div class="root">
|
2021-04-12 10:43:04 +02:00
|
|
|
<figure class="image" v-if="imageSrc && !imagePreviewLoadingError">
|
|
|
|
<img :src="imageSrc" @error="showImageLoadingError" />
|
2019-10-12 19:23:32 +02:00
|
|
|
</figure>
|
|
|
|
<figure class="image is-128x128" v-else>
|
2021-04-12 10:43:04 +02:00
|
|
|
<div
|
|
|
|
class="image-placeholder"
|
|
|
|
:class="{ error: imagePreviewLoadingError }"
|
|
|
|
>
|
|
|
|
<span class="has-text-centered" v-if="imagePreviewLoadingError">{{
|
|
|
|
$t("Error while loading the preview")
|
|
|
|
}}</span>
|
|
|
|
<span class="has-text-centered" v-else>{{ textFallback }}</span>
|
2019-10-12 19:23:32 +02:00
|
|
|
</div>
|
2019-06-17 17:15:27 +02:00
|
|
|
</figure>
|
|
|
|
|
2020-11-20 18:34:13 +01:00
|
|
|
<div class="action-buttons">
|
2021-04-12 10:43:04 +02:00
|
|
|
<p v-if="pictureFile" class="metadata">
|
|
|
|
<span class="name" :title="pictureFile.name">{{
|
|
|
|
pictureFile.name
|
|
|
|
}}</span>
|
|
|
|
<span class="size">({{ formatBytes(pictureFile.size) }})</span>
|
|
|
|
</p>
|
|
|
|
<p v-if="pictureTooBig" class="picture-too-big">
|
|
|
|
{{
|
|
|
|
$t(
|
|
|
|
"The selected picture is too heavy. You need to select a file smaller than {size}.",
|
|
|
|
{ size: formatBytes(maxSize) }
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
</p>
|
2020-11-20 18:34:13 +01:00
|
|
|
<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>
|
2021-10-10 16:24:12 +02:00
|
|
|
<b-button
|
|
|
|
type="is-text"
|
|
|
|
v-if="imageSrc"
|
|
|
|
@click="removeOrClearPicture"
|
|
|
|
@keyup.enter="removeOrClearPicture"
|
|
|
|
>
|
2020-11-20 18:34:13 +01:00
|
|
|
{{ $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;
|
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
&.error {
|
|
|
|
border: 2px solid red;
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
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;
|
2021-04-12 10:43:04 +02:00
|
|
|
|
|
|
|
.file {
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.metadata {
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
|
|
.name {
|
|
|
|
max-width: 200px;
|
|
|
|
display: block;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
|
|
|
margin-right: 5px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.picture-too-big {
|
|
|
|
color: $danger;
|
2020-11-20 18:34:13 +01:00
|
|
|
}
|
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-11-30 10:24:11 +01:00
|
|
|
@Prop({
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: "image/gif,image/png,image/jpeg,image/webp",
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
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
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
@Prop({ type: Number, required: false, default: 10_485_760 })
|
|
|
|
maxSize!: number;
|
2020-11-20 18:34:13 +01:00
|
|
|
|
|
|
|
file!: File | null;
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
imagePreviewLoadingError = false;
|
2019-09-09 11:21:42 +02:00
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
get pictureTooBig(): boolean {
|
|
|
|
return this.pictureFile?.size > this.maxSize;
|
2019-06-17 17:15:27 +02:00
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
get imageSrc(): string | null {
|
|
|
|
if (this.pictureFile !== undefined) {
|
|
|
|
if (this.pictureFile === null) return null;
|
|
|
|
try {
|
|
|
|
return URL.createObjectURL(this.pictureFile);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 18:37:31 +02:00
|
|
|
return this.defaultImage?.url;
|
2020-11-20 18:34:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
@Watch("imageSrc")
|
|
|
|
resetImageLoadingError(): void {
|
|
|
|
this.imagePreviewLoadingError = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
showImageLoadingError(): void {
|
|
|
|
this.imagePreviewLoadingError = true;
|
|
|
|
}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2021-04-12 10:43:04 +02:00
|
|
|
// https://gist.github.com/zentala/1e6f72438796d74531803cc3833c039c
|
|
|
|
formatBytes(bytes: number, decimals: number): string {
|
|
|
|
if (bytes == 0) return "0 Bytes";
|
|
|
|
const k = 1024,
|
|
|
|
dm = decimals || 2,
|
|
|
|
sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
|
|
|
|
i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|