From 1f4442694c462f2184e7fcbb61b51c6ffc1f4d40 Mon Sep 17 00:00:00 2001 From: Matthew McFarland Date: Wed, 30 Aug 2017 11:55:33 -0400 Subject: [PATCH] Add button for default GMS file download Recent UI work led to the pre-scenario MapShed model not having the ability to download a GMS file. When current conditions is the only scenario in MapShed, a button is now placed in the header which has the same functionality as that expressed in the menu options dropdown. --- .../modeling/templates/gmsDownloadForm.html | 6 +++ .../templates/scenarioAddChangesButton.html | 16 ++++-- .../templates/scenarioMenuItemOptions.html | 7 +-- src/mmw/js/src/modeling/tests.js | 7 +-- src/mmw/js/src/modeling/views.js | 50 +++++++++++++------ src/mmw/sass/base/_header.scss | 26 ++++++++-- src/mmw/sass/pages/_model.scss | 5 ++ 7 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 src/mmw/js/src/modeling/templates/gmsDownloadForm.html diff --git a/src/mmw/js/src/modeling/templates/gmsDownloadForm.html b/src/mmw/js/src/modeling/templates/gmsDownloadForm.html new file mode 100644 index 000000000..c9ef7065b --- /dev/null +++ b/src/mmw/js/src/modeling/templates/gmsDownloadForm.html @@ -0,0 +1,6 @@ +
+ + + + +
diff --git a/src/mmw/js/src/modeling/templates/scenarioAddChangesButton.html b/src/mmw/js/src/modeling/templates/scenarioAddChangesButton.html index 359525989..02f260108 100644 --- a/src/mmw/js/src/modeling/templates/scenarioAddChangesButton.html +++ b/src/mmw/js/src/modeling/templates/scenarioAddChangesButton.html @@ -1,6 +1,16 @@ -
-{% if isOnlyCurrentConditions %} +{% if isOnlyCurrentConditions and isGwlfe %} + + +{% include "./gmsDownloadForm.html" %} + +{% endif %} +
+
+ {% if isOnlyCurrentConditions %} -{% endif %} + {% endif %} +
diff --git a/src/mmw/js/src/modeling/templates/scenarioMenuItemOptions.html b/src/mmw/js/src/modeling/templates/scenarioMenuItemOptions.html index de0743b6d..f6213db81 100644 --- a/src/mmw/js/src/modeling/templates/scenarioMenuItemOptions.html +++ b/src/mmw/js/src/modeling/templates/scenarioMenuItemOptions.html @@ -15,12 +15,7 @@ {% endif %} {% if is_gwlfe %} {% endif %} diff --git a/src/mmw/js/src/modeling/tests.js b/src/mmw/js/src/modeling/tests.js index 22bea926a..82d9000f9 100644 --- a/src/mmw/js/src/modeling/tests.js +++ b/src/mmw/js/src/modeling/tests.js @@ -61,7 +61,7 @@ describe('Modeling', function() { collection = projectModel.get('scenarios'), view = new views.ScenarioToolbarView({ collection: collection, - model_package: "tr-55" + model: projectModel, }); $(sandboxSelector).html(view.render().el); @@ -73,16 +73,17 @@ describe('Modeling', function() { }); describe('ScenarioToolbarView', function() { - it('does not show the add changes button when there are non-current condition scenario', function() { + it('does not show the non-current condition scenario buttons', function() { var collection = getTestScenarioCollection(), view = new views.ScenarioToolbarView({ collection: collection, - model_package: "tr-55" + model: getTestProject(), }); $(sandboxSelector).html(view.render().el); assert.equal($('#sandbox #add-changes').text(), ''); + assert.equal($('#sandbox #download-cc-gms').text(), ''); }); }); diff --git a/src/mmw/js/src/modeling/views.js b/src/mmw/js/src/modeling/views.js index 29a904278..be0759f4f 100644 --- a/src/mmw/js/src/modeling/views.js +++ b/src/mmw/js/src/modeling/views.js @@ -72,7 +72,7 @@ var ModelingHeaderView = Marionette.LayoutView.extend({ App.currentProject.fetchGisDataIfNeeded().done(function() { self.toolbarRegion.show(new ScenarioToolbarView({ collection: self.model.get('scenarios'), - model_package: self.model.get('model_package') + model: self.model })); }); }, @@ -432,10 +432,7 @@ var ScenarioDropDownMenuOptionsView = Marionette.ItemView.extend({ templateHelpers: function() { var gis_data = this.model.getGisData().model_input, - is_gwlfe = App.currentProject.get('model_package') === models.GWLFE && - gis_data !== null && - gis_data !== '{}' && - gis_data !== ''; + is_gwlfe = App.currentProject.get('model_package') === models.GWLFE && !_.isEmpty(gis_data); return { is_gwlfe: is_gwlfe, @@ -793,13 +790,17 @@ var ScenarioToolbarView = Marionette.CompositeView.extend({ template: scenarioAddChangesButtonTmpl, collection: models.ScenariosCollection, childViewContainer: '.tab-content.scenario-toolbar-tab-content', + className: 'toolbar-container', ui: { addChangesButton: '#add-changes', + downloadGmsFile: '#download-cc-gms', + exportGmsForm: '#export-gms-form', }, events: { 'click @ui.addChangesButton': 'onAddChangesClick', + 'click @ui.downloadGmsFile': 'onGmsDownloadClick', }, collectionEvents: { @@ -807,38 +808,59 @@ var ScenarioToolbarView = Marionette.CompositeView.extend({ }, getChildView: function() { - var isGwlfe = App.currentProject.get('model_package') === 'gwlfe'; + var isGwlfe = this.modelPackage === models.GWLFE; + if (isGwlfe) { return GwlfeToolbarView; } else { return Tr55ToolbarView; } }, + childViewOptions: function(model) { var controls = models.getControlsForModelPackage( - this.options.model_package, - {is_current_conditions: model.get('is_current_conditions')} - ); + this.modelPackage, + {is_current_conditions: model.get('is_current_conditions')} + ); return { collection: controls }; }, - initialize: function(options) { - this.mergeOptions(options, ['model_package']); + initialize: function() { + this.modelPackage = this.model.get('model_package'); + this.currentConditions= this.collection.findWhere({is_current_conditions: true}); }, onAddChangesClick: function() { this.collection.createNewScenario(); }, + onGmsDownloadClick: function() { + // We can't download a file from an AJAX call. One either has to + // load the data in an iframe, or submit a form that responds with + // Content-Disposition: attachment. We prefer submitting a form. + var filename = App.currentProject.get('name').replace(/\s/g, '_') + + '__' + this.currentConditions.get('name').replace(/\s/g, '_'); + + this.ui.exportGmsForm.find('.gms-filename').val(filename); + this.ui.exportGmsForm.submit(); + }, + templateHelpers: function() { + var gisData = this.currentConditions.getGisData().model_input, + isGwlfe = this.modelPackage === models.GWLFE && !_.isEmpty(gisData), + isOnlyCurrentConditions = this.collection.length === 1 && + this.collection.first().get('is_current_conditions'); + return { - isOnlyCurrentConditions: this.collection.length === 1 && - this.collection.first().get('is_current_conditions'), + isOnlyCurrentConditions: isOnlyCurrentConditions, + isGwlfe: isGwlfe, + csrftoken: csrf.getToken(), + gis_data: gisData, }; - } + }, }); var ResultsView = Marionette.LayoutView.extend({ diff --git a/src/mmw/sass/base/_header.scss b/src/mmw/sass/base/_header.scss index c62846ac1..e9bd98d29 100644 --- a/src/mmw/sass/base/_header.scss +++ b/src/mmw/sass/base/_header.scss @@ -205,13 +205,29 @@ header { white-space: nowrap; } + #toolbar-region { - margin-right: 0; - margin-left: auto; + width: 100%; + } + + #toolbar-region > .toolbar-container { + display: flex; + flex-basis: 100%; + flex-direction: row; + height: 100%; + + .right-controls-container { + margin-left: auto; + } + #add-changes { - position: relative; - top: -2px; - color: $brand-primary; + color: $brand-primary; + height: 100%; + } + + #download-cc-gms { + font-size: 13px; + color: inherit; } } diff --git a/src/mmw/sass/pages/_model.scss b/src/mmw/sass/pages/_model.scss index 353162b46..86477c55e 100644 --- a/src/mmw/sass/pages/_model.scss +++ b/src/mmw/sass/pages/_model.scss @@ -70,3 +70,8 @@ } } } + +// Hidden form for GMS file downloads +#export-gms-form { + display: none; +}