progressbar.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 parentGallery = gallery ? gallery.parentNode : null;
  60. var divProgress = document.createElement('div');
  61. divProgress.className = 'progressDiv';
  62. divProgress.style.display = opts.show_progressbar ? "block" : "none";
  63. var divInner = document.createElement('div');
  64. divInner.className = 'progress';
  65. divProgress.appendChild(divInner);
  66. parentProgressbar.insertBefore(divProgress, progressbarContainer);
  67. if (parentGallery) {
  68. var livePreview = document.createElement('div');
  69. livePreview.className = 'livePreview';
  70. parentGallery.insertBefore(livePreview, gallery);
  71. }
  72. var removeProgressBar = function() {
  73. setTitle("");
  74. parentProgressbar.removeChild(divProgress);
  75. if (parentGallery) parentGallery.removeChild(livePreview);
  76. atEnd();
  77. };
  78. var fun = function(id_task, id_live_preview) {
  79. request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
  80. if (res.completed) {
  81. removeProgressBar();
  82. return;
  83. }
  84. var rect = progressbarContainer.getBoundingClientRect();
  85. if (rect.width) {
  86. divProgress.style.width = rect.width + "px";
  87. }
  88. let progressText = "";
  89. divInner.style.width = ((res.progress || 0) * 100.0) + '%';
  90. divInner.style.background = res.progress ? "" : "transparent";
  91. if (res.progress > 0) {
  92. progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
  93. }
  94. if (res.eta) {
  95. progressText += " ETA: " + formatTime(res.eta);
  96. }
  97. setTitle(progressText);
  98. if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
  99. progressText = res.textinfo + " " + progressText;
  100. }
  101. divInner.textContent = progressText;
  102. var elapsedFromStart = (new Date() - dateStart) / 1000;
  103. if (res.active) wasEverActive = true;
  104. if (!res.active && wasEverActive) {
  105. removeProgressBar();
  106. return;
  107. }
  108. if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
  109. removeProgressBar();
  110. return;
  111. }
  112. if (res.live_preview && gallery) {
  113. rect = gallery.getBoundingClientRect();
  114. if (rect.width) {
  115. livePreview.style.width = rect.width + "px";
  116. livePreview.style.height = rect.height + "px";
  117. }
  118. var img = new Image();
  119. img.onload = function() {
  120. livePreview.appendChild(img);
  121. if (livePreview.childElementCount > 2) {
  122. livePreview.removeChild(livePreview.firstElementChild);
  123. }
  124. };
  125. img.src = res.live_preview;
  126. }
  127. if (onProgress) {
  128. onProgress(res);
  129. }
  130. setTimeout(() => {
  131. fun(id_task, res.id_live_preview);
  132. }, opts.live_preview_refresh_period || 500);
  133. }, function() {
  134. removeProgressBar();
  135. });
  136. };
  137. fun(id_task, 0);
  138. }