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

138 lines
3.1 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: [],
current: songStub,
},
init() {
// We don't have anything to do here yet.
// How about another song then?
//
// LITTLE WING
2015-12-13 16:50:30 +00:00
// -- by Jimi Fucking Hendrick
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...
//
// [CRAZY SOLO BITCH!]
},
all() {
return this.state.songs;
},
first() {
return _.first(this.state.songs);
},
last() {
return _.last(this.state.songs);
},
/**
* 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 songs The song, or an array of songs
* @param bool replace Whether to replace the current queue
* @param bool toTop Whether to prepend of append to the queue
*/
queue(songs, replace = false, toTop = false) {
if (!Array.isArray(songs)) {
songs = [songs];
}
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);
}
}
},
/**
* Unqueue a song, or several songs at once.
*
* @param object|string|array songs The song(s) to unqueue.
*/
unqueue(songs) {
if (!Array.isArray(songs)) {
songs = [songs];
}
this.state.songs = _.difference(this.state.songs, songs);
},
/**
* Clear the current queue.
*/
clear(cb = null) {
this.state.songs = [];
if (cb) {
cb();
}
},
/**
* Get the next song in queue.
*
* @return object|null
*/
getNextSong() {
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.
*
* @return object|null
*/
getPrevSong() {
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.
*/
current(song = null) {
if (song) {
this.state.current = song;
}
return this.state.current;
},
/**
* Shuffle the queue.
*/
shuffle() {
return (this.state.songs = _.shuffle(this.state.songs));
},
};