mirror of
https://github.com/koel/koel
synced 2024-12-01 08:19:18 +00:00
120 lines
2.5 KiB
JavaScript
120 lines
2.5 KiB
JavaScript
/*eslint camelcase: ["error", {properties: "never"}]*/
|
|
|
|
import Vue from 'vue'
|
|
import { reduce, each, union, difference, take, filter, orderBy } from 'lodash'
|
|
|
|
import stub from '@/stubs/album'
|
|
import { artistStore } from '.'
|
|
|
|
export const albumStore = {
|
|
stub,
|
|
cache: [],
|
|
|
|
state: {
|
|
albums: [stub]
|
|
},
|
|
|
|
/**
|
|
* Init the store.
|
|
*
|
|
* @param {Array.<Object>} albums The array of album objects
|
|
*/
|
|
init (albums) {
|
|
// Traverse through the artists array and add their albums into our master album list.
|
|
this.all = albums
|
|
each(this.all, album => this.setupAlbum(album))
|
|
},
|
|
|
|
setupAlbum (album) {
|
|
const artist = artistStore.byId(album.artist_id)
|
|
artist.albums = union(artist.albums, [album])
|
|
|
|
Vue.set(album, 'artist', artist)
|
|
Vue.set(album, 'info', null)
|
|
Vue.set(album, 'songs', [])
|
|
Vue.set(album, 'playCount', 0)
|
|
|
|
this.cache[album.id] = album
|
|
|
|
return album
|
|
},
|
|
|
|
/**
|
|
* Get all albums in the store.
|
|
*
|
|
* @return {Array.<Object>}
|
|
*/
|
|
get all () {
|
|
return this.state.albums
|
|
},
|
|
|
|
/**
|
|
* Set all albums.
|
|
*
|
|
* @param {Array.<Object>} value
|
|
*/
|
|
set all (value) {
|
|
this.state.albums = value
|
|
},
|
|
|
|
byId (id) {
|
|
return this.cache[id]
|
|
},
|
|
|
|
/**
|
|
* Add new album/albums into the current collection.
|
|
*
|
|
* @param {Array.<Object>|Object} albums
|
|
*/
|
|
add (albums) {
|
|
albums = [].concat(albums)
|
|
each(albums, album => {
|
|
this.setupAlbum(album, album.artist)
|
|
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0)
|
|
})
|
|
|
|
this.all = union(this.all, albums)
|
|
},
|
|
|
|
purify () {
|
|
this.compact()
|
|
},
|
|
|
|
/**
|
|
* Remove empty albums from the store.
|
|
*/
|
|
compact () {
|
|
const emptyAlbums = filter(this.all, album => album.songs.length === 0)
|
|
if (!emptyAlbums.length) {
|
|
return
|
|
}
|
|
|
|
this.all = difference(this.all, emptyAlbums)
|
|
each(emptyAlbums, album => delete this.cache[album.id])
|
|
},
|
|
|
|
/**
|
|
* Get top n most-played albums.
|
|
*
|
|
* @param {Number} n
|
|
*
|
|
* @return {Array.<Object>}
|
|
*/
|
|
getMostPlayed (n = 6) {
|
|
// Only non-unknown albums with actually play count are applicable.
|
|
const applicable = filter(this.all, album => album.playCount && album.id !== 1)
|
|
return take(orderBy(applicable, 'playCount', 'desc'), n)
|
|
},
|
|
|
|
/**
|
|
* Get n most recently added albums.
|
|
*
|
|
* @param {Number} n
|
|
*
|
|
* @return {Array.<Object>}
|
|
*/
|
|
getRecentlyAdded (n = 6) {
|
|
const applicable = filter(this.all, album => album.id !== 1)
|
|
return take(orderBy(applicable, 'created_at', 'desc'), n)
|
|
}
|
|
}
|