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')
|
2014-05-22 14:24:35 -04:00
|
|
|
AuthModalView = require 'views/modal/auth_modal'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-24 12:06:22 -05:00
|
|
|
WizardSettingsView = require './wizard_settings_view'
|
2014-04-05 20:05:03 -04:00
|
|
|
JobProfileView = require './job_profile_view'
|
2014-01-12 14:54:50 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
module.exports = class SettingsView extends View
|
2014-01-03 17:28:00 -05:00
|
|
|
id: 'account-settings-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
|
|
|
|
|
|
|
events:
|
2014-01-03 17:28:00 -05:00
|
|
|
'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, 'invalid', (errors) -> forms.applyErrorsToForm(@$el, me.validationError))
|
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('#',''))
|
2014-04-05 20:05:03 -04:00
|
|
|
|
|
|
|
wizardSettingsView = new WizardSettingsView()
|
|
|
|
@listenTo wizardSettingsView, 'change', @save
|
|
|
|
@insertSubView wizardSettingsView
|
|
|
|
|
|
|
|
@jobProfileView = new JobProfileView()
|
|
|
|
@listenTo @jobProfileView, 'change', @save
|
|
|
|
@insertSubView @jobProfileView
|
2014-04-15 13:45:54 -04:00
|
|
|
_.defer => @buildPictureTreema() # Not sure why, but the Treemas don't fully build without this if you reload the page.
|
2014-04-09 19:46:44 -04:00
|
|
|
|
2014-04-18 12:53:28 -04:00
|
|
|
afterInsert: ->
|
|
|
|
super()
|
|
|
|
if me.get('anonymous')
|
2014-05-22 14:24:35 -04:00
|
|
|
@openModalView new AuthModalView()
|
2014-04-18 12:53:28 -04:00
|
|
|
|
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.subs = {}
|
2014-04-21 19:15:23 -04:00
|
|
|
c.subs[sub] = 1 for sub in c.me.getEnabledEmails()
|
2014-04-07 20:58:02 -04:00
|
|
|
c.showsJobProfileTab = me.isAdmin() or me.get('jobProfile') or location.hash.search('job-profile-') isnt -1
|
2014-01-03 13:32:13 -05:00
|
|
|
c
|
|
|
|
|
|
|
|
getSubscriptions: ->
|
2014-04-21 19:15:23 -04:00
|
|
|
inputs = ($(i) for i in $('#email-pane input[type="checkbox"].changed', @$el))
|
|
|
|
emailNames = (i.attr('name').replace('email_', '') for i in inputs)
|
|
|
|
enableds = (i.prop('checked') for i in inputs)
|
|
|
|
_.zipObject emailNames, enableds
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
toggleEmailSubscriptions: =>
|
|
|
|
subs = @getSubscriptions()
|
2014-04-21 19:15:23 -04:00
|
|
|
$('#email-pane input[type="checkbox"]', @$el).prop('checked', not _.any(_.values(subs))).addClass('changed')
|
2014-01-03 13:32:13 -05:00
|
|
|
@save()
|
|
|
|
|
2014-04-09 19:46:44 -04:00
|
|
|
buildPictureTreema: ->
|
|
|
|
data = photoURL: me.get('photoURL')
|
2014-04-11 17:59:09 -04:00
|
|
|
data.photoURL = null if data.photoURL?.search('gravatar') isnt -1 # Old style
|
2014-04-15 18:01:54 -04:00
|
|
|
schema = $.extend true, {}, me.schema()
|
2014-04-12 04:35:56 -04:00
|
|
|
schema.properties = _.pick me.schema().properties, 'photoURL'
|
2014-04-09 19:46:44 -04:00
|
|
|
schema.required = ['photoURL']
|
|
|
|
treemaOptions =
|
|
|
|
filePath: "db/user/#{me.id}"
|
|
|
|
schema: schema
|
|
|
|
data: data
|
|
|
|
callbacks: {change: @onPictureChanged}
|
|
|
|
|
|
|
|
@pictureTreema = @$el.find('#picture-treema').treema treemaOptions
|
2014-04-12 17:53:09 -04:00
|
|
|
@pictureTreema?.build()
|
|
|
|
@pictureTreema?.open()
|
2014-04-09 19:46:44 -04:00
|
|
|
@$el.find('.gravatar-fallback').toggle not me.get 'photoURL'
|
|
|
|
|
|
|
|
onPictureChanged: (e) =>
|
|
|
|
@trigger 'change'
|
|
|
|
@$el.find('.gravatar-fallback').toggle not me.get 'photoURL'
|
|
|
|
|
2014-04-21 19:15:23 -04:00
|
|
|
save: (e) ->
|
|
|
|
$(e.target).addClass('changed') if e
|
2014-01-03 13:32:13 -05:00
|
|
|
forms.clearFormAlerts(@$el)
|
|
|
|
@grabData()
|
|
|
|
res = me.validate()
|
|
|
|
if res?
|
2014-05-31 01:12:44 -04:00
|
|
|
console.error "Couldn't save because of validation errors:", res
|
2014-01-03 13:32:13 -05:00
|
|
|
forms.applyErrorsToForm(@$el, res)
|
|
|
|
return
|
2014-03-10 16:20:00 -04:00
|
|
|
|
2014-02-27 15:02:08 -05:00
|
|
|
return unless me.hasLocalChanges()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
res = me.patch()
|
2014-01-03 13:32:13 -05:00
|
|
|
return unless res
|
2014-01-19 11:00:41 -05:00
|
|
|
save = $('#save-button', @$el).text($.i18n.t('common.saving', defaultValue: 'Saving...'))
|
2014-04-10 14:58:51 -04:00
|
|
|
.removeClass('btn-danger').addClass('btn-success').show()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
res.error ->
|
|
|
|
errors = JSON.parse(res.responseText)
|
|
|
|
forms.applyErrorsToForm(@$el, errors)
|
2014-04-10 14:58:51 -04:00
|
|
|
save.text($.i18n.t('account_settings.error_saving', defaultValue: 'Error Saving')).removeClass('btn-success').addClass('btn-danger', 500)
|
2014-01-03 13:32:13 -05:00
|
|
|
res.success (model, response, options) ->
|
2014-04-10 14:58:51 -04:00
|
|
|
save.text($.i18n.t('account_settings.saved', defaultValue: 'Changes Saved')).removeClass('btn-success', 500)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
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: ->
|
2014-04-09 19:46:44 -04:00
|
|
|
me.set 'name', $('#name', @$el).val()
|
|
|
|
me.set 'email', $('#email', @$el).val()
|
2014-04-21 19:15:23 -04:00
|
|
|
for emailName, enabled of @getSubscriptions()
|
2014-05-31 01:12:44 -04:00
|
|
|
me.setEmailSubscription emailName, enabled
|
2014-04-09 19:46:44 -04:00
|
|
|
me.set 'photoURL', @pictureTreema.get('/photoURL')
|
2014-01-03 17:28:00 -05:00
|
|
|
|
|
|
|
adminCheckbox = @$el.find('#admin')
|
|
|
|
if adminCheckbox.length
|
|
|
|
permissions = []
|
|
|
|
permissions.push 'admin' if adminCheckbox.prop('checked')
|
|
|
|
me.set('permissions', permissions)
|
2014-04-05 20:05:03 -04:00
|
|
|
|
|
|
|
jobProfile = me.get('jobProfile') ? {}
|
|
|
|
updated = false
|
|
|
|
for key, val of @jobProfileView.getData()
|
|
|
|
updated = updated or jobProfile[key] isnt val
|
|
|
|
jobProfile[key] = val
|
|
|
|
if updated
|
2014-04-07 18:21:05 -04:00
|
|
|
jobProfile.updated = (new Date()).toISOString()
|
2014-04-05 20:05:03 -04:00
|
|
|
me.set 'jobProfile', jobProfile
|