codecombat/app/views/editor/modal/SaveVersionModal.coffee

87 lines
2.5 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
template = require 'templates/editor/modal/save-version-modal'
DeltaView = require 'views/editor/DeltaView'
Patch = require 'models/Patch'
forms = require 'core/forms'
2014-01-03 13:32:13 -05:00
module.exports = class SaveVersionModal extends ModalView
id: 'save-version-modal'
template: template
plain: true
modalWidthPercent: 60
2014-01-03 13:32:13 -05:00
events:
'click #save-version-button': 'saveChanges'
2014-01-03 13:32:13 -05:00
'click #cla-link': 'onClickCLALink'
'click #agreement-button': 'onAgreedToCLA'
'click #submit-patch-button': 'submitPatch'
'submit form': 'onSubmitForm'
constructor: (options) ->
super options
@model = options.model or options.level
@isPatch = not @model.hasWriteAccess()
@hasChanges = @model.hasLocalChanges()
2014-04-16 13:42:32 -04:00
afterRender: (insertDeltaView=true) ->
2014-01-03 13:32:13 -05:00
super()
@$el.find(if me.get('signedCLA') then '#accept-cla-wrapper' else '#save-version-button').hide()
changeEl = @$el.find('.changes-stub')
2014-04-16 13:42:32 -04:00
if insertDeltaView
try
2014-06-30 22:16:26 -04:00
deltaView = new DeltaView({model: @model})
2014-04-16 13:42:32 -04:00
@insertSubView(deltaView, changeEl)
catch e
console.error 'Couldn\'t create delta view:', e, e.stack
@$el.find('.commit-message input').attr('placeholder', $.i18n.t('general.commit_msg'))
2014-01-03 13:32:13 -05:00
onSubmitForm: (e) ->
e.preventDefault()
if @isPatch then @submitPatch() else @saveChanges()
saveChanges: ->
@trigger 'save-new-version', {
2014-01-03 13:32:13 -05:00
major: @$el.find('#major-version').prop('checked')
commitMessage: @$el.find('#commit-message').val()
}
submitPatch: ->
forms.clearFormAlerts @$el
patch = new Patch()
patch.set 'delta', @model.getDelta()
patch.set 'commitMessage', @$el.find('#commit-message').val()
patch.set 'target', {
'collection': _.string.underscored @model.constructor.className
'id': @model.id
}
errors = patch.validate()
forms.applyErrorsToForm(@$el, errors) if errors
res = patch.save()
return unless res
@enableModalInProgress(@$el)
res.error =>
@disableModalInProgress(@$el)
res.success =>
@hide()
2014-04-13 17:48:36 -04:00
2014-01-03 13:32:13 -05:00
onClickCLALink: ->
window.open('/cla', 'cla', 'height=800,width=900')
onAgreedToCLA: ->
@$el.find('#agreement-button').text('Saving').prop('disabled', true)
$.ajax({
2014-06-30 22:16:26 -04:00
url: '/db/user/me/agreeToCLA'
2014-01-03 13:32:13 -05:00
method: 'POST'
success: @onAgreeSucceeded
error: @onAgreeFailed
})
onAgreeSucceeded: =>
@$el.find('#agreement-button').text('Thanks!')
@$el.find('#save-version-button').show()
onAgreeFailed: =>
2014-04-13 17:48:36 -04:00
@$el.find('#agreement-button').text('Failed').prop('disabled', false)