progressbar.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. function setTitle(progress) {
  41. var title = 'Stable Diffusion';
  42. if (opts.show_progress_in_title && progress) {
  43. title = '[' + progress.trim() + '] ' + title;
  44. }
  45. if (document.title != title) {
  46. document.title = title;
  47. }
  48. }
  49. function randomId() {
  50. return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + ")";
  51. }
  52. // starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
  53. // preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
  54. // calls onProgress every time there is a progress update
  55. function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout = 40) {
  56. var dateStart = new Date();
  57. var wasEverActive = false;
  58. var parentProgressbar = progressbarContainer.parentNode;
  59. var divProgress = document.createElement('div');
  60. divProgress.className = 'progressDiv';
  61. divProgress.style.display = opts.show_progressbar ? "block" : "none";
  62. var divInner = document.createElement('div');
  63. divInner.className = 'progress';
  64. divProgress.appendChild(divInner);
  65. parentProgressbar.insertBefore(divProgress, progressbarContainer);
  66. var livePreview = null;
  67. var removeProgressBar = function() {
  68. if (!divProgress) return;
  69. setTitle("");
  70. parentProgressbar.removeChild(divProgress);
  71. if (gallery && livePreview) gallery.removeChild(livePreview);
  72. atEnd();
  73. divProgress = null;
  74. };
  75. var funProgress = function(id_task) {
  76. request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
  77. if (res.completed) {
  78. removeProgressBar();
  79. return;
  80. }
  81. let progressText = "";
  82. divInner.style.width = ((res.progress || 0) * 100.0) + '%';
  83. divInner.style.background = res.progress ? "" : "transparent";
  84. if (res.progress > 0) {
  85. progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
  86. }
  87. if (res.eta) {
  88. progressText += " ETA: " + formatTime(res.eta);
  89. }
  90. setTitle(progressText);
  91. if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
  92. progressText = res.textinfo + " " + progressText;
  93. }
  94. divInner.textContent = progressText;
  95. var elapsedFromStart = (new Date() - dateStart) / 1000;
  96. if (res.active) wasEverActive = true;
  97. if (!res.active && wasEverActive) {
  98. removeProgressBar();
  99. return;
  100. }
  101. if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
  102. removeProgressBar();
  103. return;
  104. }
  105. if (onProgress) {
  106. onProgress(res);
  107. }
  108. setTimeout(() => {
  109. funProgress(id_task, res.id_live_preview);
  110. }, opts.live_preview_refresh_period || 500);
  111. }, function() {
  112. removeProgressBar();
  113. });
  114. };
  115. var funLivePreview = function(id_task, id_live_preview) {
  116. request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
  117. if (!divProgress) {
  118. return;
  119. }
  120. if (res.live_preview && gallery) {
  121. var img = new Image();
  122. img.onload = function() {
  123. if (!livePreview) {
  124. livePreview = document.createElement('div');
  125. livePreview.className = 'livePreview';
  126. gallery.insertBefore(livePreview, gallery.firstElementChild);
  127. }
  128. livePreview.appendChild(img);
  129. if (livePreview.childElementCount > 2) {
  130. livePreview.removeChild(livePreview.firstElementChild);
  131. }
  132. };
  133. img.src = res.live_preview;
  134. }
  135. setTimeout(() => {
  136. funLivePreview(id_task, res.id_live_preview);
  137. }, opts.live_preview_refresh_period || 500);
  138. }, function() {
  139. removeProgressBar();
  140. });
  141. };
  142. funProgress(id_task, 0);
  143. if (gallery) {
  144. funLivePreview(id_task, 0);
  145. }
  146. }