Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Implemented light source addition.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggirelli committed Jun 13, 2019
1 parent 4a965e0 commit ebea38e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
33 changes: 32 additions & 1 deletion app/src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
49 changes: 42 additions & 7 deletions app/src/js/settings-sources.js
Original file line number Diff line number Diff line change
@@ -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 = $("<form class='needs-validation'>\
<div class='mb-2'>\
Source name:<input class='form-control' type='text' name='name' required />\
Expand All @@ -13,7 +44,7 @@ add_source = function() {
</div>\
Details:<input class='form-control mb-2' type='text' name='details' />\
<div class='mb-2'>\
Spectrum file:<input class='form-control' type='text' name='filepath' required />\
Spectrum file:<input class='form-control' type='text' name='path' required />\
<small class='invalid-feedback'>Something is wrong with the uploaded file. Check the console for more details.</small>\
</div>\
<input type='submit' class='btn btn-primary float-right mt-2' />\
Expand Down Expand Up @@ -70,17 +101,21 @@ 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");
} else {
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) {
Expand All @@ -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, {
Expand All @@ -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(); });
});

0 comments on commit ebea38e

Please sign in to comment.