codecombat/app/views/modal/save_version_modal.coffee

87 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
ModalView = require 'views/kinds/ModalView'
template = require 'templates/modal/save_version'
DeltaView = require 'views/editor/delta'
Patch = require 'models/Patch'
forms = require 'lib/forms'
2014-01-03 13:32:13 -05:00
module.exports = class SaveVersionModal extends ModalView
id: 'save-version-modal'
template: template
plain: true
2014-01-03 13:32:13 -05:00
events:
'click #save-version-button': 'onClickSaveButton'
'click #cla-link': 'onClickCLALink'
'click #agreement-button': 'onAgreedToCLA'
'click #submit-patch-button': 'onClickPatchButton'
constructor: (options) ->
super options
@model = options.model or options.level
new Patch() # hack to get the schema to load, delete this later
@isPatch = not @model.hasWriteAccess()
getRenderData: ->
c = super()
c.isPatch = @isPatch
c.hasChanges = @model.hasLocalChanges()
c
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
deltaView = new DeltaView({model:@model})
@insertSubView(deltaView, changeEl)
catch e
console.error "Couldn't create delta view:", e
@$el.find('.commit-message input').attr('placeholder', $.i18n.t('general.commit_msg'))
2014-01-03 13:32:13 -05:00
onClickSaveButton: ->
Backbone.Mediator.publish 'save-new-version', {
major: @$el.find('#major-version').prop('checked')
commitMessage: @$el.find('#commit-message').val()
}
onClickPatchButton: ->
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({
url: "/db/user/me/agreeToCLA"
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)