@ -11,22 +11,22 @@ document.addEventListener('DOMContentLoaded', function() {
// In order to be able to style SVG elements with CSS, and register events with javascript, we must use inline SVG (we can't use an img tag)
// For this purpose, the SVG file contents are embedded in a javascript file
document . getElementById ( 'zones-map' ) . innerHTML = ( < any > window ) [ 'climate-zones-map.svg' ] ;
container . querySelector ( '# zones-map' ) . innerHTML = ( < any > window ) [ 'climate-zones-map.svg' ] ;
document . querySelectorAll ( "[data-activate-modal]" ) . forEach ( elt = > {
container . querySelectorAll ( "[data-activate-modal]" ) . forEach ( elt = > {
elt . addEventListener ( 'click' , e = > {
document . getElementById ( elt . getAttribute ( 'data-activate-modal' ) ) . classList . toggle ( 'is-active' , true ) ;
container . querySelector ( '#' + elt . getAttribute ( 'data-activate-modal' ) ) . classList . toggle ( 'is-active' , true ) ;
} ) ;
} ) ;
document . querySelectorAll ( '.modal-close, .modal-card-head .delete' ) . forEach ( elt = > {
container . querySelectorAll ( '.modal-close, .modal-card-head .delete' ) . forEach ( elt = > {
elt . addEventListener ( 'click' , e = > {
closest ( elt , e = > e . classList . contains ( 'modal' ) ) . classList . toggle ( 'is-active' , false ) ;
} ) ;
} ) ;
let zoneSelector = < HTMLSelectElement > document . getElementById ( 'zone-selector' ) ;
document . querySelectorAll ( '.climate-zone' ) . forEach ( elt = > {
let zoneSelector = < HTMLSelectElement > container . querySelector ( '# zone-selector' ) ;
container . querySelectorAll ( '.climate-zone' ) . forEach ( elt = > {
elt . addEventListener ( 'click' , e = > {
let zoneName = elt . getAttribute ( 'id' ) ;
zoneSelector . value = zoneName ;
@ -34,14 +34,42 @@ document.addEventListener('DOMContentLoaded', function() {
} ) ;
} ) ;
document . getElementById ( 'simulate-button' ) . addEventListener ( 'click' , e = > {
container . querySelector ( '# simulate-button' ) . addEventListener ( 'click' , e = > {
let parameters : SimulationParameters = {
batteryCapacity : Number ( ( < HTMLInputElement > document . querySelector ( '[name=battery-capacity]' ) ) . value ) ,
additionalWeight : Number ( ( < HTMLInputElement > document . querySelector ( '[name=additional-weight]' ) ) . value ) ,
climateZone : ( < HTMLSelectElement > document . getElementById ( 'zone-selector' ) ) . value ,
dailyDistance : Number ( ( < HTMLInputElement > document . querySelector ( '[name=daily-distance]' ) ) . value ) ,
dailyAscendingElevation : Number ( ( < HTMLInputElement > document . querySelector ( '[name=daily-elevation]' ) ) . value ) ,
batteryCapacity : Number ( ( < HTMLInputElement > container . querySelector ( '[name=battery-capacity]' ) ) . value ) ,
additionalWeight : Number ( ( < HTMLInputElement > container . querySelector ( '[name=additional-weight]' ) ) . value ) ,
climateZone : ( < HTMLSelectElement > container . querySelector ( '# zone-selector' ) ) . value ,
dailyDistance : Number ( ( < HTMLInputElement > container . querySelector ( '[name=daily-distance]' ) ) . value ) ,
dailyAscendingElevation : Number ( ( < HTMLInputElement > container . querySelector ( '[name=daily-elevation]' ) ) . value ) ,
} ;
startSimulation ( parameters ) ;
let simulationResult = startSimulation ( parameters ) ;
let resultsContainer = container . querySelector ( '.simulation-results' ) ;
let batteryChargeGraph = resultsContainer . querySelector ( '.battery-charge-graph' ) ;
let batteryChargeGraphSvg = batteryChargeGraph . querySelector ( 'svg' ) ;
let coordinates = '' ;
let view = [ 1000 , 300 ] ;
let hoursInYear = 365 * 24 ;
for ( let dayOfYear = 0 ; dayOfYear < 365 ; ++ dayOfYear ) {
for ( let hourOfDay = 0 ; hourOfDay < 24 ; ++ hourOfDay ) {
let h = dayOfYear * 24 + hourOfDay ;
let batteryLevel = simulationResult . batteryLevel [ h ] ;
if ( h == 0 ) coordinates += 'M' ;
else if ( h == 1 ) coordinates += ' L' ;
else coordinates += ' ' ;
coordinates += Math . round ( h * view [ 0 ] / hoursInYear ) + ',' + Math . round ( view [ 1 ] - batteryLevel * view [ 1 ] / parameters . batteryCapacity ) ;
}
}
let path = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'path' ) ;
path . setAttribute ( 'class' , 'graph' ) ;
path . setAttribute ( 'd' , coordinates ) ;
path . setAttribute ( 'shape-rendering' , 'optimizeQuality' )
batteryChargeGraphSvg . append ( path ) ;
resultsContainer . classList . toggle ( 'is-hidden' , false ) ;
} ) ;
} ) ;