2020-05-31 21:17:10 +02:00
|
|
|
<template>
|
2021-01-30 17:17:04 +01:00
|
|
|
<create-edit
|
2021-01-17 18:57:57 +01:00
|
|
|
title="Set list background"
|
2021-01-30 17:17:04 +01:00
|
|
|
primary-label=""
|
|
|
|
:loading="backgroundService.loading"
|
|
|
|
class="list-background-setting"
|
|
|
|
:wide="true"
|
|
|
|
v-if="uploadBackgroundEnabled || unsplashBackgroundEnabled"
|
2021-03-21 18:11:24 +01:00
|
|
|
:tertary="hasBackground ? 'Remove Background' : ''"
|
|
|
|
@tertary="removeBackground()"
|
2021-01-17 18:57:57 +01:00
|
|
|
>
|
|
|
|
<div class="mb-4" v-if="uploadBackgroundEnabled">
|
|
|
|
<input
|
|
|
|
@change="uploadBackground"
|
|
|
|
accept="image/*"
|
|
|
|
class="is-hidden"
|
|
|
|
ref="backgroundUploadInput"
|
|
|
|
type="file"
|
|
|
|
/>
|
|
|
|
<x-button
|
|
|
|
:loading="backgroundUploadService.loading"
|
|
|
|
@click="$refs.backgroundUploadInput.click()"
|
|
|
|
type="primary"
|
|
|
|
>
|
|
|
|
Choose a background from your pc
|
|
|
|
</x-button>
|
|
|
|
</div>
|
|
|
|
<template v-if="unsplashBackgroundEnabled">
|
|
|
|
<input
|
|
|
|
:class="{'is-loading': backgroundService.loading}"
|
|
|
|
@keyup="() => newBackgroundSearch()"
|
|
|
|
class="input is-expanded"
|
|
|
|
placeholder="Search for a background..."
|
|
|
|
type="text"
|
|
|
|
v-model="backgroundSearchTerm"
|
|
|
|
/>
|
|
|
|
<p class="unsplash-link"><a href="https://unsplash.com" target="_blank">Powered by Unsplash</a></p>
|
|
|
|
<div class="image-search-result">
|
2020-06-11 19:27:21 +02:00
|
|
|
<a
|
2021-01-17 18:57:57 +01:00
|
|
|
:key="im.id"
|
|
|
|
:style="{'background-image': `url(${backgroundThumbs[im.id]})`}"
|
|
|
|
@click="() => setBackground(im.id)"
|
|
|
|
class="image"
|
|
|
|
v-for="im in backgroundSearchResult">
|
|
|
|
<a :href="`https://unsplash.com/@${im.info.author}`" target="_blank" class="info">
|
|
|
|
{{ im.info.authorName }}
|
2020-05-31 21:17:10 +02:00
|
|
|
</a>
|
|
|
|
</a>
|
|
|
|
</div>
|
2021-01-17 18:57:57 +01:00
|
|
|
<x-button
|
|
|
|
:disabled="backgroundService.loading"
|
|
|
|
@click="() => searchBackgrounds(currentPage + 1)"
|
|
|
|
class="is-load-more-button mt-4"
|
|
|
|
:shadow="false"
|
|
|
|
type="secondary"
|
|
|
|
v-if="backgroundSearchResult.length > 0"
|
|
|
|
>
|
2021-03-21 18:11:24 +01:00
|
|
|
{{ backgroundService.loading ? 'Loading...' : 'Load more photos' }}
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
|
|
|
</template>
|
2021-01-30 17:17:04 +01:00
|
|
|
</create-edit>
|
2020-05-31 21:17:10 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-03-21 18:11:24 +01:00
|
|
|
import {mapState} from 'vuex'
|
2020-09-05 22:35:52 +02:00
|
|
|
import BackgroundUnsplashService from '../../../services/backgroundUnsplash'
|
|
|
|
import BackgroundUploadService from '../../../services/backgroundUpload'
|
2021-03-21 18:11:24 +01:00
|
|
|
import ListService from '@/services/list'
|
2020-09-05 22:35:52 +02:00
|
|
|
import {CURRENT_LIST} from '@/store/mutation-types'
|
2021-01-30 17:17:04 +01:00
|
|
|
import CreateEdit from '@/components/misc/create-edit'
|
2020-05-31 21:17:10 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
export default {
|
2021-01-30 17:17:04 +01:00
|
|
|
name: 'list-setting-background',
|
|
|
|
components: {CreateEdit},
|
2020-09-05 22:35:52 +02:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
backgroundSearchTerm: '',
|
|
|
|
backgroundSearchResult: [],
|
|
|
|
backgroundService: null,
|
|
|
|
backgroundThumbs: {},
|
|
|
|
currentPage: 1,
|
|
|
|
backgroundSearchTimeout: null,
|
2020-06-11 19:27:21 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
backgroundUploadService: null,
|
2021-03-21 18:11:24 +01:00
|
|
|
listService: null,
|
2020-09-05 22:35:52 +02:00
|
|
|
}
|
|
|
|
},
|
2021-03-21 18:11:24 +01:00
|
|
|
computed: mapState({
|
|
|
|
unsplashBackgroundEnabled: state => state.config.enabledBackgroundProviders.includes('unsplash'),
|
|
|
|
uploadBackgroundEnabled: state => state.config.enabledBackgroundProviders.includes('upload'),
|
|
|
|
currentList: state => state.currentList,
|
|
|
|
hasBackground: state => state.background !== null,
|
|
|
|
}),
|
2020-09-05 22:35:52 +02:00
|
|
|
created() {
|
|
|
|
this.backgroundService = new BackgroundUnsplashService()
|
|
|
|
this.backgroundUploadService = new BackgroundUploadService()
|
2021-03-21 18:11:24 +01:00
|
|
|
this.listService = new ListService()
|
2021-01-30 17:17:04 +01:00
|
|
|
this.setTitle('Set a list background')
|
2020-09-05 22:35:52 +02:00
|
|
|
// Show the default collection of backgrounds
|
|
|
|
this.newBackgroundSearch()
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
newBackgroundSearch() {
|
|
|
|
if (!this.unsplashBackgroundEnabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// This is an extra method to reset a few things when searching to not break loading more photos.
|
|
|
|
this.$set(this, 'backgroundSearchResult', [])
|
|
|
|
this.$set(this, 'backgroundThumbs', {})
|
|
|
|
this.searchBackgrounds()
|
2020-05-31 21:17:10 +02:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
searchBackgrounds(page = 1) {
|
2020-05-31 21:17:10 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (this.backgroundSearchTimeout !== null) {
|
|
|
|
clearTimeout(this.backgroundSearchTimeout)
|
|
|
|
}
|
2020-05-31 21:17:10 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
// We're using the timeout to not search on every keypress but with a 300ms delay.
|
|
|
|
// If another key is pressed within these 300ms, the last search request is dropped and a new one is scheduled.
|
|
|
|
this.backgroundSearchTimeout = setTimeout(() => {
|
|
|
|
this.currentPage = page
|
|
|
|
this.backgroundService.getAll({}, {s: this.backgroundSearchTerm, p: page})
|
|
|
|
.then(r => {
|
|
|
|
this.backgroundSearchResult = this.backgroundSearchResult.concat(r)
|
|
|
|
r.forEach(b => {
|
|
|
|
this.backgroundService.thumb(b)
|
|
|
|
.then(t => {
|
|
|
|
this.$set(this.backgroundThumbs, b.id, t)
|
|
|
|
})
|
2020-05-31 21:17:10 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
2020-09-05 22:35:52 +02:00
|
|
|
}, 300)
|
|
|
|
},
|
|
|
|
setBackground(backgroundId) {
|
|
|
|
// Don't set a background if we're in the process of setting one
|
|
|
|
if (this.backgroundService.loading) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-11 19:27:21 +02:00
|
|
|
|
2021-01-30 17:17:04 +01:00
|
|
|
this.backgroundService.update({id: backgroundId, listId: this.$route.params.listId})
|
2020-09-05 22:35:52 +02:00
|
|
|
.then(l => {
|
|
|
|
this.$store.commit(CURRENT_LIST, l)
|
|
|
|
this.$store.commit('namespaces/setListInNamespaceById', l)
|
|
|
|
this.success({message: 'The background has been set successfully!'}, this)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
uploadBackground() {
|
|
|
|
if (this.$refs.backgroundUploadInput.files.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:17:04 +01:00
|
|
|
this.backgroundUploadService.create(this.$route.params.listId, this.$refs.backgroundUploadInput.files[0])
|
2020-09-05 22:35:52 +02:00
|
|
|
.then(l => {
|
|
|
|
this.$store.commit(CURRENT_LIST, l)
|
|
|
|
this.$store.commit('namespaces/setListInNamespaceById', l)
|
|
|
|
this.success({message: 'The background has been set successfully!'}, this)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
2020-05-31 21:17:10 +02:00
|
|
|
},
|
2021-03-21 18:11:24 +01:00
|
|
|
removeBackground() {
|
|
|
|
this.listService.removeBackground(this.currentList)
|
|
|
|
.then(l => {
|
|
|
|
this.$store.commit(CURRENT_LIST, l)
|
|
|
|
this.$store.commit('namespaces/setListInNamespaceById', l)
|
|
|
|
this.success({message: 'The background has been removed successfully!'}, this)
|
|
|
|
this.$router.back()
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
}
|
2020-05-31 21:17:10 +02:00
|
|
|
</script>
|