nopaque/app/static/js/nopaque.CorpusAnalysisClient.js
2020-03-28 19:29:19 +01:00

102 lines
4.0 KiB
JavaScript

class CorpusAnalysisClient {
constructor(corpusId, socket) {
this.callbacks = {};
this.corpusId = corpusId;
this.displays = {};
this.socket = socket;
socket.on("corpus_analysis_init", (response) => {
if (response.code === 200) {
console.log(`corpus_analysis_init: ${response.code} - ${response.msg}`);
if (this.callbacks.init) {this.callbacks.init(response.msg);}
if (this.displays.init) {this.displays.init.setVisibilityByStatus("success");}
} else {
if (this.displays.init) {
this.displays.init.errorContainer.innerHTML = `<p class="red-text"><i class="material-icons tiny">error</i> Error ${response.code}: ${response.msg}</p>`;
this.displays.init.setVisibilityByStatus("error");
}
console.error(`corpus_analysis_init: ${response.code} - ${response.msg}`);
}
});
socket.on("corpus_analysis_query", (response) => {
if (response.code === 200) {
console.log(`corpus_analysis_query: ${response.code} - ${response.msg}`);
if (this.callbacks.query) {this.callbacks.query(response.data);}
if (this.displays.query) {this.displays.query.setVisibilityByStatus("success");}
} else {
nopaque.flash("error", `Error ${response.code}: ${response.msg}`);
this.displays.query.errorContainer.innerHTML = `<p class="red-text"><i class="material-icons tiny">error</i> Error ${response.code}: ${response.msg}</p>`;
if (this.displays.query) {this.displays.query.setVisibilityByStatus("error");}
console.error(`corpus_analysis_query: ${response.code} - ${response.msg}`)
}
});
socket.on("corpus_analysis_query_results", (response) => {
console.log("corpus_analysis_query_results:")
console.log(response);
if (this.callbacks.query_results) {this.callbacks.query_results(response);}
});
}
init() {
if (this.displays.init) {
this.displays.init.errorContainer.innerHTML == "";
this.displays.init.setVisibilityByStatus("waiting");
}
this.socket.emit("corpus_analysis_init", this.corpusId);
}
query(query) {
if (this.displays.query) {
this.displays.query.errorContainer.innerHTML == "";
this.displays.query.setVisibilityByStatus("waiting");
}
nopaque.socket.emit("corpus_analysis_query", query);
}
setCallback(type, callback) {
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");
}
setVisibilityByStatus(status) {
switch (status) {
case "error":
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":
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":
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
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");}
}
}
}