2016-02-05 21:44:56 +01:00
|
|
|
# Pony.fm - A community for pony fan music.
|
2016-02-14 02:38:45 +01:00
|
|
|
# Copyright (C) 2016 Peter Deltchev
|
2016-02-05 21:44:56 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-14 17:14:30 +01:00
|
|
|
module.exports = angular.module('ponyfm').directive 'pfmTrackEditor', () ->
|
2016-02-05 21:44:56 +01:00
|
|
|
restrict: 'E'
|
|
|
|
templateUrl: '/templates/directives/track-editor.html'
|
|
|
|
scope:
|
|
|
|
trackId: '=trackId'
|
|
|
|
|
|
|
|
controller: [
|
2016-11-28 01:19:16 +01:00
|
|
|
'$scope', '$modal', 'auth', 'account-tracks', 'account-albums', 'taxonomies', 'images', '$state', 'upload'
|
|
|
|
($scope, $modal, auth, tracks, albums, taxonomies, images, $state, upload) ->
|
2016-02-05 21:44:56 +01:00
|
|
|
$scope.isDirty = false
|
|
|
|
$scope.isSaving = false
|
|
|
|
$scope.taxonomies = taxonomies
|
|
|
|
$scope.selectedSongsTitle = 'None'
|
|
|
|
$scope.selectedSongs = {}
|
|
|
|
$scope.albums = []
|
|
|
|
$scope.selectedAlbum = null
|
2016-05-16 00:03:49 +02:00
|
|
|
$scope.isAdmin = auth.data.isAdmin
|
2016-02-05 21:44:56 +01:00
|
|
|
albumsDb = {}
|
2016-12-20 22:27:41 +01:00
|
|
|
|
|
|
|
$scope.disableHwc = false
|
2016-12-17 22:16:16 +01:00
|
|
|
|
2016-11-28 01:19:16 +01:00
|
|
|
$scope.data = upload
|
|
|
|
$scope.userSlug = $state.params.slug
|
|
|
|
|
|
|
|
$scope.fileChanged = (e) ->
|
|
|
|
files = e.files
|
|
|
|
$scope.$apply -> upload.uploadNewVersion(files[0], $scope.userSlug, $scope.trackId)
|
|
|
|
|
2016-08-30 00:02:14 +02:00
|
|
|
$scope.$watch 'trackId', (newValue, oldValue) ->
|
|
|
|
$scope.updateUI()
|
|
|
|
|
2016-02-05 21:44:56 +01:00
|
|
|
$scope.selectAlbum = (album) ->
|
|
|
|
$scope.selectedAlbum = album
|
|
|
|
$scope.track.album_id = if album then album.id else null
|
|
|
|
$scope.isDirty = true
|
|
|
|
|
|
|
|
$scope.setCover = (image, type) ->
|
|
|
|
delete $scope.track.cover_id
|
|
|
|
delete $scope.track.cover
|
|
|
|
|
|
|
|
if image == null
|
|
|
|
$scope.track.remove_cover = true
|
|
|
|
else if type == 'file'
|
|
|
|
$scope.track.cover = image
|
|
|
|
else if type == 'gallery'
|
|
|
|
$scope.track.cover_id = image.id
|
|
|
|
|
|
|
|
$scope.isDirty = true
|
|
|
|
|
|
|
|
updateSongDisplay = () ->
|
|
|
|
if _.size $scope.selectedSongs
|
|
|
|
$scope.selectedSongsTitle = (_.map _.values($scope.selectedSongs), (s) -> s.title).join(', ')
|
|
|
|
else
|
|
|
|
$scope.selectedSongsTitle = 'None'
|
|
|
|
|
|
|
|
|
|
|
|
$scope.toggleSong = (song) ->
|
|
|
|
$scope.isDirty = true
|
|
|
|
if $scope.selectedSongs[song.id]
|
|
|
|
delete $scope.selectedSongs[song.id]
|
|
|
|
else
|
|
|
|
$scope.selectedSongs[song.id] = song
|
|
|
|
|
|
|
|
updateSongDisplay()
|
|
|
|
|
|
|
|
|
|
|
|
$scope.updateIsVocal = () ->
|
|
|
|
delete $scope.errors.lyrics if !$scope.track.is_vocal
|
|
|
|
|
|
|
|
$scope.updateTrack = () ->
|
|
|
|
xhr = new XMLHttpRequest()
|
|
|
|
xhr.onload = -> $scope.$apply ->
|
|
|
|
$scope.isSaving = false
|
|
|
|
if xhr.status != 200
|
|
|
|
errors =
|
|
|
|
if xhr.getResponseHeader('content-type') == 'application/json'
|
|
|
|
$.parseJSON(xhr.responseText).errors
|
|
|
|
else
|
|
|
|
['There was an unknown error!']
|
|
|
|
|
|
|
|
$scope.errors = {}
|
|
|
|
_.each errors, (value, key) -> $scope.errors[key] = value.join ', '
|
|
|
|
return
|
|
|
|
|
|
|
|
track = $.parseJSON(xhr.responseText)
|
2016-02-14 02:38:45 +01:00
|
|
|
$scope.$emit('track-updated', track)
|
2016-02-05 21:44:56 +01:00
|
|
|
|
2016-02-14 02:38:45 +01:00
|
|
|
$scope.track.is_published = true
|
2016-02-05 21:44:56 +01:00
|
|
|
$scope.isDirty = false
|
|
|
|
$scope.errors = {}
|
2016-03-06 13:27:53 +01:00
|
|
|
images.refresh(true, track.user_id)
|
2016-02-05 21:44:56 +01:00
|
|
|
|
|
|
|
formData = new FormData();
|
|
|
|
_.each $scope.track, (value, name) ->
|
|
|
|
if name == 'cover'
|
|
|
|
return if value == null
|
|
|
|
if typeof(value) == 'object'
|
|
|
|
formData.append name, value, value.name
|
2016-02-14 10:29:02 +01:00
|
|
|
|
2016-02-15 22:10:44 +01:00
|
|
|
if name == 'released_at' and value? and value != ''
|
2016-02-14 10:29:02 +01:00
|
|
|
formData.append name, value.toISOString()
|
|
|
|
|
2016-02-15 22:10:44 +01:00
|
|
|
else if value?
|
2016-02-05 21:44:56 +01:00
|
|
|
formData.append name, value
|
|
|
|
|
|
|
|
if parseInt($scope.track.track_type_id) == 2
|
2016-08-08 01:31:09 +02:00
|
|
|
formData.append 'show_song_ids', JSON.stringify(_.map(_.values($scope.selectedSongs), (s) -> s.id))
|
2016-02-05 21:44:56 +01:00
|
|
|
|
|
|
|
xhr.open 'POST', '/api/web/tracks/edit/' + $scope.track.id, true
|
|
|
|
xhr.setRequestHeader 'X-XSRF-TOKEN', $.cookie('XSRF-TOKEN')
|
|
|
|
$scope.isSaving = true
|
|
|
|
xhr.send formData
|
|
|
|
|
2016-02-14 02:38:45 +01:00
|
|
|
$scope.deleteTrack = () ->
|
2016-05-21 22:34:12 +02:00
|
|
|
modal = $modal({scope: $scope, templateUrl: 'templates/partials/delete-track-dialog.html', show: true});
|
|
|
|
|
|
|
|
$scope.confirmDeleteTrack = () ->
|
|
|
|
$.post('/api/web/tracks/delete/' + $scope.track.id)
|
|
|
|
.then -> $scope.$apply ->
|
|
|
|
$scope.$emit 'track-deleted'
|
2016-02-05 21:44:56 +01:00
|
|
|
|
2016-02-14 02:38:45 +01:00
|
|
|
# ========================================
|
|
|
|
# The part where everything gets loaded!
|
|
|
|
# ========================================
|
2016-08-30 00:02:14 +02:00
|
|
|
$scope.updateUI = () ->
|
|
|
|
tracks.getEdit($scope.trackId, true)
|
|
|
|
.then (track)->
|
|
|
|
images.refresh(true, track.user_id)
|
|
|
|
$.when(
|
|
|
|
albums.refresh(false, track.user_id),
|
|
|
|
taxonomies.refresh()
|
|
|
|
).done (albums, taxonomies)->
|
|
|
|
# Update album data
|
|
|
|
$scope.albums.length = 0
|
|
|
|
albumsDb = {}
|
|
|
|
for album in albums
|
|
|
|
albumsDb[album.id] = album
|
|
|
|
$scope.albums.push album
|
|
|
|
$scope.selectedAlbum = if track.album_id then albumsDb[track.album_id] else null
|
|
|
|
|
|
|
|
|
|
|
|
# Update track data
|
|
|
|
|
|
|
|
# The release date is in UTC - make sure we treat it as such.
|
|
|
|
if track.released_at
|
|
|
|
local_date = new Date(track.released_at)
|
|
|
|
utc_release_timestamp = local_date.getTime() + (local_date.getTimezoneOffset() * 60000);
|
|
|
|
utc_release_date = new Date(utc_release_timestamp)
|
|
|
|
else utc_release_date = ''
|
|
|
|
|
|
|
|
$scope.track =
|
|
|
|
id: track.id
|
|
|
|
title: track.title
|
|
|
|
user_id: track.user_id
|
|
|
|
username: track.username
|
|
|
|
description: track.description
|
|
|
|
lyrics: track.lyrics
|
|
|
|
is_explicit: track.is_explicit
|
|
|
|
is_downloadable: track.is_downloadable
|
|
|
|
is_vocal: track.is_vocal
|
|
|
|
license_id: track.license_id
|
|
|
|
genre_id: track.genre_id
|
|
|
|
track_type_id: track.track_type_id
|
|
|
|
released_at: utc_release_date
|
|
|
|
remove_cover: false
|
|
|
|
cover_id: track.cover_id
|
|
|
|
cover_url: track.cover_url
|
|
|
|
album_id: track.album_id
|
|
|
|
is_published: track.is_published
|
|
|
|
is_listed: track.is_listed
|
2016-11-21 01:42:43 +01:00
|
|
|
# Seasonal
|
|
|
|
hwc_submit: track.hwc_submit
|
2016-08-30 00:02:14 +02:00
|
|
|
|
|
|
|
$scope.selectedSongs = {}
|
|
|
|
$scope.selectedSongs[song.id] = song for song in track.show_songs
|
|
|
|
updateSongDisplay()
|
2016-02-05 21:44:56 +01:00
|
|
|
|
|
|
|
$scope.touchModel = -> $scope.isDirty = true
|
|
|
|
|
|
|
|
$scope.$on '$locationChangeStart', (e) ->
|
|
|
|
return if !$scope.isDirty
|
|
|
|
e.preventDefault() if !confirm('Are you sure you want to leave this page without saving your changes?')
|
2016-08-30 00:02:14 +02:00
|
|
|
|
|
|
|
$scope.updateUI()
|
2016-02-05 21:44:56 +01:00
|
|
|
]
|