update nopaque forms

This commit is contained in:
Patrick Jentsch 2021-12-02 13:48:48 +01:00
parent 962e58f2d3
commit d8f11a9759
4 changed files with 140 additions and 87 deletions

View File

@ -5,72 +5,151 @@
var nopaque = {}; var nopaque = {};
nopaque.Forms = {}; nopaque.Forms = {};
nopaque.Forms.init = function() { nopaque.Forms.autoInit = () => {
var abortRequestElement, parentElement, progressElement, progressModal, const nopaqueSubmitForms = document.querySelectorAll('.nopaque-submit-form');
progressModalElement, request, submitElement; let nopaqueSubmitForm;
for (let form of document.querySelectorAll(".nopaque-submit-form")) { for (nopaqueSubmitForm of nopaqueSubmitForms) {
submitElement = form.querySelector('button[type="submit"]'); nopaqueSubmitForm.addEventListener('submit', (event) => {
submitElement.addEventListener("click", function() { event.preventDefault();
for (let selectElement of form.querySelectorAll('select')) {
if (selectElement.value === "") { const request = new XMLHttpRequest();
parentElement = selectElement.closest(".input-field"); const selectElements = nopaqueSubmitForm.querySelectorAll('select');
parentElement.querySelector(".select-dropdown").classList.add("invalid"); let abortElement;
for (let helperTextElement of parentElement.querySelectorAll(".helper-text")) { let helperTextElement;
let helperTextElements;
let inputFieldElement;
let modal;
let modalElement;
let progressElement;
let selectElement;
let tmp;
// Check if select elements are filled out properly
for (selectElement of selectElements) {
if (selectElement.value === '') {
inputFieldElement = selectElement.closest('.input-field');
inputFieldElement.querySelector('.select-dropdown').classList.add('invalid');
helperTextElements = inputFieldElement.querySelectorAll('.helper-text');
for (helperTextElement of helperTextElements) {
helperTextElement.remove(); helperTextElement.remove();
} }
parentElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">Please select an option.</span>`); inputFieldElement.insertAdjacentHTML(
'beforeend',
'<span class="helper-text error-color-text">Please select an option.</span>'
);
return;
} }
} }
});
request = new XMLHttpRequest(); // Setup modal
if (form.dataset.hasOwnProperty("progressModal")) { tmp = document.createElement('div');
progressModalElement = document.getElementById(form.dataset.progressModal); tmp.innerHTML = `
progressModal = M.Modal.getInstance(progressModalElement); <div class="modal">
progressModal.options.dismissible = false; <div class="modal-content">
abortRequestElement = progressModalElement.querySelector(".abort-request"); <h4><i class="material-icons left">file_upload</i>Uploading files...</h4>
abortRequestElement.addEventListener("click", function() {request.abort();}); <div class="progress">
progressElement = progressModalElement.querySelector(".determinate"); <div class="determinate" style="width: 0%"></div>
} </div>
form.addEventListener("submit", function(event) { </div>
event.preventDefault(); <div class="modal-footer">
var formData; <a href="#!" class="btn red waves-effect waves-light abort">Cancel</a>
</div>
formData = new FormData(form); </div>
// Initialize progress modal `.trim();
if (progressModalElement) { modalElement = document.querySelector('#modals').appendChild(tmp.firstChild);
progressElement.style.width = "0%"; modal = M.Modal.init(
progressModal.open(); modalElement,
} {
request.open("POST", window.location.href); dismissible: false,
request.send(formData); onCloseEnd: () => {
}); modal.destroy();
request.addEventListener("load", function(event) { modalElement.remove();
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$="${field}"]`).closest(".input-field");
for (let error of errors) {
fieldElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">${error}</span>`);
} }
} }
if (progressModalElement) { );
progressModal.close(); modal.open();
}
} // Setup abort handling
if (request.status === 500) { abortElement = modalElement.querySelector('.abort');
location.reload(); abortElement.addEventListener('click', event => {request.abort();});
} request.addEventListener('abort', event => {
}); request.abort();
if (progressModalElement) { modal.close();
request.upload.addEventListener("progress", function(event) {
progressElement.style.width = Math.floor(100 * event.loaded / event.total).toString() + "%";
}); });
// Setup load handling (after the request completed)
request.addEventListener('load', event => {
const response = JSON.parse(request.responseText);
let inputError;
let inputErrors;
let inputFieldElement;
let inputName;
if (request.status === 201) {
window.location.href = response.redirect_url;
}
if (request.status === 400) {
for ([inputName, inputErrors] of Object.entries(response)) {
inputFieldElement = nopaqueSubmitForm.querySelector(`input[name="${inputName}"], select[name="${inputName}"]`).closest('.input-field');
for (inputError of inputErrors) {
inputFieldElement.insertAdjacentHTML(
'beforeend',
`<span class="helper-text red-text">${inputError}</span>`
);
}
}
}
if (request.status === 500) {
location.reload();
}
modal.close();
});
// Setup progress handling
progressElement = modalElement.querySelector('.progress > .determinate');
request.upload.addEventListener('progress', event => {
const progress = Math.floor(100 * event.loaded / event.total);
progressElement.style.width = `${progress}%`;
});
request.open('POST', window.location.href);
request.send(new FormData(nopaqueSubmitForm));
});
}
}
nopaque.RessourceList = {};
nopaque.RessourceList.autoInit = () => {
const nopaqueRessourceListElements = document.querySelectorAll('.nopaque-ressource-list[data-ressource-type]:not(.no-autoinit)');
let nopaqueRessourceListElement;
for (nopaqueRessourceListElement of nopaqueRessourceListElements) {
switch (nopaqueRessourceListElement.dataset.ressourceType) {
case 'Corpus':
new CorpusList(nopaqueRessourceListElement);
break;
case 'CorpusFile':
new CorpusFileList(nopaqueRessourceListElement);
break;
case 'Job':
new JobList(nopaqueRessourceListElement);
break;
case 'JobInput':
new JobInputList(nopaqueRessourceListElement);
break;
case 'JobResult':
new JobResultList(nopaqueRessourceListElement);
break;
case 'QueryResult':
new QueryResultList(nopaqueRessourceListElement);
break;
case 'User':
new UserList(nopaqueRessourceListElement);
break;
default:
break;
} }
} }
} }

View File

@ -40,33 +40,7 @@
app.addEventListener('users.patch', patch => jobStatusNotifier.usersPatchHandler(patch)); app.addEventListener('users.patch', patch => jobStatusNotifier.usersPatchHandler(patch));
app.getUserById(currentUserId).then(user => {}, error => {throw JSON.stringify(error)}); app.getUserById(currentUserId).then(user => {}, error => {throw JSON.stringify(error)});
{% endif %} {% endif %}
nopaque.Forms.init(); nopaque.Forms.autoInit();
for (let nopaqueRessourceListElement of document.querySelectorAll('.nopaque-ressource-list[data-ressource-type]:not(.no-autoinit)')) { nopaque.RessourceList.autoInit();
switch (nopaqueRessourceListElement.dataset.ressourceType) {
case 'Corpus':
new CorpusList(nopaqueRessourceListElement);
break;
case 'CorpusFile':
new CorpusFileList(nopaqueRessourceListElement);
break;
case 'Job':
new JobList(nopaqueRessourceListElement);
break;
case 'JobInput':
new JobInputList(nopaqueRessourceListElement);
break;
case 'JobResult':
new JobResultList(nopaqueRessourceListElement);
break;
case 'QueryResult':
new QueryResultList(nopaqueRessourceListElement);
break;
case 'User':
new UserList(nopaqueRessourceListElement);
break;
default:
break;
}
}
for (let flashedMessage of {{ get_flashed_messages(with_categories=True)|tojson }}) {app.flash(flashedMessage[1], flashedMessage[0]);} for (let flashedMessage of {{ get_flashed_messages(with_categories=True)|tojson }}) {app.flash(flashedMessage[1], flashedMessage[0]);}
</script> </script>

View File

@ -127,7 +127,7 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
var versionField = document.querySelector('#add-nlp-job-form-version'); let versionField = document.querySelector('#add-job-form-version');
versionField.addEventListener('change', (event) => { versionField.addEventListener('change', (event) => {
let url = new URL(window.location.href); let url = new URL(window.location.href);
url.search = `?version=${event.target.value}`; url.search = `?version=${event.target.value}`;

View File

@ -154,7 +154,7 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
var versionField = document.querySelector('#add-ocr-job-form-version'); let versionField = document.querySelector('#add-job-form-version');
versionField.addEventListener('change', (event) => { versionField.addEventListener('change', (event) => {
let url = new URL(window.location.href); let url = new URL(window.location.href);
url.search = `?version=${event.target.value}`; url.search = `?version=${event.target.value}`;