nopaque/web/app/static/js/nopaque.Results.js

112 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-04-02 12:22:03 +00:00
class Results {
constructor(data, jsList , metaData) {
this.data = data;
this.jsList = jsList;
this.metaData = metaData
2020-06-25 08:51:51 +00:00
this.subResultsData = new Data();
2020-04-02 12:22:03 +00:00
}
2020-04-09 08:17:04 +00:00
clearAll() {
this.jsList.clear();
this.jsList.update();
this.data.init();
this.metaData.init();
2020-06-25 08:51:51 +00:00
this.subResultsData.init();
2020-04-07 11:13:48 +00:00
}
2020-06-25 08:51:51 +00:00
2020-04-07 11:13:48 +00:00
}
class Data {
2020-04-07 11:13:48 +00:00
// Sets empty object structure. Also usefull to delete old results.
2020-04-08 09:37:34 +00:00
// matchCount default is 0
2020-04-07 11:13:48 +00:00
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;
2020-06-25 08:51:51 +00:00
this["corpus_type"] = "results";
this["query"] = "";
2020-04-07 11:13:48 +00:00
}
2020-06-25 08:51:51 +00:00
addData(jsonData, key=null) {
if (key !== null) {
Object.assign(this[key], jsonData);
} else if (key === null) {
Object.assign(this, jsonData)
}
}
2020-04-07 11:13:48 +00:00
// 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;
}
2020-04-02 12:22:03 +00:00
2020-04-07 11:13:48 +00:00
// function creates a unique and safe filename for the download
2020-04-27 11:25:29 +00:00
createDownloadFilename(suffix) {
2020-04-07 11:13:48 +00:00
let today;
let currentDate;
let currentTime;
let safeFilename;
let resultFilename;
// get and create metadata
today = new Date();
2020-04-08 09:37:34 +00:00
currentDate = `${today.getUTCFullYear()}` +
`-${(today.getUTCMonth() + 1)}` +
`-${today.getUTCDate()}`;
currentTime = `${today.getUTCHours()}h` +
`${today.getUTCMinutes()}m` +
`${today.getUTCSeconds()}s`;
2020-04-07 11:13:48 +00:00
safeFilename = this.query.replace(/[^a-z0-9_-]/gi, "_");
2020-04-27 11:25:29 +00:00
resultFilename = `UTC-${currentDate}_${currentTime}_${safeFilename}_${suffix}`;
2020-04-07 11:13:48 +00:00
return resultFilename
}
2020-04-08 09:37:34 +00:00
// Function to download data as Blob created from string
// should be private but that is not yet a feature of javascript 08.04.2020
2020-04-27 11:25:29 +00:00
download(downloadElement, dataStr, filename, type, filenameSlug) {
2020-04-07 11:13:48 +00:00
console.log("Start Download!");
2020-04-27 11:25:29 +00:00
let file;
2020-04-07 11:13:48 +00:00
filename += filenameSlug;
file = new Blob([dataStr], {type: type});
var url = URL.createObjectURL(file);
2020-04-27 11:25:29 +00:00
downloadElement.href = url;
downloadElement.download = filename;
2020-04-07 11:13:48 +00:00
}
// function to download the results as JSON
2020-04-27 11:25:29 +00:00
downloadJSONRessource(resultFilename, downloadData, downloadElement) {
2020-04-07 11:13:48 +00:00
let dataStr;
// stringify JSON object for json download
2020-04-08 09:37:34 +00:00
// use tabs to save some space
2020-04-27 11:25:29 +00:00
dataStr = JSON.stringify(downloadData, undefined, "\t");
2020-04-07 11:13:48 +00:00
// start actual download
this.download(downloadElement, dataStr, resultFilename, "text/json", ".json")
}
2020-06-25 08:51:51 +00:00
// createSubResultsData from subResultsTmpData
createSubResultsData() {
let tmp = [...results.jsList.addToSubResultsIdsToShow].sort(function(a, b){return a-b});
let dataIndexes = [];
tmp.forEach((index) => dataIndexes.push(index - 1));
console.log(dataIndexes);
results.jsList.getMatchWithContext(dataIndexes, "sub-results");
// TODO: save incoming matche infos with saveSubResultsChoices.
// TODO: trigger this function on dl btn click and seta flag that it has run to avoid double execution
// also set this flag to false if addToSubResultsIdsToShow has been altered
}
}
class MetaData {
2020-04-30 12:45:54 +00:00
// Sets empty object structure when no input is given.
2020-05-04 09:05:17 +00:00
// if json object like input is given class fields are created from this
2020-04-30 12:45:54 +00:00
init(json = {}) {
Object.assign(this, json);
}
2020-04-02 12:22:03 +00:00
}