Merge branch 'fix/no-reset-form' into 'master'

Don't reset the ventilation visibility when we hit back in the browser.

See merge request cara/cara!57
This commit is contained in:
Gabriella Azzopardi 2020-11-07 08:58:15 +00:00
commit 7e7e89d45b
3 changed files with 59 additions and 65 deletions

View file

@ -1,4 +1,3 @@
from cara.models import Model
from dataclasses import dataclass
import html
import typing
@ -46,7 +45,7 @@ class FormData:
@classmethod
def from_dict(cls, form_data: typing.Dict) -> "FormData":
valid_na_values = ['windows_open', 'ventilation_type', 'mechanical_ventilation_type']
valid_na_values = ['windows_open', 'mechanical_ventilation_type']
for name in valid_na_values:
if not form_data.get(name, ''):
form_data[name] = 'not-applicable'
@ -113,10 +112,11 @@ class FormData:
infected_finish=time_string_to_minutes(form_data['infected_finish']),
)
def build_model(self) -> Model:
def build_model(self) -> models.Model:
return model_from_form(self)
def ventilation(self) -> models.Ventilation:
always_on = models.PeriodicInterval(period=120, duration=120)
# Initializes a ventilation instance as a window if 'natural' is selected, or as a HEPA-filter otherwise
if self.ventilation_type == 'natural':
if self.windows_open == 'interval':
@ -141,17 +141,17 @@ class FormData:
inside_temp=inside_temp, outside_temp=outside_temp, cd_b=0.6,
window_height=self.window_height,
opening_length=self.opening_distance * self.windows_number)
elif self.ventilation_type == "no-ventilation":
ventilation = models.AirChange(active=always_on, air_exch=0.)
else:
if self.mechanical_ventilation_type == 'air_changes':
ventilation = models.AirChange(active=models.PeriodicInterval(period=120, duration=120),
air_exch=self.air_changes)
ventilation = models.AirChange(active=always_on, air_exch=self.air_changes)
else:
ventilation = models.HVACMechanical(active=models.PeriodicInterval(period=120, duration=120),
q_air_mech=self.air_supply)
ventilation = models.HVACMechanical(
active=always_on, q_air_mech=self.air_supply)
if self.hepa_option:
hepa = models.HEPAFilter(active=models.PeriodicInterval(period=120, duration=120),
q_air_mech=250.)
hepa = models.HEPAFilter(active=always_on, q_air_mech=250.)
return models.MultipleVentilation((ventilation,hepa))
else:
return ventilation
@ -269,7 +269,6 @@ def model_from_form(form: FormData) -> models.Model:
def baseline_raw_form_data():
# Note: This isn't a special "baseline". It can be updated as required.
return {
'RADIO_ventilation_type': 'natural',
'activity_finish': '18:00',
'activity_start': '09:00',
'activity_type': 'office',
@ -296,7 +295,7 @@ def baseline_raw_form_data():
'simulation_name': 'Test',
'single_event_date': '',
'total_people': '10',
'ventilation_type': '',
'ventilation_type': 'natural',
'volume_type': 'room_volume',
'window_height': '2',
'window_width': '2',
@ -309,7 +308,7 @@ ACTIVITY_TYPES = {'office', 'training', 'workshop'}
EVENT_TYPES = {'single_event', 'recurrent_event'}
MECHANICAL_VENTILATION_TYPES = {'air_changes', 'air_supply', 'not-applicable'}
MASK_WEARING = {'continuous', 'removed'}
VENTILATION_TYPES = {'natural', 'mechanical', 'not-applicable'}
VENTILATION_TYPES = {'natural', 'mechanical', 'no-ventilation'}
VOLUME_TYPES = {'room_volume', 'room_dimensions'}
WINDOWS_OPEN = {'always', 'interval', 'breaks', 'not-applicable'}

View file

@ -36,30 +36,27 @@ Beta v1.0.0 <span style="float:right; font-weight:bold">Please send feedback to
<!-- Ventilation Options -->
Ventilation type:
<input type="hidden" id="ventilation_type" name="ventilation_type" value=""/>
<input type="radio" id="mechanical" name="RADIO_ventilation_type" value="mechanical" onclick="show_hide('DIVmechanical_ventilation', 'DIVnatural_ventilation', this)">Mechanical</input>
<input type="radio" id="natural" name="RADIO_ventilation_type" value="natural" onclick="show_hide('DIVnatural_ventilation', 'DIVmechanical_ventilation', this)">Natural</input><br>
<input type="radio" id="no-ventilation" name="ventilation_type" value="no-ventilation" checked>No ventilation</input>
<input type="radio" id="mechanical" name="ventilation_type" value="mechanical" data-enables="DIVmechanical_ventilation">Mechanical</input>
<input type="radio" id="natural" name="ventilation_type" value="natural" data-enables="DIVnatural_ventilation">Natural</input><br>
<div id="DIVmechanical_ventilation" style="display:none">
<input type="hidden" id="mechanical_ventilation_type" name="mechanical_ventilation_type" value=""/>
<input type="radio" id="air_type_supply" name="RADIO_mechanical_ventilation_type" value="air_supply" onclick="require_fields(this)">
<input type="radio" id="air_type_supply" name="mechanical_ventilation_type" value="air_supply" onclick="require_fields(this)">
Air supply flow rate &nbsp;&nbsp; <input type="number" step=0.01 id="air_supply" name="air_supply" min="0" placeholder="(m³ / hour)"><br>
<input type="radio" id="air_type_changes" name="RADIO_mechanical_ventilation_type" value="air_changes" onclick="require_fields(this)">
<input type="radio" id="air_type_changes" name="mechanical_ventilation_type" value="air_changes" onclick="require_fields(this)">
Air changes per hour &nbsp;&nbsp; <input type="number" step=0.01 id="air_changes" name="air_changes" min="0"><br>
</div>
<div id="DIVnatural_ventilation" style="display:none">
<input type="hidden" id="windows_open" name="windows_open" value=""/>
Number of windows: <input type="number" id="windows_number" name="windows_number" min="0"><br>
Height of window: <input type="number" step=0.01 id="window_height" name="window_height" placeholder="meters" min="0"><br>
Width of window: <input type="number" step=0.01 id="window_width" name="window_width" placeholder="meters" min="0"><br>
Opening distance: <input type="number" step=0.01 id="opening_distance" name="opening_distance" placeholder="meters" min="0"><br>
Windows open: <input type="radio" id="always" name="RADIO_windows_open" value="always", onChange="update_windows_open(this)">
Windows open: <input type="radio" id="always" name="windows_open" value="always">
<label for="always">Always</label>
<input type="radio" id="interval" name="RADIO_windows_open" value="interval", onChange="update_windows_open(this)">
<input type="radio" id="interval" name="windows_open" value="interval">
<label for="interval">10 min / 2h</label>
<!--input type="radio" id="breaks" name="RADIO_windows_open" value="breaks">
<label for="breaks">Breaks</label--><br>
<br>
</div>
HEPA filtration:

View file

@ -1,40 +1,27 @@
/* -------Show/Hide DIVs------- */
function show(show, var_id, obj) {
var show = document.getElementById(show);
if (show.style.display === "none") {
show.style.display = "block";
document.getElementById(var_id).value = 1;
} else {
show.style.display = "none";
document.getElementById(var_id).value = 0;
}
require_fields(obj);
function on_ventilation_type_change(){
ventilation_types = $('input[type=radio][name=ventilation_type]');
ventilation_types.each(function( index ) {
if (this.checked) {
getChildElement($(this)).show();
require_fields(this);
} else {
getChildElement($(this)).hide();
unrequire_fields(this);
// Clear the inputs for this newly hidden child element.
getChildElement($(this)).find('input').not('input[type=radio]').val('');
getChildElement($(this)).find('input[type=radio]').prop("checked", false);
getChildElement($(this)).find('input').prop("required", false);
}
});
}
function show_hide(show, hide, obj) {
var show = document.getElementById(show);
var hide = document.getElementById(hide);
var ventilation_type = document.getElementById("ventilation_type");
var mechanical_ventilation_type = document.getElementById("mechanical_ventilation_type");
if (show.style.display === "block") {
show.style.display = "none";
obj.checked = false;
ventilation_type.value = "";
mechanical_ventilation_type.value = "";
unrequire_fields(obj);
} else if (show.style.display === "none") {
show.style.display = "block";
hide.style.display = "none";
ventilation_type.value = obj.id;
require_fields(obj);
} }
function update_windows_open(obj) {
var windows_open = document.getElementById("windows_open");
windows_open.value = obj.id;
function getChildElement(elem) {
// Get the element named in the given element's data-enables attribute.
return $("#" + elem.data("enables"));
}
/* -------Required fields------- */
function require_fields(obj){
switch(obj.id) {
@ -119,17 +106,11 @@ function require_natural_ventilation(option) {
function require_air_changes(option) {
$("#air_changes").prop('required',option);
if (option) {
var mechanical_ventilation_type = document.getElementById("mechanical_ventilation_type");
mechanical_ventilation_type.value = "air_changes";
} }
}
function require_air_supply(option) {
$("#air_supply").prop('required',option);
if (option) {
var mechanical_ventilation_type = document.getElementById("mechanical_ventilation_type");
mechanical_ventilation_type.value = "air_supply";
} }
}
function require_single_event(option) {
$("#single_event_date").prop('required',option);
@ -224,7 +205,24 @@ function objectifyForm(formArray) {
return returnArray;
}
$(document).ready(function() {
/* ------ On Load ---------- */
$( document ).ready(function() {
// When the document is ready, deal with the fact that we may be here
// as a result of a forward/back browser action. If that is the case, update
// the visibility of some of our inputs.
// When the ventilation_type changes we want to make its respective
// children show/hide.
ventilation_types = $("input[type=radio][name=ventilation_type]");
ventilation_types.change(on_ventilation_type_change);
// Call the function now to handle forward/back button presses in the browser.
on_ventilation_type_change();
$("input[name=mechanical_ventilation_type]").change(function(){
console.log('Changed!');
})
// Setup the maximum number of people at page load (to handle back/forward),
// and update it when total people is changed.
setMaxInfectedPeople();
@ -234,4 +232,4 @@ $(document).ready(function() {
if(radioValue.val()){
require_fields(radioValue.get(0));
}
})
});