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

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import _ from 'lodash';
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) {
this.state.artists = artists;
2015-12-13 04:42:28 +00:00
// Traverse through artists array to get the cover and number of songs for each.
_.each(this.state.artists, artist => {
2015-12-22 09:53:03 +00:00
this.getImage(artist);
2015-12-14 13:13:12 +00:00
artist.songCount = _.reduce(artist.albums, (count, album) => count + album.songs.length, 0);
2015-12-13 04:42:28 +00:00
});
2015-12-22 17:46:54 +00:00
albumStore.init(this.state.artists);
2015-12-13 04:42:28 +00:00
},
all() {
return this.state.artists;
},
/**
* 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) {
2015-12-14 13:13:12 +00:00
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) {
// If the artist already has a proper image, just return it.
if (artist.image) {
return artist.image;
}
// Otherwise, we try to get an image from one of their albums.
artist.image = config.unknownCover;
2015-12-13 04:42:28 +00:00
artist.albums.every(album => {
// If there's a "real" cover, use it.
2015-12-22 09:53:03 +00:00
if (album.image != config.unknownCover) {
artist.image = album.cover;
2015-12-13 04:42:28 +00:00
// 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
},
};