codecombat/app/views/account/settings_view.coffee

132 lines
4.3 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/RootView'
template = require 'templates/account/settings'
{me} = require('lib/auth')
forms = require('lib/forms')
User = require('models/User')
WizardSettingsView = require './wizard_settings_view'
2014-01-12 14:54:50 -05:00
2014-01-03 13:32:13 -05:00
module.exports = class SettingsView extends View
id: 'account-settings-view'
2014-01-03 13:32:13 -05:00
template: template
events:
'click #save-button': 'save'
'change #settings-panes input': 'save'
2014-01-03 13:32:13 -05:00
'click #toggle-all-button': 'toggleEmailSubscriptions'
constructor: (options) ->
@save = _.debounce(@save, 200)
super options
return unless me
2014-03-24 02:53:41 -04:00
@listenTo(me, 'change', @refreshPicturePane) # depends on gravatar load
@listenTo(me, 'invalid', (errors) -> forms.applyErrorsToForm(@$el, me.validationError))
2014-01-03 13:32:13 -05:00
window.f = @getSubscriptions
2014-03-24 02:53:41 -04:00
refreshPicturePane: ->
2014-01-03 13:32:13 -05:00
h = $(@template(@getRenderData()))
newPane = $('#picture-pane', h)
oldPane = $('#picture-pane')
active = oldPane.hasClass('active')
oldPane.replaceWith(newPane)
newPane.i18n()
newPane.addClass('active') if active
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
$('#settings-tabs a', @$el).click((e) =>
e.preventDefault()
$(e.target).tab('show')
# make sure errors show up in the general pane, but keep the password pane clean
$('#password-pane input').val('')
@save() unless $(e.target).attr('href') is '#password-pane'
forms.clearFormAlerts($('#password-pane', @$el))
)
@chooseTab(location.hash.replace('#',''))
WizardSettingsView = new WizardSettingsView()
2014-03-24 12:58:34 -04:00
@listenTo(WizardSettingsView, 'change', @save)
@insertSubView WizardSettingsView
2014-01-03 13:32:13 -05:00
chooseTab: (category) ->
id = "##{category}-pane"
pane = $(id, @$el)
return @chooseTab('general') unless pane.length or category is 'general'
loc = "a[href=#{id}]"
$(loc, @$el).tab('show')
$('.tab-pane').removeClass('active')
pane.addClass('active')
@currentTab = category
getRenderData: ->
c = super()
return c unless me
c.gravatarName = c.me?.gravatarName()
c.photos = me.gravatarPhotoURLs()
c.chosenPhoto = me.getPhotoURL()
c.subs = {}
2014-03-10 16:20:00 -04:00
c.subs[sub] = 1 for sub in c.me.get('emailSubscriptions') or ['announcement', 'notification', 'tester', 'level_creator', 'developer']
2014-01-03 13:32:13 -05:00
c
getSubscriptions: ->
inputs = $('#email-pane input[type="checkbox"]', @$el)
inputs = ($(i) for i in inputs)
subs = (i.attr('name') for i in inputs when i.prop('checked'))
2014-01-03 13:32:13 -05:00
subs = (s.replace('email_', '') for s in subs)
subs
toggleEmailSubscriptions: =>
subs = @getSubscriptions()
$('#email-pane input[type="checkbox"]', @$el).prop('checked', not Boolean(subs.length))
2014-01-03 13:32:13 -05:00
@save()
2014-03-24 12:58:34 -04:00
save: ->
2014-01-03 13:32:13 -05:00
forms.clearFormAlerts(@$el)
@grabData()
res = me.validate()
if res?
forms.applyErrorsToForm(@$el, res)
return
2014-03-10 16:20:00 -04:00
return unless me.hasLocalChanges()
2014-01-03 13:32:13 -05:00
res = me.save()
return unless res
save = $('#save-button', @$el).text($.i18n.t('common.saving', defaultValue: 'Saving...'))
.addClass('btn-info').show().removeClass('btn-danger')
2014-01-03 13:32:13 -05:00
res.error ->
errors = JSON.parse(res.responseText)
forms.applyErrorsToForm(@$el, errors)
save.text($.i18n.t('account_settings.error_saving', defaultValue: 'Error Saving')).removeClass('btn-info').addClass('btn-danger')
res.success (model, response, options) ->
save.text($.i18n.t('account_settings.saved', defaultValue: 'Changes Saved')).removeClass('btn-info')
grabData: ->
@grabPasswordData()
@grabOtherData()
grabPasswordData: ->
password1 = $('#password', @$el).val()
password2 = $('#password2', @$el).val()
bothThere = Boolean(password1) and Boolean(password2)
if bothThere and password1 isnt password2
message = $.i18n.t('account_settings.password_mismatch', defaultValue: 'Password does not match.')
err = [message:message, property:'password2', formatted:true]
forms.applyErrorsToForm(@$el, err)
return
if bothThere
me.set('password', password1)
grabOtherData: ->
me.set('name', $('#name', @$el).val())
me.set('email', $('#email', @$el).val())
me.set('emailSubscriptions', @getSubscriptions())
adminCheckbox = @$el.find('#admin')
if adminCheckbox.length
permissions = []
permissions.push 'admin' if adminCheckbox.prop('checked')
me.set('permissions', permissions)