koel/resources/assets/js/stores/album.js

189 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import Vue from 'vue'
2016-12-17 08:10:08 +00:00
import { reduce, each, union, difference, take, filter, orderBy } from 'lodash'
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
import { secondsToHis } from '../utils'
import stub from '../stubs/album'
import { songStore, artistStore } from '.'
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const albumStore = {
2016-06-25 16:05:24 +00:00
stub,
2016-12-17 07:31:08 +00:00
cache: [],
2016-06-25 16:05:24 +00:00
state: {
2016-11-26 03:25:35 +00:00
albums: [stub]
2016-06-25 16:05:24 +00:00
},
/**
* Init the store.
*
* @param {Array.<Object>} artists The array of artists to extract album data from.
*/
2016-11-26 03:25:35 +00:00
init (artists) {
2016-06-25 16:05:24 +00:00
// Traverse through the artists array and add their albums into our master album list.
this.all = reduce(artists, (albums, artist) => {
// While we're doing so, for each album, we get its length
// and keep a back reference to the artist too.
each(artist.albums, album => {
2016-11-26 03:25:35 +00:00
this.setupAlbum(album, artist)
})
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return albums.concat(artist.albums)
}, [])
2016-06-25 16:05:24 +00:00
// Then we init the song store.
2016-11-26 03:25:35 +00:00
songStore.init(this.all)
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
setupAlbum (album, artist) {
Vue.set(album, 'playCount', 0)
Vue.set(album, 'artist', artist)
Vue.set(album, 'info', null)
this.getLength(album)
this.cache[album.id] = album
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return album
2016-06-25 16:05:24 +00:00
},
/**
* Get all albums in the store.
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
get all () {
return this.state.albums
2016-06-25 16:05:24 +00:00
},
/**
* Set all albums.
*
* @param {Array.<Object>} value
*/
2016-11-26 03:25:35 +00:00
set all (value) {
this.state.albums = value
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
byId (id) {
2016-12-17 07:31:08 +00:00
return this.cache[id]
2016-06-25 16:05:24 +00:00
},
/**
* Get the total length of an album by summing up its songs' duration.
* The length will also be converted into a H:i:s format and stored as fmtLength.
*
* @param {Object} album
*
* @return {String} The H:i:s format of the album length.
*/
2016-11-26 03:25:35 +00:00
getLength (album) {
Vue.set(album, 'length', reduce(album.songs, (length, song) => length + song.length, 0))
Vue.set(album, 'fmtLength', secondsToHis(album.length))
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return album.fmtLength
2016-06-25 16:05:24 +00:00
},
/**
* Add new album/albums into the current collection.
*
* @param {Array.<Object>|Object} albums
*/
2016-11-26 03:25:35 +00:00
add (albums) {
albums = [].concat(albums)
2016-06-25 16:05:24 +00:00
each(albums, a => {
this.setupAlbum(a, a.artist)
2016-11-26 03:25:35 +00:00
a.playCount = reduce(a.songs, (count, song) => count + song.playCount, 0)
})
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
this.all = union(this.all, albums)
2016-06-25 16:05:24 +00:00
},
/**
* Add song(s) into an album.
*
* @param {Object} album
* @param {Array.<Object>|Object} song
*/
2016-11-26 03:25:35 +00:00
addSongsIntoAlbum (album, songs) {
songs = [].concat(songs)
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
album.songs = union(album.songs ? album.songs : [], songs)
2016-06-25 16:05:24 +00:00
each(songs, song => {
2016-11-26 03:25:35 +00:00
song.album_id = album.id
song.album = album
})
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0)
this.getLength(album)
2016-06-25 16:05:24 +00:00
},
/**
* Remove song(s) from an album.
*
* @param {Object} album
* @param {Array.<Object>|Object} songs
*/
2016-11-26 03:25:35 +00:00
removeSongsFromAlbum (album, songs) {
album.songs = difference(album.songs, [].concat(songs))
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0)
this.getLength(album)
2016-06-25 16:05:24 +00:00
},
/**
* Checks if an album is empty.
*
* @param {Object} album
*
* @return {boolean}
*/
2016-11-26 03:25:35 +00:00
isAlbumEmpty (album) {
return !album.songs.length
2016-06-25 16:05:24 +00:00
},
/**
* Remove album(s) from the store.
*
* @param {Array.<Object>|Object} albums
*/
2016-11-26 03:25:35 +00:00
remove (albums) {
albums = [].concat(albums)
this.all = difference(this.all, albums)
2016-06-25 16:05:24 +00:00
// Remove from the artist as well
each(albums, album => {
2016-11-26 03:25:35 +00:00
artistStore.removeAlbumsFromArtist(album.artist, album)
2016-12-17 08:10:08 +00:00
// Delete the cache while we're here
delete this.cache[album.id]
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
/**
* Get top n most-played albums.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
getMostPlayed (n = 6) {
2016-06-25 16:05:24 +00:00
// Only non-unknown albums with actually play count are applicable.
const applicable = filter(this.all, album => {
2016-11-26 03:25:35 +00:00
return album.playCount && album.id !== 1
})
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return take(orderBy(applicable, 'playCount', 'desc'), n)
2016-06-25 16:05:24 +00:00
},
/**
* Get n most recently added albums.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
getRecentlyAdded (n = 6) {
const applicable = filter(this.all, album => album.id !== 1)
2016-11-26 03:25:35 +00:00
return take(orderBy(applicable, 'created_at', 'desc'), n)
}
}