2017-06-23 21:00:42 +00:00
|
|
|
<template>
|
2018-07-17 11:09:13 +00:00
|
|
|
<span :title="title" :class="['ui', {'tiny': discrete}, {'buttons': !dropdownOnly && !iconOnly}]">
|
2018-03-05 20:51:40 +00:00
|
|
|
<button
|
2018-07-17 11:09:13 +00:00
|
|
|
v-if="!dropdownOnly"
|
2018-09-06 17:39:38 +00:00
|
|
|
:title="labels.playNow"
|
2018-05-06 21:50:09 +00:00
|
|
|
@click="addNext(true)"
|
2018-05-10 15:52:13 +00:00
|
|
|
:disabled="!playable"
|
2018-07-17 11:09:13 +00:00
|
|
|
:class="buttonClasses.concat(['ui', {loading: isLoading}, {'mini': discrete}, {disabled: !playable}])">
|
|
|
|
<i :class="[playIconClass, 'icon']"></i>
|
|
|
|
<template v-if="!discrete && !iconOnly"><slot><translate>Play</translate></slot></template>
|
2017-06-23 21:00:42 +00:00
|
|
|
</button>
|
2018-07-17 11:09:13 +00:00
|
|
|
<div v-if="!discrete && !iconOnly" :class="['ui', {disabled: !playable}, 'floating', 'dropdown', {'icon': !dropdownOnly}, {'button': !dropdownOnly}]">
|
|
|
|
<i :class="dropdownIconClasses.concat(['icon'])"></i>
|
2017-06-23 21:00:42 +00:00
|
|
|
<div class="menu">
|
2018-09-06 17:39:38 +00:00
|
|
|
<div class="item" :disabled="!playable" @click="add" :title="labels.addToQueue"><i class="plus icon"></i><translate>Add to queue</translate></div>
|
|
|
|
<div class="item" :disabled="!playable" @click="addNext()" :title="labels.playNext"><i class="step forward icon"></i><translate>Play next</translate></div>
|
2018-09-22 13:48:15 +00:00
|
|
|
<div class="item" :disabled="!playable" @click="addNext(true)" :title="labels.playNow"><i class="play icon"></i><translate>Play now</translate></div>
|
2017-06-23 21:00:42 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2018-07-17 11:09:13 +00:00
|
|
|
</span>
|
2017-06-23 21:00:42 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-03-21 10:58:26 +00:00
|
|
|
import axios from 'axios'
|
2017-06-23 21:00:42 +00:00
|
|
|
import jQuery from 'jquery'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
// we can either have a single or multiple tracks to play when clicked
|
|
|
|
tracks: {type: Array, required: false},
|
|
|
|
track: {type: Object, required: false},
|
2018-07-17 11:09:13 +00:00
|
|
|
dropdownIconClasses: {type: Array, required: false, default: () => { return ['dropdown'] }},
|
|
|
|
playIconClass: {type: String, required: false, default: 'play icon'},
|
|
|
|
buttonClasses: {type: Array, required: false, default: () => { return ['button'] }},
|
2018-03-21 10:58:26 +00:00
|
|
|
playlist: {type: Object, required: false},
|
2018-05-22 20:22:53 +00:00
|
|
|
discrete: {type: Boolean, default: false},
|
2018-07-17 11:09:13 +00:00
|
|
|
dropdownOnly: {type: Boolean, default: false},
|
|
|
|
iconOnly: {type: Boolean, default: false},
|
2018-05-22 20:22:53 +00:00
|
|
|
artist: {type: Number, required: false},
|
2018-09-06 18:35:02 +00:00
|
|
|
album: {type: Number, required: false},
|
|
|
|
isPlayable: {type: Boolean, required: false, default: null}
|
2017-06-23 21:00:42 +00:00
|
|
|
},
|
2018-03-05 20:51:40 +00:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
},
|
2017-06-23 21:00:42 +00:00
|
|
|
mounted () {
|
2018-05-06 21:50:09 +00:00
|
|
|
jQuery(this.$el).find('.ui.dropdown').dropdown()
|
2017-06-23 21:00:42 +00:00
|
|
|
},
|
2018-02-19 19:14:39 +00:00
|
|
|
computed: {
|
2018-07-01 19:50:50 +00:00
|
|
|
labels () {
|
|
|
|
return {
|
2018-09-06 17:39:38 +00:00
|
|
|
playNow: this.$gettext('Play now'),
|
|
|
|
addToQueue: this.$gettext('Add to current queue'),
|
|
|
|
playNext: this.$gettext('Play next')
|
2018-07-01 19:50:50 +00:00
|
|
|
}
|
|
|
|
},
|
2018-05-10 15:52:13 +00:00
|
|
|
title () {
|
|
|
|
if (this.playable) {
|
2018-09-22 13:48:15 +00:00
|
|
|
return this.$gettext('Play now')
|
2018-05-10 15:52:13 +00:00
|
|
|
} else {
|
|
|
|
if (this.track) {
|
2018-09-06 18:35:02 +00:00
|
|
|
return this.$gettext('This track is not available in any library you have access to')
|
2018-05-10 15:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-03-21 10:58:26 +00:00
|
|
|
playable () {
|
2018-09-06 18:35:02 +00:00
|
|
|
if (this.isPlayable) {
|
|
|
|
return true
|
|
|
|
}
|
2017-06-23 21:00:42 +00:00
|
|
|
if (this.track) {
|
2018-09-06 18:35:02 +00:00
|
|
|
return this.track.is_playable
|
2018-03-21 10:58:26 +00:00
|
|
|
} else if (this.tracks) {
|
2018-09-06 18:35:02 +00:00
|
|
|
return this.tracks.filter((t) => {
|
|
|
|
return t.is_playable
|
|
|
|
}).length > 0
|
2017-06-23 21:00:42 +00:00
|
|
|
}
|
2018-03-21 10:58:26 +00:00
|
|
|
return false
|
2018-02-19 19:14:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2018-06-05 17:11:44 +00:00
|
|
|
getTracksPage (page, params, resolve, tracks) {
|
|
|
|
if (page > 10) {
|
|
|
|
// it's 10 * 100 tracks already, let's stop here
|
|
|
|
resolve(tracks)
|
|
|
|
}
|
|
|
|
// when fetching artists/or album tracks, sometimes, we may have to fetch
|
|
|
|
// multiple pages
|
|
|
|
let self = this
|
|
|
|
params['page_size'] = 100
|
|
|
|
params['page'] = page
|
|
|
|
tracks = tracks || []
|
|
|
|
axios.get('tracks/', {params: params}).then((response) => {
|
|
|
|
response.data.results.forEach(t => {
|
|
|
|
tracks.push(t)
|
|
|
|
})
|
|
|
|
if (response.data.next) {
|
|
|
|
self.getTracksPage(page + 1, params, resolve, tracks)
|
|
|
|
} else {
|
|
|
|
resolve(tracks)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-03-21 10:58:26 +00:00
|
|
|
getPlayableTracks () {
|
|
|
|
let self = this
|
2018-06-05 17:11:44 +00:00
|
|
|
this.isLoading = true
|
2018-03-21 10:58:26 +00:00
|
|
|
let getTracks = new Promise((resolve, reject) => {
|
|
|
|
if (self.track) {
|
|
|
|
resolve([self.track])
|
|
|
|
} else if (self.tracks) {
|
|
|
|
resolve(self.tracks)
|
|
|
|
} else if (self.playlist) {
|
|
|
|
let url = 'playlists/' + self.playlist.id + '/'
|
2018-07-27 17:49:53 +00:00
|
|
|
axios.get(url + 'tracks/').then((response) => {
|
2018-03-21 10:58:26 +00:00
|
|
|
resolve(response.data.results.map(plt => {
|
|
|
|
return plt.track
|
|
|
|
}))
|
|
|
|
})
|
2018-05-22 20:22:53 +00:00
|
|
|
} else if (self.artist) {
|
2018-06-05 17:11:44 +00:00
|
|
|
let params = {'artist': self.artist, 'ordering': 'album__release_date,position'}
|
|
|
|
self.getTracksPage(1, params, resolve)
|
2018-05-22 20:22:53 +00:00
|
|
|
} else if (self.album) {
|
2018-06-05 17:11:44 +00:00
|
|
|
let params = {'album': self.album, 'ordering': 'position'}
|
|
|
|
self.getTracksPage(1, params, resolve)
|
2018-03-21 10:58:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return getTracks.then((tracks) => {
|
2018-06-05 17:11:44 +00:00
|
|
|
setTimeout(e => {
|
|
|
|
self.isLoading = false
|
|
|
|
}, 250)
|
2018-03-21 10:58:26 +00:00
|
|
|
return tracks.filter(e => {
|
2018-09-06 18:35:02 +00:00
|
|
|
return e.is_playable === true
|
2018-03-21 10:58:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2018-02-19 19:14:39 +00:00
|
|
|
add () {
|
2018-03-21 10:58:26 +00:00
|
|
|
let self = this
|
|
|
|
this.getPlayableTracks().then((tracks) => {
|
2018-06-07 11:06:21 +00:00
|
|
|
self.$store.dispatch('queue/appendMany', {tracks: tracks}).then(() => self.addMessage(tracks))
|
2018-03-21 10:58:26 +00:00
|
|
|
})
|
2017-06-23 21:00:42 +00:00
|
|
|
},
|
|
|
|
addNext (next) {
|
2018-03-21 10:58:26 +00:00
|
|
|
let self = this
|
2018-05-06 21:50:09 +00:00
|
|
|
let wasEmpty = this.$store.state.queue.tracks.length === 0
|
2018-03-21 10:58:26 +00:00
|
|
|
this.getPlayableTracks().then((tracks) => {
|
2018-06-07 11:06:21 +00:00
|
|
|
self.$store.dispatch('queue/appendMany', {tracks: tracks, index: self.$store.state.queue.currentIndex + 1}).then(() => self.addMessage(tracks))
|
2018-05-06 21:50:09 +00:00
|
|
|
let goNext = next && !wasEmpty
|
|
|
|
if (goNext) {
|
2018-03-21 10:58:26 +00:00
|
|
|
self.$store.dispatch('queue/next')
|
|
|
|
}
|
|
|
|
})
|
2018-06-07 11:06:21 +00:00
|
|
|
},
|
|
|
|
addMessage (tracks) {
|
|
|
|
if (tracks.length < 1) {
|
|
|
|
return
|
|
|
|
}
|
2018-06-30 13:28:47 +00:00
|
|
|
let msg = this.$ngettext('%{ count } track was added to your queue', '%{ count } tracks were added to your queue', tracks.length)
|
2018-06-07 11:06:21 +00:00
|
|
|
this.$store.commit('ui/addMessage', {
|
2018-06-30 13:28:47 +00:00
|
|
|
content: this.$gettextInterpolate(msg, {count: tracks.length}),
|
2018-06-07 11:06:21 +00:00
|
|
|
date: new Date()
|
|
|
|
})
|
2017-06-23 21:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<!-- Add "scoped" attribute to limit CSS to this component only -->
|
|
|
|
<style scoped>
|
|
|
|
i {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
</style>
|