2014-01-03 13:32:13 -05:00
|
|
|
ModalView = require 'views/kinds/ModalView'
|
|
|
|
template = require 'templates/modal/save_version'
|
2014-04-10 14:13:33 -04:00
|
|
|
DeltaView = require 'views/editor/delta'
|
2014-04-10 16:09:44 -04:00
|
|
|
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
|
2014-04-10 14:13:33 -04:00
|
|
|
plain: true
|
2014-04-17 17:23:33 -04:00
|
|
|
modalWidthPercent: 60
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #save-version-button': 'onClickSaveButton'
|
|
|
|
'click #cla-link': 'onClickCLALink'
|
|
|
|
'click #agreement-button': 'onAgreedToCLA'
|
2014-04-10 16:09:44 -04:00
|
|
|
'click #submit-patch-button': 'onClickPatchButton'
|
2014-04-10 14:13:33 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2014-04-12 11:48:49 -04:00
|
|
|
@model = options.model or options.level
|
2014-04-10 16:09:44 -04:00
|
|
|
@isPatch = not @model.hasWriteAccess()
|
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
c.isPatch = @isPatch
|
|
|
|
c.hasChanges = @model.hasLocalChanges()
|
|
|
|
c
|
2014-04-10 14:13:33 -04:00
|
|
|
|
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()
|
2014-04-10 14:13:33 -04:00
|
|
|
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
|
2014-04-12 11:48:49 -04:00
|
|
|
@$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()
|
|
|
|
}
|
|
|
|
|
2014-04-10 16:09:44 -04:00
|
|
|
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
|
2014-04-17 20:09:01 -04:00
|
|
|
patch.set 'editPath', document.location.pathname
|
2014-04-10 16:09:44 -04:00
|
|
|
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)
|