]>
Commit | Line | Data |
---|---|---|
5d8de758 A |
1 | const sendBookmarkForm = (basePath, formElement) => { |
2 | const inputs = formElement | |
3 | .querySelectorAll('input[type="text"], textarea, input[type="checkbox"], input[type="hidden"]'); | |
4 | ||
5 | const formData = new FormData(); | |
6 | [...inputs].forEach((input) => { | |
47ac77ad A |
7 | if (input.getAttribute('type') === 'checkbox') { |
8 | formData.append(input.getAttribute('name'), input.checked); | |
9 | } else { | |
10 | formData.append(input.getAttribute('name'), input.value); | |
11 | } | |
5d8de758 A |
12 | }); |
13 | ||
14 | return new Promise((resolve, reject) => { | |
15 | const xhr = new XMLHttpRequest(); | |
16 | xhr.open('POST', `${basePath}/admin/shaare`); | |
17 | xhr.onload = () => { | |
18 | if (xhr.status !== 200) { | |
19 | alert(`An error occurred. Return code: ${xhr.status}`); | |
20 | reject(); | |
21 | } else { | |
6a716758 | 22 | formElement.closest('.edit-link-container').remove(); |
5d8de758 A |
23 | resolve(); |
24 | } | |
25 | }; | |
26 | xhr.send(formData); | |
27 | }); | |
28 | }; | |
29 | ||
30 | const sendBookmarkDelete = (buttonElement, formElement) => ( | |
31 | new Promise((resolve, reject) => { | |
32 | const xhr = new XMLHttpRequest(); | |
93175b6e | 33 | xhr.open('GET', `${buttonElement.href}&source=batch`); |
5d8de758 | 34 | xhr.onload = () => { |
93175b6e | 35 | if (xhr.status !== 204) { |
5d8de758 A |
36 | alert(`An error occurred. Return code: ${xhr.status}`); |
37 | reject(); | |
38 | } else { | |
6a716758 | 39 | formElement.closest('.edit-link-container').remove(); |
5d8de758 A |
40 | resolve(); |
41 | } | |
42 | }; | |
43 | xhr.send(); | |
44 | }) | |
45 | ); | |
46 | ||
47 | const redirectIfEmptyBatch = (basePath, formElements, path) => { | |
48 | if (formElements == null || formElements.length === 0) { | |
49 | window.location.href = `${basePath}${path}`; | |
50 | } | |
51 | }; | |
52 | ||
53 | (() => { | |
54 | const basePath = document.querySelector('input[name="js_base_path"]').value; | |
55 | const getForms = () => document.querySelectorAll('form[name="linkform"]'); | |
56 | ||
57 | const cancelButtons = document.querySelectorAll('[name="cancel-batch-link"]'); | |
58 | if (cancelButtons != null) { | |
59 | [...cancelButtons].forEach((cancelButton) => { | |
60 | cancelButton.addEventListener('click', (e) => { | |
61 | e.preventDefault(); | |
62 | e.target.closest('form[name="linkform"]').remove(); | |
63 | redirectIfEmptyBatch(basePath, getForms(), '/admin/add-shaare'); | |
64 | }); | |
65 | }); | |
66 | } | |
67 | ||
68 | const saveButtons = document.querySelectorAll('[name="save_edit"]'); | |
69 | if (saveButtons != null) { | |
70 | [...saveButtons].forEach((saveButton) => { | |
71 | saveButton.addEventListener('click', (e) => { | |
72 | e.preventDefault(); | |
73 | ||
74 | const formElement = e.target.closest('form[name="linkform"]'); | |
75 | sendBookmarkForm(basePath, formElement) | |
76 | .then(() => redirectIfEmptyBatch(basePath, getForms(), '/')); | |
77 | }); | |
78 | }); | |
79 | } | |
80 | ||
81 | const saveAllButtons = document.querySelectorAll('[name="save_edit_batch"]'); | |
82 | if (saveAllButtons != null) { | |
83 | [...saveAllButtons].forEach((saveAllButton) => { | |
84 | saveAllButton.addEventListener('click', (e) => { | |
85 | e.preventDefault(); | |
86 | ||
6a716758 A |
87 | const forms = [...getForms()]; |
88 | const nbForm = forms.length; | |
89 | let current = 0; | |
90 | const progressBar = document.querySelector('.progressbar > div'); | |
91 | const progressBarCurrent = document.querySelector('.progressbar-current'); | |
92 | ||
93 | document.querySelector('.dark-layer').style.display = 'block'; | |
94 | document.querySelector('.progressbar-max').innerHTML = nbForm; | |
95 | progressBarCurrent.innerHTML = current; | |
96 | ||
5d8de758 | 97 | const promises = []; |
6a716758 A |
98 | forms.forEach((formElement) => { |
99 | promises.push(sendBookmarkForm(basePath, formElement).then(() => { | |
100 | current += 1; | |
101 | progressBar.style.width = `${(current * 100) / nbForm}%`; | |
102 | progressBarCurrent.innerHTML = current; | |
103 | })); | |
5d8de758 A |
104 | }); |
105 | ||
106 | Promise.all(promises).then(() => { | |
3d6278e8 | 107 | window.location.href = `${basePath}/`; |
5d8de758 A |
108 | }); |
109 | }); | |
110 | }); | |
111 | } | |
112 | ||
113 | const deleteButtons = document.querySelectorAll('[name="delete_link"]'); | |
114 | if (deleteButtons != null) { | |
115 | [...deleteButtons].forEach((deleteButton) => { | |
116 | deleteButton.addEventListener('click', (e) => { | |
117 | e.preventDefault(); | |
118 | ||
119 | const formElement = e.target.closest('form[name="linkform"]'); | |
120 | sendBookmarkDelete(e.target, formElement) | |
121 | .then(() => redirectIfEmptyBatch(basePath, getForms(), '/')); | |
122 | }); | |
123 | }); | |
124 | } | |
125 | })(); |