nopaque/app/static/js/nopaque.js
2020-03-30 17:27:24 +02:00

229 lines
7.6 KiB
JavaScript

/*
* The nopaque object is used as a namespace for nopaque specific functions and
* variables.
*/
var nopaque = {};
// nopaque ressources
nopaque.socket = undefined;
nopaque.corpora = undefined;
nopaque.corporaSubscribers = [];
nopaque.jobs = undefined;
nopaque.jobsSubscribers = [];
nopaque.foreignCorpora = undefined;
nopaque.foreignCorporaSubscribers = [];
nopaque.foreignJobs = undefined;
nopaque.foreignJobsSubscribers = [];
nopaque.user = {};
nopaque.user.isAuthenticated = undefined;
nopaque.user.settings = {}
nopaque.user.settings.darkMode = undefined;
nopaque.flashedMessages = undefined;
// nopaque functions
nopaque.socket = {};
nopaque.socket.init = function() {
nopaque.socket = io({transports: ['websocket']});
// 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);
nopaque.jobs = jsonpatch.apply_patch(nopaque.jobs, patch);
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);}
});
}
nopaque.Forms = {};
nopaque.Forms.init = function() {
var abortRequestElement, parentElement, progressElement, progressModal,
progressModalElement, request, submitElement;
for (let form of document.querySelectorAll(".nopaque-job-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, progressModalTitleElement;
formData = new FormData(form);
// Initialize progress modal
if (progressModalElement) {
progressModalTitleElement = progressModalElement.querySelector(".title");
progressModalTitleElement.innerText = formData.get("title");
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) {
for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) {
fieldElement = form.querySelector(`input[name="add-job-form-${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.Navigation = {};
nopaque.Navigation.init = function() {
/* ### Initialize sidenav-main ### */
for (let entry of document.querySelectorAll("#sidenav-main a")) {
if (entry.href === window.location.href) {
entry.parentNode.classList.add("active");
}
}
}
nopaque.flash = function() {
var classes, toast, toastActionElement;
switch (arguments.length) {
case 1:
category = "message";
message = arguments[0];
break;
case 2:
category = arguments[0];
message = arguments[1];
break;
default:
console.error("Usage: nopaque.flash(message) or nopaque.flash(category, message)")
}
switch (category) {
case "error":
classes = "red";
break;
default:
classes = "";
}
toast = M.toast({classes: classes,
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"]');
if (toastActionElement) {
toastActionElement.addEventListener("click", function() {
toast.dismiss();
});
}
}
document.addEventListener("DOMContentLoaded", function() {
// Disable all option elements with no value
for (let optionElement of document.querySelectorAll('option[value=""]')) {
optionElement.disabled = true;
}
M.AutoInit();
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});
nopaque.Forms.init();
nopaque.Navigation.init();
while (nopaque.flashedMessages.length) {
nopaque.flash(...nopaque.flashedMessages.shift());
}
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");
}
});