mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-28 10:06:08 -05:00
67 lines
2 KiB
CoffeeScript
67 lines
2 KiB
CoffeeScript
ModalView = require 'views/kinds/ModalView'
|
|
template = require 'templates/editor/patch_modal'
|
|
DeltaView = require 'views/editor/delta'
|
|
auth = require 'lib/auth'
|
|
|
|
module.exports = class PatchModal extends ModalView
|
|
id: "patch-modal"
|
|
template: template
|
|
plain: true
|
|
modalWidthPercent: 60
|
|
@DOC_SKIP_PATHS = ['_id','version', 'commitMessage', 'parent', 'created', 'slug', 'index', '__v', 'patches']
|
|
|
|
events:
|
|
'click #withdraw-button': 'withdrawPatch'
|
|
'click #reject-button': 'rejectPatch'
|
|
'click #accept-button': 'acceptPatch'
|
|
|
|
constructor: (@patch, @targetModel, options) ->
|
|
super(options)
|
|
targetID = @patch.get('target').id
|
|
if targetID is @targetModel.id
|
|
@originalSource = @targetModel.clone(false)
|
|
else
|
|
@originalSource = new @targetModel.constructor({_id:targetID})
|
|
@supermodel.loadModel @originalSource, 'source_document'
|
|
|
|
getRenderData: ->
|
|
c = super()
|
|
c.isPatchCreator = @patch.get('creator') is auth.me.id
|
|
c.isPatchRecipient = @targetModel.hasWriteAccess()
|
|
c.status = @patch.get 'status'
|
|
c.patch = @patch
|
|
c
|
|
|
|
afterRender: ->
|
|
return unless @supermodel.finished()
|
|
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
|
|
pendingModel.applyDelta(@patch.get('delta'))
|
|
pendingModel.loaded = true
|
|
|
|
@deltaView = new DeltaView({model:pendingModel, headModel:headModel, skipPaths: PatchModal.DOC_SKIP_PATHS})
|
|
changeEl = @$el.find('.changes-stub')
|
|
@insertSubView(@deltaView, changeEl)
|
|
super()
|
|
|
|
acceptPatch: ->
|
|
delta = @deltaView.getApplicableDelta()
|
|
@targetModel.applyDelta(delta)
|
|
@patch.setStatus('accepted')
|
|
@trigger 'accepted-patch'
|
|
@hide()
|
|
|
|
rejectPatch: ->
|
|
@patch.setStatus('rejected')
|
|
@hide()
|
|
|
|
withdrawPatch: ->
|
|
@patch.setStatus('withdrawn')
|
|
@hide()
|