Queue.vue 16.6 KB
Newer Older
1
<template>
Agate's avatar
Agate committed
2
  <section class="main with-background component-queue" :aria-label="labels.queue">
3
4
5
    <div :class="['ui vertical stripe queue segment', playerFocused ? 'player-focused' : '']">
      <div class="ui fluid container">
        <div class="ui stackable grid" id="queue-grid">
Agate's avatar
Agate committed
6
          <div class="ui six wide column current-track">
7
8
            <div class="ui basic segment" id="player">
              <template v-if="currentTrack">
9
10
                <img ref="cover" alt="" v-if="currentTrack.cover && currentTrack.cover.urls.large_square_crop" :src="$store.getters['instance/absoluteUrl'](currentTrack.cover.urls.large_square_crop)">
                <img ref="cover" alt="" v-else-if="currentTrack.album && currentTrack.album.cover && currentTrack.album.cover.urls.large_square_crop" :src="$store.getters['instance/absoluteUrl'](currentTrack.album.cover.urls.large_square_crop)">
11
                <img class="ui image" alt="" v-else src="../assets/audio/default-cover.png">
12
                <h1 class="ui header">
Ciarán Ainsworth's avatar
Ciarán Ainsworth committed
13
14
15
                  <div class="content ellipsis">
                    <router-link class="small header discrete link track" :to="{name: 'library.tracks.detail', params: {id: currentTrack.id }}">
                      {{ currentTrack.title }}
16
                    </router-link>
Ciarán Ainsworth's avatar
Ciarán Ainsworth committed
17
                    <div class="sub header ellipsis">
18
19
20
21
                      <router-link class="discrete link artist" :to="{name: 'library.artists.detail', params: {id: currentTrack.artist.id }}">{{ currentTrack.artist.name }}</router-link>
                      <template v-if="currentTrack.album"> /
                        <router-link class="discrete link album" :to="{name: 'library.albums.detail', params: {id: currentTrack.album.id }}">{{ currentTrack.album.title }}</router-link>
                      </template>
22
23
24
25
                    </div>
                  </div>
                </h1>
                <div class="ui small warning message" v-if="currentTrack && errored">
26
                  <h3 class="header">
27
                    <translate translate-context="Sidebar/Player/Error message.Title">The track cannot be loaded</translate>
28
                  </h3>
29
30
31
32
33
34
35
36
                  <p v-if="hasNext && playing && $store.state.player.errorCount < $store.state.player.maxConsecutiveErrors">
                    <translate translate-context="Sidebar/Player/Error message.Paragraph">The next track will play automatically in a few seconds…</translate>
                    <i class="loading spinner icon"></i>
                  </p>
                  <p>
                    <translate translate-context="Sidebar/Player/Error message.Paragraph">You may have a connectivity issue.</translate>
                  </p>
                </div>
37
                <div class="additional-controls tablet-and-below">
38
39
40
41
42
43
44
45
46
                  <track-favorite-icon
                    v-if="$store.state.auth.authenticated"
                    :track="currentTrack"></track-favorite-icon>
                  <track-playlist-icon
                    v-if="$store.state.auth.authenticated"
                    :track="currentTrack"></track-playlist-icon>
                  <button
                    v-if="$store.state.auth.authenticated"
                    @click="$store.dispatch('moderation/hide', {type: 'artist', target: currentTrack.artist})"
47
                    :class="['ui', 'really', 'basic', 'circular', 'icon', 'button']"
48
49
50
51
52
53
54
55
56
                    :aria-label="labels.addArtistContentFilter"
                    :title="labels.addArtistContentFilter">
                    <i :class="['eye slash outline', 'basic', 'icon']"></i>
                  </button>
                </div>
                <div class="progress-wrapper">
                  <div class="progress-area" v-if="currentTrack && !errored">
                    <div
                      ref="progress"
Agate's avatar
Agate committed
57
                      :class="['ui', 'small', 'vibrant', {'indicating': isLoadingAudio}, 'progress']"
58
59
60
61
62
63
64
65
                      @click="touchProgress">
                      <div class="buffer bar" :data-percent="bufferProgress" :style="{ 'width': bufferProgress + '%' }"></div>
                      <div class="position bar" :data-percent="progress" :style="{ 'width': progress + '%' }"></div>
                    </div>
                  </div>
                  <div class="progress-area" v-else>
                    <div
                      ref="progress"
Agate's avatar
Agate committed
66
                      :class="['ui', 'small', 'vibrant', 'progress']">
67
68
69
70
71
72
                      <div class="buffer bar"></div>
                      <div class="position bar"></div>
                    </div>
                  </div>
                  <div class="progress">
                    <template v-if="!isLoadingAudio">
Agate's avatar
Agate committed
73
                      <a href="" :aria-label="labels.restart" class="left floated timer discrete start" @click.prevent="setCurrentTime(0)">{{currentTimeFormatted}}</a>
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
                      <span class="right floated timer total">{{durationFormatted}}</span>
                    </template>
                    <template v-else>
                      <span class="left floated timer">00:00</span>
                      <span class="right floated timer">00:00</span>
                    </template>
                  </div>
                </div>
                <div class="player-controls tablet-and-below">
                  <template>
                    <span
                      role="button"
                      :title="labels.previousTrack"
                      :aria-label="labels.previousTrack"
                      class="control"
                      @click.prevent.stop="$store.dispatch('queue/previous')"
                      :disabled="emptyQueue">
                        <i :class="['ui', 'backward step', {'disabled': emptyQueue}, 'icon']"></i>
                    </span>

                    <span
                      role="button"
                      v-if="!playing"
                      :title="labels.play"
                      :aria-label="labels.play"
                      @click.prevent.stop="togglePlay"
                      class="control">
                        <i :class="['ui', 'play', {'disabled': !currentTrack}, 'icon']"></i>
                    </span>
                    <span
                      role="button"
                      v-else
                      :title="labels.pause"
                      :aria-label="labels.pause"
                      @click.prevent.stop="togglePlay"
                      class="control">
                        <i :class="['ui', 'pause', {'disabled': !currentTrack}, 'icon']"></i>
                    </span>
                    <span
                      role="button"
                      :title="labels.next"
                      :aria-label="labels.next"
                      class="control"
                      @click.prevent.stop="$store.dispatch('queue/next')"
                      :disabled="!hasNext">
                        <i :class="['ui', {'disabled': !hasNext}, 'forward step', 'icon']" ></i>
                    </span>
                  </template>
                </div>
              </template>
            </div>
          </div>
Agate's avatar
Agate committed
126
          <div class="ui ten wide column queue-column">
127
128
129
130
            <div class="ui basic clearing fixed-header segment">
              <h2 class="ui header">
                <div class="content">
                  <button
heyarne's avatar
heyarne committed
131
132
133
134
135
136
                    class="ui right floated basic button"
                    @click="$store.commit('ui/queueFocused', null)">
                      <translate translate-context="*/Queue/*/Verb">Close</translate>
                  </button>
                  <button
                    class="ui right floated basic button danger"
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
                    @click="$store.dispatch('queue/clean')">
                      <translate translate-context="*/Queue/*/Verb">Clear</translate>
                  </button>
                  {{ labels.queue }}
                  <div class="sub header">
                    <div>
                      <translate translate-context="Sidebar/Queue/Text" :translate-params="{index: queue.currentIndex + 1, length: queue.tracks.length}">
                        Track %{ index } of %{ length }
                      </translate><template v-if="!$store.state.radios.running"> -
                        <span :title="labels.duration">
                          {{ timeLeft }}
                        </span>
                      </template>
                    </div>
                  </div>
                </div>
              </h2>
            </div>
            <table class="ui compact very basic fixed single line selectable unstackable table">
              <draggable v-model="tracks" tag="tbody" @update="reorder" handle=".handle">
                <tr
                  v-for="(track, index) in tracks"
                  :key="index"
                  :class="['queue-item', {'active': index === queue.currentIndex}]">
                  <td class="handle">
Agate's avatar
Agate committed
162
                    <i class="grip lines icon"></i>
163
164
                  </td>
                  <td class="image-cell" @click="$store.dispatch('queue/currentIndex', index)">
165
166
                    <img class="ui mini image" alt="" v-if="track.cover && track.cover.urls.original" :src="$store.getters['instance/absoluteUrl'](track.cover.urls.medium_square_crop)">
                    <img class="ui mini image" alt="" v-else-if="track.album && track.album.cover && track.album.cover.urls.original" :src="$store.getters['instance/absoluteUrl'](track.album.cover.urls.medium_square_crop)">
167
                    <img class="ui mini image" alt="" v-else src="../assets/audio/default-cover.png">
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                  </td>
                  <td colspan="3" @click="$store.dispatch('queue/currentIndex', index)">
                    <button class="title reset ellipsis" :title="track.title" :aria-label="labels.selectTrack">
                      <strong>{{ track.title }}</strong><br />
                      <span>
                        {{ track.artist.name }}
                      </span>
                    </button>
                  </td>
                  <td class="duration-cell">
                    <template v-if="track.uploads.length > 0">
                      {{ time.durationFormatted(track.uploads[0].duration) }}
                    </template>
                  </td>
                  <td class="controls">
                    <template v-if="$store.getters['favorites/isFavorite'](track.id)">
                      <i class="pink heart icon"></i>
                    </template>
186
                    <button :aria-label="labels.removeFromQueue" :title="labels.removeFromQueue" @click.stop="cleanTrack(index)" :class="['ui', 'really', 'tiny', 'basic', 'circular', 'icon', 'button']">
187
188
189
190
191
192
                      <i class="x icon"></i>
                    </button>
                  </td>
                </tr>
              </draggable>
            </table>
193
194
195

            <div v-if="$store.state.radios.running" class="ui info message">
              <div class="content">
196
                <h3 class="header">
197
                  <i class="feed icon"></i> <translate translate-context="Sidebar/Player/Title">You have a radio playing</translate>
198
                </h3>
199
                <p><translate translate-context="Sidebar/Player/Paragraph">New tracks will be appended here automatically.</translate></p>
200
                <button @click="$store.dispatch('radios/stop')" class="ui basic primary button"><translate translate-context="*/Player/Button.Label/Short, Verb">Stop radio</translate></button>
201
202
              </div>
            </div>
203
204
205
206
207
208
209
210
211
212
213
214
          </div>
        </div>
      </div>
    </div>
  </section>
</template>
<script>
import { mapState, mapGetters, mapActions } from "vuex"
import $ from 'jquery'
import moment from "moment"
import lodash from '@/lodash'
import time from "@/utils/time"
215
import createFocusTrap from 'focus-trap'
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import store from "@/store"

export default {
  components: {
    TrackFavoriteIcon:  () => import(/* webpackChunkName: "auth-audio" */ "@/components/favorites/TrackFavoriteIcon"),
    TrackPlaylistIcon:  () => import(/* webpackChunkName: "auth-audio" */ "@/components/playlists/TrackPlaylistIcon"),
    VolumeControl:  () => import(/* webpackChunkName: "audio" */ "@/components/audio/VolumeControl"),
    draggable:  () => import(/* webpackChunkName: "draggable" */ "vuedraggable"),
  },
  data () {
    return {
      showVolume: false,
      isShuffling: false,
      tracksChangeBuffer: null,
230
      focusTrap: null,
231
232
233
234
235
      time
    }
  },
  mounted () {
    let self = this
236
237
    this.focusTrap = createFocusTrap(this.$el, {allowOutsideClick: () => { return true }})
    this.focusTrap.activate()
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    this.$nextTick(() => {
      setTimeout(() => {
        this.scrollToCurrent()
        // delay is to let transition work
      }, 400);
    })
  },
  computed: {
    ...mapState({
      currentIndex: state => state.queue.currentIndex,
      playing: state => state.player.playing,
      isLoadingAudio: state => state.player.isLoadingAudio,
      volume: state => state.player.volume,
      looping: state => state.player.looping,
      duration: state => state.player.duration,
      bufferProgress: state => state.player.bufferProgress,
      errored: state => state.player.errored,
      currentTime: state => state.player.currentTime,
      queue: state => state.queue
    }),
    ...mapGetters({
      currentTrack: "queue/currentTrack",
      hasNext: "queue/hasNext",
      emptyQueue: "queue/isEmpty",
      durationFormatted: "player/durationFormatted",
      currentTimeFormatted: "player/currentTimeFormatted",
      progress: "player/progress"
    }),
    tracks: {
      get() {
        return this.$store.state.queue.tracks
      },
      set(value) {
        this.tracksChangeBuffer = value
      }
    },
    labels () {
      return {
        queue: this.$pgettext('*/*/*', 'Queue'),
        duration: this.$pgettext('*/*/*', 'Duration'),
Agate's avatar
Agate committed
278
279
        addArtistContentFilter: this.$pgettext('Sidebar/Player/Icon.Tooltip/Verb', 'Hide content from this artist…'),
        restart: this.$pgettext('*/*/*', 'Restart track'),
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
      }
    },
    timeLeft () {
      let seconds = lodash.sum(
        this.queue.tracks.slice(this.queue.currentIndex).map((t) => {
          return (t.uploads || []).map((u) => {
            return u.duration || 0
          })[0] || 0
        })
      )
      return moment(this.$store.state.ui.lastDate).add(seconds, 'seconds').fromNow(true)
    },
    sliderVolume: {
      get () {
        return this.volume
      },
      set (v) {
        this.$store.commit("player/volume", v)
      }
    },
    playerFocused () {
      return this.$store.state.ui.queueFocused === 'player'
    }
  },
  methods: {
    ...mapActions({
      cleanTrack: "queue/cleanTrack",
      mute: "player/mute",
      unmute: "player/unmute",
      clean: "queue/clean",
      toggleMute: "player/toggleMute",
      togglePlay: "player/togglePlay",
    }),
    reorder: function(event) {
      this.$store.commit("queue/reorder", {
        tracks: this.tracksChangeBuffer,
        oldIndex: event.oldIndex,
        newIndex: event.newIndex
      })
    },
    scrollToCurrent() {
      let current = $(this.$el).find('.queue-item.active')[0]
      if (!current) {
        return
      }
      const elementRect = current.getBoundingClientRect();
      const absoluteElementTop = elementRect.top + window.pageYOffset;
      const middle = absoluteElementTop - (window.innerHeight / 2);
      window.scrollTo({top: middle, behaviour: 'smooth'});
    },
    touchProgress(e) {
      let time
      let target = this.$refs.progress
      time = (e.layerX / target.offsetWidth) * this.duration
      this.$emit('touch-progress', time)
    },
    shuffle() {
      let disabled = this.queue.tracks.length === 0
      if (this.isShuffling || disabled) {
        return
      }
      let self = this
      let msg = this.$pgettext('Content/Queue/Message', "Queue shuffled!")
      this.isShuffling = true
      setTimeout(() => {
        self.$store.dispatch("queue/shuffle", () => {
          self.isShuffling = false
          self.$store.commit("ui/addMessage", {
            content: msg,
            date: new Date()
          })
        })
      }, 100)
    },
  },
  watch: {
    "$store.state.ui.queueFocused": {
      handler (v) {
        if (v === 'queue') {
          this.$nextTick(() => {
            this.scrollToCurrent()
          })
        }
      },
      immediate: true
    },
    '$store.state.queue.currentIndex': {
      handler () {
        this.$nextTick(() => {
          this.scrollToCurrent()
        })
      },
    },
    '$store.state.queue.tracks': {
      handler (v) {
        if (!v || v.length === 0) {
          this.$store.commit('ui/queueFocused', null)
        }
      },
      immediate: true
    },
    "$route.fullPath" () {
      this.$store.commit('ui/queueFocused', null)
    }
  }
}
</script>