codecombat/app/views/editor/patch_modal.coffee

75 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-04-12 00:11:52 -04:00
ModalView = require 'views/kinds/ModalView'
template = require 'templates/editor/patch_modal'
DeltaView = require 'views/editor/delta'
auth = require 'lib/auth'
2014-04-12 00:11:52 -04:00
module.exports = class PatchModal extends ModalView
id: "patch-modal"
template: template
plain: true
modalWidthPercent: 60
2014-06-24 17:25:01 -04:00
@DOC_SKIP_PATHS = ['_id','version', 'commitMessage', 'parent', 'created', 'slug', 'index', '__v', 'patches', 'creator']
events:
'click #withdraw-button': 'withdrawPatch'
'click #reject-button': 'rejectPatch'
'click #accept-button': 'acceptPatch'
2014-04-12 00:11:52 -04:00
constructor: (@patch, @targetModel, options) ->
super(options)
targetID = @patch.get('target').id
2014-05-05 18:19:35 -04:00
if targetID is @targetModel.id
@originalSource = @targetModel.clone(false)
2014-04-12 00:11:52 -04:00
else
2014-05-05 18:19:35 -04:00
@originalSource = new @targetModel.constructor({_id:targetID})
@supermodel.loadModel @originalSource, 'source_document'
applyDelta: ->
2014-06-24 17:25:01 -04:00
@headModel = null
if @targetModel.hasWriteAccess()
@headModel = @originalSource.clone(false)
@headModel.markToRevert true
@headModel.set(@targetModel.attributes)
@headModel.loaded = true
@pendingModel = @originalSource.clone(false)
@pendingModel.markToRevert true
@deltaWorked = @pendingModel.applyDelta(@patch.get('delta'))
@pendingModel.loaded = true
render: ->
2014-07-04 14:05:52 -04:00
@applyDelta() if @supermodel.finished()
2014-06-24 17:25:01 -04:00
super()
2014-04-12 00:11:52 -04:00
getRenderData: ->
c = super()
c.isPatchCreator = @patch.get('creator') is auth.me.id
c.isPatchRecipient = @targetModel.hasWriteAccess()
c.status = @patch.get 'status'
c.patch = @patch
2014-06-24 17:25:01 -04:00
c.deltaWorked = @deltaWorked
2014-04-12 00:11:52 -04:00
c
2014-04-12 00:11:52 -04:00
afterRender: ->
2014-06-24 17:25:01 -04:00
return super() unless @supermodel.finished() and @deltaWorked
@deltaView = new DeltaView({model:@pendingModel, headModel:@headModel, skipPaths: PatchModal.DOC_SKIP_PATHS})
2014-04-12 00:11:52 -04:00
changeEl = @$el.find('.changes-stub')
@insertSubView(@deltaView, changeEl)
super()
2014-04-12 00:11:52 -04:00
acceptPatch: ->
delta = @deltaView.getApplicableDelta()
@targetModel.applyDelta(delta)
@targetModel.saveBackupNow()
2014-04-17 18:44:19 -04:00
@patch.setStatus('accepted')
@trigger 'accepted-patch'
@hide()
rejectPatch: ->
@patch.setStatus('rejected')
@hide()
withdrawPatch: ->
@patch.setStatus('withdrawn')
@hide()