update: implemeted playlist sort functionality

This commit is contained in:
Maximilian Walter 2016-06-01 21:02:16 +02:00
parent b73d1753b3
commit d735cb6816
5 changed files with 103 additions and 19 deletions

View file

@ -91,7 +91,7 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
public static function summary()
{
return self::select('id', 'title', 'user_id', 'slug', 'created_at', 'is_public', 'description', 'comment_count',
'download_count', 'view_count', 'favourite_count');
'download_count', 'view_count', 'favourite_count', 'track_count');
}
public function scopeUserDetails($query)

View file

@ -211,7 +211,7 @@ ponyfm.config [
abstract: true
state.state 'content.playlists.list',
url: '?page'
url: '^/playlists?filter&page'
controller: 'playlists-list'
templateUrl: '/templates/playlists/list.html'

View file

@ -23,6 +23,6 @@ window.pfm.preloaders['playlists-list'] = [
module.exports = angular.module('ponyfm').controller "playlists-list", [
'$scope', 'playlists', '$state',
($scope, playlists, $state) ->
playlists.fetchList($state.params.page).done (searchResults) ->
playlists.mainQuery.fetch().done (searchResults) ->
$scope.playlists = searchResults.playlists
]

View file

@ -29,26 +29,56 @@ module.exports = angular.module('ponyfm').controller "playlists", [
$scope.query = playlists.mainQuery
$scope.filters = playlists.filters
$scope.setFilter = (filter, value) ->
$scope.query.setFilter filter, value
$state.transitionTo 'content.playlists.list', {filter: $scope.query.toFilterString()}
$scope.clearFilter = (filter) ->
$scope.query.clearFilter filter
$state.transitionTo 'content.playlists.list', {filter: $scope.query.toFilterString()}
playlists.mainQuery.listen (searchResults) ->
$scope.playlists = searchResults.playlists
refreshPages = (list) ->
$scope.playlists = list.playlists
$scope.currentPage = parseInt(list.current_page)
$scope.totalPages = parseInt(list.total_pages)
$scope.currentPage = parseInt(searchResults.current_page)
$scope.totalPages = parseInt(searchResults.total_pages)
delete $scope.nextPage
delete $scope.prevPage
$scope.nextPage = $scope.currentPage + 1 if $scope.currentPage < $scope.totalPages
$scope.prevPage = $scope.currentPage - 1 if $scope.currentPage > 1
$scope.pages = [1..$scope.totalPages]
$scope.allPages = [1..$scope.totalPages]
playlists.fetchList($state.params.page).done refreshPages
$scope.$on 'playlists-feteched', (e, list) -> refreshPages(list)
# TODO: turn this into a directive
# The actual first page will always be in the paginator.
$scope.pages = [1]
# This logic determines how many pages to add prior to the current page, if any.
firstPage = Math.max(2, $scope.currentPage-3)
$scope.pages = $scope.pages.concat [firstPage..$scope.currentPage] unless $scope.currentPage == 1
pagesLeftToAdd = 8-$scope.pages.length
lastPage = Math.min($scope.totalPages - 1, $scope.currentPage+1+pagesLeftToAdd)
$scope.pages = $scope.pages.concat([$scope.currentPage+1..lastPage]) unless $scope.currentPage >= lastPage
# The actual last page will always be in the paginator.
$scope.pages.push($scope.totalPages) unless $scope.totalPages in $scope.pages
$scope.pageSelectorShown = false
$scope.gotoPage = (page) ->
return if !page
$state.transitionTo 'content.playlists.list', {page: page}
$state.transitionTo 'content.playlists.list', {filter: $state.params.filter, page: page}
$scope.showPageSelector = () ->
$scope.pageSelectorShown = true
focus('#pagination-jump-destination')
$scope.hidePageSelector = () ->
$scope.pageSelectorShown = false
$scope.jumpToPage = (inputPageNumber) ->
$scope.gotoPage(inputPageNumber)
$scope.$on '$destroy', -> playlists.mainQuery = playlists.createQuery()
]

View file

@ -18,6 +18,7 @@ module.exports = angular.module('ponyfm').factory('playlists', [
'$rootScope', '$state', '$http', 'auth'
($rootScope, $state, $http, auth) ->
playlistDef = null
filterDef = null
playlists = {}
playlistPages = []
@ -51,6 +52,55 @@ module.exports = angular.module('ponyfm').factory('playlists', [
currentFilter.selectedObject = {}
currentFilter.title = 'Any'
setPage: (page) ->
@page = page
@cachedDef = null
setFilter: (type, value) ->
@cachedDef = null
@page = 1
@filters[type] = value
toFilterString: ->
parts = []
_.each @availableFilters, (filter, name) =>
filterName = filter.name
if filter.type == 'single'
return if @filters[name].query == ''
parts.push(filterName + '-' + @filters[name].query)
else
return if @filters[name].selectedArray.length == 0
parts.push(filterName + '-' + _.map(@filters[name].selectedArray, (f) -> f.id).join '-')
return parts.join '!'
fromFilterString: (str) ->
@hasLoadedFilters = true
@cachedDef = null
@resetFilters()
filters = (str || "").split '!'
for queryFilter in filters
parts = queryFilter.split '-'
queryName = parts[0]
filterName = null
filter = null
for name,f of @availableFilters
continue if f.name != queryName
filterName = name
filter = f
return if !filter
if filter.type == 'single'
filterToSet = _.find filter.values, (f) -> f.query == parts[1]
filterToSet = (_.find filter.values, (f) -> f.isDefault) if filterToSet == null
@setFilter filterName, filterToSet
else
@toggleListFilter filterName, id for id in _.rest parts, 1
listen: (listener) ->
@listeners.push listener
@cachedDef.done listener if @cachedDef
@ -58,11 +108,10 @@ module.exports = angular.module('ponyfm').factory('playlists', [
fetch: () ->
return @cachedDef if @cachedDef
@cachedDef = new $.Deferred()
trackDef = @cachedDef
playlistDef = @cachedDef
query = '/api/web/playlists?'
parts = ['page=' + @page]
_.each @availableFilters, (filter, name) =>
if filter.type == 'single'
@ -78,10 +127,9 @@ module.exports = angular.module('ponyfm').factory('playlists', [
for listener in @listeners
listener playlists
playlistDef.resolve playlists
trackDef.resolve tracks
trackDef.promise()
playlistDef.promise()
self =
@ -91,6 +139,9 @@ module.exports = angular.module('ponyfm').factory('playlists', [
createQuery: -> new Query self.filters
loadFilters: ->
return filterDef if filterDef
filterDef = new $.Deferred()
self.filters.sort =
type: 'single'
name: 'sort'
@ -104,6 +155,9 @@ module.exports = angular.module('ponyfm').factory('playlists', [
]
self.mainQuery = self.createQuery()
filterDef.resolve self
filterDef.promise()
fetchList: (page, force) ->
force = force || false