codecombat/app/views/editor/ForkModal.coffee

44 lines
1.5 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
2014-08-13 17:26:41 -04:00
template = require 'templates/editor/fork-modal'
forms = require 'core/forms'
2014-08-13 17:26:41 -04:00
module.exports = class ForkModal extends ModalView
id: 'fork-modal'
template: template
instant: false
events:
'click #fork-model-confirm-button': 'forkModel'
'submit form': 'forkModel'
constructor: (options) ->
super options
@editorPath = options.editorPath # like 'level' or 'thang'
@model = options.model
@modelClass = @model.constructor
forkModel: (e) ->
e.preventDefault()
2014-08-13 17:26:41 -04:00
@showLoading()
forms.clearFormAlerts(@$el)
newModel = new @modelClass($.extend(true, {}, @model.attributes))
newModel.unset '_id'
newModel.unset 'version'
newModel.unset 'creator'
newModel.unset 'created'
newModel.unset 'original'
newModel.unset 'parent'
newModel.set 'commitMessage', "Forked from #{@model.get('name')}"
newModel.set 'name', @$el.find('#fork-model-name').val()
2014-09-08 19:03:29 -04:00
if @model.schema().properties.permissions
2014-08-13 17:26:41 -04:00
newModel.set 'permissions', [access: 'owner', target: me.id]
newPathPrefix = "editor/#{@editorPath}/"
res = newModel.save(null, {type: 'POST'}) # Override PUT so we can trigger postFirstVersion logic
2014-08-13 17:26:41 -04:00
return unless res
res.error =>
@hideLoading()
forms.applyErrorsToForm(@$el.find('form'), JSON.parse(res.responseText))
res.success =>
@hide()
application.router.navigate(newPathPrefix + newModel.get('slug'), {trigger: true})