Playlists sorting; Default to by name

This commit is contained in:
James Barnsley
2016-12-14 08:51:11 +13:00
parent e599692c3a
commit 5d1598e887
4 changed files with 20 additions and 8 deletions

3
src/js/bootstrap.js vendored
View File

@ -50,7 +50,8 @@ var initialState = {
spotify: {
connected: false,
country: 'NZ',
locale: 'en_NZ'
locale: 'en_NZ',
me: false
},
ui: {
context_menu: {

View File

@ -255,24 +255,33 @@ export let createRange = function (indexes){
* @return array
**/
export let sortItems = function (array, property, reverse = false){
function compare(a,b) {
var a_value = a
var a_property_split = property.split('.')
for( var i = 0; i < a_property_split.length; i++ ){
if( typeof(a_value[a_property_split[i]]) === 'undefined' ) return -1
a_value = a_value[a_property_split[i]]
if( typeof(a_value[a_property_split[i]]) === 'undefined' ){
a_value = false
}else{
a_value = a_value[a_property_split[i]]
}
}
var b_value = b
var b_property_split = property.split('.')
for( var i = 0; i < b_property_split.length; i++ ){
if( typeof(b_value[b_property_split[i]]) === 'undefined' ) return -1
b_value = b_value[b_property_split[i]]
if( typeof(b_value[b_property_split[i]]) === 'undefined' ){
b_value = false
}else{
b_value = b_value[b_property_split[i]]
}
}
if( typeof(a_value) === 'boolean'){
return a_value
if( a_value && !b_value ) return -1
if( !a_value && b_value ) return 1
return 0
}else if( typeof(a_value) === 'string'){
if(a_value.toLowerCase() > b_value.toLowerCase()) return 1
@ -280,7 +289,8 @@ export let sortItems = function (array, property, reverse = false){
}else{
if( parseInt(a_value) > parseInt(b_value) ) return 1
return -1
if( parseInt(a_value) < parseInt(b_value) ) return -1
return 0
}
}

View File

@ -312,6 +312,7 @@ export default function reducer(ui = {}, action){
if( !action.data ) return ui
var playlists = [...ui.playlists, ...action.data]
playlists = helpers.mergeDuplicates(playlists, 'uri')
helpers.sortItems(playlists, 'name')
return Object.assign({}, ui, {
playlists: playlists
});

View File

@ -35,7 +35,7 @@ class LibraryPlaylists extends React.Component{
renderView(){
if( !this.props.playlists ) return null
var playlists = this.props.playlists
var playlists = Object.assign([], this.props.playlists)
if( this.props.sort ){
playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse)
}