koel/resources/assets/js/stores/queue.js

222 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import { head, last, each, includes, union, difference, map, shuffle as _shuffle, first } from 'lodash'
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const queueStore = {
2016-06-25 16:05:24 +00:00
state: {
songs: [],
2016-11-26 03:25:35 +00:00
current: null
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
init () {
2016-06-25 16:05:24 +00:00
// We don't have anything to do here yet.
// How about another song then?
//
// LITTLE WING
// -- by Jimi Fucking Hendrix
//
// Well she's walking
// Through the clouds
// With a circus mind
// That's running wild
// Butterflies and zebras and moonbeams and fairytales
// That's all she ever thinks about
// Riding with the wind
//
// When I'm sad
// She comes to me
// With a thousand smiles
// She gives to me free
// It's alright she said
// It's alright
// Take anything you want from me
// Anything...
},
/**
* Get all queued songs.
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
get all () {
return this.state.songs
2016-06-25 16:05:24 +00:00
},
/**
* Set all queued songs.
*
* @param {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
set all (songs) {
this.state.songs = songs
2016-06-25 16:05:24 +00:00
},
/**
* The first song in the queue.
*
* @return {?Object}
*/
2016-11-26 03:25:35 +00:00
get first () {
return head(this.all)
2016-06-25 16:05:24 +00:00
},
/**
* The last song in the queue.
*
* @return {?Object}
*/
2016-11-26 03:25:35 +00:00
get last () {
return last(this.all)
2016-06-25 16:05:24 +00:00
},
/**
* Determine if the queue contains a song.
*
* @param {Object} song
*
* @return {Boolean}
*/
2016-11-26 03:25:35 +00:00
contains (song) {
return includes(this.all, song)
2016-06-25 16:05:24 +00:00
},
/**
* Add a list of songs to the end of the current queue,
* or replace the current queue as a whole if `replace` is true.
*
* @param {Object|Array.<Object>} songs The song, or an array of songs
* @param {Boolean} replace Whether to replace the current queue
* @param {Boolean} toTop Whether to prepend or append to the queue
*/
2016-11-26 03:25:35 +00:00
queue (songs, replace = false, toTop = false) {
songs = [].concat(songs)
2016-06-25 16:05:24 +00:00
if (replace) {
2016-11-26 03:25:35 +00:00
this.all = songs
2016-06-25 16:05:24 +00:00
} else {
2016-11-26 03:25:35 +00:00
this.all = toTop ? union(songs, this.all) : union(this.all, songs)
2016-06-25 16:05:24 +00:00
}
},
/**
* Queue song(s) to after the current song.
*
* @param {Array.<Object>|Object} songs
*/
2016-11-26 03:25:35 +00:00
queueAfterCurrent (songs) {
songs = [].concat(songs)
2016-06-25 16:05:24 +00:00
if (!this.current || !this.all.length) {
2016-11-26 03:25:35 +00:00
return this.queue(songs)
2016-06-25 16:05:24 +00:00
}
// First we unqueue the songs to make sure there are no duplicates.
2016-11-26 03:25:35 +00:00
this.unqueue(songs)
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
const head = this.all.splice(0, this.indexOf(this.current) + 1)
this.all = head.concat(songs, this.all)
2016-06-25 16:05:24 +00:00
},
/**
* Unqueue a song, or several songs at once.
*
* @param {Object|String|Array.<Object>} songs The song(s) to unqueue
*/
2016-11-26 03:25:35 +00:00
unqueue (songs) {
this.all = difference(this.all, [].concat(songs))
2016-06-25 16:05:24 +00:00
},
/**
* Move some songs to after a target.
*
* @param {Array.<Object>} songs Songs to move
* @param {Object} target The target song object
*/
2016-11-26 03:25:35 +00:00
move (songs, target) {
const $targetIndex = this.indexOf(target)
2016-06-25 16:05:24 +00:00
each(songs, song => {
2016-11-26 03:25:35 +00:00
this.all.splice(this.indexOf(song), 1)
this.all.splice($targetIndex, 0, song)
})
2016-06-25 16:05:24 +00:00
},
/**
* Clear the current queue.
*/
2016-11-26 03:25:35 +00:00
clear () {
this.all = []
this.current = null
2016-06-25 16:05:24 +00:00
},
/**
* Get index of a song in the queue.
*
* @param {Object} song
*
* @return {?Integer}
*/
2016-11-26 03:25:35 +00:00
indexOf (song) {
return this.all.indexOf(song)
2016-06-25 16:05:24 +00:00
},
/**
* The next song in queue.
*
* @return {?Object}
*/
2016-11-26 03:25:35 +00:00
get next () {
2016-06-25 16:05:24 +00:00
if (!this.current) {
2016-11-26 03:25:35 +00:00
return first(this.all)
2016-06-25 16:05:24 +00:00
}
2016-11-26 03:25:35 +00:00
const idx = map(this.all, 'id').indexOf(this.current.id) + 1
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return idx >= this.all.length ? null : this.all[idx]
2016-06-25 16:05:24 +00:00
},
/**
* The previous song in queue.
*
* @return {?Object}
*/
2016-11-26 03:25:35 +00:00
get previous () {
2016-06-25 16:05:24 +00:00
if (!this.current) {
2016-11-26 03:25:35 +00:00
return last(this.all)
2016-06-25 16:05:24 +00:00
}
2016-11-26 03:25:35 +00:00
const idx = map(this.all, 'id').indexOf(this.current.id) - 1
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return idx < 0 ? null : this.all[idx]
2016-06-25 16:05:24 +00:00
},
/**
* The current song.
*
* @return {Object}
*/
2016-11-26 03:25:35 +00:00
get current () {
return this.state.current
2016-06-25 16:05:24 +00:00
},
/**
* Set a song as the current queued song.
*
* @param {Object} song
*
* @return {Object} The queued song.
*/
2016-11-26 03:25:35 +00:00
set current (song) {
this.state.current = song
return this.state.current
2016-06-25 16:05:24 +00:00
},
/**
* Shuffle the queue.
*
* @return {Array.<Object>} The shuffled array of song objects
*/
2016-11-26 03:25:35 +00:00
shuffle () {
this.all = _shuffle(this.all)
return this.all
}
}