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

147 lines
4.1 KiB
CoffeeScript
Raw Normal View History

2013-07-30 06:53:57 +02:00
window.pfm.preloaders['account-albums-edit'] = [
'account-tracks', 'account-albums', '$state'
(tracks, albums, $state) ->
defs = [tracks.refresh()]
if $state.params.album_id
defs.push albums.getEdit($state.params.album_id, true)
$.when.all defs
]
2013-07-28 09:09:10 +02:00
angular.module('ponyfm').controller "account-albums-edit", [
2013-07-30 06:53:57 +02:00
'$scope', '$state', '$dialog', 'account-albums'
($scope, $state, $dialog, albums) ->
2013-07-28 10:35:31 +02:00
$scope.isNew = $state.params.album_id == undefined
2013-07-28 09:09:10 +02:00
$scope.data.isEditorOpen = true
$scope.errors = {}
$scope.isDirty = false
2013-07-28 10:35:31 +02:00
$scope.album = {}
$scope.isSaving = false
2013-07-28 19:45:21 +02:00
$scope.tracks = []
$scope.trackIds = {}
$scope.toggleTrack = (track) ->
if $scope.trackIds[track.id]
delete $scope.trackIds[track.id]
$scope.tracks.splice ($scope.tracks.indexOf track), 1
else
$scope.trackIds[track.id] = track
$scope.tracks.push track
$scope.isDirty = true
$scope.sortTracks = () ->
$scope.isDirty = true
2013-07-28 09:09:10 +02:00
$scope.touchModel = -> $scope.isDirty = true
2013-07-28 19:45:21 +02:00
$scope.setCover = (image, type) ->
delete $scope.album.cover_id
delete $scope.album.cover
if image == null
$scope.album.remove_cover = true
else if type == 'file'
$scope.album.cover = image
else if type == 'gallery'
$scope.album.cover_id = image.id
$scope.isDirty = true
2013-07-28 09:09:10 +02:00
$scope.$on '$destroy', -> $scope.data.isEditorOpen = false
2013-07-28 10:35:31 +02:00
$scope.saveAlbum = ->
2013-07-28 19:45:21 +02:00
return if !$scope.isNew && !$scope.isDirty
2013-07-28 10:35:31 +02:00
url =
if $scope.isNew
'/api/web/albums/create'
else
2013-07-28 19:45:21 +02:00
'/api/web/albums/edit/' + $state.params.album_id
2013-07-28 10:35:31 +02:00
xhr = new XMLHttpRequest()
xhr.onload = -> $scope.$apply ->
$scope.isSaving = false
2013-07-28 19:45:21 +02:00
response = $.parseJSON(xhr.responseText)
2013-07-28 10:35:31 +02:00
if xhr.status != 200
$scope.errors = {}
_.each response.errors, (value, key) -> $scope.errors[key] = value.join ', '
return
$scope.$emit 'album-updated'
if $scope.isNew
2013-07-28 19:45:21 +02:00
$scope.isDirty = false
$scope.$emit 'album-created'
2013-07-28 10:35:31 +02:00
$state.transitionTo 'account-content.albums.edit', {album_id: response.id}
else
2013-07-30 06:53:57 +02:00
$scope.isDirty = false
$scope.data.selectedAlbum.title = $scope.album.title
$scope.data.selectedAlbum.covers.normal = response.real_cover_url
2013-07-28 10:35:31 +02:00
formData = new FormData()
_.each $scope.album, (value, name) ->
if name == 'cover'
return if value == null
if typeof(value) == 'object'
formData.append name, value, value.name
else
formData.append name, value
2013-07-28 19:45:21 +02:00
formData.append 'track_ids', _.map($scope.tracks, (t) -> t.id).join()
2013-07-28 10:35:31 +02:00
xhr.open 'POST', url, true
xhr.setRequestHeader 'X-Token', pfm.token
$scope.isSaving = true
xhr.send formData
2013-07-28 19:45:21 +02:00
$scope.deleteAlbum = () ->
$dialog.messageBox('Delete ' + $scope.album.title, 'Are you sure you want to delete "' + $scope.album.title + '"? This cannot be undone.', [
{result: 'ok', label: 'Yes', cssClass: 'btn-danger'}, {result: 'cancel', label: 'No', cssClass: 'btn-primary'}
]).open().then (res) ->
return if res == 'cancel'
$.post('/api/web/albums/delete/' + $scope.album.id, {_token: window.pfm.token})
.then -> $scope.$apply ->
$scope.$emit 'album-deleted'
$state.transitionTo 'account-content.albums'
2013-07-28 10:35:31 +02:00
$scope.setCover = (image, type) ->
delete $scope.album.cover_id
delete $scope.album.cover
if image == null
$scope.album.remove_cover = true
else if type == 'file'
$scope.album.cover = image
else if type == 'gallery'
$scope.album.cover_id = image.id
$scope.isDirty = true
2013-07-28 19:45:21 +02:00
2013-07-30 06:53:57 +02:00
if !$scope.isNew
albums.getEdit($state.params.album_id).done (album) ->
$scope.album =
id: album.id
title: album.title
description: album.description
remove_cover: false
cover: album.cover_url
$scope.tracks = []
$scope.tracks.push track for track in album.tracks
$scope.trackIds[track.id] = track for track in album.tracks
else
$scope.album =
title: ''
description: ''
window.onbeforeunload = ->
return if !$scope.isDirty
"Are you sure you want to leave this page without saving your changes?"
$scope.$on '$locationChangeStart', (e) ->
return if !$scope.isDirty
2013-07-28 19:45:21 +02:00
e.preventDefault() if !confirm('Are you sure you want to leave this page without saving your changes?')
2013-07-28 09:09:10 +02:00
]