koel/resources/assets/js/stores/playlist.js

139 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import _ from 'lodash';
2016-03-11 09:55:04 +00:00
import NProgress from 'nprogress';
2015-12-13 04:42:28 +00:00
import http from '../services/http';
import stub from '../stubs/playlist';
import songStore from './song';
export default {
stub,
2016-03-11 09:55:04 +00:00
2015-12-13 04:42:28 +00:00
state: {
playlists: [],
},
init(playlists) {
this.state.playlists = playlists;
2015-12-13 04:42:28 +00:00
_.each(this.state.playlists, this.getSongs);
},
2016-01-17 14:26:24 +00:00
/**
* Get all playlists of the current user.
2016-03-11 09:55:04 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {Array.<Object>}
*/
2015-12-13 04:42:28 +00:00
all() {
return this.state.playlists;
},
/**
* Get all songs in a playlist.
*
2016-01-17 14:26:24 +00:00
* return {Array.<Object>}
2015-12-13 04:42:28 +00:00
*/
getSongs(playlist) {
2015-12-14 13:13:12 +00:00
return (playlist.songs = songStore.byIds(playlist.songs));
2015-12-13 04:42:28 +00:00
},
2016-01-03 10:24:12 +00:00
/**
* Create a new playlist, optionally with its songs.
2016-03-11 09:55:04 +00:00
*
2016-01-17 14:26:24 +00:00
* @param {String} name Name of the playlist
* @param {Array.<Object>} songs An array of song objects
* @param {?Function} cb
2016-01-03 10:24:12 +00:00
*/
2015-12-13 04:42:28 +00:00
store(name, songs, cb = null) {
2016-01-03 10:24:12 +00:00
if (songs.length) {
// Extract the IDs from the song objects.
songs = _.pluck(songs, 'id');
}
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-29 01:35:22 +00:00
http.post('playlist', { name, songs }, response => {
var playlist = response.data;
2015-12-14 04:29:16 +00:00
playlist.songs = songs;
2015-12-13 04:42:28 +00:00
this.getSongs(playlist);
this.state.playlists.push(playlist);
if (cb) {
cb();
}
});
},
2016-01-07 09:03:38 +00:00
/**
* Delete a playlist.
2016-03-11 09:55:04 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {Object} playlist
* @param {?Function} cb
*/
2015-12-13 04:42:28 +00:00
delete(playlist, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-13 04:42:28 +00:00
http.delete(`playlist/${playlist.id}`, {}, () => {
this.state.playlists = _.without(this.state.playlists, playlist);
if (cb) {
cb();
}
});
},
2016-01-07 09:03:38 +00:00
/**
* Add songs into a playlist.
2016-03-11 09:55:04 +00:00
*
2016-01-17 14:26:24 +00:00
* @param {Object} playlist
* @param {Array.<Object>} songs
* @param {?Function} cb
2016-01-07 09:03:38 +00:00
*/
2015-12-13 04:42:28 +00:00
addSongs(playlist, songs, cb = null) {
var count = playlist.songs.length;
2015-12-13 04:42:28 +00:00
playlist.songs = _.union(playlist.songs, songs);
if (count === playlist.songs.length) {
return;
}
2015-12-13 04:42:28 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}
});
},
2016-01-07 09:03:38 +00:00
/**
* Remove songs from a playlist.
2016-03-11 09:55:04 +00:00
*
2016-01-17 14:26:24 +00:00
* @param {Object} playlist
2016-03-11 09:55:04 +00:00
* @param {Array.<Object>} songs
2016-01-17 14:26:24 +00:00
* @param {?Function} cb
2016-01-07 09:03:38 +00:00
*/
2015-12-13 04:42:28 +00:00
removeSongs(playlist, songs, cb = null) {
playlist.songs = _.difference(playlist.songs, songs);
2016-03-11 09:55:04 +00:00
2015-12-13 04:42:28 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}
});
},
2016-01-07 09:03:38 +00:00
/**
* Update a playlist (just change its name).
2016-03-11 09:55:04 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {Object} playlist
* @param {?Function} cb
*/
2015-12-13 04:42:28 +00:00
update(playlist, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-13 04:42:28 +00:00
http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => {
if (cb) {
cb();
}
});
},
};