diff --git a/TrimSP.html b/TrimSP.html index 53a8f1a..94e807a 100644 --- a/TrimSP.html +++ b/TrimSP.html @@ -31,14 +31,14 @@ - + - + + @@ -374,8 +375,22 @@ // Open the default Layers tab document.getElementById("btnLayers").click(); - // Set up after first load (electron specific menu calls) - firstLoad(); - + let webOrApp = amIWeb(); + + if (webOrApp) { + console.log('this script is not running in Node.js'); + // Run as a web app + //let row = document.getElementById("FileRow"); + //row.parentNode.removeChild(row); + //row = document.getElementById("FolderRow"); + //row.parentNode.removeChild(row); + document.getElementById("FileRow").style.visibility = "hidden";; + document.getElementById("FolderRow").style.visibility = "hidden";; + } else { + console.log('this script is running in Node.js'); + // Run as standalone app + // Set up after first load (electron specific menu calls) + firstLoad(); + } diff --git a/TrimSPlib.js b/TrimSPlib.js index 837badc..865b751 100644 --- a/TrimSPlib.js +++ b/TrimSPlib.js @@ -1045,15 +1045,25 @@ function sum(array){ function startSequence(All) { let cmd = ''; let trimBin = All['trimPath']+"/trimspNL"; - // Check if the trimspNL binary is found - if (!fileExists(trimBin)) { - // if not found, try in PATH and hope for the best - trimBin = "trimspNL"; + let randStr = ''; + let webOrApp = amIWeb(); + // Only for Node.js + if (!webOrApp) { + // Check if the trimspNL binary is found + if (!fileExists(trimBin)) { + // if not found, try in PATH and hope for the best + trimBin = "trimspNL"; + } + + // Check if workPath exists otherwise create it + checkDir(All['workPath']); // from TrimSPelec.js, Electron/Node specific + } else { + // Add random string (5 char) to file names + randStr = (Math.random() + 1).toString(36).substring(7); + All["fileNamePrefix"] = randStr; + All["workPath"] = "/tmp/" + randStr; } - // Check if workPath exists otherwise create it - checkDir(All['workPath']); // from TrimSPelec.js, Electron/Node specific - let Progress =0; document.getElementById("pBar").style.width = Progress + "%"; document.getElementById("pBar").innerHTML = Progress + "%"; @@ -1120,15 +1130,17 @@ function startSequence(All) { let eingabe1=CreateInpFile(All); if (eingabe1=="ERROR") {return(0);} let FILENAME=All["workPath"]+"/"+All["fileNamePrefix"]+"_"+ScanName+SValue; - writeAsciiFile(FILENAME+".inp",eingabe1) - // Prepare command and execute - cmd = "cd " + All["workPath"]; - cmd += ";cp " + FILENAME + ".inp eingabe1.inp"; - cmd += ";" + trimBin; - cmd += "; mv -f ausgabe1.rge " + FILENAME + ".reg"; - cmd += "; mv -f ausgabe1.out " + FILENAME + ".out"; - cmd += "; mv -f ausgabe1.err " + FILENAME + ".err"; - execute(cmd); + writeAsciiFile(FILENAME+".inp",eingabe1); + if (!webOrApp) { + // Prepare command and execute + cmd = "cd " + All["workPath"]; + cmd += ";cp " + FILENAME + ".inp eingabe1.inp"; + cmd += ";" + trimBin; + cmd += "; mv -f ausgabe1.rge " + FILENAME + ".reg"; + cmd += "; mv -f ausgabe1.out " + FILENAME + ".out"; + cmd += "; mv -f ausgabe1.err " + FILENAME + ".err"; + execute(cmd); + } // Single run: End // Read stopping profiels @@ -1182,7 +1194,7 @@ function startSequence(All) { writeAsciiFile(seq_file, data); // Remove the fort.33 and other redundant files - cmd="cd "+All["workPath"]+";rm -f eingabe1.inp; rm -f fort.33"; + cmd="cd " + All["workPath"] + ";rm -f eingabe1.inp; rm -f fort.33"; execute(cmd); Progress=100; document.getElementById("pBar").style.width = Progress + "%"; @@ -1190,17 +1202,13 @@ function startSequence(All) { return(0); } -function readDatFile(filename,flag) { +function readDatFile(filename) { // Read column data file and return - // flag=1 use XMLHttpRequest // cols - labels of columns // data - 2D array with the columns let lines = ""; - if (flag) { - lines = loadAscii(filename); - } else { - lines = readAsciiFile(filename); - } + lines = readAsciiFile(filename); + let data = []; let i=0; // line counter for (let line of lines.trim().split('\n')) { @@ -1323,106 +1331,13 @@ function startSim() { // "http://musruser.psi.ch/cgi-bin/TrimSP.cgi?TrimSPcfg="+TrimSPcfg+'go=start'; } -// Function to load and ascii file -// async read, be careful with use -function loadAscii(file){ - var xhttp; - if (file == "" || file == undefined || file == null) { - return 0; +function amIWeb() { + if (typeof module !== 'undefined' && module.exports) { + // Run as standalone app + return 0; } else { - // Make an HTTP request using the attribute value as the file name: - xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4) { - if (this.status == 404) { - console.log("File "+file+" not found."); - } - } - } - file += "?" + Date(); - xhttp.open("GET", file, false); - xhttp.send(); - return(xhttp.responseText); + // Run as a web app + return 1; } } -function sendToCGI(){ - let args = ""; - let All = prep_cfg(1); - for (key of Object.keys(All)) { - args += "&" + key + "=" + All[key] ; - } - let url = "http://musruser.psi.ch/cgi-bin/TrimSP2.cgi?go=start" + args; - console.log(url); - - // document.getElementById("form_id").action = url; - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - // Action to be performed when the document is read; - let parser = new DOMParser(); - let xmlDoc = parser.parseFromString(xhttp.responseText,"text/html"); - console.log(xmlDoc.getElementById("links").innerHTML); - document.getElementById("links").innerHTML = xmlDoc.getElementById("links").innerHTML; - // Now we can plot things - plotThings(All); - } - }; - xhttp.open("GET",url,true); - //xhttp.responseType = 'document'; - xhttp.send(); -} - -function plotThings(All) { - let fileNamePrefix = All["fileNamePrefix"]; - let path = "/tmp/"+fileNamePrefix+"/"; - //let fileName = path + fileNamePrefix + ".rge"; - //let rgeDat = loadAscii(fileName); - //console.log(rgeDat); - if (All["scanSeq"]) { - // For multiple runs or a scan - if (All["scanType"]=="scanVals") { - var SValues=All["scanList"].split(/,/); - var SdzValues=All["scanListdz"].split(/,/); - if (SValues.length == SdzValues.length) {All["SdzFlag"]=1;} - } else { - for (let Val=parseInt(All["scanFrom"]);Val<=parseInt(All["scanTo"]);Val=Val+parseInt(All["scanStep"])) { - SValues.push(Val); - } - } - - let ScanName = ""; - let ScanAttrib = ""; - ScanName = "E"; - ScanAttrib = "valEnergy"; - if (All["comboScan"]==1) { - ScanName = "SigE"; - ScanAttrib = "sigEnergy"; - } else if (All["comboScan"]==2) { - ScanName = "Angle"; - ScanAttrib = "valAngle"; - } else if (All["comboScan"]==3) { - ScanName = "SigAngle"; - ScanAttrib = "sigAngle"; - } else if (All["comboScan"]==4) { - ScanName = "N"; - ScanAttrib = "numberProj"; - } else if (All["comboScan"]==5) { - ScanName = "Ld"+All["scandL"]; - ScanAttrib = "L"+All["scandL"]+"d"; - } - - let iScan=0; - for (var SValue of SValues) { - let fileName = path + fileNamePrefix + "_" + ScanName + SValue + ".rge"; - let [cols,data]= readDatFile(fileName,1); - console.log(fileName,cols,data); - } - } else { - // For a single run - let fileName = path + fileNamePrefix + ".rge"; - let [cols,data]= readDatFile(fileName,1); - console.log(fileName,cols,data); - } - -} diff --git a/TrimSPweb.js b/TrimSPweb.js new file mode 100644 index 0000000..d54f4f9 --- /dev/null +++ b/TrimSPweb.js @@ -0,0 +1,79 @@ +// This file contains function that are web app specific +function getFiles(dir, filelist){ + fileList = []; + return fileList; +} + +function execute(command) { +// exec(command, {stdio: 'inherit'}); +} + +function checkDir(directory) { + // Check whether directory exists, if not create it + return(1); +} + +function writeAsciiFile(filename,content) { + // Write string content into ascii file filename + // Go via CGI script + //console.log(filename, content); + // Prepare CGI args + let cgiargs = "?fn="+filename; + let lines = content.split(/\n/); + for (let i=0; i
@@ -46,6 +46,7 @@ id="browseFolde" onclick="ipcRenderer.send('browseFolder');">