From ebea38e1f11acf0e121d6d8613a9b3e8553fe928 Mon Sep 17 00:00:00 2001 From: Gabriele Girelli Date: Thu, 13 Jun 2019 10:24:21 +0200 Subject: [PATCH] Implemented light source addition. --- app/src/js/main.js | 33 ++++++++++++++++++++++- app/src/js/settings-sources.js | 49 +++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/app/src/js/main.js b/app/src/js/main.js index be5cab8..f5aa005 100644 --- a/app/src/js/main.js +++ b/app/src/js/main.js @@ -9,9 +9,40 @@ $(document).ready(function() { check_spectra = function(path) { // Check if a spectra file is properly formatted - return false; + var data = fs.readFileSync(path, 'utf-8').split("\n"); + for (var i = 0; i < data.length; i++) { + data[i] = data[i].replace("\r", ""); + data[i] = data[i].replace("\n", ""); + if (i == data.length-1 & 0 == data[i].length ) break; + if ( -1 == data[i].indexOf("\t") ) { + console.error("Missing column separator '\t' in row #" + (i+1) + "."); + return false; + } + var cols = data[i].split("\t"); + var ncols = data[i].split("\t").length; + if ( 2 != ncols ) { + console.error("Expected 2 columns, " + ncols + " found in row #" + (i+1) + "."); + return false; + } + if ( i > 0 ) { + if ( Number.isNaN(parseFloat(cols[0])) ) { + console.error("Expected float, found '" + cols[0] + "' in column #1 of row #" + (i+1) + "."); + return false; + } + if ( Number.isNaN(parseFloat(cols[1])) ) { + console.error("Expected float, found '" + cols[1] + "' in column #2 of row #" + (i+1) + "."); + return false; + } + } + } + if ( data[0] != "w\tri" ) { + console.error("Missing header line 'w\tri'."); + return false; + } + return true; } read_spectra = function(path) { // Read and parse (re-sample) a spectra file + return path; } \ No newline at end of file diff --git a/app/src/js/settings-sources.js b/app/src/js/settings-sources.js index d7e1658..2f2cb81 100644 --- a/app/src/js/settings-sources.js +++ b/app/src/js/settings-sources.js @@ -1,5 +1,36 @@ -add_source = function() { +add_source = function(data) { + // Add a new light source + var selectedTemplate = eset.get("selected-template"); + var required_keys = ['name', 'peak', 'color', 'path', 'details']; + + var source = {}; + var keys = []; + for (var i = data.length - 1; i >= 0; i--) { + var e = data[i]; + keys.push(e.name); + source[e.name] = e.value; + } + + for (var i = required_keys.length - 1; i >= 0; i--) { + var k = required_keys[i]; + console.log(k); + if ( -1 == keys.indexOf(k) ) { + toastr.error("Missing required '" + k + "' data. No source added."); + return false; + } + } + + source.peak = parseFloat(source.peak); + source.path = read_spectra(source.path); + + var sourceName = source.name + delete source.name; + eset.set("templates." + selectedTemplate + ".sources." + sourceName, source); +} + +add_source_dialog = function() { + // Show dialog to add a new light source var addSourceForm = $("
\
\ Source name:\ @@ -13,7 +44,7 @@ add_source = function() {
\ Details:\
\ - Spectrum file:\ + Spectrum file:\ Something is wrong with the uploaded file. Check the console for more details.\
\ \ @@ -70,7 +101,7 @@ add_source = function() { } } - var spectraElem = $(this).find("input[name='filepath']"); + var spectraElem = $(this).find("input[name='path']"); var spectrapath = spectraElem.val(); if ( check_spectra(spectrapath) ) { spectraElem.addClass("is-valid"); @@ -78,9 +109,13 @@ add_source = function() { spectraElem.addClass("is-invalid"); } - //$(this).addClass("was-validated"); + if ( 0 == $(this).find(".is-invalid").length ) { + $(this).addClass("was-validated"); - //bootbox.hideAll(); + add_source($(this).serializeArray()); + + bootbox.hideAll(); + } }); addSourceForm.find("input[type='button']").click(function(e) { @@ -90,7 +125,7 @@ add_source = function() { e.preventDefault(); }); - addSourceForm.find("input[name='filepath']").focus(function(e) { + addSourceForm.find("input[name='path']").focus(function(e) { // Trigger open file dialog $(this).blur(); var filepath = dialog.showOpenDialog(null, { @@ -115,5 +150,5 @@ add_source = function() { } $(function() { - $("#settings-add-source-btn").click(function(e) { add_source(); }); + $("#settings-add-source-btn").click(function(e) { add_source_dialog(); }); });