gamebrary/src/components/WallpapersList.vue

141 lines
3.1 KiB
Vue
Raw Normal View History

2021-02-18 00:50:37 +00:00
<template lang="html">
2021-02-20 16:53:38 +00:00
<div class="wallpapers">
2021-02-21 18:17:58 +00:00
<preview-wallpaper-modal
:wallpaper="activeWallpaper"
:selectable="selectable"
@selected="selected"
2021-02-21 18:17:58 +00:00
/>
2021-02-21 18:13:06 +00:00
2021-02-18 00:50:37 +00:00
<b-card
v-for="wallpaper in wallpapers"
:key="wallpaper.name"
:img-src="wallpaper.url"
:img-alt="wallpaper.name"
img-top
bg-variant="transparent"
img-width="180"
2021-02-21 18:13:06 +00:00
class="mb-3 overflow-hidden word-wrap clickable"
@click="openModal(wallpaper)"
2021-02-18 00:50:37 +00:00
>
<h6>
{{ wallpaper.name }}
<b-badge v-if="wallpaper.metadata && wallpaper.metadata.size">
{{ bytesToSize(wallpaper.metadata.size) }}
</b-badge>
</h6>
<b-button
variant="danger"
@click.stop="confirmDeleteWallpaper(wallpaper)"
2021-02-18 00:50:37 +00:00
>
<i class="fas fa-trash-alt fa-fw" aria-hidden />
</b-button>
<b-button
v-if="selectable"
variant="primary"
@click.stop="selected(wallpaper)"
2021-02-18 00:50:37 +00:00
>
<i
v-if="saving"
class="fas fa-sync fa-spin fa-fw"
aria-hidden
/>
<span v-else>Select</span>
</b-button>
</b-card>
</div>
</template>
<script>
2021-02-21 18:13:06 +00:00
import PreviewWallpaperModal from '@/components/Wallpapers/PreviewWallpaperModal';
2021-02-18 00:50:37 +00:00
import { mapState } from 'vuex';
export default {
2021-02-21 18:13:06 +00:00
components: {
PreviewWallpaperModal,
},
2021-02-18 00:50:37 +00:00
props: {
selectable: Boolean,
saving: Boolean,
},
2021-02-21 18:13:06 +00:00
data() {
return {
activeWallpaper: false,
};
},
2021-02-18 00:50:37 +00:00
computed: {
...mapState(['wallpapers']),
},
methods: {
2021-02-21 18:13:06 +00:00
openModal(wallpaper) {
this.activeWallpaper = wallpaper;
this.$bvModal.show('previewWallpaper');
},
selected(wallpaper) {
2021-02-18 00:50:37 +00:00
if (this.selectable) {
this.$emit('selected', wallpaper);
}
},
bytesToSize(bytes) {
// TODO: put in util
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 0);
return `${Math.round(bytes / (1024 ** i), 2)} ${sizes[i]}`;
},
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', { variant: 'danger' });
});
this.$bvToast.toast('File deleted');
// 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>
.wallpapers {
display: grid;
grid-column-gap: 1rem;
grid-template-columns: 1fr 1fr 1fr;
@media(max-width: 720px) {
grid-template-columns: 1fr 1fr;
}
}
</style>