ui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // various functions for interaction with ui.py not large enough to warrant putting them in separate files
  2. function set_theme(theme) {
  3. var gradioURL = window.location.href;
  4. if (!gradioURL.includes('?__theme=')) {
  5. window.location.replace(gradioURL + '?__theme=' + theme);
  6. }
  7. }
  8. function all_gallery_buttons() {
  9. var allGalleryButtons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small');
  10. var visibleGalleryButtons = [];
  11. allGalleryButtons.forEach(function(elem) {
  12. if (elem.parentElement.offsetParent) {
  13. visibleGalleryButtons.push(elem);
  14. }
  15. });
  16. return visibleGalleryButtons;
  17. }
  18. function selected_gallery_button() {
  19. return all_gallery_buttons().find(elem => elem.classList.contains('selected')) ?? null;
  20. }
  21. function selected_gallery_index() {
  22. return all_gallery_buttons().findIndex(elem => elem.classList.contains('selected'));
  23. }
  24. function gallery_container_buttons(gallery_container) {
  25. return gradioApp().querySelectorAll(`#${gallery_container} .thumbnail-item.thumbnail-small`);
  26. }
  27. function selected_gallery_index_id(gallery_container) {
  28. return Array.from(gallery_container_buttons(gallery_container)).findIndex(elem => elem.classList.contains('selected'));
  29. }
  30. function extract_image_from_gallery(gallery) {
  31. if (gallery.length == 0) {
  32. return [null];
  33. }
  34. if (gallery.length == 1) {
  35. return [gallery[0]];
  36. }
  37. var index = selected_gallery_index();
  38. if (index < 0 || index >= gallery.length) {
  39. // Use the first image in the gallery as the default
  40. index = 0;
  41. }
  42. return [gallery[index]];
  43. }
  44. window.args_to_array = Array.from; // Compatibility with e.g. extensions that may expect this to be around
  45. function switch_to_txt2img() {
  46. gradioApp().querySelector('#tabs').querySelectorAll('button')[0].click();
  47. return Array.from(arguments);
  48. }
  49. function switch_to_img2img_tab(no) {
  50. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  51. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[no].click();
  52. }
  53. function switch_to_img2img() {
  54. switch_to_img2img_tab(0);
  55. return Array.from(arguments);
  56. }
  57. function switch_to_sketch() {
  58. switch_to_img2img_tab(1);
  59. return Array.from(arguments);
  60. }
  61. function switch_to_inpaint() {
  62. switch_to_img2img_tab(2);
  63. return Array.from(arguments);
  64. }
  65. function switch_to_inpaint_sketch() {
  66. switch_to_img2img_tab(3);
  67. return Array.from(arguments);
  68. }
  69. function switch_to_extras() {
  70. gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click();
  71. return Array.from(arguments);
  72. }
  73. function get_tab_index(tabId) {
  74. let buttons = gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button');
  75. for (let i = 0; i < buttons.length; i++) {
  76. if (buttons[i].classList.contains('selected')) {
  77. return i;
  78. }
  79. }
  80. return 0;
  81. }
  82. function create_tab_index_args(tabId, args) {
  83. var res = Array.from(args);
  84. res[0] = get_tab_index(tabId);
  85. return res;
  86. }
  87. function get_img2img_tab_index() {
  88. let res = Array.from(arguments);
  89. res.splice(-2);
  90. res[0] = get_tab_index('mode_img2img');
  91. return res;
  92. }
  93. function create_submit_args(args) {
  94. var res = Array.from(args);
  95. // As it is currently, txt2img and img2img send back the previous output args (txt2img_gallery, generation_info, html_info) whenever you generate a new image.
  96. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  97. // I don't know why gradio is sending outputs along with inputs, but we can prevent sending the image gallery here, which seems to be an issue for some.
  98. // If gradio at some point stops sending outputs, this may break something
  99. if (Array.isArray(res[res.length - 3])) {
  100. res[res.length - 3] = null;
  101. }
  102. return res;
  103. }
  104. function setSubmitButtonsVisibility(tabname, showInterrupt, showSkip, showInterrupting) {
  105. gradioApp().getElementById(tabname + '_interrupt').style.display = showInterrupt ? "block" : "none";
  106. gradioApp().getElementById(tabname + '_skip').style.display = showSkip ? "block" : "none";
  107. gradioApp().getElementById(tabname + '_interrupting').style.display = showInterrupting ? "block" : "none";
  108. }
  109. function showSubmitButtons(tabname, show) {
  110. setSubmitButtonsVisibility(tabname, !show, !show, false);
  111. }
  112. function showSubmitInterruptingPlaceholder(tabname) {
  113. setSubmitButtonsVisibility(tabname, false, true, true);
  114. }
  115. function showRestoreProgressButton(tabname, show) {
  116. var button = gradioApp().getElementById(tabname + "_restore_progress");
  117. if (!button) return;
  118. button.style.setProperty('display', show ? 'flex' : 'none', 'important');
  119. }
  120. function submit() {
  121. showSubmitButtons('txt2img', false);
  122. var id = randomId();
  123. localSet("txt2img_task_id", id);
  124. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
  125. showSubmitButtons('txt2img', true);
  126. localRemove("txt2img_task_id");
  127. showRestoreProgressButton('txt2img', false);
  128. });
  129. var res = create_submit_args(arguments);
  130. res[0] = id;
  131. return res;
  132. }
  133. function submit_txt2img_upscale() {
  134. var res = submit(...arguments);
  135. res[2] = selected_gallery_index();
  136. return res;
  137. }
  138. function submit_img2img() {
  139. showSubmitButtons('img2img', false);
  140. var id = randomId();
  141. localSet("img2img_task_id", id);
  142. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
  143. showSubmitButtons('img2img', true);
  144. localRemove("img2img_task_id");
  145. showRestoreProgressButton('img2img', false);
  146. });
  147. var res = create_submit_args(arguments);
  148. res[0] = id;
  149. res[1] = get_tab_index('mode_img2img');
  150. return res;
  151. }
  152. function submit_extras() {
  153. showSubmitButtons('extras', false);
  154. var id = randomId();
  155. requestProgress(id, gradioApp().getElementById('extras_gallery_container'), gradioApp().getElementById('extras_gallery'), function() {
  156. showSubmitButtons('extras', true);
  157. });
  158. var res = create_submit_args(arguments);
  159. res[0] = id;
  160. console.log(res);
  161. return res;
  162. }
  163. function restoreProgressTxt2img() {
  164. showRestoreProgressButton("txt2img", false);
  165. var id = localGet("txt2img_task_id");
  166. if (id) {
  167. showSubmitInterruptingPlaceholder('txt2img');
  168. requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
  169. showSubmitButtons('txt2img', true);
  170. }, null, 0);
  171. }
  172. return id;
  173. }
  174. function restoreProgressImg2img() {
  175. showRestoreProgressButton("img2img", false);
  176. var id = localGet("img2img_task_id");
  177. if (id) {
  178. showSubmitInterruptingPlaceholder('img2img');
  179. requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
  180. showSubmitButtons('img2img', true);
  181. }, null, 0);
  182. }
  183. return id;
  184. }
  185. /**
  186. * Configure the width and height elements on `tabname` to accept
  187. * pasting of resolutions in the form of "width x height".
  188. */
  189. function setupResolutionPasting(tabname) {
  190. var width = gradioApp().querySelector(`#${tabname}_width input[type=number]`);
  191. var height = gradioApp().querySelector(`#${tabname}_height input[type=number]`);
  192. for (const el of [width, height]) {
  193. el.addEventListener('paste', function(event) {
  194. var pasteData = event.clipboardData.getData('text/plain');
  195. var parsed = pasteData.match(/^\s*(\d+)\D+(\d+)\s*$/);
  196. if (parsed) {
  197. width.value = parsed[1];
  198. height.value = parsed[2];
  199. updateInput(width);
  200. updateInput(height);
  201. event.preventDefault();
  202. }
  203. });
  204. }
  205. }
  206. onUiLoaded(function() {
  207. showRestoreProgressButton('txt2img', localGet("txt2img_task_id"));
  208. showRestoreProgressButton('img2img', localGet("img2img_task_id"));
  209. setupResolutionPasting('txt2img');
  210. setupResolutionPasting('img2img');
  211. });
  212. function modelmerger() {
  213. var id = randomId();
  214. requestProgress(id, gradioApp().getElementById('modelmerger_results_panel'), null, function() {});
  215. var res = create_submit_args(arguments);
  216. res[0] = id;
  217. return res;
  218. }
  219. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  220. var name_ = prompt('Style name:');
  221. return [name_, prompt_text, negative_prompt_text];
  222. }
  223. function confirm_clear_prompt(prompt, negative_prompt) {
  224. if (confirm("Delete prompt?")) {
  225. prompt = "";
  226. negative_prompt = "";
  227. }
  228. return [prompt, negative_prompt];
  229. }
  230. var opts = {};
  231. onAfterUiUpdate(function() {
  232. if (Object.keys(opts).length != 0) return;
  233. var json_elem = gradioApp().getElementById('settings_json');
  234. if (json_elem == null) return;
  235. var textarea = json_elem.querySelector('textarea');
  236. var jsdata = textarea.value;
  237. opts = JSON.parse(jsdata);
  238. executeCallbacks(optionsAvailableCallbacks); /*global optionsAvailableCallbacks*/
  239. executeCallbacks(optionsChangedCallbacks); /*global optionsChangedCallbacks*/
  240. Object.defineProperty(textarea, 'value', {
  241. set: function(newValue) {
  242. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  243. var oldValue = valueProp.get.call(textarea);
  244. valueProp.set.call(textarea, newValue);
  245. if (oldValue != newValue) {
  246. opts = JSON.parse(textarea.value);
  247. }
  248. executeCallbacks(optionsChangedCallbacks);
  249. },
  250. get: function() {
  251. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  252. return valueProp.get.call(textarea);
  253. }
  254. });
  255. json_elem.parentElement.style.display = "none";
  256. });
  257. onOptionsChanged(function() {
  258. var elem = gradioApp().getElementById('sd_checkpoint_hash');
  259. var sd_checkpoint_hash = opts.sd_checkpoint_hash || "";
  260. var shorthash = sd_checkpoint_hash.substring(0, 10);
  261. if (elem && elem.textContent != shorthash) {
  262. elem.textContent = shorthash;
  263. elem.title = sd_checkpoint_hash;
  264. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash;
  265. }
  266. });
  267. let txt2img_textarea, img2img_textarea = undefined;
  268. function restart_reload() {
  269. document.body.style.backgroundColor = "var(--background-fill-primary)";
  270. document.body.innerHTML = '<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  271. var requestPing = function() {
  272. requestGet("./internal/ping", {}, function(data) {
  273. location.reload();
  274. }, function() {
  275. setTimeout(requestPing, 500);
  276. });
  277. };
  278. setTimeout(requestPing, 2000);
  279. return [];
  280. }
  281. // Simulate an `input` DOM event for Gradio Textbox component. Needed after you edit its contents in javascript, otherwise your edits
  282. // will only visible on web page and not sent to python.
  283. function updateInput(target) {
  284. let e = new Event("input", {bubbles: true});
  285. Object.defineProperty(e, "target", {value: target});
  286. target.dispatchEvent(e);
  287. }
  288. var desiredCheckpointName = null;
  289. function selectCheckpoint(name) {
  290. desiredCheckpointName = name;
  291. gradioApp().getElementById('change_checkpoint').click();
  292. }
  293. function currentImg2imgSourceResolution(w, h, scaleBy) {
  294. var img = gradioApp().querySelector('#mode_img2img > div[style="display: block;"] img');
  295. return img ? [img.naturalWidth, img.naturalHeight, scaleBy] : [0, 0, scaleBy];
  296. }
  297. function updateImg2imgResizeToTextAfterChangingImage() {
  298. // At the time this is called from gradio, the image has no yet been replaced.
  299. // There may be a better solution, but this is simple and straightforward so I'm going with it.
  300. setTimeout(function() {
  301. gradioApp().getElementById('img2img_update_resize_to').click();
  302. }, 500);
  303. return [];
  304. }
  305. function setRandomSeed(elem_id) {
  306. var input = gradioApp().querySelector("#" + elem_id + " input");
  307. if (!input) return [];
  308. input.value = "-1";
  309. updateInput(input);
  310. return [];
  311. }
  312. function switchWidthHeight(tabname) {
  313. var width = gradioApp().querySelector("#" + tabname + "_width input[type=number]");
  314. var height = gradioApp().querySelector("#" + tabname + "_height input[type=number]");
  315. if (!width || !height) return [];
  316. var tmp = width.value;
  317. width.value = height.value;
  318. height.value = tmp;
  319. updateInput(width);
  320. updateInput(height);
  321. return [];
  322. }
  323. var onEditTimers = {};
  324. // calls func after afterMs milliseconds has passed since the input elem has been edited by user
  325. function onEdit(editId, elem, afterMs, func) {
  326. var edited = function() {
  327. var existingTimer = onEditTimers[editId];
  328. if (existingTimer) clearTimeout(existingTimer);
  329. onEditTimers[editId] = setTimeout(func, afterMs);
  330. };
  331. elem.addEventListener("input", edited);
  332. return edited;
  333. }