codecombat/app/views/editor/patch_modal.coffee

65 lines
1.8 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
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
if false
@originalSource = targetModel.clone(false)
@onOriginalLoaded()
else
@originalSource = new targetModel.constructor({_id:targetID})
@originalSource.fetch()
@listenToOnce @originalSource, 'sync', @onOriginalLoaded
@addResourceToLoad(@originalSource)
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-04-12 00:11:52 -04:00
c
afterRender: ->
return if @originalSource.loading
headModel = null
if @targetModel.hasWriteAccess()
headModel = @originalSource.clone(false)
headModel.set(@targetModel.attributes)
2014-04-12 00:11:52 -04:00
pendingModel = @originalSource.clone(false)
pendingModel.applyDelta(@patch.get('delta'))
@deltaView = new DeltaView({model:pendingModel, headModel:headModel})
changeEl = @$el.find('.changes-stub')
@insertSubView(@deltaView, changeEl)
super()
acceptPatch: ->
delta = @deltaView.getApplicableDelta()
@targetModel.applyDelta(delta)
@targetModel.addPatchToAcceptOnSave(@patch)
@trigger 'accepted-patch'
@hide()
rejectPatch: ->
@patch.setStatus('rejected')
@hide()
withdrawPatch: ->
@patch.setStatus('withdrawn')
@hide()