2015-10-25 06:17:45 +01:00
|
|
|
# Pony.fm - A community for pony fan music.
|
|
|
|
# Copyright (C) 2015 Peter Deltchev
|
|
|
|
#
|
|
|
|
# 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').controller "account-settings", [
|
2016-12-28 16:07:28 +01:00
|
|
|
'$scope', 'auth', '$state', 'activity-types',
|
|
|
|
($scope, auth, $state, activityTypes) ->
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.settings = {}
|
|
|
|
$scope.errors = {}
|
|
|
|
$scope.isDirty = false
|
2016-12-28 16:07:28 +01:00
|
|
|
$scope.unsubscribeMessage = activityTypes.getUnsubscribeMessage()
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
$scope.touchModel = () ->
|
|
|
|
$scope.isDirty = true
|
|
|
|
|
2016-06-06 05:05:51 +02:00
|
|
|
$scope.updateUrl = (newSlug) ->
|
|
|
|
$state.go(
|
|
|
|
'content.artist.account.settings',
|
|
|
|
{slug: newSlug},
|
|
|
|
{location: 'replace', reload: true}
|
|
|
|
).then ->
|
|
|
|
$scope.$emit('user-updated')
|
|
|
|
|
|
|
|
|
|
|
|
$scope.refresh = (refreshSlug = false) ->
|
|
|
|
currentSlug = if refreshSlug then $scope.settings.slug else $state.params.slug
|
|
|
|
auth.refresh()
|
|
|
|
|
|
|
|
$.getJSON('/api/web/account/settings/' + currentSlug)
|
2015-10-25 03:35:37 +01:00
|
|
|
.done (res) -> $scope.$apply ->
|
|
|
|
$scope.settings = res
|
|
|
|
|
2016-06-06 05:05:51 +02:00
|
|
|
if refreshSlug
|
|
|
|
# Ensures the slug in the URL is up to date - the "content.artist"
|
|
|
|
# state depends on it to fetch updated profile data.
|
|
|
|
$scope.updateUrl(currentSlug)
|
|
|
|
|
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.setAvatar = (image, type) ->
|
|
|
|
delete $scope.settings.avatar_id
|
|
|
|
delete $scope.settings.avatar
|
|
|
|
|
|
|
|
if type == 'file'
|
|
|
|
$scope.settings.avatar = image
|
|
|
|
else if type == 'gallery'
|
|
|
|
$scope.settings.avatar_id = image.id
|
|
|
|
|
|
|
|
$scope.isDirty = true
|
|
|
|
|
|
|
|
$scope.updateAccount = () ->
|
|
|
|
return if !$scope.isDirty
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest()
|
|
|
|
xhr.onload = -> $scope.$apply ->
|
|
|
|
$scope.isSaving = false
|
|
|
|
response = $.parseJSON(xhr.responseText)
|
|
|
|
if xhr.status != 200
|
|
|
|
$scope.errors = {}
|
2016-06-06 05:05:51 +02:00
|
|
|
_.each response.errors, (value, key) -> $scope.errors[key] = value.join ' '
|
2015-10-25 03:35:37 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
$scope.isDirty = false
|
|
|
|
$scope.errors = {}
|
2016-06-06 05:05:51 +02:00
|
|
|
$scope.refresh(true)
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
formData = new FormData()
|
|
|
|
|
|
|
|
_.each $scope.settings, (value, name) ->
|
|
|
|
if name == 'avatar'
|
|
|
|
return if value == null
|
|
|
|
if typeof(value) == 'object'
|
|
|
|
formData.append name, value, value.name
|
2016-12-25 03:11:42 +01:00
|
|
|
else if name == 'notifications'
|
|
|
|
formData.append name, JSON.stringify(value)
|
2015-10-25 03:35:37 +01:00
|
|
|
else
|
|
|
|
formData.append name, value
|
|
|
|
|
2016-05-17 01:30:05 +02:00
|
|
|
xhr.open 'POST', '/api/web/account/settings/save/' + $state.params.slug, true
|
2015-11-11 22:45:44 +01:00
|
|
|
xhr.setRequestHeader 'X-XSRF-TOKEN', $.cookie('XSRF-TOKEN')
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.isSaving = true
|
|
|
|
xhr.send formData
|
|
|
|
|
|
|
|
$scope.refresh()
|
|
|
|
|
|
|
|
$scope.$on '$stateChangeStart', (e) ->
|
|
|
|
return if $scope.selectedTrack == null || !$scope.isDirty
|
|
|
|
e.preventDefault() if !confirm('Are you sure you want to leave this page without saving your changes?')
|
2015-09-13 04:19:45 +02:00
|
|
|
]
|