nopaque/app/static/js/nopaque.CorpusAnalysisClient.js

197 lines
6.6 KiB
JavaScript
Raw Normal View History

class CorpusAnalysisClient {
constructor(corpusId, socket) {
this.callbacks = {};
this.corpusId = corpusId;
this.displays = {};
this.socket = socket;
2020-04-01 12:27:01 +00:00
this.resultList = {}; // why do you save it here? we have a global variable results
2020-04-01 11:44:06 +00:00
// js list options and intialization
let displayOptionsData = this.getDisplayOptions(displayOptionsFormElement);
let resultListOptions = {page: displayOptionsData["hitsPerPage"],
pagination: [{
name: "paginationTop",
paginationClass: "paginationTop",
innerWindow: 8,
outerWindow: 1
}, {
paginationClass: "paginationBottom",
innerWindow: 8,
outerWindow: 1
}],
valueNames: ["titles", "lc", "c", "rc", {data: ["index"]}],
item: `<span></span>`};
this.resultList = new ResultList("result-list", resultListOptions);
// socket on event fpr corpous analysis initialization
2020-03-28 19:45:33 +00:00
socket.on("pj_corpus_analysis_init", (response) => {
2020-03-29 18:33:00 +00:00
var errorText;
if (response.code === 200) {
2020-03-28 19:45:33 +00:00
console.log(`pj_corpus_analysis_init: ${response.code} - ${response.msg}`);
2020-04-01 11:44:06 +00:00
if (this.callbacks.init != undefined) {
this.callbacks.init(response.payload);
}
if (this.displays.init != undefined) {
this.displays.init.setVisibilityByStatus("success");
}
} else {
2020-03-29 18:33:00 +00:00
errorText = `Error ${response.code} - ${response.msg}`;
2020-04-01 11:44:06 +00:00
if (this.displays.init.errorContainer != undefined) {
this.displays.init.errorContainer.innerHTML = `<p class="red-text"><i class="material-icons tiny">error</i> ${errorText}</p>`;
}
if (this.displays.init != undefined) {
this.displays.init.setVisibilityByStatus("error");
}
2020-03-29 17:40:29 +00:00
console.error(`pj_corpus_analysis_init: ${errorText}`);
}
});
2020-04-01 11:44:06 +00:00
// socket on event for recieveing query results
2020-03-28 19:45:33 +00:00
socket.on("pj_corpus_analysis_query", (response) => {
2020-03-29 17:40:29 +00:00
var errorText;
if (response.code === 200) {
2020-03-28 19:45:33 +00:00
console.log(`pj_corpus_analysis_query: ${response.code} - ${response.msg}`);
2020-04-01 11:44:06 +00:00
if (this.callbacks.query != undefined) {
this.callbacks.query(response.payload);
}
if (this.displays.query != undefined) {
this.displays.query.setVisibilityByStatus("success");
}
} else {
2020-03-29 17:40:29 +00:00
errorText = `Error ${response.payload.code} - ${response.payload.msg}`;
nopaque.flash("error", errorText);
2020-04-01 11:44:06 +00:00
if (this.displays.query.errorContainer != undefined) {
this.displays.query.errorContainer.innerHTML = `<p class="red-text"><i class="material-icons tiny">error</i> ${errorText}</p>`;
}
if (this.displays.query != undefined) {
this.displays.query.setVisibilityByStatus("error");
}
2020-03-29 17:40:29 +00:00
console.error(`pj_corpus_analysis_query: ${errorText}`);
}
});
2020-03-28 19:45:33 +00:00
socket.on("pj_corpus_analysis_query_results", (response) => {
2020-03-29 17:40:29 +00:00
if (this.callbacks.query_results != undefined) {this.callbacks.query_results(response.payload);}
});
}
init() {
2020-04-01 11:44:06 +00:00
if (this.displays.init.errorContainer != undefined) {
this.displays.init.errorContainer.innerHTML == "";
}
if (this.displays.init != undefined) {
this.displays.init.setVisibilityByStatus("waiting");
}
2020-03-28 19:45:33 +00:00
this.socket.emit("pj_corpus_analysis_init", this.corpusId);
}
2020-04-01 11:44:06 +00:00
sendQuery(queryStr) {
let displayOptionsData;
let resultListOptions;
if (this.displays.query.errorContainer != undefined) {
this.displays.query.errorContainer.innerHTML == "";
}
if (this.displays.query != undefined) {
this.displays.query.setVisibilityByStatus("waiting");
}
this.resultList.clear(); // empty list for new query
2020-03-31 08:17:04 +00:00
nopaque.socket.emit("pj_corpus_analysis_query", queryStr);
}
getQueryStr(queryFormElement) {
// gets query
let queryFormData;
let queryStr;
queryFormData = new FormData(queryFormElement);
queryStr = queryFormData.get("query-form-query");
return queryStr
}
2020-04-01 11:44:06 +00:00
getDisplayOptions(displayOptionsFormElement) {
// gets display options parameters
let displayOptionsFormData
let displayOptionsData;
displayOptionsFormData = new FormData(displayOptionsFormElement);
displayOptionsData = {"resultsPerPage": displayOptionsFormData.get("display-options-form-results_per_page"),
"resultsContex": displayOptionsFormData.get("display-options-form-result_context"),
"expertMode": displayOptionsFormData.get("display-options-form-expert_mode")};
console.log(displayOptionsData);
return displayOptionsData
}
setCallback(type, callback) {
2020-03-31 08:17:04 +00:00
// saves callback functions into an object. Key is function type, callback
// is the callback function
this.callbacks[type] = callback;
}
setDisplay(type, display) {
this.displays[type] = display;
}
}
class CorpusAnalysisDisplay {
constructor(element) {
this.element = element;
this.errorContainer = element.querySelector(".error-container");
this.showOnError = element.querySelectorAll(".show-on-error");
this.showOnSuccess = element.querySelectorAll(".show-on-success");
this.showWhileWaiting = element.querySelectorAll(".show-while-waiting");
2020-04-01 11:44:06 +00:00
this.hideOnComplete = element.querySelectorAll(".hide-on-complete")
}
setVisibilityByStatus(status) {
switch (status) {
case "error":
2020-04-01 11:44:06 +00:00
for (let element of this.showOnError) {
element.classList.remove("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
break;
case "success":
2020-04-01 11:44:06 +00:00
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.remove("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
break;
case "waiting":
2020-04-01 11:44:06 +00:00
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.remove("hide");
}
break;
default:
// Hide all
2020-04-01 11:44:06 +00:00
for (let element of this.showOnError) {
element.classList.add("hide");
}
for (let element of this.showOnSuccess) {
element.classList.add("hide");
}
for (let element of this.showWhileWaiting) {
element.classList.add("hide");
}
}
}
}