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

193 lines
7.3 KiB
JavaScript
Raw Normal View History

2020-02-04 09:10:22 +00:00
/*
* The nopaque object is used as a namespace for nopaque specific functions and
* variables.
*/
var nopaque = {};
2020-01-17 09:42:02 +00:00
// User data
nopaque.user = {};
nopaque.user.settings = {};
nopaque.user.settings.darkMode = undefined;
nopaque.corporaSubscribers = [];
nopaque.jobsSubscribers = [];
nopaque.queryResultsSubscribers = [];
2020-01-17 09:42:02 +00:00
// 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
2020-10-23 06:51:46 +00:00
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) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.user.corpora);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.jobsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.user.jobs);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.queryResultsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.user.query_results);
2020-10-23 06:51:46 +00:00
}
});
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) {
2020-12-07 15:10:40 +00:00
subscriber.update(corpora_patch);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.jobsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.update(jobs_patch);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.queryResultsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.update(query_results_patch);
2020-10-23 06:51:46 +00:00
}
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");
}
}
2020-10-23 06:51:46 +00:00
}
});
2020-10-23 06:51:46 +00:00
nopaque.socket.on("foreign_user_data_stream_init", function(msg) {
nopaque.foreignUser = JSON.parse(msg);
for (let subscriber of nopaque.foreignCorporaSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.foreignUser.corpora);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.foreignJobsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.foreignUser.jobs);
2020-10-23 06:51:46 +00:00
}
for (let subscriber of nopaque.foreignQueryResultsSubscribers) {
2020-12-07 15:10:40 +00:00
subscriber.init(nopaque.foreignUser.query_results);
2020-10-23 06:51:46 +00:00
}
});
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"));
2020-12-07 15:10:40 +00:00
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);}
2020-10-23 06:51:46 +00:00
});
2020-02-18 13:39:05 +00:00
nopaque.Forms = {};
nopaque.Forms.init = function() {
2020-02-18 13:44:31 +00:00
var abortRequestElement, parentElement, progressElement, progressModal,
progressModalElement, request, submitElement;
for (let form of document.querySelectorAll(".nopaque-submit-form")) {
2020-02-18 13:44:31 +00:00
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");
2020-02-18 13:53:35 +00:00
parentElement.querySelector(".select-dropdown").classList.add("invalid");
2020-02-18 13:44:31 +00:00
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");
}
2020-01-23 10:19:37 +00:00
form.addEventListener("submit", function(event) {
event.preventDefault();
2020-04-17 09:04:09 +00:00
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;
2020-01-23 10:19:37 +00:00
}
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>`);
}
2020-01-23 10:19:37 +00:00
}
if (progressModalElement) {
progressModal.close();
}
}
if (request.status === 500) {
location.reload();
2020-01-23 10:19:37 +00:00
}
});
if (progressModalElement) {
request.upload.addEventListener("progress", function(event) {
progressElement.style.width = Math.floor(100 * event.loaded / event.total).toString() + "%";
});
}
2020-01-23 10:19:37 +00:00
}
}
2020-10-23 06:51:46 +00:00
nopaque.flash = function(message, category) {
let toast;
let toastActionElement;
switch (category) {
2020-04-27 13:22:20 +00:00
case "corpus":
message = `<i class="left material-icons">book</i>${message}`;
break;
case "error":
2020-04-27 13:22:20 +00:00
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:
2020-04-27 13:22:20 +00:00
message = `<i class="left material-icons">notifications</i>${message}`;
}
2020-01-17 09:42:02 +00:00
2020-04-27 13:22:20 +00:00
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"]');
2020-10-23 06:51:46 +00:00
toastActionElement.addEventListener('click', () => {toast.dismiss();});
2020-01-17 09:42:02 +00:00
}