thumbnails-update.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * Update the thumbnail of the link with the current i index in ids.
  9. * It contains a recursive call to retrieve the thumb of the next link when it succeed.
  10. * It also update the progress bar and other visual feedback elements.
  11. *
  12. * @param {array} ids List of LinkID to update
  13. * @param {int} i Current index in ids
  14. * @param {object} elements List of DOM element to avoid retrieving them at each iteration
  15. */
  16. function updateThumb(ids, i, elements) {
  17. const xhr = new XMLHttpRequest();
  18. xhr.open('POST', '?do=ajax_thumb_update');
  19. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  20. xhr.responseType = 'json';
  21. xhr.onload = () => {
  22. if (xhr.status !== 200) {
  23. alert(`An error occurred. Return code: ${xhr.status}`);
  24. } else {
  25. const { response } = xhr;
  26. i += 1;
  27. elements.progressBar.style.width = `${(i * 100) / ids.length}%`;
  28. elements.current.innerHTML = i;
  29. elements.title.innerHTML = response.title;
  30. if (response.thumbnail !== false) {
  31. elements.thumbnail.innerHTML = `<img src="${response.thumbnail}">`;
  32. }
  33. if (i < ids.length) {
  34. updateThumb(ids, i, elements);
  35. }
  36. }
  37. };
  38. xhr.send(`id=${ids[i]}`);
  39. }
  40. (() => {
  41. const ids = document.getElementsByName('ids')[0].value.split(',');
  42. const elements = {
  43. progressBar: document.querySelector('.progressbar > div'),
  44. current: document.querySelector('.progress-current'),
  45. thumbnail: document.querySelector('.thumbnail-placeholder'),
  46. title: document.querySelector('.thumbnail-link-title'),
  47. };
  48. updateThumb(ids, 0, elements);
  49. })();