Pony.fm/public/scripts/app/controllers/account-tracks.coffee

103 lines
2.7 KiB
CoffeeScript
Raw Normal View History

2013-07-30 06:53:57 +02:00
window.pfm.preloaders['account-tracks'] = [
'account-tracks', 'account-albums', 'taxonomies'
(tracks, albums, taxonomies) ->
$.when.all [tracks.refresh(null, true), albums.refresh(true), taxonomies.refresh()]
]
2013-07-28 09:09:10 +02:00
angular.module('ponyfm').controller "account-tracks", [
2013-07-30 06:53:57 +02:00
'$scope', '$state', 'taxonomies', '$dialog', 'lightbox', 'account-albums', 'account-tracks'
($scope, $state, taxonomies, $dialog, lightbox, albums, tracks) ->
$scope.data =
selectedTrack: null
$scope.tracks = []
2013-07-28 19:45:21 +02:00
tracksDb = {}
2013-07-28 08:07:25 +02:00
2013-07-30 06:53:57 +02:00
setTracks = (tracks) ->
$scope.tracks.length = 0
tracksDb = {}
for track in tracks
tracksDb[track.id] = track
$scope.tracks.push track
if $state.params.track_id
$scope.data.selectedTrack = tracksDb[$state.params.track_id]
tracks.refresh().done setTracks
2013-07-25 23:33:04 +02:00
$scope.filters =
published: [
{title: 'Either', query: ''},
{title: 'Yes', query: 'published=1'},
{title: 'No', query: 'published=0'}]
sort: [
{title: 'Newest to Oldest', query: 'order=created_at,desc'},
{title: 'Oldest to Newest', query: 'order=created_at,asc'}]
genres: {}
trackTypes: {}
$scope.filter =
published: $scope.filters.published[0]
sort: $scope.filters.sort[0]
genres: {}
trackTypes: {}
$scope.titles =
genres: 'All'
trackTypes: 'All'
2013-07-30 06:53:57 +02:00
for genre in taxonomies.genres
$scope.filters.genres[genre.id] =
id: genre.id
title: genre.name
query: 'genres[]=' + genre.id
for type in taxonomies.trackTypes
$scope.filters.trackTypes[type.id] =
id: type.id
title: type.title
query: 'types[]=' + type.id
2013-07-25 23:33:04 +02:00
$scope.updateFilter = (type, filter) ->
$scope.filter[type] = filter
$scope.refreshList()
$scope.toggleFilter = (type, id) ->
if !$scope.filter[type][id]
$scope.filter[type][id] = $scope.filters[type][id]
else
delete $scope.filter[type][id]
length = _.keys($scope.filter[type]).length
if length == 1
$scope.titles[type] = _.map($scope.filter[type], (f) -> f.title).join ', '
else if length > 1
$scope.titles[type] = length + ' selected'
else
$scope.titles[type] = 'All'
$scope.refreshList()
$scope.refreshList = () ->
parts = [$scope.filter.sort.query, $scope.filter.published.query]
_.each $scope.filter.genres, (g) -> parts.push g.query
_.each $scope.filter.trackTypes, (g) -> parts.push g.query
query = parts.join '&'
2013-07-30 06:53:57 +02:00
tracks.refresh(query).done setTracks
2013-07-25 23:33:04 +02:00
2013-07-30 06:53:57 +02:00
$scope.selectTrack = (track) ->
$scope.data.selectedTrack = track
2013-07-25 23:33:04 +02:00
$scope.$on '$stateChangeSuccess', () ->
if $state.params.track_id
2013-07-30 06:53:57 +02:00
$scope.selectTrack tracksDb[$state.params.track_id]
2013-07-25 23:33:04 +02:00
else
2013-07-30 06:53:57 +02:00
$scope.selectTrack null
2013-07-28 19:45:21 +02:00
2013-07-30 06:53:57 +02:00
$scope.$on 'track-deleted', () ->
tracks.clearCache()
$scope.refreshList()
2013-07-25 23:33:04 +02:00
]