Skip to content

Commit

Permalink
Issues/#2759 start year (#2761)
Browse files Browse the repository at this point in the history
* Set year of a scenario with the establishment shot

* Remove whitespace between 'Other' and &hellip
  • Loading branch information
grdw authored and antw committed Apr 6, 2018
1 parent 846e9da commit 7f7fed0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
10 changes: 9 additions & 1 deletion app/assets/javascripts/establishment_shot/scenario_creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ EstablishmentShot.ScenarioCreator = (function () {

return {
create: function (scope) {
var rawEndYear = window.location.search.match(/\?end_year=(\d{4})/),
endYear = rawEndYear ? rawEndYear[1] : scope.data.defaultYear;

if (!(endYear >= scope.data.minYear && endYear <= scope.data.maxYear)) {
throw "endYear is not in range: " + endYear;
}

return $.ajax({
type: 'POST',
dataType: 'json',
url: scope.host,
data: {
scenario: {
area_code: scope.area
area_code: scope.area,
end_year: endYear
}
}
});
Expand Down
12 changes: 11 additions & 1 deletion app/assets/javascripts/intro.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,24 @@ $ ->

areaSelect.change(areaOnChange).change()

setParamForEstablishmentShot = ->
$('.dataset_information > a')[0].search = "end_year=#{ $(this).val() }"

# Checks the select tag to show custom year field select when
# other is clicked.
checkYearRadioButtons = ->
elements = $('#end_year option:selected')

if elements.val() == 'other'
cYearSelect.select2(minimumResultsForSearch: -1).show()
cYearSelect
.select2(minimumResultsForSearch: -1)
.show()
.change(setParamForEstablishmentShot)
.change()

else
setParamForEstablishmentShot.call(this)

if cYearSelect.hasClass('select2-hidden-accessible')
cYearSelect.select2('destroy').hide()

Expand Down
14 changes: 14 additions & 0 deletions app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,18 @@ def flags
Dir.glob("icons/areas/*.png")
end
end

def preset_years
custom_years
.select { |year| (year % 10).zero? }
.push([I18n.t('scenario.other').html_safe, 'other'])
end

def other_year_selected
!preset_years.include?(Current.setting.end_year)
end

def custom_years
(Setting::MIN_YEAR..Setting::MAX_YEAR)
end
end
4 changes: 4 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
class Setting
extend ActiveModel::Naming

MIN_YEAR = 2013
DEFAULT_YEAR = 2050
MAX_YEAR = 2050

# A list of all attributes which may be stored in the Setting, and their
# default values.
def self.default_attributes
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/dataset.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
= image_tag 'reports/print.svg'
%span= t('report.print')

.page#establishment_shot{ data: { area: @area.area, host: APP_CONFIG[:api_url] } }
.page#establishment_shot{ data: { area: @area.area, host: APP_CONFIG[:api_url], max_year: Setting::MAX_YEAR, min_year: Setting::MIN_YEAR, default_year: Setting::DEFAULT_YEAR } }
%h1
= t('establishment_shot.header_html', area: I18n.t(@area.area, default: [@area.area.to_sym, @area.area.humanize]))
%span.loading
Expand Down
7 changes: 2 additions & 5 deletions app/views/pages/root_page/_year_select.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
%p= t("choose_year")
- preset_years = [2020, 2030, 2040, 2050, ["#{ I18n.t('scenario.overig') }&hellip;".html_safe, 'other']]
- other_year_selected = !preset_years.include?(Current.setting.end_year)
= select_tag :end_year, options_for_select(preset_years, other_year_selected ? "other" : (Current.setting.end_year || 2050))
- custom_years = (2013..2050).to_a.map {|i| [i,i]}
= select_tag :other_year, options_for_select(custom_years, other_year_selected ? {:selected => Current.setting.end_year} : {}), {}.merge(other_year_selected ? {} : {:style => 'display:none'})
= select_tag :end_year, options_for_select(preset_years, other_year_selected ? 'other' : (Current.setting.end_year || Setting::DEFAULT_YEAR))
= select_tag :other_year, options_for_select(custom_years, other_year_selected ? { selected: Current.setting.end_year } : {}), {}.merge(other_year_selected ? {} : { style: 'display:none' })
2 changes: 1 addition & 1 deletion config/locales/en_etm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ en:
title: "Title"
warning: "WARNING: This scenario was created in an earlier version of the ETM. Some of your slider settings may therefore deviate from their original values. Please review the scenario to ensure it still meets its objectives."
preset_warning: "WARNING: This scenario was created in an earlier version of the ETM. Some of the slider settings may therefore deviate from their original values. Quintel has tried to ensure that this scenario still lives up to its creator's intentions."
overig: "Other"
other: "Other&hellip;"
actueel: "Featured"
regional: "Regional"
high_energy_productivity_growth: "High Energy Productivity Growth"
Expand Down
2 changes: 1 addition & 1 deletion config/locales/nl_etm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ nl:
warning: "WAARSCHUWING: Dit scenario is gecreëerd in een oudere versie van het ETM. Hierdoor kunnen sommige slider-instellingen afwijken van hun oorspronkelijke waarden. Verzeker u ervan dat het scenario nog steeds voldoet aan de doelstellingen."
preset_warning: "WAARSCHUWING: Dit scenario is gecreëerd in een oudere versie van het ETM. Hierdoor kunnen sommige slider-instellingen afwijken van hun oorspronkelijke waarden. Quintel heeft getracht te verzekeren dat het scenario nog steeds voldoet aan de intenties van de auteur."
created_by: "Gemaakt door"
overig: "Overig"
other: "Overig&hellip;"
actueel: "Actueel"
regional: "Regionaal"
high_energy_productivity_growth: "High Energy Productivity Growth"
Expand Down

0 comments on commit 7f7fed0

Please sign in to comment.