codecombat/app/views/editor/modal/VersionsModal.coffee

72 lines
2.2 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
template = require 'templates/editor/modal/versions-modal'
DeltaView = require 'views/editor/DeltaView'
PatchModal = require 'views/editor/PatchModal'
nameLoader = require 'core/NameLoader'
CocoCollection = require 'collections/CocoCollection'
deltasLib = require 'core/deltas'
class VersionsViewCollection extends CocoCollection
2014-06-30 22:16:26 -04:00
url: ''
model: null
initialize: (@url, @levelID, @model) ->
super()
@url = url + levelID + '/versions'
@model = model
module.exports = class VersionsModal extends ModalView
template: template
2014-04-17 14:22:33 -04:00
plain: true
modalWidthPercent: 80
# needs to be overwritten by child
2014-06-30 22:16:26 -04:00
id: ''
url: ''
page: ''
events:
'change input.select': 'onSelectionChanged'
constructor: (options, @ID, @model) ->
super options
@original = new model(_id: @ID)
@original = @supermodel.loadModel(@original, 'document').model
@listenToOnce(@original, 'sync', @onViewSync)
2014-03-24 02:53:41 -04:00
onViewSync: ->
@versions = new VersionsViewCollection(@url, @original.attributes.original, @model)
@versions = @supermodel.loadCollection(@versions, 'versions').model
@listenTo(@versions, 'sync', @onVersionsFetched)
onVersionsFetched: ->
ids = (p.get('creator') for p in @versions.models)
jqxhrOptions = nameLoader.loadNames ids
@supermodel.addRequestResource('user_names', jqxhrOptions).load() if jqxhrOptions
onSelectionChanged: ->
rows = @$el.find 'input.select:checked'
deltaEl = @$el.find '.delta-view'
2014-05-08 14:10:22 -04:00
@removeSubView(@deltaView) if @deltaView
@deltaView = null
2014-06-30 22:16:26 -04:00
if rows.length isnt 2 then return
laterVersion = new @model(_id: $(rows[0]).val())
earlierVersion = new @model(_id: $(rows[1]).val())
2014-07-15 14:23:00 -04:00
@deltaView = new DeltaView({
model: earlierVersion
comparisonModel: laterVersion
skipPaths: deltasLib.DOC_SKIP_PATHS
2014-07-15 14:23:00 -04:00
loadModels: true
})
@insertSubView(@deltaView, deltaEl)
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
context = super(context)
context.page = @page
if @versions
context.dataList = (m.attributes for m in @versions.models)
for version in context.dataList
version.creator = nameLoader.getName(version.creator)
context