codecombat/app/views/modal/wizard_settings_modal.coffee

51 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/ModalView'
template = require 'templates/modal/wizard_settings'
WizardSprite = require 'lib/surface/WizardSprite'
ThangType = require 'models/ThangType'
{me} = require 'lib/auth'
forms = require('lib/forms')
2014-01-03 13:32:13 -05:00
module.exports = class WizardSettingsModal extends View
2014-01-03 13:32:13 -05:00
id: "wizard-settings-modal"
template: template
closesOnClickOutside: false
events:
'change #wizard-settings-name': 'onNameChange'
'click #wizard-settings-done': 'onWizardSettingsDone'
2014-01-03 13:32:13 -05:00
afterRender: ->
WizardSettingsView = require 'views/account/wizard_settings_view'
view = new WizardSettingsView()
@insertSubView view
2014-01-03 13:32:13 -05:00
onNameChange: ->
2014-01-03 13:32:13 -05:00
me.set('name', $('#wizard-settings-name').val())
@checkNameExists()
2014-01-03 13:32:13 -05:00
checkNameExists: ->
forms.clearFormAlerts(@$el)
success = (id) => forms.applyErrorsToForm(@$el, {property:'name', message:'is already taken'}) if id and id isnt me.id
$.ajax("/db/user/#{me.get('name')}/nameToID", {success: success})
onWizardSettingsDone: ->
forms.clearFormAlerts(@$el)
res = me.validate()
if res?
forms.applyErrorsToForm(@$el, res)
return
res = me.save()
return unless res
save = $('#save-button', @$el).text($.i18n.t('common.saving', defaultValue: 'Saving...'))
.addClass('btn-info').show().removeClass('btn-danger')
res.error =>
errors = JSON.parse(res.responseText)
forms.applyErrorsToForm(@$el, errors)
@disableModalInProgress(@$el)
res.success (model, response, options) =>
@hide()
@enableModalInProgress(@$el)
me.save()