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/save-version-modal'
|
2014-07-23 10:02:45 -04:00
|
|
|
DeltaView = require 'views/editor/DeltaView'
|
2014-04-10 16:09:44 -04:00
|
|
|
Patch = require 'models/Patch'
|
2014-11-28 20:49:41 -05:00
|
|
|
forms = require 'core/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:
|
2014-08-21 15:49:06 -04:00
|
|
|
'click #save-version-button': 'saveChanges'
|
2014-01-03 13:32:13 -05:00
|
|
|
'click #cla-link': 'onClickCLALink'
|
|
|
|
'click #agreement-button': 'onAgreedToCLA'
|
2014-05-15 15:37:39 -04:00
|
|
|
'click #submit-patch-button': 'submitPatch'
|
2014-08-21 15:49:06 -04:00
|
|
|
'submit form': 'onSubmitForm'
|
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()
|
2015-12-24 14:21:12 -05:00
|
|
|
@hasChanges = @model.hasLocalChanges()
|
2014-04-10 16:09:44 -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
|
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
|
2014-09-01 17:27:47 -04:00
|
|
|
console.error 'Couldn\'t create delta view:', e, e.stack
|
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
|
|
|
|
2014-08-21 15:49:06 -04:00
|
|
|
onSubmitForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
if @isPatch then @submitPatch() else @saveChanges()
|
2014-08-27 15:24:03 -04:00
|
|
|
|
2014-08-21 15:49:06 -04:00
|
|
|
saveChanges: ->
|
2015-10-21 18:34:07 -04:00
|
|
|
@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()
|
|
|
|
}
|
|
|
|
|
2014-05-15 15:37:39 -04:00
|
|
|
submitPatch: ->
|
2014-04-10 16:09:44 -04:00
|
|
|
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)
|