progressbar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // code related to showing and updating progressbar shown as the image is being made
  2. function rememberGallerySelection() {
  3. }
  4. function getGallerySelectedIndex() {
  5. }
  6. function request(url, data, handler, errorHandler) {
  7. var xhr = new XMLHttpRequest();
  8. xhr.open("POST", url, true);
  9. xhr.setRequestHeader("Content-Type", "application/json");
  10. xhr.onreadystatechange = function() {
  11. if (xhr.readyState === 4) {
  12. if (xhr.status === 200) {
  13. try {
  14. var js = JSON.parse(xhr.responseText);
  15. handler(js);
  16. } catch (error) {
  17. console.error(error);
  18. errorHandler();
  19. }
  20. } else {
  21. errorHandler();
  22. }
  23. }
  24. };
  25. var js = JSON.stringify(data);
  26. xhr.send(js);
  27. }
  28. function pad2(x) {
  29. return x < 10 ? '0' + x : x;
  30. }
  31. function formatTime(secs) {
  32. if (secs > 3600) {
  33. return pad2(Math.floor(secs / 60 / 60)) + ":" + pad2(Math.floor(secs / 60) % 60) + ":" + pad2(Math.floor(secs) % 60);
  34. } else if (secs > 60) {
  35. return pad2(Math.floor(secs / 60)) + ":" + pad2(Math.floor(secs) % 60);
  36. } else {
  37. return Math.floor(secs) + "s";
  38. }
  39. }
  40. var originalAppTitle = undefined;
  41. onUiLoaded(function() {
  42. originalAppTitle = document.title;
  43. });
  44. function setTitle(progress) {
  45. var title = originalAppTitle;
  46. if (opts.show_progress_in_title && progress) {
  47. title = '[' + progress.trim() + '] ' + title;
  48. }
  49. if (document.title != title) {
  50. document.title = title;
  51. }
  52. }
  53. function randomId() {
  54. return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + ")";
  55. }
  56. // starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
  57. // preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
  58. // calls onProgress every time there is a progress update
  59. function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout = 40) {
  60. var dateStart = new Date();
  61. var wasEverActive = false;
  62. var parentProgressbar = progressbarContainer.parentNode;
  63. var divProgress = document.createElement('div');
  64. divProgress.className = 'progressDiv';
  65. divProgress.style.display = opts.show_progressbar ? "block" : "none";
  66. var divInner = document.createElement('div');
  67. divInner.className = 'progress';
  68. divProgress.appendChild(divInner);
  69. parentProgressbar.insertBefore(divProgress, progressbarContainer);
  70. var livePreview = null;
  71. var removeProgressBar = function() {
  72. if (!divProgress) return;
  73. setTitle("");
  74. parentProgressbar.removeChild(divProgress);
  75. if (gallery && livePreview) gallery.removeChild(livePreview);
  76. atEnd();
  77. divProgress = null;
  78. };
  79. var funProgress = function(id_task) {
  80. request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
  81. if (res.completed) {
  82. removeProgressBar();
  83. return;
  84. }
  85. let progressText = "";
  86. divInner.style.width = ((res.progress || 0) * 100.0) + '%';
  87. divInner.style.background = res.progress ? "" : "transparent";
  88. if (res.progress > 0) {
  89. progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
  90. }
  91. if (res.eta) {
  92. progressText += " ETA: " + formatTime(res.eta);
  93. }
  94. setTitle(progressText);
  95. if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
  96. progressText = res.textinfo + " " + progressText;
  97. }
  98. divInner.textContent = progressText;
  99. var elapsedFromStart = (new Date() - dateStart) / 1000;
  100. if (res.active) wasEverActive = true;
  101. if (!res.active && wasEverActive) {
  102. removeProgressBar();
  103. return;
  104. }
  105. if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
  106. removeProgressBar();
  107. return;
  108. }
  109. if (onProgress) {
  110. onProgress(res);
  111. }
  112. setTimeout(() => {
  113. funProgress(id_task, res.id_live_preview);
  114. }, opts.live_preview_refresh_period || 500);
  115. }, function() {
  116. removeProgressBar();
  117. });
  118. };
  119. var funLivePreview = function(id_task, id_live_preview) {
  120. request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
  121. if (!divProgress) {
  122. return;
  123. }
  124. if (res.live_preview && gallery) {
  125. var img = new Image();
  126. img.onload = function() {
  127. if (!livePreview) {
  128. livePreview = document.createElement('div');
  129. livePreview.className = 'livePreview';
  130. gallery.insertBefore(livePreview, gallery.firstElementChild);
  131. }
  132. livePreview.appendChild(img);
  133. if (livePreview.childElementCount > 2) {
  134. livePreview.removeChild(livePreview.firstElementChild);
  135. }
  136. };
  137. img.src = res.live_preview;
  138. }
  139. setTimeout(() => {
  140. funLivePreview(id_task, res.id_live_preview);
  141. }, opts.live_preview_refresh_period || 500);
  142. }, function() {
  143. removeProgressBar();
  144. });
  145. };
  146. funProgress(id_task, 0);
  147. if (gallery) {
  148. funLivePreview(id_task, 0);
  149. }
  150. }