2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2014-04-12 00:11:52 -04:00
|
|
|
template = require 'templates/editor/patch_modal'
|
2014-07-23 10:02:45 -04:00
|
|
|
DeltaView = require 'views/editor/DeltaView'
|
2014-11-28 20:49:41 -05:00
|
|
|
auth = require 'core/auth'
|
|
|
|
deltasLib = require 'core/deltas'
|
2014-04-12 00:11:52 -04:00
|
|
|
|
|
|
|
module.exports = class PatchModal extends ModalView
|
2014-07-04 20:54:30 -04:00
|
|
|
id: 'patch-modal'
|
2014-04-12 00:11:52 -04:00
|
|
|
template: template
|
|
|
|
plain: true
|
2014-04-17 17:23:33 -04:00
|
|
|
modalWidthPercent: 60
|
2015-03-28 16:54:44 -04:00
|
|
|
instant: true
|
2014-05-02 15:32:41 -04:00
|
|
|
|
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
|
|
|
|
2015-03-28 16:54:44 -04:00
|
|
|
shortcuts:
|
|
|
|
'a': 'acceptPatch'
|
|
|
|
'r': 'rejectPatch'
|
|
|
|
|
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'
|
2014-07-04 10:43:23 -04:00
|
|
|
|
|
|
|
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
|
2014-07-04 10:43:23 -04:00
|
|
|
|
|
|
|
render: ->
|
2014-07-04 14:05:52 -04:00
|
|
|
@applyDelta() if @supermodel.finished()
|
2014-06-24 17:25:01 -04:00
|
|
|
super()
|
2014-05-02 15:32:41 -04:00
|
|
|
|
2014-04-12 00:11:52 -04:00
|
|
|
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-06-24 17:25:01 -04:00
|
|
|
c.deltaWorked = @deltaWorked
|
2014-04-12 00:11:52 -04:00
|
|
|
c
|
2014-05-02 15:32:41 -04:00
|
|
|
|
2014-04-12 00:11:52 -04:00
|
|
|
afterRender: ->
|
2014-06-24 17:25:01 -04:00
|
|
|
return super() unless @supermodel.finished() and @deltaWorked
|
2014-10-27 20:11:48 -04:00
|
|
|
@deltaView = new DeltaView({model:@pendingModel, headModel:@headModel, skipPaths: deltasLib.DOC_SKIP_PATHS})
|
2014-04-12 00:11:52 -04:00
|
|
|
changeEl = @$el.find('.changes-stub')
|
|
|
|
@insertSubView(@deltaView, changeEl)
|
|
|
|
super()
|
2014-05-02 15:32:41 -04:00
|
|
|
|
2014-04-12 00:11:52 -04:00
|
|
|
acceptPatch: ->
|
|
|
|
delta = @deltaView.getApplicableDelta()
|
2014-04-12 01:33:09 -04:00
|
|
|
@targetModel.applyDelta(delta)
|
2014-07-03 20:41:34 -04:00
|
|
|
@targetModel.saveBackupNow()
|
2014-04-17 18:44:19 -04:00
|
|
|
@patch.setStatus('accepted')
|
2014-04-17 17:23:33 -04:00
|
|
|
@trigger 'accepted-patch'
|
2014-04-12 01:33:09 -04:00
|
|
|
@hide()
|
2014-05-02 15:32:41 -04:00
|
|
|
|
2014-04-12 01:33:09 -04:00
|
|
|
rejectPatch: ->
|
|
|
|
@patch.setStatus('rejected')
|
|
|
|
@hide()
|
2014-05-02 15:32:41 -04:00
|
|
|
|
2014-04-12 01:33:09 -04:00
|
|
|
withdrawPatch: ->
|
|
|
|
@patch.setStatus('withdrawn')
|
2014-05-02 15:32:41 -04:00
|
|
|
@hide()
|