2020-10-10 17:40:26 +02:00
|
|
|
const sendBookmarkForm = (basePath, formElement) => {
|
|
|
|
const inputs = formElement
|
|
|
|
.querySelectorAll('input[type="text"], textarea, input[type="checkbox"], input[type="hidden"]');
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
[...inputs].forEach((input) => {
|
2021-01-19 11:55:50 +01:00
|
|
|
if (input.getAttribute('type') === 'checkbox') {
|
|
|
|
formData.append(input.getAttribute('name'), input.checked);
|
|
|
|
} else {
|
|
|
|
formData.append(input.getAttribute('name'), input.value);
|
|
|
|
}
|
2020-10-10 17:40:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', `${basePath}/admin/shaare`);
|
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status !== 200) {
|
|
|
|
alert(`An error occurred. Return code: ${xhr.status}`);
|
|
|
|
reject();
|
|
|
|
} else {
|
2020-10-23 12:53:49 +02:00
|
|
|
formElement.closest('.edit-link-container').remove();
|
2020-10-10 17:40:26 +02:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send(formData);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const sendBookmarkDelete = (buttonElement, formElement) => (
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
2021-01-19 12:44:48 +01:00
|
|
|
xhr.open('GET', `${buttonElement.href}&source=batch`);
|
2020-10-10 17:40:26 +02:00
|
|
|
xhr.onload = () => {
|
2021-01-19 12:44:48 +01:00
|
|
|
if (xhr.status !== 204) {
|
2020-10-10 17:40:26 +02:00
|
|
|
alert(`An error occurred. Return code: ${xhr.status}`);
|
|
|
|
reject();
|
|
|
|
} else {
|
2020-10-23 12:53:49 +02:00
|
|
|
formElement.closest('.edit-link-container').remove();
|
2020-10-10 17:40:26 +02:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const redirectIfEmptyBatch = (basePath, formElements, path) => {
|
|
|
|
if (formElements == null || formElements.length === 0) {
|
|
|
|
window.location.href = `${basePath}${path}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(() => {
|
|
|
|
const basePath = document.querySelector('input[name="js_base_path"]').value;
|
|
|
|
const getForms = () => document.querySelectorAll('form[name="linkform"]');
|
|
|
|
|
|
|
|
const cancelButtons = document.querySelectorAll('[name="cancel-batch-link"]');
|
|
|
|
if (cancelButtons != null) {
|
|
|
|
[...cancelButtons].forEach((cancelButton) => {
|
|
|
|
cancelButton.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.target.closest('form[name="linkform"]').remove();
|
|
|
|
redirectIfEmptyBatch(basePath, getForms(), '/admin/add-shaare');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const saveButtons = document.querySelectorAll('[name="save_edit"]');
|
|
|
|
if (saveButtons != null) {
|
|
|
|
[...saveButtons].forEach((saveButton) => {
|
|
|
|
saveButton.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const formElement = e.target.closest('form[name="linkform"]');
|
|
|
|
sendBookmarkForm(basePath, formElement)
|
|
|
|
.then(() => redirectIfEmptyBatch(basePath, getForms(), '/'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const saveAllButtons = document.querySelectorAll('[name="save_edit_batch"]');
|
|
|
|
if (saveAllButtons != null) {
|
|
|
|
[...saveAllButtons].forEach((saveAllButton) => {
|
|
|
|
saveAllButton.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2020-10-23 12:53:49 +02:00
|
|
|
const forms = [...getForms()];
|
|
|
|
const nbForm = forms.length;
|
|
|
|
let current = 0;
|
|
|
|
const progressBar = document.querySelector('.progressbar > div');
|
|
|
|
const progressBarCurrent = document.querySelector('.progressbar-current');
|
|
|
|
|
|
|
|
document.querySelector('.dark-layer').style.display = 'block';
|
|
|
|
document.querySelector('.progressbar-max').innerHTML = nbForm;
|
|
|
|
progressBarCurrent.innerHTML = current;
|
|
|
|
|
2020-10-10 17:40:26 +02:00
|
|
|
const promises = [];
|
2020-10-23 12:53:49 +02:00
|
|
|
forms.forEach((formElement) => {
|
|
|
|
promises.push(sendBookmarkForm(basePath, formElement).then(() => {
|
|
|
|
current += 1;
|
|
|
|
progressBar.style.width = `${(current * 100) / nbForm}%`;
|
|
|
|
progressBarCurrent.innerHTML = current;
|
|
|
|
}));
|
2020-10-10 17:40:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all(promises).then(() => {
|
2021-01-19 14:26:04 +01:00
|
|
|
window.location.href = `${basePath}/`;
|
2020-10-10 17:40:26 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteButtons = document.querySelectorAll('[name="delete_link"]');
|
|
|
|
if (deleteButtons != null) {
|
|
|
|
[...deleteButtons].forEach((deleteButton) => {
|
|
|
|
deleteButton.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const formElement = e.target.closest('form[name="linkform"]');
|
|
|
|
sendBookmarkDelete(e.target, formElement)
|
|
|
|
.then(() => redirectIfEmptyBatch(basePath, getForms(), '/'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|