From e3c5add8e7f471d690704ce164b472f053275152 Mon Sep 17 00:00:00 2001 From: Taka Date: Fri, 13 Nov 2020 21:31:38 +1100 Subject: [PATCH] Tightened up how the scenario date is unloaded. --- vasl_templates/webapp/static/scenarios.js | 14 +++++--------- vasl_templates/webapp/static/snippets.js | 16 ++++++++-------- vasl_templates/webapp/static/utils.js | 21 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/vasl_templates/webapp/static/scenarios.js b/vasl_templates/webapp/static/scenarios.js index 27136e4..28b4a19 100644 --- a/vasl_templates/webapp/static/scenarios.js +++ b/vasl_templates/webapp/static/scenarios.js @@ -508,16 +508,15 @@ function insertPlayerFlags( $target, scenario ) function loadObaInfo( $target, scenario, scenarioDateOverride ) { // initialize - var theater = getEffectiveTheater( scenario.theater ) ; + var theater = getEffectiveTheater( scenario.theater ) ; var scenarioDate = scenario.scenario_date_iso ; if ( ! theater || ( !scenarioDate && !scenarioDateOverride ) ) return ; if ( scenarioDateOverride ) { - scenarioDateOverride = scenarioDateOverride.toISOString().substring( 0, 10 ) ; - if ( scenarioDateOverride.substring(0,7) == scenarioDate.substring(0,7) ) + if ( scenarioDateOverride[3].substring(0,7) == scenarioDate.substring(0,7) ) scenarioDateOverride = null ; else - scenarioDate = scenarioDateOverride ; + scenarioDate = scenarioDateOverride[3] ; } var params = { SCENARIO_THEATER: theater, @@ -556,7 +555,7 @@ function loadObaInfo( $target, scenario, scenarioDateOverride ) // update the date warning if ( scenarioDateOverride ) { $target.find( ".date-warning .val" ).text( - parseInt( scenarioDateOverride.substring(5,7) ) + "/" + scenarioDateOverride.substring(2,4) + scenarioDateOverride[1] + "/" + scenarioDateOverride[2].toString().substring(2,4) ) ; $target.find( ".date-warning" ).show() ; } @@ -801,10 +800,7 @@ function getImportFieldCurrVal_date( importField ) { var scenarioDate = get_scenario_date() ; if ( ! scenarioDate ) return null ; - return [ - scenarioDate.toISOString().substring( 0, 10 ), - scenarioDate.getDate() + " " + get_month_name(scenarioDate.getMonth()) + ", " + scenarioDate.getFullYear() - ] ; + return [ scenarioDate[3], scenarioDate[4] ] ; } function doImportField_date( importField, newVal ) { diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index 86c3e2b..2714862 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -822,11 +822,11 @@ function unload_snippet_params( unpack_scenario_date, template_id ) if ( unpack_scenario_date ) { var scenario_date = get_scenario_date() ; if ( scenario_date ) { - params.SCENARIO_DAY_OF_MONTH = scenario_date.getDate() ; + params.SCENARIO_DAY_OF_MONTH = scenario_date[0] ; params.SCENARIO_DAY_OF_MONTH_POSTFIX = make_formatted_day_of_month( params.SCENARIO_DAY_OF_MONTH ) ; - params.SCENARIO_MONTH = 1 + scenario_date.getMonth() ; - params.SCENARIO_MONTH_NAME = get_month_name( scenario_date.getMonth() ) ; - params.SCENARIO_YEAR = scenario_date.getFullYear() ; + params.SCENARIO_MONTH = scenario_date[1] ; + params.SCENARIO_MONTH_NAME = get_month_name( params.SCENARIO_MONTH ) ; + params.SCENARIO_YEAR = scenario_date[2] ; } } @@ -1872,7 +1872,7 @@ function unload_params_for_save( includeMetadata ) // save the scenario date in ISO-8601 format var scenario_date = get_scenario_date() ; if ( scenario_date ) - params.SCENARIO_DATE = scenario_date.toISOString().substring( 0, 10 ) ; + params.SCENARIO_DATE = scenario_date[3] ; // save some admin metadata if ( includeMetadata ) { @@ -2177,11 +2177,11 @@ function _is_scenario_in_or_after( month, year ) { var scenario_date = get_scenario_date() ; if ( ! scenario_date ) return false ; - if ( scenario_date.getFullYear() > year ) + if ( scenario_date[2] > year ) return true ; - if ( scenario_date.getFullYear() < year ) + if ( scenario_date[2] < year ) return false ; - return scenario_date.getMonth() >= month-1 ; + return scenario_date[1] >= month ; } function is_pf_available() { return _is_scenario_in_or_after( 10, 1943 ) ; } diff --git a/vasl_templates/webapp/static/utils.js b/vasl_templates/webapp/static/utils.js index 8112cb1..93a6c3a 100644 --- a/vasl_templates/webapp/static/utils.js +++ b/vasl_templates/webapp/static/utils.js @@ -87,7 +87,15 @@ function get_scenario_date() var scenario_date = $("input[name='SCENARIO_DATE']").datepicker( "getDate" ) ; if ( ! scenario_date ) return null ; - return scenario_date ; + // NOTE: Returning a Javascript Date object creates massive headaches since it is adjusted + // for the current timezone, so we avoid problems by extracting the date fields here, and + // discarding the time fields. + var date=scenario_date.getDate(), month=1+scenario_date.getMonth(), year=scenario_date.getFullYear() ; + return [ + date, month, year, + year + "-" + pad(month,2,"0") + "-" + pad(date,2,"0"), + date + " " + get_month_name(month) + ", " + year + ] ; } function is_template_available( template_id ) @@ -447,7 +455,7 @@ function make_formatted_day_of_month( dom ) function get_month_name( month ) { // get the name of the month - return _MONTH_NAMES[ month ] ; + return _MONTH_NAMES[ month-1 ] ; } // -------------------------------------------------------------------- @@ -495,6 +503,15 @@ function escapeHTML( val ) { return new Option(val).innerHTML ; } function trimString( val ) { return val ? val.trim() : val ; } function fpFmt( val, nDigits ) { return val.toFixed( nDigits ) ; } +function pad( val, len, ch ) +{ + // left-pad the value + val = val.toString() ; + while( val.length < len ) + val = ch + val ; + return val ; +} + function pluralString( n, str1, str2, combine ) { var val = (n == 1) ? str1 : str2 ;