koel/resources/assets/js/stores/artist.js

196 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-02-09 04:57:08 +00:00
import Vue from 'vue';
import {
reduce,
each,
find,
union,
difference,
take,
filter,
2016-03-31 08:58:46 +00:00
orderBy
} from 'lodash';
2015-12-13 04:42:28 +00:00
import config from '../config';
2016-01-15 07:27:25 +00:00
import stub from '../stubs/artist';
2015-12-13 04:42:28 +00:00
import albumStore from './album';
export default {
2016-01-15 07:27:25 +00:00
stub,
2015-12-13 04:42:28 +00:00
state: {
artists: [],
},
/**
* Init the store.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} artists The array of artists we got from the server.
2015-12-13 04:42:28 +00:00
*/
2015-12-22 17:46:54 +00:00
init(artists) {
2016-04-05 07:38:10 +00:00
this.all = artists;
2015-12-13 04:42:28 +00:00
// Traverse through artists array to get the cover and number of songs for each.
2016-04-05 07:38:10 +00:00
each(this.all, artist => {
2016-03-05 09:01:12 +00:00
this.setupArtist(artist);
2015-12-13 04:42:28 +00:00
});
2015-12-22 17:46:54 +00:00
2016-04-05 07:38:10 +00:00
albumStore.init(this.all);
2015-12-13 04:42:28 +00:00
},
2016-03-05 09:01:12 +00:00
setupArtist(artist) {
this.getImage(artist);
Vue.set(artist, 'playCount', 0);
Vue.set(artist, 'songCount', reduce(artist.albums, (count, album) => count + album.songs.length, 0));
2016-03-05 09:01:12 +00:00
Vue.set(artist, 'info', null);
return artist;
},
2016-04-05 07:38:10 +00:00
/**
* Get all artists.
*
* @return {Array.<Object>}
*/
2016-03-18 04:45:12 +00:00
get all() {
2015-12-13 04:42:28 +00:00
return this.state.artists;
},
2016-04-05 07:38:10 +00:00
/**
* Set all artists.
*
* @param {Array.<Object>} value
*/
set all(value) {
this.state.artists = value;
},
/**
* Get an artist object by its ID.
*
* @param {Number} id
*/
2016-03-05 09:01:12 +00:00
byId(id) {
2016-03-31 08:58:46 +00:00
return find(this.all, { id });
2016-03-05 09:01:12 +00:00
},
/**
2016-04-05 07:38:10 +00:00
* Adds an artist/artists into the current collection.
2016-03-05 09:01:12 +00:00
*
2016-04-05 07:38:10 +00:00
* @param {Array.<Object>|Object} artists
*/
add(artists) {
artists = [].concat(artists);
each(artists, a => this.setupArtist(a));
this.all = union(this.all, artists);
},
/**
* Remove artist(s) from the store.
*
* @param {Array.<Object>|Object} artists
2016-03-05 09:01:12 +00:00
*/
2016-04-05 07:38:10 +00:00
remove(artists) {
this.all = difference(this.all, [].concat(artists));
2016-03-05 09:01:12 +00:00
},
2016-04-05 07:38:10 +00:00
/**
* Add album(s) into an artist.
*
* @param {Object} artist
* @param {Array.<Object>|Object} albums
*
*/
2016-03-05 09:01:12 +00:00
addAlbumsIntoArtist(artist, albums) {
albums = [].concat(albums);
artist.albums = union(artist.albums ? artist.albums : [], albums);
2016-03-05 09:01:12 +00:00
2016-04-05 07:38:10 +00:00
each(albums, album => {
2016-03-05 09:01:12 +00:00
album.artist_id = artist.id;
album.artist = artist;
});
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
2016-03-05 09:01:12 +00:00
},
/**
* Remove album(s) from an artist.
*
* @param {Object} artist
* @param {Array.<Object>|Object} albums
*/
removeAlbumsFromArtist(artist, albums) {
artist.albums = difference(artist.albums, [].concat(albums));
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
2016-03-05 09:01:12 +00:00
},
/**
* Checks if an artist is empty.
*
* @param {Object} artist
*
* @return {boolean}
*/
isArtistEmpty(artist) {
return !artist.albums.length;
},
2015-12-13 04:42:28 +00:00
/**
* Get all songs performed by an artist.
*
2015-12-22 09:53:03 +00:00
* @param {Object} artist
2015-12-13 04:42:28 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {Array.<Object>}
2015-12-13 04:42:28 +00:00
*/
getSongsByArtist(artist) {
if (!artist.songs) {
artist.songs = reduce(artist.albums, (songs, album) => songs.concat(album.songs), []);
2015-12-13 04:42:28 +00:00
}
return artist.songs;
},
/**
2015-12-22 09:53:03 +00:00
* Get the artist's image.
2015-12-13 04:42:28 +00:00
*
2015-12-22 09:53:03 +00:00
* @param {Object} artist
2015-12-13 04:42:28 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {String}
2015-12-13 04:42:28 +00:00
*/
2015-12-22 09:53:03 +00:00
getImage(artist) {
2016-04-05 07:38:10 +00:00
if (!artist.image) {
// Try to get an image from one of the albums.
artist.image = config.unknownCover;
artist.albums.every(album => {
// If there's a "real" cover, use it.
if (album.image !== config.unknownCover) {
artist.image = album.cover;
// I want to break free.
return false;
}
});
2015-12-22 09:53:03 +00:00
}
return artist.image;
2015-12-13 04:42:28 +00:00
},
/**
* Get top n most-played artists.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
getMostPlayed(n = 6) {
// Only non-unknown artists with actually play count are applicable.
2016-04-05 07:38:10 +00:00
const applicable = filter(this.all, artist => {
return artist.playCount && artist.id !== 1;
});
2016-03-31 08:58:46 +00:00
return take(orderBy(applicable, 'playCount', 'desc'), n);
},
2015-12-13 04:42:28 +00:00
};