PlayButton.vue 5.65 KB
Newer Older
1
<template>
2
  <span :title="title" :class="['ui', {'tiny': discrete}, {'buttons': !dropdownOnly && !iconOnly}]">
3
    <button
4
      v-if="!dropdownOnly"
Eliot Berriot's avatar
Eliot Berriot committed
5
      :title="labels.addToQueue"
6
      @click="addNext(true)"
7
      :disabled="!playable"
8
9
10
      :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>
11
    </button>
12
13
    <div v-if="!discrete && !iconOnly" :class="['ui', {disabled: !playable}, 'floating', 'dropdown', {'icon': !dropdownOnly}, {'button': !dropdownOnly}]">
      <i :class="dropdownIconClasses.concat(['icon'])"></i>
14
      <div class="menu">
15
16
17
        <div class="item" :disabled="!playable" @click="add"><i class="plus icon"></i><translate>Add to queue</translate></div>
        <div class="item" :disabled="!playable" @click="addNext()"><i class="step forward icon"></i><translate>Play next</translate></div>
        <div class="item" :disabled="!playable" @click="addNext(true)"><i class="arrow down icon"></i><translate>Play now</translate></div>
18
19
      </div>
    </div>
20
  </span>
21
22
23
</template>

<script>
24
import axios from 'axios'
25
26
27
28
29
30
31
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},
32
33
34
    dropdownIconClasses: {type: Array, required: false, default: () => { return ['dropdown'] }},
    playIconClass: {type: String, required: false, default: 'play icon'},
    buttonClasses: {type: Array, required: false, default: () => { return ['button'] }},
35
    playlist: {type: Object, required: false},
36
    discrete: {type: Boolean, default: false},
37
38
    dropdownOnly: {type: Boolean, default: false},
    iconOnly: {type: Boolean, default: false},
39
40
    artist: {type: Number, required: false},
    album: {type: Number, required: false}
41
  },
42
43
44
45
46
  data () {
    return {
      isLoading: false
    }
  },
47
  mounted () {
48
    jQuery(this.$el).find('.ui.dropdown').dropdown()
49
  },
50
  computed: {
Eliot Berriot's avatar
Eliot Berriot committed
51
52
53
54
55
    labels () {
      return {
        addToQueue: this.$gettext('Add to current queue')
      }
    },
56
57
    title () {
      if (this.playable) {
Eliot Berriot's avatar
Eliot Berriot committed
58
        return this.$gettext('Play immediatly')
59
60
      } else {
        if (this.track) {
Eliot Berriot's avatar
Eliot Berriot committed
61
          return this.$gettext('This track is not imported and cannot be played')
62
63
64
        }
      }
    },
65
    playable () {
66
      if (this.track) {
67
        return this.track.files.length > 0
68
69
70
71
      } else if (this.tracks) {
        return this.tracks.length > 0
      } else if (this.playlist) {
        return true
72
73
74
75
      } else if (this.artist) {
        return true
      } else if (this.album) {
        return true
76
      }
77
      return false
78
79
80
    }
  },
  methods: {
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    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)
        }
      })
    },
103
104
    getPlayableTracks () {
      let self = this
105
      this.isLoading = true
106
107
108
109
110
111
112
      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 + '/'
Eliot Berriot's avatar
Eliot Berriot committed
113
          axios.get(url + 'tracks/').then((response) => {
114
115
116
117
            resolve(response.data.results.map(plt => {
              return plt.track
            }))
          })
118
        } else if (self.artist) {
119
120
          let params = {'artist': self.artist, 'ordering': 'album__release_date,position'}
          self.getTracksPage(1, params, resolve)
121
        } else if (self.album) {
122
123
          let params = {'album': self.album, 'ordering': 'position'}
          self.getTracksPage(1, params, resolve)
124
125
126
        }
      })
      return getTracks.then((tracks) => {
127
128
129
        setTimeout(e => {
          self.isLoading = false
        }, 250)
130
131
132
133
134
        return tracks.filter(e => {
          return e.files.length > 0
        })
      })
    },
135
    add () {
136
137
      let self = this
      this.getPlayableTracks().then((tracks) => {
138
        self.$store.dispatch('queue/appendMany', {tracks: tracks}).then(() => self.addMessage(tracks))
139
      })
140
141
    },
    addNext (next) {
142
      let self = this
143
      let wasEmpty = this.$store.state.queue.tracks.length === 0
144
      this.getPlayableTracks().then((tracks) => {
145
        self.$store.dispatch('queue/appendMany', {tracks: tracks, index: self.$store.state.queue.currentIndex + 1}).then(() => self.addMessage(tracks))
146
147
        let goNext = next && !wasEmpty
        if (goNext) {
148
149
150
          self.$store.dispatch('queue/next')
        }
      })
151
152
153
154
155
    },
    addMessage (tracks) {
      if (tracks.length < 1) {
        return
      }
Eliot Berriot's avatar
Eliot Berriot committed
156
      let msg = this.$ngettext('%{ count } track was added to your queue', '%{ count } tracks were added to your queue', tracks.length)
157
      this.$store.commit('ui/addMessage', {
Eliot Berriot's avatar
Eliot Berriot committed
158
        content: this.$gettextInterpolate(msg, {count: tracks.length}),
159
160
        date: new Date()
      })
161
162
163
164
165
166
167
168
169
170
171
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
i {
  cursor: pointer;
}
</style>