2014-11-29 16:36:41 -05:00
SaveVersionModal = require ' views/editor/modal/SaveVersionModal '
2014-01-03 13:32:13 -05:00
template = require ' templates/editor/level/save '
2014-11-28 20:49:41 -05:00
forms = require ' core/forms '
2014-01-03 13:32:13 -05:00
LevelComponent = require ' models/LevelComponent '
LevelSystem = require ' models/LevelSystem '
2014-07-23 10:02:45 -04:00
DeltaView = require ' views/editor/DeltaView '
PatchModal = require ' views/editor/PatchModal '
2014-11-28 20:49:41 -05:00
deltasLib = require ' core/deltas '
2014-01-03 13:32:13 -05:00
2014-07-23 10:02:45 -04:00
module.exports = class SaveLevelModal extends SaveVersionModal
2014-01-03 13:32:13 -05:00
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
2015-01-11 13:10:55 -05:00
@buildTime = options . buildTime
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-08-05 16:40:36 -04:00
context.modifiedComponents = _ . filter @ supermodel . getModels ( LevelComponent ) , @ shouldSaveEntity
2014-01-03 13:32:13 -05:00
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-13 17:48:36 -04:00
2014-04-09 22:07:44 -04:00
afterRender: ->
2014-04-16 13:42:32 -04:00
super ( false )
2014-04-09 22:07:44 -04:00
changeEls = @ $el . find ( ' .changes-stub ' )
models = if @ lastContext . levelNeedsSave then [ @ level ] else [ ]
models = models . concat @ lastContext . modifiedComponents
models = models . concat @ lastContext . modifiedSystems
2014-05-08 17:56:02 -04:00
models = ( m for m in models when m . hasWriteAccess ( ) )
2014-04-09 22:07:44 -04:00
for changeEl , i in changeEls
model = models [ i ]
2014-04-13 17:48:36 -04:00
try
2014-10-27 20:11:48 -04:00
deltaView = new DeltaView ( { model: model , skipPaths: deltasLib . DOC_SKIP_PATHS } )
2014-04-13 17:48:36 -04:00
@ insertSubView ( deltaView , $ ( changeEl ) )
catch e
2014-06-30 22:16:26 -04:00
console . error ' Couldn \' t create delta view: ' , e
2014-01-03 13:32:13 -05:00
shouldSaveEntity: (m) ->
2014-05-08 17:56:02 -04:00
return false unless m . hasWriteAccess ( )
2014-08-15 12:43:39 -04:00
if m . get ( ' system ' ) is ' ai ' and m . get ( ' name ' ) is ' Jitters ' and m . type ( ) is ' LevelComponent '
2014-08-14 13:28:50 -04:00
# Trying to debug the occasional phantom all-Components-must-be-saved bug
console . log " Should we save " , m . get ( ' system ' ) , m . get ( ' name ' ) , m , " ? localChanges: " , m . hasLocalChanges ( ) , " version: " , m . get ( ' version ' ) , ' isPublished: ' , m . isPublished ( ) , ' collection: ' , m . collection
return false
2014-08-06 10:43:56 -04:00
return true if m . hasLocalChanges ( )
2014-10-19 15:44:58 -04:00
console . error " Trying to check major version of #{ m . type ( ) } #{ m . get ( ' name ' ) } , but it doesn ' t have a version: " , m unless m . get ( ' version ' )
2014-01-03 13:32:13 -05:00
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
2014-08-21 15:49:06 -04:00
commitLevel: (e) ->
e . preventDefault ( )
2015-01-11 13:10:55 -05:00
@ level . set ' buildTime ' , @ buildTime
2014-01-03 13:32:13 -05:00
modelsToSave = [ ]
2014-05-30 20:24:53 -04:00
formsToSave = [ ]
2014-01-03 13:32:13 -05:00
for form in @ $el . find ( ' form ' )
# Level form is first, then LevelComponents' forms, then LevelSystems' forms
fields = { }
for field in $ ( form ) . serializeArray ( )
2014-06-30 22:16:26 -04:00
fields [ field . name ] = if field . value is ' on ' then true else field . value
2014-01-03 13:32:13 -05:00
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 )
2014-06-30 22:16:26 -04:00
console . log ' Couldn \' t find model for ' , kind , fields , ' from ' , @ supermodel . models unless model
2014-01-03 13:32:13 -05:00
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
2014-05-30 20:24:53 -04:00
formsToSave . push form
2014-06-23 21:25:58 -04:00
2014-05-30 20:24:53 -04:00
for model in modelsToSave
if errors = model . getValidationErrors ( )
messages = ( " \t #{ error . dataPath } : #{ error . message } " for error in errors )
messages = messages . join ( ' <br /> ' )
@ $el . find ( ' # errors-wrapper .errors ' ) . html ( messages )
@ $el . find ( ' # errors-wrapper ' ) . removeClass ( ' hide ' )
return
2014-01-03 13:32:13 -05:00
2014-05-30 20:24:53 -04:00
@ showLoading ( )
tuples = _ . zip ( modelsToSave , formsToSave )
for [ newModel , form ] in tuples
2014-10-28 16:48:17 -04:00
newModel . updateI18NCoverage ( ) if newModel . get ( ' i18nCoverage ' )
2014-12-29 12:14:43 -05:00
res = newModel . save ( null , { type: ' POST ' } ) # Override PUT so we can trigger postNewVersion logic
2014-01-03 13:32:13 -05:00
do (newModel, form) =>
res . error =>
@ hideLoading ( )
2014-06-30 22:16:26 -04:00
console . log ' Got errors: ' , JSON . parse ( res . responseText )
2014-01-03 13:32:13 -05:00
forms . applyErrorsToForm ( $ ( form ) , JSON . parse ( res . responseText ) )
res . success =>
modelsToSave = _ . without modelsToSave , newModel
2014-06-23 21:25:58 -04:00
oldModel = _ . find @ supermodel . models , (m) -> m . get ( ' original ' ) is newModel . get ( ' original ' )
oldModel . clearBackup ( ) # Otherwise looking at old versions is confusing.
2014-01-03 13:32:13 -05:00
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