2020-03-28 18:29:19 +00:00
|
|
|
class CorpusAnalysisClient {
|
|
|
|
constructor(corpusId, socket) {
|
|
|
|
this.callbacks = {};
|
|
|
|
this.corpusId = corpusId;
|
|
|
|
this.displays = {};
|
|
|
|
this.socket = socket;
|
2020-04-01 11:44:06 +00:00
|
|
|
|
2020-04-23 08:37:44 +00:00
|
|
|
// socket on event for corpous analysis initialization
|
2020-04-06 13:28:58 +00:00
|
|
|
socket.on("corpus_analysis_init", (response) => {
|
2020-04-27 14:16:57 +00:00
|
|
|
let errorText;
|
2020-03-29 18:33:00 +00:00
|
|
|
|
2020-03-28 18:29:19 +00:00
|
|
|
if (response.code === 200) {
|
2020-04-06 13:28:58 +00:00
|
|
|
console.log(`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);
|
2020-04-27 14:16:57 +00:00
|
|
|
this.callbacks.get_metadata(); // should hold the function getMetaData
|
2020-04-01 11:44:06 +00:00
|
|
|
}
|
|
|
|
if (this.displays.init != undefined) {
|
|
|
|
this.displays.init.setVisibilityByStatus("success");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
} 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) {
|
2020-04-23 08:37:44 +00:00
|
|
|
this.displays.init.errorContainer.innerHTML = `<p class="red-text">` +
|
|
|
|
`<i class="material-icons tiny">error</i> ${errorText}</p>`;
|
2020-04-01 11:44:06 +00:00
|
|
|
}
|
|
|
|
if (this.displays.init != undefined) {
|
|
|
|
this.displays.init.setVisibilityByStatus("error");
|
|
|
|
}
|
2020-04-06 13:28:58 +00:00
|
|
|
console.error(`corpus_analysis_init: ${errorText}`);
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-27 14:16:57 +00:00
|
|
|
// socket on event for recieving meta
|
|
|
|
socket.on('corpus_analysis_send_meta_data', (response) => {
|
|
|
|
let errorText;
|
|
|
|
|
|
|
|
if (response.code === 200) {
|
|
|
|
console.log(`corpus_analysis_send_meta_data: ${response.code} - ${response.msg} - ${response.desc}`);
|
|
|
|
if (this.callbacks.recv_meta_data != undefined) {
|
|
|
|
this.callbacks.recv_meta_data(response.payload);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errorText = `Error ${response.code} - ${response.msg}`;
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
console.error(`corpus_analysis_send_meta_data: ${errorText}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-01 11:44:06 +00:00
|
|
|
// socket on event for recieveing query results
|
2020-04-06 13:28:58 +00:00
|
|
|
socket.on("corpus_analysis_query", (response) => {
|
2020-04-27 14:16:57 +00:00
|
|
|
let errorText;
|
2020-03-29 17:40:29 +00:00
|
|
|
|
2020-03-28 18:29:19 +00:00
|
|
|
if (response.code === 200) {
|
2020-04-06 13:28:58 +00:00
|
|
|
console.log(`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");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
} else {
|
2020-03-29 17:40:29 +00:00
|
|
|
errorText = `Error ${response.payload.code} - ${response.payload.msg}`;
|
2020-04-27 13:22:20 +00:00
|
|
|
nopaque.flash(errorText, "error");
|
2020-04-01 11:44:06 +00:00
|
|
|
if (this.displays.query.errorContainer != undefined) {
|
2020-04-23 08:37:44 +00:00
|
|
|
this.displays.query.errorContainer.innerHTML = `<p class="red-text">`+
|
|
|
|
`<i class="material-icons tiny">error</i> ${errorText}</p>`;
|
2020-04-01 11:44:06 +00:00
|
|
|
}
|
|
|
|
if (this.displays.query != undefined) {
|
|
|
|
this.displays.query.setVisibilityByStatus("error");
|
|
|
|
}
|
2020-04-06 13:28:58 +00:00
|
|
|
console.error(`corpus_analysis_query: ${errorText}`);
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-23 08:37:44 +00:00
|
|
|
|
2020-04-06 13:28:58 +00:00
|
|
|
socket.on("corpus_analysis_query_results", (response) => {
|
2020-04-23 08:37:44 +00:00
|
|
|
if (this.callbacks.query_results != undefined) {
|
|
|
|
this.callbacks.query_results(response.payload);
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
});
|
2020-04-09 08:17:04 +00:00
|
|
|
|
2020-04-23 08:37:44 +00:00
|
|
|
socket.on("corpus_analysis_inspect_match", (response) => {
|
|
|
|
if (this.callbacks.query_match_context != undefined) {
|
|
|
|
this.callbacks.query_match_context(response);
|
|
|
|
}
|
|
|
|
});
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-06 13:28:58 +00:00
|
|
|
this.socket.emit("corpus_analysis_init", this.corpusId);
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:16:57 +00:00
|
|
|
getMetaData() {
|
|
|
|
// just emits thos to tell the server to gather all meta dat infos and send
|
|
|
|
// those back
|
|
|
|
this.socket.emit("corpus_analysis_get_meta_data", this.corpusId);
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:45:02 +00:00
|
|
|
query(queryStr) {
|
2020-04-01 11:44:06 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-04-06 13:28:58 +00:00
|
|
|
nopaque.socket.emit("corpus_analysis_query", queryStr);
|
2020-03-31 08:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 18:29:19 +00:00
|
|
|
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
|
2020-03-28 18:29:19 +00:00
|
|
|
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")
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-03-28 18:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|