mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 09:15:41 +00:00
193 lines
7.3 KiB
JavaScript
193 lines
7.3 KiB
JavaScript
/*
|
|
* The nopaque object is used as a namespace for nopaque specific functions and
|
|
* variables.
|
|
*/
|
|
var nopaque = {};
|
|
|
|
// User data
|
|
nopaque.user = {};
|
|
nopaque.user.settings = {};
|
|
nopaque.user.settings.darkMode = undefined;
|
|
nopaque.corporaSubscribers = [];
|
|
nopaque.jobsSubscribers = [];
|
|
nopaque.queryResultsSubscribers = [];
|
|
|
|
// Foreign user (user inspected with admin credentials) data
|
|
nopaque.foreignUser = {};
|
|
nopaque.foreignUser.isAuthenticated = undefined;
|
|
nopaque.foreignUser.settings = {};
|
|
nopaque.foreignUser.settings.darkMode = undefined;
|
|
nopaque.foreignCorporaSubscribers = [];
|
|
nopaque.foreignJobsSubscribers = [];
|
|
nopaque.foreignQueryResultsSubscribers = [];
|
|
|
|
// nopaque functions
|
|
nopaque.socket = io({transports: ['websocket']});
|
|
// Add event handlers
|
|
nopaque.socket.on("user_data_stream_init", function(msg) {
|
|
nopaque.user = JSON.parse(msg);
|
|
for (let subscriber of nopaque.corporaSubscribers) {
|
|
subscriber.init(nopaque.user.corpora);
|
|
}
|
|
for (let subscriber of nopaque.jobsSubscribers) {
|
|
subscriber.init(nopaque.user.jobs);
|
|
}
|
|
for (let subscriber of nopaque.queryResultsSubscribers) {
|
|
subscriber.init(nopaque.user.query_results);
|
|
}
|
|
});
|
|
|
|
nopaque.socket.on("user_data_stream_update", function(msg) {
|
|
var patch;
|
|
|
|
patch = JSON.parse(msg);
|
|
nopaque.user = jsonpatch.apply_patch(nopaque.user, patch);
|
|
corpora_patch = patch.filter(operation => operation.path.startsWith("/corpora"));
|
|
jobs_patch = patch.filter(operation => operation.path.startsWith("/jobs"));
|
|
query_results_patch = patch.filter(operation => operation.path.startsWith("/query_results"));
|
|
for (let subscriber of nopaque.corporaSubscribers) {
|
|
subscriber.update(corpora_patch);
|
|
}
|
|
for (let subscriber of nopaque.jobsSubscribers) {
|
|
subscriber.update(jobs_patch);
|
|
}
|
|
for (let subscriber of nopaque.queryResultsSubscribers) {
|
|
subscriber.update(query_results_patch);
|
|
}
|
|
if (["all", "end"].includes(nopaque.user.settings.job_status_site_notifications)) {
|
|
for (operation of jobs_patch) {
|
|
/* "/jobs/{jobId}/..." -> ["{jobId}", ...] */
|
|
pathArray = operation.path.split("/").slice(2);
|
|
if (operation.op === "replace" && pathArray[1] === "status") {
|
|
if (nopaque.user.settings.job_status_site_notifications === "end" && !["complete", "failed"].includes(operation.value)) {continue;}
|
|
nopaque.flash(`[<a href="/jobs/${pathArray[0]}">${nopaque.user.jobs[pathArray[0]].title}</a>] New status: ${operation.value}`, "job");
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
nopaque.socket.on("foreign_user_data_stream_init", function(msg) {
|
|
nopaque.foreignUser = JSON.parse(msg);
|
|
for (let subscriber of nopaque.foreignCorporaSubscribers) {
|
|
subscriber.init(nopaque.foreignUser.corpora);
|
|
}
|
|
for (let subscriber of nopaque.foreignJobsSubscribers) {
|
|
subscriber.init(nopaque.foreignUser.jobs);
|
|
}
|
|
for (let subscriber of nopaque.foreignQueryResultsSubscribers) {
|
|
subscriber.init(nopaque.foreignUser.query_results);
|
|
}
|
|
});
|
|
|
|
nopaque.socket.on("foreign_user_data_stream_update", function(msg) {
|
|
var patch;
|
|
|
|
patch = JSON.parse(msg);
|
|
nopaque.foreignUser = jsonpatch.apply_patch(nopaque.foreignUser, patch);
|
|
corpora_patch = patch.filter(operation => operation.path.startsWith("/corpora"));
|
|
jobs_patch = patch.filter(operation => operation.path.startsWith("/jobs"));
|
|
query_results_patch = patch.filter(operation => operation.path.startsWith("/query_results"));
|
|
for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber.update(corpora_patch);}
|
|
for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber.update(jobs_patch);}
|
|
for (let subscriber of nopaque.foreignQueryResultsSubscribers) {subscriber.update(query_results_patch);}
|
|
});
|
|
|
|
nopaque.Forms = {};
|
|
nopaque.Forms.init = function() {
|
|
var abortRequestElement, parentElement, progressElement, progressModal,
|
|
progressModalElement, request, submitElement;
|
|
|
|
for (let form of document.querySelectorAll(".nopaque-submit-form")) {
|
|
submitElement = form.querySelector('button[type="submit"]');
|
|
submitElement.addEventListener("click", function() {
|
|
for (let selectElement of form.querySelectorAll('select')) {
|
|
if (selectElement.value === "") {
|
|
parentElement = selectElement.closest(".input-field");
|
|
parentElement.querySelector(".select-dropdown").classList.add("invalid");
|
|
for (let helperTextElement of parentElement.querySelectorAll(".helper-text")) {
|
|
helperTextElement.remove();
|
|
}
|
|
parentElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">Please select an option.</span>`);
|
|
}
|
|
}
|
|
});
|
|
|
|
request = new XMLHttpRequest();
|
|
if (form.dataset.hasOwnProperty("progressModal")) {
|
|
progressModalElement = document.getElementById(form.dataset.progressModal);
|
|
progressModal = M.Modal.getInstance(progressModalElement);
|
|
progressModal.options.dismissible = false;
|
|
abortRequestElement = progressModalElement.querySelector(".abort-request");
|
|
abortRequestElement.addEventListener("click", function() {request.abort();});
|
|
progressElement = progressModalElement.querySelector(".determinate");
|
|
}
|
|
form.addEventListener("submit", function(event) {
|
|
event.preventDefault();
|
|
var formData;
|
|
|
|
formData = new FormData(form);
|
|
// Initialize progress modal
|
|
if (progressModalElement) {
|
|
progressElement.style.width = "0%";
|
|
progressModal.open();
|
|
}
|
|
request.open("POST", window.location.href);
|
|
request.send(formData);
|
|
});
|
|
request.addEventListener("load", function(event) {
|
|
var fieldElement;
|
|
|
|
if (request.status === 201) {
|
|
window.location.href = JSON.parse(this.responseText).redirect_url;
|
|
}
|
|
if (request.status === 400) {
|
|
console.log(request);
|
|
for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) {
|
|
fieldElement = form.querySelector(`input[name$="${field}"]`).closest(".input-field");
|
|
for (let error of errors) {
|
|
fieldElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">${error}</span>`);
|
|
}
|
|
}
|
|
if (progressModalElement) {
|
|
progressModal.close();
|
|
}
|
|
}
|
|
if (request.status === 500) {
|
|
location.reload();
|
|
}
|
|
});
|
|
if (progressModalElement) {
|
|
request.upload.addEventListener("progress", function(event) {
|
|
progressElement.style.width = Math.floor(100 * event.loaded / event.total).toString() + "%";
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
nopaque.flash = function(message, category) {
|
|
let toast;
|
|
let toastActionElement;
|
|
|
|
switch (category) {
|
|
case "corpus":
|
|
message = `<i class="left material-icons">book</i>${message}`;
|
|
break;
|
|
case "error":
|
|
message = `<i class="left material-icons red-text">error</i>${message}`;
|
|
break;
|
|
case "job":
|
|
message = `<i class="left material-icons">work</i>${message}`;
|
|
break;
|
|
default:
|
|
message = `<i class="left material-icons">notifications</i>${message}`;
|
|
}
|
|
|
|
toast = M.toast({html: `<span>${message}</span>
|
|
<button data-action="close" class="btn-flat toast-action white-text">
|
|
<i class="material-icons">close</i>
|
|
</button>`});
|
|
toastActionElement = toast.el.querySelector('.toast-action[data-action="close"]');
|
|
toastActionElement.addEventListener('click', () => {toast.dismiss();});
|
|
}
|