2020-02-04 09:10:22 +00:00
|
|
|
/*
|
|
|
|
* The nopaque object is used as a namespace for nopaque specific functions and
|
|
|
|
* variables.
|
|
|
|
*/
|
2020-01-20 08:49:39 +00:00
|
|
|
var nopaque = {};
|
2020-01-17 09:42:02 +00:00
|
|
|
|
2020-01-20 08:49:39 +00:00
|
|
|
// nopaque ressources
|
2020-03-26 16:32:26 +00:00
|
|
|
nopaque.socket = undefined;
|
2020-01-17 09:42:02 +00:00
|
|
|
|
2020-01-31 12:41:46 +00:00
|
|
|
nopaque.corpora = undefined;
|
|
|
|
nopaque.corporaSubscribers = [];
|
|
|
|
nopaque.jobs = undefined;
|
|
|
|
nopaque.jobsSubscribers = [];
|
2020-01-17 09:42:02 +00:00
|
|
|
|
2020-01-31 12:41:46 +00:00
|
|
|
nopaque.foreignCorpora = undefined;
|
|
|
|
nopaque.foreignCorporaSubscribers = [];
|
|
|
|
nopaque.foreignJobs = undefined;
|
|
|
|
nopaque.foreignJobsSubscribers = [];
|
2020-01-20 08:49:39 +00:00
|
|
|
|
2020-03-26 16:32:26 +00:00
|
|
|
nopaque.user = {};
|
|
|
|
nopaque.user.isAuthenticated = undefined;
|
|
|
|
nopaque.user.settings = {}
|
|
|
|
nopaque.user.settings.darkMode = undefined;
|
|
|
|
|
2020-03-27 09:11:19 +00:00
|
|
|
nopaque.flashedMessages = undefined;
|
2020-01-20 08:49:39 +00:00
|
|
|
|
|
|
|
// nopaque functions
|
2020-03-26 16:32:26 +00:00
|
|
|
nopaque.socket = {};
|
|
|
|
nopaque.socket.init = function() {
|
2020-03-30 15:27:24 +00:00
|
|
|
nopaque.socket = io({transports: ['websocket']});
|
2020-03-26 16:32:26 +00:00
|
|
|
// Add event handlers
|
|
|
|
nopaque.socket.on("corpora_init", function(msg) {
|
|
|
|
nopaque.corpora = JSON.parse(msg);
|
|
|
|
for (let subscriber of nopaque.corporaSubscribers) {subscriber._init(nopaque.corpora);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("jobs_init", function(msg) {
|
|
|
|
nopaque.jobs = JSON.parse(msg);
|
|
|
|
for (let subscriber of nopaque.jobsSubscribers) {subscriber._init(nopaque.jobs);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("corpora_update", function(msg) {
|
|
|
|
var patch;
|
|
|
|
|
|
|
|
patch = JSON.parse(msg);
|
|
|
|
nopaque.corpora = jsonpatch.apply_patch(nopaque.corpora, patch);
|
|
|
|
for (let subscriber of nopaque.corporaSubscribers) {subscriber._update(patch);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("jobs_update", function(msg) {
|
|
|
|
var patch;
|
|
|
|
|
|
|
|
patch = JSON.parse(msg);
|
2020-04-27 11:50:54 +00:00
|
|
|
nopaque.jobs = jsonpatch.apply_patch(nopaque.jobs, patch);
|
|
|
|
if (["all", "end"].includes(nopaque.user.settings.jobStatusSiteNotifications)) {
|
|
|
|
for (operation of patch) {
|
|
|
|
/* "/jobId/valueName" -> ["jobId", "valueName"] */
|
|
|
|
pathArray = operation.path.split("/").slice(1);
|
|
|
|
if (operation.op === "replace" && pathArray[1] === "status") {
|
|
|
|
if (nopaque.user.settings.jobStatusSiteNotifications === "end" && !["complete", "failed"].includes(operation.value)) {continue;}
|
2020-04-27 13:22:20 +00:00
|
|
|
nopaque.flash(`[<a href="/jobs/${pathArray[0]}">${nopaque.jobs[pathArray[0]].title}</a>] New status: ${operation.value}`, "job");
|
2020-04-27 11:50:54 +00:00
|
|
|
}
|
2020-04-24 13:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 16:32:26 +00:00
|
|
|
for (let subscriber of nopaque.jobsSubscribers) {subscriber._update(patch);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("foreign_corpora_init", function(msg) {
|
|
|
|
nopaque.foreignCorpora = JSON.parse(msg);
|
|
|
|
for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber._init(nopaque.foreignCorpora);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("foreign_jobs_init", function(msg) {
|
|
|
|
nopaque.foreignJobs = JSON.parse(msg);
|
|
|
|
for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber._init(nopaque.foreignJobs);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("foreign_corpora_update", function(msg) {
|
|
|
|
var patch;
|
|
|
|
|
|
|
|
patch = JSON.parse(msg);
|
|
|
|
nopaque.foreignCorpora = jsonpatch.apply_patch(nopaque.foreignCorpora, patch);
|
|
|
|
for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber._update(patch);}
|
|
|
|
});
|
|
|
|
|
|
|
|
nopaque.socket.on("foreign_jobs_update", function(msg) {
|
|
|
|
var patch;
|
|
|
|
|
|
|
|
patch = JSON.parse(msg);
|
|
|
|
nopaque.foreignJobs = jsonpatch.apply_patch(nopaque.foreignJobs, patch);
|
|
|
|
for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber._update(patch);}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
2020-02-18 11:01:36 +00:00
|
|
|
|
2020-04-16 07:40:38 +00:00
|
|
|
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>`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-24 09:27:11 +00:00
|
|
|
request = new XMLHttpRequest();
|
2020-01-31 12:41:46 +00:00
|
|
|
if (form.dataset.hasOwnProperty("progressModal")) {
|
2020-01-24 09:27:11 +00:00
|
|
|
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;
|
2020-01-24 09:27:11 +00:00
|
|
|
|
|
|
|
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) {
|
2020-01-31 12:41:46 +00:00
|
|
|
var fieldElement;
|
2020-01-24 09:27:11 +00:00
|
|
|
|
|
|
|
if (request.status === 201) {
|
2020-01-31 12:41:46 +00:00
|
|
|
window.location.href = JSON.parse(this.responseText).redirect_url;
|
2020-01-23 10:19:37 +00:00
|
|
|
}
|
2020-01-24 09:27:11 +00:00
|
|
|
if (request.status === 400) {
|
2020-04-15 12:57:17 +00:00
|
|
|
console.log(request);
|
2020-01-24 09:27:11 +00:00
|
|
|
for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) {
|
2020-04-16 07:40:38 +00:00
|
|
|
fieldElement = form.querySelector(`input[name$="${field}"]`).closest(".input-field");
|
2020-01-24 09:27:11 +00:00
|
|
|
for (let error of errors) {
|
2020-01-31 12:41:46 +00:00
|
|
|
fieldElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">${error}</span>`);
|
2020-01-24 09:27:11 +00:00
|
|
|
}
|
2020-01-23 10:19:37 +00:00
|
|
|
}
|
2020-01-24 09:27:11 +00:00
|
|
|
if (progressModalElement) {
|
|
|
|
progressModal.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (request.status === 500) {
|
|
|
|
location.reload();
|
2020-01-23 10:19:37 +00:00
|
|
|
}
|
|
|
|
});
|
2020-01-24 09:27:11 +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-02-18 13:39:05 +00:00
|
|
|
nopaque.Navigation = {};
|
|
|
|
nopaque.Navigation.init = function() {
|
|
|
|
/* ### Initialize sidenav-main ### */
|
|
|
|
for (let entry of document.querySelectorAll("#sidenav-main a")) {
|
2020-01-20 08:49:39 +00:00
|
|
|
if (entry.href === window.location.href) {
|
|
|
|
entry.parentNode.classList.add("active");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-27 09:32:14 +00:00
|
|
|
nopaque.flash = function() {
|
|
|
|
var classes, toast, toastActionElement;
|
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 1:
|
|
|
|
category = "message";
|
|
|
|
message = arguments[0];
|
|
|
|
break;
|
|
|
|
case 2:
|
2020-04-27 13:22:20 +00:00
|
|
|
message = arguments[0];
|
|
|
|
category = arguments[1];
|
2020-03-27 09:32:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-04-27 13:22:20 +00:00
|
|
|
console.error("Usage: nopaque.flash(message) or nopaque.flash(message, category)")
|
2020-03-27 09:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (category) {
|
2020-04-27 13:22:20 +00:00
|
|
|
case "corpus":
|
|
|
|
message = `<i class="left material-icons">book</i>${message}`;
|
|
|
|
break;
|
2020-03-27 09:32:14 +00:00
|
|
|
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}`;
|
2020-03-27 09:32:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-04-27 13:22:20 +00:00
|
|
|
message = `<i class="left material-icons">notifications</i>${message}`;
|
2020-03-27 09:32:14 +00:00
|
|
|
}
|
2020-01-17 09:42:02 +00:00
|
|
|
|
2020-04-27 13:22:20 +00:00
|
|
|
toast = M.toast({html: `<span>${message}</span>
|
2020-01-31 12:41:46 +00:00
|
|
|
<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-01-17 09:42:02 +00:00
|
|
|
if (toastActionElement) {
|
|
|
|
toastActionElement.addEventListener("click", function() {
|
|
|
|
toast.dismiss();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-17 09:49:04 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
2020-03-27 09:11:19 +00:00
|
|
|
// Disable all option elements with no value
|
|
|
|
for (let optionElement of document.querySelectorAll('option[value=""]')) {
|
|
|
|
optionElement.disabled = true;
|
|
|
|
}
|
2020-01-17 09:42:02 +00:00
|
|
|
M.AutoInit();
|
2020-01-31 12:41:46 +00:00
|
|
|
M.CharacterCounter.init(document.querySelectorAll('input[data-length][type="text"]'));
|
|
|
|
M.Dropdown.init(document.querySelectorAll("#nav-notifications, #nav-account"),
|
|
|
|
{alignment: "right", constrainWidth: false, coverTrigger: false});
|
2020-02-18 13:39:05 +00:00
|
|
|
nopaque.Forms.init();
|
|
|
|
nopaque.Navigation.init();
|
2020-03-26 16:32:26 +00:00
|
|
|
while (nopaque.flashedMessages.length) {
|
2020-04-27 13:22:20 +00:00
|
|
|
flashedMessage = nopaque.flashedMessages.shift();
|
|
|
|
nopaque.flash(flashedMessage[1], flashedMessage[0]);
|
2020-03-26 16:32:26 +00:00
|
|
|
}
|
|
|
|
if (nopaque.user.isAuthenticated) {
|
|
|
|
if (nopaque.user.settings.darkMode) {
|
|
|
|
DarkReader.enable({"brightness": 150, "contrast": 100, "sepia": 0});
|
|
|
|
}
|
|
|
|
nopaque.socket.init();
|
|
|
|
nopaque.socket.emit("user_ressources_init");
|
|
|
|
}
|
2020-01-17 09:42:02 +00:00
|
|
|
});
|