2014-01-03 13:32:13 -05:00
|
|
|
SaveVersionModal = require 'views/modal/save_version_modal'
|
|
|
|
template = require 'templates/editor/level/save'
|
|
|
|
forms = require 'lib/forms'
|
|
|
|
LevelComponent = require 'models/LevelComponent'
|
|
|
|
LevelSystem = require 'models/LevelSystem'
|
2014-04-09 22:07:44 -04:00
|
|
|
DeltaView = require 'views/editor/delta'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class LevelSaveView extends SaveVersionModal
|
|
|
|
template: template
|
|
|
|
instant: false
|
|
|
|
modalWidthPercent: 60
|
2014-04-10 14:13:33 -04:00
|
|
|
plain: true
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #save-version-button': 'commitLevel'
|
|
|
|
'submit form': 'commitLevel'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@level = options.level
|
2014-02-16 18:30:00 -05:00
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
getRenderData: (context={}) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
context = super(context)
|
|
|
|
context.level = @level
|
2014-02-06 13:30:49 -05:00
|
|
|
context.levelNeedsSave = @level.hasLocalChanges()
|
2014-01-03 13:32:13 -05:00
|
|
|
context.modifiedComponents = _.filter @supermodel.getModels(LevelComponent), @shouldSaveEntity
|
|
|
|
context.modifiedSystems = _.filter @supermodel.getModels(LevelSystem), @shouldSaveEntity
|
2014-04-10 16:09:44 -04:00
|
|
|
context.hasChanges = (context.levelNeedsSave or context.modifiedComponents.length or context.modifiedSystems.length)
|
2014-04-09 22:07:44 -04:00
|
|
|
@lastContext = context
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
2014-04-09 22:07:44 -04:00
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
changeEls = @$el.find('.changes-stub')
|
|
|
|
models = if @lastContext.levelNeedsSave then [@level] else []
|
|
|
|
models = models.concat @lastContext.modifiedComponents
|
|
|
|
models = models.concat @lastContext.modifiedSystems
|
|
|
|
for changeEl, i in changeEls
|
|
|
|
model = models[i]
|
|
|
|
deltaView = new DeltaView({model:model})
|
|
|
|
@insertSubView(deltaView, $(changeEl))
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
shouldSaveEntity: (m) ->
|
|
|
|
return true if m.hasLocalChanges()
|
|
|
|
return true if (m.get('version').major is 0 and m.get('version').minor is 0) or not m.isPublished() and not m.collection
|
|
|
|
# Sometimes we have two versions: one in a search collection and one with a URL. We only save changes to the latter.
|
|
|
|
false
|
|
|
|
|
|
|
|
commitLevel: ->
|
|
|
|
modelsToSave = []
|
|
|
|
@showLoading()
|
|
|
|
for form in @$el.find('form')
|
|
|
|
# Level form is first, then LevelComponents' forms, then LevelSystems' forms
|
|
|
|
fields = {}
|
|
|
|
for field in $(form).serializeArray()
|
|
|
|
fields[field.name] = if field.value is "on" then true else field.value
|
|
|
|
isLevelForm = $(form).attr('id') is 'save-level-form'
|
|
|
|
if isLevelForm
|
|
|
|
model = @level
|
|
|
|
else
|
|
|
|
[kind, klass] = if $(form).hasClass 'component-form' then ['component', LevelComponent] else ['system', LevelSystem]
|
|
|
|
model = @supermodel.getModelByOriginalAndMajorVersion klass, fields["#{kind}-original"], parseInt(fields["#{kind}-parent-major-version"], 10)
|
|
|
|
console.log "Couldn't find model for", kind, fields, "from", @supermodel.models unless model
|
|
|
|
newModel = if fields.major then model.cloneNewMajorVersion() else model.cloneNewMinorVersion()
|
|
|
|
newModel.set 'commitMessage', fields['commit-message']
|
|
|
|
modelsToSave.push newModel
|
|
|
|
if isLevelForm
|
|
|
|
@level = newModel
|
|
|
|
if fields['publish'] and not @level.isPublished()
|
|
|
|
@level.publish()
|
|
|
|
else if @level.isPublished() and not newModel.isPublished()
|
|
|
|
newModel.publish() # Publish any LevelComponents that weren't published yet
|
|
|
|
|
|
|
|
res = newModel.save()
|
|
|
|
do (newModel, form) =>
|
|
|
|
res.error =>
|
|
|
|
@hideLoading()
|
|
|
|
console.log "Got errors:", JSON.parse(res.responseText)
|
|
|
|
forms.applyErrorsToForm($(form), JSON.parse(res.responseText))
|
|
|
|
res.success =>
|
|
|
|
modelsToSave = _.without modelsToSave, newModel
|
|
|
|
unless modelsToSave.length
|
|
|
|
url = "/editor/level/#{@level.get('slug') or @level.id}"
|
|
|
|
document.location.href = url
|
2014-02-16 18:30:00 -05:00
|
|
|
@hide() # This will destroy everything, so do it last
|