gamebrary/src/pages/Wallpapers.vue

186 lines
4.8 KiB
Vue
Raw Normal View History

<template lang="html">
2020-11-21 06:32:14 +00:00
<!-- TODO: show space used -->
<!-- TODO: allow to apply wallpaper to board from here -->
2020-11-21 14:31:02 +00:00
<b-container class="pt-3">
2020-11-21 06:32:14 +00:00
<!-- TODO: translate "browse" -->
<!-- TODO: add skeleton -->
<!-- TODO: add progress bar -->
<!-- TODO: sort by -->
2020-11-21 14:31:02 +00:00
<div class="d-flex justify-content-between align-items-center mb-2">
2020-11-21 06:32:14 +00:00
<h2>{{ $t('wallpapers.title') }}</h2>
<b-button
variant="primary"
2020-11-21 14:31:02 +00:00
@click="triggerFileUpload"
2020-11-21 06:32:14 +00:00
>
2020-11-21 14:31:02 +00:00
<b-spinner
label="Spinning"
v-if="saving"
/>
<span v-else>
{{ $t('wallpapers.form.label') }}
</span>
2020-11-21 06:32:14 +00:00
</b-button>
</div>
<b-form-file
2020-11-21 14:31:02 +00:00
class="d-none file-input"
2020-11-21 06:32:14 +00:00
v-model="file"
accept="image/*"
:browse-text="$t('wallpapers.form.upload')"
:placeholder="$t('wallpapers.form.placeholder')"
@input="uploadWallpaper"
2020-09-26 00:09:20 +00:00
/>
2020-11-21 06:32:14 +00:00
<b-alert v-if="isDuplicate && !saving && file && file.name" show variant="warning">
{{ $t('wallpapers.form.duplicateMessage', { fileName: file.name }) }}
</b-alert>
<!-- <h5>{{ $t('wallpapers.list.title') }}</h5> -->
2020-11-21 14:31:02 +00:00
<!-- <small class="d-block text-center">{{ formattedSpaceUsed }} of 64MB used</small> -->
<!-- <b-progress :value="spaceUsed" max="67108864" variant="success" class="mb-3" /> -->
2020-11-21 06:32:14 +00:00
<b-card
v-if="wallpapers.length"
v-for="wallpaper in wallpapers"
:key="wallpaper.name"
:img-src="wallpaper.url"
:img-alt="wallpaper.name"
img-left
img-width="180"
class="mb-3"
>
2020-11-21 14:31:02 +00:00
<p>{{ wallpaper.name }}
<b-badge v-if="wallpaper.metadata">{{ bytesToSize(wallpaper.metadata.size) }}</b-badge>
</p>
2020-11-21 06:32:14 +00:00
<b-button
variant="danger"
size="sm"
@click="confirmDeleteWallpaper(wallpaper)"
2020-10-14 00:35:40 +00:00
>
2020-11-21 06:32:14 +00:00
<icon name="trash" white />
</b-button>
</b-card>
<b-alert show v-else>You don't have any wallpapers.</b-alert>
</b-container>
</template>
<script>
2020-10-14 00:35:40 +00:00
import { mapState, mapGetters } from 'vuex';
export default {
data() {
return {
file: null,
saving: false,
loading: false,
2020-11-21 06:32:14 +00:00
isPaid: true,
wallpaperUrls: [],
};
},
computed: {
...mapState(['user', 'board', 'wallpapers']),
2020-10-14 00:35:40 +00:00
...mapGetters(['nightMode']),
existingFiles() {
return this.wallpapers.map(({ name }) => name);
},
isDuplicate() {
const { file, existingFiles } = this;
return file && file.name && existingFiles.includes(file.name);
},
2020-11-21 06:32:14 +00:00
formattedSpaceUsed() {
return this.spaceUsed
? this.bytesToSize(this.spaceUsed)
: null;
},
2020-11-21 14:31:02 +00:00
// spaceUsed() {
// return this.wallpapers.reduce((total, file) => total + file.metadata.size, 0);
// },
},
methods: {
2020-11-21 14:31:02 +00:00
triggerFileUpload() {
document.querySelector('.file-input input').click();
},
2020-11-02 20:24:11 +00:00
bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
2020-11-02 20:37:38 +00:00
if (bytes === 0) return '0 Byte';
2020-11-02 20:24:11 +00:00
2020-11-02 20:37:38 +00:00
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 0);
2020-11-02 20:24:11 +00:00
2020-11-02 20:37:38 +00:00
return `${Math.round(bytes / (1024 ** i), 2)} ${sizes[i]}`;
2020-11-02 20:24:11 +00:00
},
2020-11-01 16:20:48 +00:00
uploadWallpaper() {
2020-10-31 17:43:20 +00:00
if (this.isDuplicate) {
return this.$bvToast.toast('File already exists', { title: '!', variant: 'warning' });
}
if (!this.file) {
2020-11-01 16:20:48 +00:00
return false;
2020-10-31 17:43:20 +00:00
}
this.saving = true;
2020-11-01 16:20:48 +00:00
return this.$store.dispatch('UPLOAD_WALLPAPER', this.file)
2020-10-31 17:43:20 +00:00
.then(() => {
this.$bvToast.toast('File uploaded', { title: 'Success', variant: 'success' });
this.file = null;
this.saving = false;
this.$bus.$emit('WALLPAPER_UPLOADED');
})
.catch(() => {
this.saving = false;
this.$bvToast.toast('There was an error uploading wallpaper', { title: 'Error', variant: 'danger' });
});
},
confirmDeleteWallpaper(file) {
this.$bvModal.msgBoxConfirm('Wallpaper will be permanently removed', {
title: 'Are you sure you want to delete this file?',
okVariant: 'danger',
okTitle: 'Yes',
})
.then((value) => {
if (value) {
this.deleteFile(file);
}
});
},
async deleteFile(file) {
await this.$store.dispatch('DELETE_WALLPAPER', file)
.catch(() => {
this.$bvToast.toast('There was an error deleting wallpaper', { title: 'Error', variant: 'danger' });
});
this.$bvToast.toast(file.name, { title: 'File deleted', variant: 'success' });
// const { board } = this;
// TODO: handle wallpapers in use
// if (board.wallpaper && this.board.wallpaper === file.path) {
// this.$bus.$emit('RELOAD_WALLPAPER');
// }
},
},
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
.delete-file {
bottom: .5rem;
right: .5rem;
}
</style>