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

59 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2013-07-28 09:09:10 +02:00
angular.module('ponyfm').controller "account-albums", [
'$scope', '$state', 'taxonomies', '$dialog', 'lightbox'
($scope, $state, taxonomies, $dialog, lightbox) ->
2013-07-28 10:35:31 +02:00
albumsDb = {}
2013-07-28 19:45:21 +02:00
lastIndex = null
2013-07-28 09:09:10 +02:00
2013-07-28 10:35:31 +02:00
$scope.albums = []
2013-07-28 09:09:10 +02:00
$scope.data =
isEditorOpen: false
selectedAlbum: null
2013-07-28 19:45:21 +02:00
tracksDb: {}
refreshTrackDatabase = () ->
$.getJSON('/api/web/tracks/owned?published=true&in_album=false')
.done (tracks) -> $scope.$apply ->
$scope.data.tracksDb[track.id] = track for track in tracks
2013-07-28 09:09:10 +02:00
2013-07-28 10:35:31 +02:00
refreshList = () ->
$.getJSON('/api/web/albums/owned')
.done (albums) -> $scope.$apply ->
2013-07-28 19:45:21 +02:00
index = 0
album.index = index++ for album in albums
2013-07-28 10:35:31 +02:00
albumsDb[album.id] = album for album in albums
$scope.albums = albums
2013-07-28 19:45:21 +02:00
if $state.params.album_id != undefined
selectAlbum albumsDb[$state.params.album_id]
else if lastIndex != null
if $scope.albums.length
album = null
if $scope.albums.length > lastIndex + 1
album = $scope.albums[lastIndex]
else if lastIndex > 0
album = $scope.albums[lastIndex - 1]
else
album = $scope.albums[0]
$state.transitionTo 'account-content.albums.edit', {album_id: album.id}
2013-07-28 10:35:31 +02:00
selectAlbum = (album) -> $scope.data.selectedAlbum = album
2013-07-28 09:09:10 +02:00
$scope.$on '$stateChangeSuccess', () ->
if $state.params.album_id
selectAlbum albumsDb[$state.params.album_id]
else
selectAlbum null
2013-07-28 10:35:31 +02:00
2013-07-28 19:45:21 +02:00
$scope.$on 'album-created', () -> refreshList()
$scope.$on 'album-deleted', () ->
lastIndex = $scope.data.selectedAlbum.index
refreshList()
$scope.$on 'album-updated', () ->
refreshTrackDatabase()
2013-07-28 10:35:31 +02:00
refreshList()
2013-07-28 19:45:21 +02:00
refreshTrackDatabase()
2013-07-28 09:09:10 +02:00
]