From e7bf7694f1cd03d072ac58a39bbcb318b05cabcc Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 5 Aug 2022 16:28:24 +0200 Subject: [PATCH 01/12] Added a function to generate the CVS file from the inputs selected by the user. --- cara/apps/calculator/static/js/report.js | 58 +++++++++++++++++++ .../templates/base/calculator.report.html.j2 | 45 ++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/cara/apps/calculator/static/js/report.js b/cara/apps/calculator/static/js/report.js index add174ce..a876f405 100644 --- a/cara/apps/calculator/static/js/report.js +++ b/cara/apps/calculator/static/js/report.js @@ -860,4 +860,62 @@ function copy_clipboard(shareable_link) { .tooltip('show'); navigator.clipboard.writeText(shareable_link); +} + +function export_csv() { + ``` + This function generates a CSV file according to the user's input. + It is composed of a list of lists. + The first item of the main list corresponds to the columns' name. + The remaining items correspond to each of the file row, i.e. the + respective data from the selected inputs. + ``` + let final_export = []; + + // Verify which items are checked + let export_lists = document.getElementsByName('checkedItems'); + let checked_items = []; + let has_alternative_scenario = false; + export_lists.forEach(e => { + if (e.checked) { + if (e.id != "Alternative Scenarios") checked_items.push(e.id); + else if (e.id == "Alternative Scenarios") { + Object.entries(alternative_scenarios).map((scenario) => { + if (scenario[0] != 'Current scenario') { + checked_items.push(`Alternative scenario - ${scenario[0]}`); + has_alternative_scenario = true; + }; + }); + } + } + }); + final_export.push(checked_items); + + // Add the data for each column. + times.forEach((e, i) => { + let this_row = []; + checked_items.includes("Times") && this_row.push(times[i]); + checked_items.includes("Concentrations") && this_row.push(concentrations[i]); + checked_items.includes("Cumulative Dose") && this_row.push(cumulative_doses[i]); + checked_items.includes("Long-Range Dose") && this_row.push(long_range_cumulative_doses[i]); + if (has_alternative_scenario) { + Object.entries(alternative_scenarios).map((scenario) => { + if (scenario[0] != 'Current scenario') { + this_row.push(scenario[1].concentrations[i]); + }; + }); + }; + final_export.push(this_row); + }); + + // Prepare the CSV file. + let csvContent = "data:text/csv;charset=utf8," + + final_export.map(e => e.join(",")).join("\n"); + var encodedUri = encodeURI(csvContent); + // Set a name for the file. + var link = document.createElement("a"); + link.setAttribute("href", encodedUri); + link.setAttribute("download", "report_data.csv"); + document.body.appendChild(link); + link.click(); } \ No newline at end of file diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index 0b6b63c8..6d45a4b7 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -33,6 +33,51 @@ + + + + {% endblock report_header %} From 81bb0ed79e357901ad20ba307e5a818af9915cbd Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 5 Aug 2022 16:36:53 +0200 Subject: [PATCH 02/12] Added button on header --- cara/apps/calculator/static/css/report.css | 3 +++ cara/apps/calculator/static/js/report.js | 13 ++++++------- cara/apps/static/css/style.css | 2 +- cara/apps/templates/base/calculator.report.html.j2 | 6 +++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cara/apps/calculator/static/css/report.css b/cara/apps/calculator/static/css/report.css index f0e07d90..fb0ff142 100644 --- a/cara/apps/calculator/static/css/report.css +++ b/cara/apps/calculator/static/css/report.css @@ -144,6 +144,9 @@ p.notes { #button_alternative_full_exposure, #button_alternative_hide_high_concentration { display: none!important; } + #export-csv { + display: none; + } } diff --git a/cara/apps/calculator/static/js/report.js b/cara/apps/calculator/static/js/report.js index a876f405..23e9b1aa 100644 --- a/cara/apps/calculator/static/js/report.js +++ b/cara/apps/calculator/static/js/report.js @@ -863,13 +863,12 @@ function copy_clipboard(shareable_link) { } function export_csv() { - ``` - This function generates a CSV file according to the user's input. - It is composed of a list of lists. - The first item of the main list corresponds to the columns' name. - The remaining items correspond to each of the file row, i.e. the - respective data from the selected inputs. - ``` + // This function generates a CSV file according to the user's input. + // It is composed of a list of lists. + // The first item of the main list corresponds to the columns' name. + // The remaining items correspond to each of the file row, i.e. the + // respective data from the selected inputs. + let final_export = []; // Verify which items are checked diff --git a/cara/apps/static/css/style.css b/cara/apps/static/css/style.css index 28d28bb4..f619877f 100644 --- a/cara/apps/static/css/style.css +++ b/cara/apps/static/css/style.css @@ -296,7 +296,7 @@ footer img { #report_version { font-size: .5rem; } - #download-pdf, #pdf_qrcode_aref { + #download-pdf, #pdf_qrcode_aref, #export-csv { display: none; } #scale_warning{ diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index 6d45a4b7..06069c67 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -30,12 +30,12 @@

REPORT - {{ form.simulation_name }}

Created {{ creation_date }} using CARA calculator version v{{ form.calculator_version }}

- + + - - + diff --git a/cara/apps/templates/base/index.html.j2 b/cara/apps/templates/base/index.html.j2 index 0bb7a3ac..d683ada9 100644 --- a/cara/apps/templates/base/index.html.j2 +++ b/cara/apps/templates/base/index.html.j2 @@ -2,7 +2,6 @@ {% set active_page="home/" %} {% block main %} - {#
#}
From 17b3419de7a6242bc9f58577c2484f42479ff924 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 12 Aug 2022 14:19:11 +0100 Subject: [PATCH 05/12] handled download button disable function --- cara/apps/calculator/static/js/report.js | 9 +++++++++ cara/apps/templates/base/calculator.report.html.j2 | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cara/apps/calculator/static/js/report.js b/cara/apps/calculator/static/js/report.js index 0b5ff244..a08d6f4a 100644 --- a/cara/apps/calculator/static/js/report.js +++ b/cara/apps/calculator/static/js/report.js @@ -863,6 +863,15 @@ function copy_clipboard(shareable_link) { } function display_rename_column(bool, id) { + // Handle the disable property of the download button + let download_button = document.getElementById('downloadCSV'); + if (document.querySelectorAll('input[type="checkbox"]:checked').length == 0) { + download_button.disabled = true; + } + else { + download_button.disabled = false; + } + // Change the visibility of renaming section if (bool) document.getElementById(id).style.display = 'flex'; else document.getElementById(id).style.display = 'none'; } diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index cb64de6b..e202c1c1 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -48,9 +48,9 @@ From aef19f4f96ffec575c01e299af6f3ccce7d447dc Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 12 Aug 2022 14:34:50 +0100 Subject: [PATCH 06/12] bug on download alternative scenarios --- cara/apps/calculator/static/js/report.js | 7 +++---- cara/apps/templates/base/calculator.report.html.j2 | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cara/apps/calculator/static/js/report.js b/cara/apps/calculator/static/js/report.js index a08d6f4a..0c1a3aff 100644 --- a/cara/apps/calculator/static/js/report.js +++ b/cara/apps/calculator/static/js/report.js @@ -891,17 +891,16 @@ function export_csv() { let has_alternative_scenario = false; export_lists.forEach(e => { if (e.checked) { - let has_rename = document.getElementById(`${e.id}__rename`).value; - let column_name = has_rename != '' ? has_rename : e.id; if (e.id != "Alternative Scenarios") { + let has_rename = document.getElementById(`${e.id}__rename`).value; + let column_name = has_rename != '' ? has_rename : e.id; checked_names.push(column_name); checked_items.push(e.id); } else if (e.id == "Alternative Scenarios") { Object.entries(alternative_scenarios).map((scenario) => { if (scenario[0] != 'Current scenario') { - checked_names.push(`Alternative scenario - ${column_name}`); - checked_items.push(`Alternative scenario - ${e.id}`); + checked_names.push(`Alternative scenario - ${scenario[0]}`); has_alternative_scenario = true; }; }); diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index e202c1c1..b5658105 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -82,7 +82,7 @@
{% else %}
- +
{% endif %} From a130cfb9649f92cf7a629b750c0d19748046b96d Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 22 Aug 2022 10:55:26 +0100 Subject: [PATCH 07/12] changed button location --- .../templates/base/calculator.report.html.j2 | 125 +++++++++--------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/cara/apps/templates/base/calculator.report.html.j2 b/cara/apps/templates/base/calculator.report.html.j2 index b5658105..9bea7cbd 100644 --- a/cara/apps/templates/base/calculator.report.html.j2 +++ b/cara/apps/templates/base/calculator.report.html.j2 @@ -30,70 +30,9 @@

REPORT - {{ form.simulation_name }}

Created {{ creation_date }} using CARA calculator version v{{ form.calculator_version }}

- - + - - - {% endblock report_header %} @@ -284,6 +223,68 @@ {% endif %} {% endblock report_results %} + +
+ + + {% block report_footer %} - + + + @@ -223,8 +225,6 @@ {% endif %} {% endblock report_results %} - -
- + @@ -226,12 +226,12 @@ {% endif %} {% endblock report_results %} - +