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

233 lines
5 KiB
JavaScript
Raw Normal View History

import {
2016-03-31 08:58:46 +00:00
head,
last,
2016-04-05 07:38:10 +00:00
each,
includes,
union,
difference,
2016-03-31 08:58:46 +00:00
map,
2016-06-06 10:05:39 +00:00
shuffle as _shuffle,
2016-04-04 07:05:44 +00:00
first
} from 'lodash';
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const queueStore = {
2015-12-13 04:42:28 +00:00
state: {
songs: [],
2016-01-07 08:11:11 +00:00
current: null,
2015-12-13 04:42:28 +00:00
},
2016-03-13 17:00:32 +00:00
2015-12-13 04:42:28 +00:00
init() {
// We don't have anything to do here yet.
// How about another song then?
2016-03-13 17:00:32 +00:00
//
2015-12-13 04:42:28 +00:00
// LITTLE WING
2015-12-30 04:14:47 +00:00
// -- by Jimi Fucking Hendrix
2016-03-13 17:00:32 +00:00
//
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
2016-03-13 17:00:32 +00:00
//
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
/**
2016-04-04 06:50:33 +00:00
* Get all queued songs.
2016-03-13 17:00:32 +00:00
*
2016-01-19 11:00:23 +00:00
* @return {Array.<Object>}
*/
2016-03-18 04:45:12 +00:00
get all() {
2015-12-13 04:42:28 +00:00
return this.state.songs;
},
2016-04-04 06:50:33 +00:00
/**
* Set all queued songs.
*
* @param {Array.<Object>}
*/
set all(songs) {
this.state.songs = songs;
},
2016-01-19 11:00:23 +00:00
/**
2016-03-18 04:45:12 +00:00
* The first song in the queue.
2016-03-13 17:00:32 +00:00
*
2016-01-19 11:00:23 +00:00
* @return {?Object}
*/
2016-03-18 04:45:12 +00:00
get first() {
2016-04-04 06:50:33 +00:00
return head(this.all);
2015-12-13 04:42:28 +00:00
},
2016-01-19 11:00:23 +00:00
/**
2016-03-18 04:45:12 +00:00
* The last song in the queue.
2016-03-13 17:00:32 +00:00
*
* @return {?Object}
2016-01-19 11:00:23 +00:00
*/
2016-03-18 04:45:12 +00:00
get last() {
2016-04-04 06:50:33 +00:00
return last(this.all);
2015-12-13 04:42:28 +00:00
},
2016-01-07 08:11:11 +00:00
/**
* Determine if the queue contains a song.
2016-03-13 17:00:32 +00:00
*
2016-01-07 08:11:11 +00:00
* @param {Object} song
2016-03-13 17:00:32 +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);
2016-01-07 08:11:11 +00:00
},
2015-12-13 04:42:28 +00:00
/**
2016-03-13 17:00:32 +00:00
* Add a list of songs to the end of the current queue,
2015-12-13 04:42:28 +00:00
* 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) {
2016-04-04 06:50:33 +00:00
this.all = songs;
2015-12-13 04:42:28 +00:00
} else {
2016-04-04 06:50:33 +00:00
this.all = toTop ? union(songs, this.all) : union(this.all, songs);
2015-12-13 04:42:28 +00:00
}
},
2016-01-07 08:11:11 +00:00
/**
* Queue song(s) to after the current song.
2016-03-13 17:00:32 +00:00
*
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
2016-04-04 06:50:33 +00:00
if (!this.current || !this.all.length) {
2016-01-07 08:11:11 +00:00
return this.queue(songs);
}
2016-04-10 09:12:16 +00:00
// First we unqueue the songs to make sure there are no duplicates.
this.unqueue(songs);
2016-04-07 10:49:08 +00:00
2016-04-04 06:50:33 +00:00
const head = this.all.splice(0, this.indexOf(this.current) + 1);
this.all = head.concat(songs, this.all);
2016-01-07 08:11:11 +00:00
},
2015-12-13 04:42:28 +00:00
/**
* Unqueue a song, or several songs at once.
2016-03-13 17:00:32 +00:00
*
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-04-04 06:50:33 +00:00
this.all = difference(this.all, [].concat(songs));
2015-12-13 04:42:28 +00:00
},
/**
* Move some songs to after a target.
2016-03-13 17:00:32 +00:00
*
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) {
2016-03-28 13:38:14 +00:00
const $targetIndex = this.indexOf(target);
2016-04-05 07:38:10 +00:00
each(songs, song => {
2016-04-04 06:50:33 +00:00
this.all.splice(this.indexOf(song), 1);
this.all.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) {
2016-04-04 06:50:33 +00:00
this.all = [];
this.current = null;
2016-05-03 04:08:24 +00:00
cb && cb();
2015-12-13 04:42:28 +00:00
},
/**
* Get index of a song in the queue.
2016-03-13 17:00:32 +00:00
*
* @param {Object} song
2016-03-13 17:00:32 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {?Integer}
*/
indexOf(song) {
2016-04-04 06:50:33 +00:00
return this.all.indexOf(song);
},
2015-12-13 04:42:28 +00:00
/**
2016-03-18 04:45:12 +00:00
* The next song in queue.
2016-03-13 17:00:32 +00:00
*
2016-01-07 09:03:38 +00:00
* @return {?Object}
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
get next() {
if (!this.current) {
2016-04-04 06:50:33 +00:00
return first(this.all);
2016-01-07 08:11:11 +00:00
}
2016-04-04 06:50:33 +00:00
const idx = map(this.all, 'id').indexOf(this.current.id) + 1;
2015-12-13 04:42:28 +00:00
2016-04-04 06:50:33 +00:00
return idx >= this.all.length ? null : this.all[idx];
2015-12-13 04:42:28 +00:00
},
/**
2016-03-18 04:45:12 +00:00
* The previous song in queue.
2016-03-13 17:00:32 +00:00
*
2016-01-07 09:03:38 +00:00
* @return {?Object}
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
get previous() {
if (!this.current) {
2016-04-04 06:50:33 +00:00
return last(this.all);
2016-01-07 08:11:11 +00:00
}
2016-04-04 06:50:33 +00:00
const idx = map(this.all, 'id').indexOf(this.current.id) - 1;
2015-12-13 04:42:28 +00:00
2016-04-04 06:50:33 +00:00
return idx < 0 ? null : this.all[idx];
2015-12-13 04:42:28 +00:00
},
/**
2016-03-18 04:45:12 +00:00
* The current song.
2016-01-07 09:03:38 +00:00
*
* @return {Object}
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
get current() {
2016-03-13 17:00:32 +00:00
return this.state.current;
2015-12-13 04:42:28 +00:00
},
2016-03-18 04:45:12 +00:00
/**
* Set a song as the current queued song.
*
* @param {Object} song
*
* @return {Object} The queued song.
*/
set current(song) {
2016-05-05 10:38:54 +00:00
return this.state.current = song;
2016-03-18 04:45:12 +00:00
},
2015-12-13 04:42:28 +00:00
/**
* 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() {
2016-06-06 10:05:39 +00:00
return this.all = _shuffle(this.all);
2015-12-13 04:42:28 +00:00
},
};