]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - assets/common/js/shaare-batch.js
1 const sendBookmarkForm
= (basePath
, formElement
) => {
2 const inputs
= formElement
3 .querySelectorAll('input[type="text"], textarea, input[type="checkbox"], input[type="hidden"]');
5 const formData
= new FormData();
6 [...inputs
].forEach((input
) => {
7 formData
.append(input
.getAttribute('name'), input
.value
);
10 return new Promise((resolve
, reject
) => {
11 const xhr
= new XMLHttpRequest();
12 xhr
.open('POST', `${basePath}/admin/shaare`);
14 if (xhr
.status
!== 200) {
15 alert(`An error occurred. Return code: ${xhr.status}`);
26 const sendBookmarkDelete
= (buttonElement
, formElement
) => (
27 new Promise((resolve
, reject
) => {
28 const xhr
= new XMLHttpRequest();
29 xhr
.open('GET', buttonElement
.href
);
31 if (xhr
.status
!== 200) {
32 alert(`An error occurred. Return code: ${xhr.status}`);
43 const redirectIfEmptyBatch
= (basePath
, formElements
, path
) => {
44 if (formElements
== null || formElements
.length
=== 0) {
45 window
.location
.href
= `${basePath}${path}`;
50 const basePath
= document
.querySelector('input[name="js_base_path"]').value
;
51 const getForms
= () => document
.querySelectorAll('form[name="linkform"]');
53 const cancelButtons
= document
.querySelectorAll('[name="cancel-batch-link"]');
54 if (cancelButtons
!= null) {
55 [...cancelButtons
].forEach((cancelButton
) => {
56 cancelButton
.addEventListener('click', (e
) => {
58 e
.target
.closest('form[name="linkform"]').remove();
59 redirectIfEmptyBatch(basePath
, getForms(), '/admin/add-shaare');
64 const saveButtons
= document
.querySelectorAll('[name="save_edit"]');
65 if (saveButtons
!= null) {
66 [...saveButtons
].forEach((saveButton
) => {
67 saveButton
.addEventListener('click', (e
) => {
70 const formElement
= e
.target
.closest('form[name="linkform"]');
71 sendBookmarkForm(basePath
, formElement
)
72 .then(() => redirectIfEmptyBatch(basePath
, getForms(), '/'));
77 const saveAllButtons
= document
.querySelectorAll('[name="save_edit_batch"]');
78 if (saveAllButtons
!= null) {
79 [...saveAllButtons
].forEach((saveAllButton
) => {
80 saveAllButton
.addEventListener('click', (e
) => {
84 [...getForms()].forEach((formElement
) => {
85 promises
.push(sendBookmarkForm(basePath
, formElement
));
88 Promise
.all(promises
).then(() => {
89 window
.location
.href
= basePath
|| '/';
95 const deleteButtons
= document
.querySelectorAll('[name="delete_link"]');
96 if (deleteButtons
!= null) {
97 [...deleteButtons
].forEach((deleteButton
) => {
98 deleteButton
.addEventListener('click', (e
) => {
101 const formElement
= e
.target
.closest('form[name="linkform"]');
102 sendBookmarkDelete(e
.target
, formElement
)
103 .then(() => redirectIfEmptyBatch(basePath
, getForms(), '/'));