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

144 lines
3.2 KiB
JavaScript
Raw Normal View History

import {
each,
2016-03-31 08:58:46 +00:00
map,
difference,
union
} 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);
2015-12-13 04:42:28 +00:00
},
2016-01-17 14:26:24 +00:00
/**
2016-03-20 13:42:33 +00:00
* 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>}
*/
2016-03-20 13:42:33 +00:00
get all() {
2015-12-13 04:42:28 +00:00
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.
2016-03-31 08:58:46 +00:00
songs = map(songs, 'id');
2016-01-03 10:24:12 +00:00
}
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-29 01:35:22 +00:00
http.post('playlist', { name, songs }, response => {
2016-03-28 13:38:14 +00:00
const 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);
2015-12-13 04:42:28 +00:00
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) {
2016-03-28 13:38:14 +00:00
const count = playlist.songs.length;
playlist.songs = union(playlist.songs, songs);
2015-12-13 04:42:28 +00:00
if (count === playlist.songs.length) {
return;
}
2016-03-31 08:58:46 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => {
2015-12-13 04:42:28 +00:00
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
2016-03-31 08:58:46 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => {
2015-12-13 04:42:28 +00:00
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();
}
});
},
};