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

215 lines
5 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import _ from 'lodash';
import songStub from '../stubs/song';
export default {
state: {
songs: [],
2016-01-07 08:11:11 +00:00
current: null,
2015-12-13 04:42:28 +00:00
},
init() {
// We don't have anything to do here yet.
// How about another song then?
//
// LITTLE WING
2015-12-30 04:14:47 +00:00
// -- by Jimi Fucking Hendrix
2015-12-13 04:42:28 +00:00
//
// Well she's walking
2015-12-13 16:52:35 +00:00
// Through the clouds
2015-12-13 04:42:28 +00:00
// 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
//
2015-12-13 16:52:35 +00:00
// When I'm sad
2015-12-13 04:42:28 +00:00
// 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...
},
2016-01-19 11:00:23 +00:00
/**
* Get all queued songs.
*
* @return {Array.<Object>}
*/
2015-12-13 04:42:28 +00:00
all() {
return this.state.songs;
},
2016-01-19 11:00:23 +00:00
/**
* Get the first song in the queue.
*
* @return {?Object}
*/
2015-12-13 04:42:28 +00:00
first() {
return _.first(this.state.songs);
},
2016-01-19 11:00:23 +00:00
/**
* Get the last song in the queue.
*
* @return {?Object}
*/
2015-12-13 04:42:28 +00:00
last() {
return _.last(this.state.songs);
},
2016-01-07 08:11:11 +00:00
/**
* Determine if the queue contains a song.
*
* @param {Object} song
2016-01-07 09:03:38 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {Boolean}
2016-01-07 08:11:11 +00:00
*/
contains(song) {
return _.includes(this.all(), song);
},
2015-12-13 04:42:28 +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.
*
2016-01-17 14:26:24 +00:00
* @param {Object|Array.<Object>} songs The song, or an array of songs
* @param {Boolean} replace Whether to replace the current queue
2016-01-19 11:00:23 +00:00
* @param {Boolean} toTop Whether to prepend or append to the queue
2015-12-13 04:42:28 +00:00
*/
queue(songs, replace = false, toTop = false) {
2016-01-21 10:21:46 +00:00
songs = [].concat(songs);
2015-12-13 04:42:28 +00:00
if (replace) {
this.state.songs = songs;
} else {
if (toTop) {
this.state.songs = _.union(songs, this.state.songs);
} else {
this.state.songs = _.union(this.state.songs, songs);
}
}
},
2016-01-07 08:11:11 +00:00
/**
* Queue song(s) to after the current song.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>|Object} songs
2016-01-07 08:11:11 +00:00
*/
queueAfterCurrent(songs) {
2016-01-21 10:21:46 +00:00
songs = [].concat(songs);
2016-01-07 08:11:11 +00:00
if (!this.state.current || !this.state.songs.length) {
return this.queue(songs);
}
var head = this.state.songs.splice(0, this.indexOf(this.state.current) + 1);
2016-01-07 08:11:11 +00:00
this.state.songs = head.concat(songs, this.state.songs);
},
2015-12-13 04:42:28 +00:00
/**
* Unqueue a song, or several songs at once.
*
2016-01-19 11:00:23 +00:00
* @param {Object|String|Array.<Object>} songs The song(s) to unqueue
2015-12-13 04:42:28 +00:00
*/
unqueue(songs) {
2016-01-21 10:21:46 +00:00
this.state.songs = _.difference(this.state.songs, [].concat(songs));
2015-12-13 04:42:28 +00:00
},
/**
* Move some songs to after a target.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} songs Songs to move
* @param {Object} target The target song object
*/
move(songs, target) {
var $targetIndex = this.indexOf(target);
songs.forEach(song => {
this.state.songs.splice(this.indexOf(song), 1);
this.state.songs.splice($targetIndex, 0, song);
});
},
2015-12-13 04:42:28 +00:00
/**
* Clear the current queue.
2016-01-19 11:00:23 +00:00
*
* @param {?Function} cb The function to execute after clearing
2015-12-13 04:42:28 +00:00
*/
clear(cb = null) {
this.state.songs = [];
2016-01-07 08:11:11 +00:00
this.state.current = null;
2015-12-13 04:42:28 +00:00
if (cb) {
cb();
}
},
/**
* Get index of a song in the queue.
*
* @param {Object} song
*
2016-01-17 14:26:24 +00:00
* @return {?Integer}
*/
indexOf(song) {
return _.indexOf(this.state.songs, song);
},
2015-12-13 04:42:28 +00:00
/**
* Get the next song in queue.
*
2016-01-07 09:03:38 +00:00
* @return {?Object}
2015-12-13 04:42:28 +00:00
*/
getNextSong() {
2016-01-07 08:11:11 +00:00
if (!this.current()) {
return _.first(this.state.songs);
}
2015-12-13 04:42:28 +00:00
var i = _.pluck(this.state.songs, 'id').indexOf(this.current().id) + 1;
return i >= this.state.songs.length ? null : this.state.songs[i];
},
/**
* Get the previous song in queue.
*
2016-01-07 09:03:38 +00:00
* @return {?Object}
2015-12-13 04:42:28 +00:00
*/
getPrevSong() {
2016-01-07 08:11:11 +00:00
if (!this.current()) {
return _.last(this.state.songs);
}
2015-12-13 04:42:28 +00:00
var i = _.pluck(this.state.songs, 'id').indexOf(this.current().id) - 1;
return i < 0 ? null : this.state.songs[i];
},
/**
* Get or set the current song.
2016-01-07 09:03:38 +00:00
*
* @param {?Object} song
*
* @return {Object}
2015-12-13 04:42:28 +00:00
*/
current(song = null) {
if (song) {
this.state.current = song;
}
return this.state.current;
},
/**
* Shuffle the queue.
2016-01-19 11:00:23 +00:00
*
* @return {Array.<Object>} The shuffled array of song objects
2015-12-13 04:42:28 +00:00
*/
shuffle() {
return (this.state.songs = _.shuffle(this.state.songs));
},
};