2019-05-22 12:12:11 +00:00
|
|
|
<template>
|
2019-06-17 15:15:27 +00:00
|
|
|
<div class="root">
|
2021-04-12 08:43:04 +00:00
|
|
|
<figure class="image" v-if="imageSrc && !imagePreviewLoadingError">
|
|
|
|
<img :src="imageSrc" @error="showImageLoadingError" />
|
2019-10-12 17:23:32 +00:00
|
|
|
</figure>
|
|
|
|
<figure class="image is-128x128" v-else>
|
2021-04-12 08:43:04 +00: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 17:23:32 +00:00
|
|
|
</div>
|
2019-06-17 15:15:27 +00:00
|
|
|
</figure>
|
|
|
|
|
2020-11-20 17:34:13 +00:00
|
|
|
<div class="action-buttons">
|
2021-04-12 08:43:04 +00: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 17:34:13 +00: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 14:24:12 +00:00
|
|
|
<b-button
|
|
|
|
type="is-text"
|
|
|
|
v-if="imageSrc"
|
|
|
|
@click="removeOrClearPicture"
|
|
|
|
@keyup.enter="removeOrClearPicture"
|
|
|
|
>
|
2020-11-20 17:34:13 +00:00
|
|
|
{{ $t("Clear") }}
|
|
|
|
</b-button>
|
|
|
|
</div>
|
2019-06-17 15:15:27 +00:00
|
|
|
</div>
|
2019-05-22 12:12:11 +00:00
|
|
|
</template>
|
|
|
|
|
2019-10-12 17:23:32 +00:00
|
|
|
<style scoped lang="scss">
|
2021-11-04 17:14:36 +00:00
|
|
|
@use "@/styles/_mixins" as *;
|
2020-02-18 07:57:00 +00:00
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
figure.image {
|
2021-11-04 17:14:36 +00:00
|
|
|
@include margin-right(30px);
|
2020-02-18 07:57:00 +00:00
|
|
|
max-height: 200px;
|
|
|
|
max-width: 200px;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
.image-placeholder {
|
|
|
|
background-color: grey;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
border-radius: 100%;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
2021-04-12 08:43:04 +00:00
|
|
|
&.error {
|
|
|
|
border: 2px solid red;
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
span {
|
|
|
|
flex: 1;
|
|
|
|
color: #eee;
|
2019-06-17 15:15:27 +00:00
|
|
|
}
|
2020-02-18 07:57:00 +00:00
|
|
|
}
|
2020-11-20 17:34:13 +00:00
|
|
|
|
|
|
|
.action-buttons {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-04-12 08:43:04 +00:00
|
|
|
|
|
|
|
.file {
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.metadata {
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
|
|
.name {
|
|
|
|
max-width: 200px;
|
|
|
|
display: block;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
2021-11-04 17:14:36 +00:00
|
|
|
@include margin-right(5px);
|
2021-04-12 08:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.picture-too-big {
|
|
|
|
color: $danger;
|
2020-11-20 17:34:13 +00:00
|
|
|
}
|
2019-06-17 15:15:27 +00:00
|
|
|
</style>
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
<script lang="ts">
|
2020-11-26 10:41:13 +00:00
|
|
|
import { IMedia } from "@/types/media.model";
|
2020-02-18 07:57:00 +00:00
|
|
|
import { Component, Model, Prop, Vue, Watch } from "vue-property-decorator";
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
@Component
|
|
|
|
export default class PictureUpload extends Vue {
|
2020-02-18 07:57:00 +00:00
|
|
|
@Model("change", { type: File }) readonly pictureFile!: File;
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
@Prop({ type: Object, required: false }) defaultImage!: IMedia;
|
2020-09-29 07:53:48 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
@Prop({
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: "image/gif,image/png,image/jpeg,image/webp",
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
accept!: string;
|
|
|
|
|
|
|
|
@Prop({
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default() {
|
2020-09-29 07:53:48 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2020-02-18 07:57:00 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return this.$t("Avatar");
|
|
|
|
},
|
|
|
|
})
|
|
|
|
textFallback!: string;
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2021-04-12 08:43:04 +00:00
|
|
|
@Prop({ type: Number, required: false, default: 10_485_760 })
|
|
|
|
maxSize!: number;
|
2020-11-20 17:34:13 +00:00
|
|
|
|
|
|
|
file!: File | null;
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2021-04-12 08:43:04 +00:00
|
|
|
imagePreviewLoadingError = false;
|
2019-09-09 09:21:42 +00:00
|
|
|
|
2021-04-12 08:43:04 +00:00
|
|
|
get pictureTooBig(): boolean {
|
|
|
|
return this.pictureFile?.size > this.maxSize;
|
2019-06-17 15:15:27 +00:00
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2021-04-12 08:43:04 +00: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 16:37:31 +00:00
|
|
|
return this.defaultImage?.url;
|
2020-11-20 17:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onFileChanged(file: File | null): void {
|
2020-02-18 07:57:00 +00:00
|
|
|
this.$emit("change", file);
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2020-11-20 17:34:13 +00:00
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeOrClearPicture(): Promise<void> {
|
|
|
|
this.onFileChanged(null);
|
2019-06-17 15:15:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 08:43:04 +00:00
|
|
|
@Watch("imageSrc")
|
|
|
|
resetImageLoadingError(): void {
|
|
|
|
this.imagePreviewLoadingError = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
showImageLoadingError(): void {
|
|
|
|
this.imagePreviewLoadingError = true;
|
|
|
|
}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2021-04-12 08:43:04 +00: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 12:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|