10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-11-04 05:03:53 +01:00

Add external file support

This commit is contained in:
Mickaël Véril 2019-10-02 19:20:37 +02:00
parent 7186c7695d
commit 595b90f798
6 changed files with 91 additions and 20 deletions

View File

@ -34,11 +34,34 @@ draft: false
break;
}
window.dats=await Promise.all(proms)
reloadSelect()
await reloadContent()
reloadFileSelector()
await reloadSelect()
})
}
function reloadSelect(){
function reloadFileSelector(){
document.getElementById("customFiles_input").value = ""
}
async function reloadSelect(){
radioval=$('#form_dat > input[name=datatype]:checked').val();
for(file of Array.from(document.getElementById("customFiles_input").files))
{
switch (radioval) {
case "abs":
var dat=await AbsDataFile.loadAsync(file)
window.dats.push(dat)
break;
case "fluo":
dat=await FluoDataFile.loadAsync(file)
window.dats.push(dat)
break;
case "ZPE":
dat=await ZPEDataFile.loadAsync(file)
window.dats.push(dat)
break;
}
}
$('#form_dat > select').each(function(){
$(this).find('option[value!=""]').remove()
const vals=uniq(window.dats.map((d)=>{return d[$(this).attr("name")]}))
@ -49,6 +72,7 @@ draft: false
$(this).append(op)
}
})
await reloadContent()
}
async function reloadStat(){
var stb=document.getElementById("stat_table_b");
@ -162,7 +186,7 @@ draft: false
var lnkfile=document.createElement("a")
lnkfile.target="_blank"
lnkfile.innerText="Go to file"
lnkfile.href=d.sourceFile.viewerUrl
lnkfile.href=await d.sourceFile.getViewerURL()
row.appendChild(lnkfile)
dtb.appendChild(row)
}
@ -179,6 +203,9 @@ draft: false
<input type="radio" id="zpe" value="zpe" name="datatype">
<label for="zpe">\(\Delta \text{ZPE}\)</label>
<br/>
<label for="customFiles">Add custom data file</label>
<input type="file" multiple onchange="reloadSelect()" id="customFiles_input"></input>
<br/>
<label for="mol_select">Molecule</label>
<select multiple id="mol_select" onchange="reloadContent()" name="molecule"></select>
<label for="code_select">Code</label>

View File

@ -114,7 +114,7 @@ draft: false
node=document.createElement("a")
node.innerText="Go to file"
node.target='_blank'
node.href=element.viewerUrl
node.href= await element.getViewerURL()
break
case "Array":
k=element[0]

View File

@ -24,21 +24,30 @@ draft: false
console.warn(mystr);
}
queryDic=parseQueryStringToDictionary()
key="dataFile"
if(queryDic[key]!=undefined){
var filename=queryDic[key];
var path=getFullDataPath(filename);
var textPromise=getTextFromFileAsync(path)
key=["file","fileBase64"]
if(key[0] in queryDic){
var filename=queryDic[key[0]];
var textPromise=getTextFromFileUrlAsync(filename)
btn_download=document.getElementById("btn_download");
btn_download.onclick=function(){
window.open(path,"_self");
window.open(filename,"_self");
}
document.getElementById('fileTitle').innerText=filename;
document.getElementById('fileContent').innerText=await textPromise;
document.getElementById('file').hidden=false;
document.getElementById("div_btn").hidden=false;
} else if(key[1] in queryDic){
var base64=queryDic[key[1]];
btn_download=document.getElementById("btn_download");
btn_download.onclick=function(){
window.open(String.raw`data:application/pdf;base64,${Base64}`,"_self")
}
else {
document.getElementById('fileTitle').hudden=true
document.getElementById('fileContent').innerText=atob(base64)
document.getElementById('file').hidden=false;
document.getElementById("div_btn").hidden=false;
}
else{
document.getElementById('error_P').hidden=false;
errNode;
}

View File

@ -111,7 +111,16 @@ class dataFileBase {
this.sourceFile=null
}
static async loadAsync(file) {
var dat = this.loadString(await getTextFromFileAsync(getFullDataPath(file)));
switch (trueTypeOf(file)) {
case "string":
file=getFullDataPath(file)
var str=await getTextFromFileUrlAsync(file)
break;
case "File":
var str=await getTextFromUploadedFileAsync(file)
break
}
var dat = this.loadString(str);
dat.sourceFile=new websiteFile(file)
return dat
}

View File

@ -1,7 +1,7 @@
async function getTextFromFileAsync(file) {
async function getTextFromFileUrlAsync(url) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open("GET",file, true);
req.open("GET",url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {//when a good response is given do this
@ -18,3 +18,19 @@ async function getTextFromFileAsync(file) {
req.send();
});
}
async function getTextFromUploadedFileAsync(inputFile){
const temporaryFileReader = new FileReader();
return new Promise((resolve, reject) => {
temporaryFileReader.onerror = () => {
temporaryFileReader.abort();
reject(new DOMException("Problem parsing input file."));
};
temporaryFileReader.onload = () => {
resolve(temporaryFileReader.result);
};
temporaryFileReader.readAsText(inputFile);
});
};

View File

@ -1,8 +1,18 @@
class websiteFile{
constructor(path){
this.path=path
constructor(file){
this.file=file
}
async getViewerURL(){
var urlbase="/view"
switch (trueTypeOf(this.file)) {
case "string":
return String.raw`${urlbase}?file=${this.file}`
break
case "File":
var base64=btoa(await getTextFromUploadedFileAsync(this.file))
return String.raw`${urlbase}?fileBase64=${base64}`;
break
}
get viewerUrl(){
return String.raw`/view?dataFile=${this.path}`
}
}