2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-11-29 16:36:41 -05:00
|
|
|
template = require 'templates/editor/modal/new-model-modal'
|
2014-11-28 20:49:41 -05:00
|
|
|
forms = require 'core/forms'
|
2014-08-05 08:33:33 -04:00
|
|
|
|
|
|
|
module.exports = class NewModelModal extends ModalView
|
|
|
|
id: 'new-model-modal'
|
|
|
|
template: template
|
|
|
|
plain: false
|
|
|
|
|
|
|
|
events:
|
2014-08-08 11:14:57 -04:00
|
|
|
'click button.new-model-submit': 'onModelSubmitted'
|
|
|
|
'submit form': 'onModelSubmitted'
|
2014-08-05 08:33:33 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2014-09-11 13:50:23 -04:00
|
|
|
@modelClass = options.model
|
2014-08-05 08:33:33 -04:00
|
|
|
@modelLabel = options.modelLabel
|
2015-06-29 14:13:15 -04:00
|
|
|
@newModelTitle = "editor.new_#{_.string.slugify @modelLabel}_title"
|
2014-08-05 09:14:51 -04:00
|
|
|
@properties = options.properties
|
2014-08-05 08:33:33 -04:00
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
makeNewModel: ->
|
2014-09-11 13:50:23 -04:00
|
|
|
model = new @modelClass
|
2014-08-08 11:14:57 -04:00
|
|
|
name = @$el.find('#name').val()
|
2014-08-05 08:33:33 -04:00
|
|
|
model.set('name', name)
|
2016-01-21 15:14:37 -05:00
|
|
|
if @modelClass.name is 'Level'
|
2016-01-20 13:48:53 -05:00
|
|
|
model.set('tasks', @modelClass.schema.default.tasks)
|
2014-09-11 13:50:23 -04:00
|
|
|
if model.schema().properties.permissions
|
2014-08-05 08:33:33 -04:00
|
|
|
model.set 'permissions', [{access: 'owner', target: me.id}]
|
2014-08-05 09:14:51 -04:00
|
|
|
model.set(key, prop) for key, prop of @properties if @properties?
|
2014-08-08 11:14:57 -04:00
|
|
|
model
|
|
|
|
|
|
|
|
onModelSubmitted: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
model = @makeNewModel()
|
2014-12-29 12:14:43 -05:00
|
|
|
res = model.save(null, {type: 'POST'}) # Override PUT so we can trigger postFirstVersion logic if needed
|
2014-08-05 08:33:33 -04:00
|
|
|
return unless res
|
|
|
|
|
|
|
|
forms.clearFormAlerts @$el
|
|
|
|
@showLoading(@$el.find('.modal-body'))
|
|
|
|
res.error =>
|
|
|
|
@hideLoading()
|
|
|
|
forms.applyErrorsToForm(@$el, JSON.parse(res.responseText))
|
|
|
|
#Backbone.Mediator.publish 'model-save-fail', model
|
|
|
|
res.success =>
|
|
|
|
@$el.modal('hide')
|
2014-08-08 11:14:57 -04:00
|
|
|
@trigger 'model-created', model
|
2014-08-05 08:33:33 -04:00
|
|
|
#Backbone.Mediator.publish 'model-save-success', model
|
|
|
|
|
2015-12-23 13:33:55 -05:00
|
|
|
afterInsert: ->
|
|
|
|
super()
|
|
|
|
_.delay (=> @$el?.find('#name').focus()), 500
|