mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
class Results {
|
|
constructor(resultsJSON, resultsList) {
|
|
this.resultsJSON = resultsJSON;
|
|
this.resultsList = resultsList;
|
|
}
|
|
|
|
clear_all() {
|
|
this.resultsList.clear();
|
|
this.resultsList.update();
|
|
this.resultsJSON.init();
|
|
}
|
|
}
|
|
|
|
|
|
class ResultsJSON {
|
|
// Sets empty object structure. Also usefull to delete old results.
|
|
init(matchCount = 0) {
|
|
this["matches"] = []; // list of all c with lc and rc
|
|
this["cpos_lookup"] = {}; // object contains all this key value pair
|
|
this["text_lookup"] = {}; // same as above for all text ids
|
|
this["match_count"] = matchCount;
|
|
}
|
|
|
|
// get query as string from form Element
|
|
getQueryStr(queryFormElement) {
|
|
// gets query
|
|
let queryFormData;
|
|
let queryStr;
|
|
queryFormData = new FormData(queryFormElement);
|
|
queryStr = queryFormData.get("query-form-query");
|
|
this["query"] = queryStr;
|
|
}
|
|
|
|
// function creates a unique and safe filename for the download
|
|
createDownloadFilename() {
|
|
let today;
|
|
let currentDate;
|
|
let currentTime;
|
|
let safeFilename;
|
|
let resultFilename;
|
|
// get and create metadata
|
|
today = new Date();
|
|
currentDate = today.getUTCFullYear() + '-' + (today.getUTCMonth() +1) + '-' + today.getUTCDate();
|
|
currentTime = today.getUTCHours() + ":" + today.getUTCMinutes() + ":" + today.getUTCSeconds();
|
|
safeFilename = this.query.replace(/[^a-z0-9_-]/gi, "_");
|
|
resultFilename = "UTC-" + currentDate + "_" + currentTime + "_" + safeFilename;
|
|
return resultFilename
|
|
}
|
|
|
|
// Function to download data as a Blob created from a string, should be multi purpose
|
|
download(downloadElem, dataStr, filename, type, filenameSlug) {
|
|
let file;
|
|
console.log("Start Download!");
|
|
filename += filenameSlug;
|
|
file = new Blob([dataStr], {type: type});
|
|
if (window.navigator.msSaveOrOpenBlob) {// IE10+
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
}
|
|
else { // Others
|
|
var url = URL.createObjectURL(file);
|
|
downloadElem.href = url;
|
|
downloadElem.download = filename;
|
|
}
|
|
}
|
|
|
|
// function to download the results as JSON
|
|
downloadJSONRessource(resultFilename) {
|
|
let dataStr;
|
|
let downloadElement;
|
|
// stringify JSON object for json download
|
|
dataStr = JSON.stringify(results.resultsJSON, undefined, "\t"); // use tabs to save some space
|
|
// get downloadResultsElement
|
|
downloadElement = document.getElementById("download-results-json");
|
|
// start actual download
|
|
this.download(downloadElement, dataStr, resultFilename, "text/json", ".json")
|
|
}
|
|
} |