codecombat/app/views/editor/level/components/NewLevelComponentModal.coffee

43 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:22:26 -04:00
ModalView = require 'views/kinds/ModalView'
2014-01-03 13:32:13 -05:00
template = require 'templates/editor/level/component/new'
LevelComponent = require 'models/LevelComponent'
forms = require 'lib/forms'
{me} = require 'lib/auth'
module.exports = class NewLevelComponentModal extends ModalView
2014-06-30 22:16:26 -04:00
id: 'editor-level-component-new-modal'
2014-01-03 13:32:13 -05:00
template: template
instant: false
modalWidthPercent: 60
events:
'click #new-level-component-submit': 'makeNewLevelComponent'
'submit form': 'makeNewLevelComponent'
getRenderData: ->
c = super()
c.systems = LevelComponent.schema.properties.system.enum
c
2014-01-03 13:32:13 -05:00
makeNewLevelComponent: (e) ->
e.preventDefault()
system = @$el.find('#level-component-system').val()
name = @$el.find('#level-component-name').val()
component = new LevelComponent()
component.set 'system', system
component.set 'name', name
component.set 'code', component.get('code').replace(/AttacksSelf/g, name)
component.set 'permissions', [{access: 'owner', target: me.id}] # Private until saved in a published Level
res = component.save()
return unless res
@showLoading()
res.error =>
@hideLoading()
2014-06-30 22:16:26 -04:00
console.log 'Got errors:', JSON.parse(res.responseText)
2014-01-03 13:32:13 -05:00
forms.applyErrorsToForm(@$el, JSON.parse(res.responseText))
res.success =>
@supermodel.registerModel component
2014-01-03 13:32:13 -05:00
Backbone.Mediator.publish 'edit-level-component', original: component.get('_id'), majorVersion: 0
@hide()