]>
Commit | Line | Data |
---|---|---|
28f26524 A |
1 | /** |
2 | * Script used in the thumbnails update page. | |
3 | * | |
4 | * It retrieves the list of link IDs to update, and execute AJAX requests | |
5 | * to update their thumbnails, while updating the progress bar. | |
6 | */ | |
7 | ||
8 | /** | |
9 | * Update the thumbnail of the link with the current i index in ids. | |
10 | * It contains a recursive call to retrieve the thumb of the next link when it succeed. | |
11 | * It also update the progress bar and other visual feedback elements. | |
12 | * | |
818b3193 | 13 | * @param {string} basePath Shaarli subfolder for XHR requests |
28f26524 A |
14 | * @param {array} ids List of LinkID to update |
15 | * @param {int} i Current index in ids | |
16 | * @param {object} elements List of DOM element to avoid retrieving them at each iteration | |
17 | */ | |
818b3193 | 18 | function updateThumb(basePath, ids, i, elements) { |
28f26524 | 19 | const xhr = new XMLHttpRequest(); |
6132d647 | 20 | xhr.open('PATCH', `${basePath}/admin/shaare/${ids[i]}/update-thumbnail`); |
28f26524 A |
21 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
22 | xhr.responseType = 'json'; | |
23 | xhr.onload = () => { | |
24 | if (xhr.status !== 200) { | |
25 | alert(`An error occurred. Return code: ${xhr.status}`); | |
26 | } else { | |
27 | const { response } = xhr; | |
28 | i += 1; | |
29 | elements.progressBar.style.width = `${(i * 100) / ids.length}%`; | |
30 | elements.current.innerHTML = i; | |
31 | elements.title.innerHTML = response.title; | |
32 | if (response.thumbnail !== false) { | |
6132d647 | 33 | elements.thumbnail.innerHTML = `<img src="${basePath}/${response.thumbnail}">`; |
28f26524 A |
34 | } |
35 | if (i < ids.length) { | |
764d34a7 | 36 | updateThumb(basePath, ids, i, elements); |
28f26524 A |
37 | } |
38 | } | |
39 | }; | |
6132d647 | 40 | xhr.send(); |
28f26524 A |
41 | } |
42 | ||
43 | (() => { | |
818b3193 | 44 | const basePath = document.querySelector('input[name="js_base_path"]').value; |
28f26524 A |
45 | const ids = document.getElementsByName('ids')[0].value.split(','); |
46 | const elements = { | |
47 | progressBar: document.querySelector('.progressbar > div'), | |
48 | current: document.querySelector('.progress-current'), | |
49 | thumbnail: document.querySelector('.thumbnail-placeholder'), | |
50 | title: document.querySelector('.thumbnail-link-title'), | |
51 | }; | |
818b3193 | 52 | updateThumb(basePath, ids, 0, elements); |
28f26524 | 53 | })(); |