2014-04-12 00:11:52 -04:00
|
|
|
ModalView = require 'views/kinds/ModalView'
|
|
|
|
template = require 'templates/editor/patch_modal'
|
|
|
|
DeltaView = require 'views/editor/delta'
|
2014-04-12 01:33:09 -04:00
|
|
|
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
|
2014-04-17 17:23:33 -04:00
|
|
|
modalWidthPercent: 60
|
2014-04-12 01:33:09 -04:00
|
|
|
|
|
|
|
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()
|
2014-04-12 01:33:09 -04:00
|
|
|
c.isPatchCreator = @patch.get('creator') is auth.me.id
|
|
|
|
c.isPatchRecipient = @targetModel.hasWriteAccess()
|
|
|
|
c.status = @patch.get 'status'
|
2014-04-17 17:23:33 -04:00
|
|
|
c.patch = @patch
|
2014-04-12 00:11:52 -04:00
|
|
|
c
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
return if @originalSource.loading
|
2014-04-17 17:23:33 -04:00
|
|
|
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()
|
2014-04-12 01:33:09 -04:00
|
|
|
@targetModel.applyDelta(delta)
|
|
|
|
@targetModel.addPatchToAcceptOnSave(@patch)
|
2014-04-17 17:23:33 -04:00
|
|
|
@trigger 'accepted-patch'
|
2014-04-12 01:33:09 -04:00
|
|
|
@hide()
|
|
|
|
|
|
|
|
rejectPatch: ->
|
|
|
|
@patch.setStatus('rejected')
|
|
|
|
@hide()
|
|
|
|
|
|
|
|
withdrawPatch: ->
|
|
|
|
@patch.setStatus('withdrawn')
|
|
|
|
@hide()
|