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

171 lines
3.7 KiB
JavaScript
Raw Normal View History

import {
each,
2016-03-31 08:58:46 +00:00
map,
difference,
2016-03-31 09:53:10 +00:00
union,
without
} 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) {
2016-04-05 07:38:10 +00:00
this.all = playlists;
each(this.all, 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;
},
2016-04-05 07:38:10 +00:00
/**
* Set all playlists.
*
* @param {Array.<Object>} value
*/
set all(value) {
this.state.playlists = value;
},
2015-12-13 04:42:28 +00:00
/**
* 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-04-05 07:38:10 +00:00
/**
* Add a playlist/playlists into the store.
*
* @param {Array.<Object>|Object} playlists
*/
add(playlists) {
this.all = union(this.all, [].concat(playlists));
},
/**
* Remove a playlist/playlists from the store.
*
* @param {Array.<Object>|Object} playlist
*/
remove(playlists) {
this.all = difference(this.all, [].concat(playlists));
},
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);
2016-04-05 07:38:10 +00:00
this.add(playlist);
2015-12-13 04:42:28 +00:00
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}`, {}, () => {
2016-04-05 07:38:10 +00:00
this.remove(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();
}
});
},
};