From 32a95afbd0fb6145fc0a1e970af36467383f1cee Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Thu, 26 Feb 2015 11:31:16 -0800 Subject: [PATCH] Allowing reordering of levels in Campaign. Reversing CampaignEditor levels before save so that when the save magically reverses them again, the order is preserved. --- .../editor/campaign/CampaignEditorView.coffee | 1 + .../editor/campaign/SaveCampaignModal.coffee | 22 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/views/editor/campaign/CampaignEditorView.coffee b/app/views/editor/campaign/CampaignEditorView.coffee index 6cd81aefc..072e4c7b1 100644 --- a/app/views/editor/campaign/CampaignEditorView.coffee +++ b/app/views/editor/campaign/CampaignEditorView.coffee @@ -266,6 +266,7 @@ module.exports = class CampaignEditorView extends RootView class LevelsNode extends TreemaObjectNode valueClass: 'treema-levels' @levels: {} + ordered: true buildValueForDisplay: (valEl, data) -> @buildValueForDisplaySimply valEl, ''+_.size(data) diff --git a/app/views/editor/campaign/SaveCampaignModal.coffee b/app/views/editor/campaign/SaveCampaignModal.coffee index 69bee3904..e4a5a23d7 100644 --- a/app/views/editor/campaign/SaveCampaignModal.coffee +++ b/app/views/editor/campaign/SaveCampaignModal.coffee @@ -6,13 +6,13 @@ module.exports = class SaveCampaignModal extends ModalView id: 'save-campaign-modal' template: template plain: true - + events: 'click #save-button': 'onClickSaveButton' constructor: (options, @modelsToSave) -> super(options) - + getRenderData: -> c = super() c.modelsToSave = @modelsToSave @@ -26,9 +26,21 @@ module.exports = class SaveCampaignModal extends ModalView @insertSubView(deltaView, $el) ) super() - + onClickSaveButton: -> @showLoading() + @reverseLevelsBeforeSave() modelsBeingSaved = (model.patch() for model in @modelsToSave.models) - modelsBeingSaved = modelsBeingSaved - $.when(_.compact(modelsBeingSaved)...).done(-> document.location.reload()) \ No newline at end of file + $.when(_.compact(modelsBeingSaved)...).done(-> document.location.reload()) + + reverseLevelsBeforeSave: -> + # For some unfathomable reason, not in our code anywhere, the levels get reversed during the save somehow. + # Since we want to maintain their orders, we reverse them first, so that when they're reversed again, it's right. + # Yaaaay! + return unless campaign = _.find @modelsToSave.models, (m) -> m.constructor.className is 'Campaign' + levelsReversed = {} + levels = campaign.get 'levels' + levelIDs = _.keys(levels).reverse() + for levelID in levelIDs + levelsReversed[levelID] = levels[levelID] + campaign.set 'levels', levelsReversed