ui.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // various functions for interaction with ui.py not large enough to warrant putting them in separate files
  2. function set_theme(theme){
  3. gradioURL = window.location.href
  4. if (!gradioURL.includes('?__theme=')) {
  5. window.location.replace(gradioURL + '?__theme=' + theme);
  6. }
  7. }
  8. function selected_gallery_index(){
  9. var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item')
  10. var button = gradioApp().querySelector('[style="display: block;"].tabitem div[id$=_gallery] .gallery-item.\\!ring-2')
  11. var result = -1
  12. buttons.forEach(function(v, i){ if(v==button) { result = i } })
  13. return result
  14. }
  15. function extract_image_from_gallery(gallery){
  16. if(gallery.length == 1){
  17. return [gallery[0]]
  18. }
  19. index = selected_gallery_index()
  20. if (index < 0 || index >= gallery.length){
  21. return [null]
  22. }
  23. return [gallery[index]];
  24. }
  25. function args_to_array(args){
  26. res = []
  27. for(var i=0;i<args.length;i++){
  28. res.push(args[i])
  29. }
  30. return res
  31. }
  32. function switch_to_txt2img(){
  33. gradioApp().querySelector('#tabs').querySelectorAll('button')[0].click();
  34. return args_to_array(arguments);
  35. }
  36. function switch_to_img2img(){
  37. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  38. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[0].click();
  39. return args_to_array(arguments);
  40. }
  41. function switch_to_inpaint(){
  42. gradioApp().querySelector('#tabs').querySelectorAll('button')[1].click();
  43. gradioApp().getElementById('mode_img2img').querySelectorAll('button')[2].click();
  44. return args_to_array(arguments);
  45. }
  46. function switch_to_extras(){
  47. gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click();
  48. return args_to_array(arguments);
  49. }
  50. function get_tab_index(tabId){
  51. var res = 0
  52. gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i){
  53. if(button.className.indexOf('bg-white') != -1)
  54. res = i
  55. })
  56. return res
  57. }
  58. function create_tab_index_args(tabId, args){
  59. var res = []
  60. for(var i=0; i<args.length; i++){
  61. res.push(args[i])
  62. }
  63. res[0] = get_tab_index(tabId)
  64. return res
  65. }
  66. function get_extras_tab_index(){
  67. const [,,...args] = [...arguments]
  68. return [get_tab_index('mode_extras'), get_tab_index('extras_resize_mode'), ...args]
  69. }
  70. function create_submit_args(args){
  71. res = []
  72. for(var i=0;i<args.length;i++){
  73. res.push(args[i])
  74. }
  75. // 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.
  76. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate.
  77. // 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.
  78. // If gradio at some point stops sending outputs, this may break something
  79. if(Array.isArray(res[res.length - 3])){
  80. res[res.length - 3] = null
  81. }
  82. return res
  83. }
  84. function submit(){
  85. requestProgress('txt2img')
  86. return create_submit_args(arguments)
  87. }
  88. function submit_img2img(){
  89. requestProgress('img2img')
  90. res = create_submit_args(arguments)
  91. res[0] = get_tab_index('mode_img2img')
  92. return res
  93. }
  94. function ask_for_style_name(_, prompt_text, negative_prompt_text) {
  95. name_ = prompt('Style name:')
  96. return [name_, prompt_text, negative_prompt_text]
  97. }
  98. function confirm_clear_prompt(prompt, negative_prompt) {
  99. if(confirm("Delete prompt?")) {
  100. prompt = ""
  101. negative_prompt = ""
  102. }
  103. return [prompt, negative_prompt]
  104. }
  105. opts = {}
  106. onUiUpdate(function(){
  107. if(Object.keys(opts).length != 0) return;
  108. json_elem = gradioApp().getElementById('settings_json')
  109. if(json_elem == null) return;
  110. textarea = json_elem.querySelector('textarea')
  111. jsdata = textarea.value
  112. opts = JSON.parse(jsdata)
  113. executeCallbacks(optionsChangedCallbacks);
  114. Object.defineProperty(textarea, 'value', {
  115. set: function(newValue) {
  116. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  117. var oldValue = valueProp.get.call(textarea);
  118. valueProp.set.call(textarea, newValue);
  119. if (oldValue != newValue) {
  120. opts = JSON.parse(textarea.value)
  121. }
  122. executeCallbacks(optionsChangedCallbacks);
  123. },
  124. get: function() {
  125. var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
  126. return valueProp.get.call(textarea);
  127. }
  128. });
  129. json_elem.parentElement.style.display="none"
  130. if (!txt2img_textarea) {
  131. txt2img_textarea = gradioApp().querySelector("#txt2img_prompt > label > textarea");
  132. txt2img_textarea?.addEventListener("input", () => update_token_counter("txt2img_token_button"));
  133. }
  134. if (!img2img_textarea) {
  135. img2img_textarea = gradioApp().querySelector("#img2img_prompt > label > textarea");
  136. img2img_textarea?.addEventListener("input", () => update_token_counter("img2img_token_button"));
  137. }
  138. show_all_pages = gradioApp().getElementById('settings_show_all_pages')
  139. settings_tabs = gradioApp().querySelector('#settings div')
  140. if(show_all_pages && settings_tabs){
  141. settings_tabs.appendChild(show_all_pages)
  142. show_all_pages.onclick = function(){
  143. gradioApp().querySelectorAll('#settings > div').forEach(function(elem){
  144. elem.style.display = "block";
  145. })
  146. }
  147. }
  148. })
  149. onOptionsChanged(function(){
  150. elem = gradioApp().getElementById('sd_checkpoint_hash')
  151. sd_checkpoint_hash = opts.sd_checkpoint_hash || ""
  152. shorthash = sd_checkpoint_hash.substr(0,10)
  153. if(elem && elem.textContent != shorthash){
  154. elem.textContent = shorthash
  155. elem.title = sd_checkpoint_hash
  156. elem.href = "https://google.com/search?q=" + sd_checkpoint_hash
  157. }
  158. })
  159. let txt2img_textarea, img2img_textarea = undefined;
  160. let wait_time = 800
  161. let token_timeout;
  162. function update_txt2img_tokens(...args) {
  163. update_token_counter("txt2img_token_button")
  164. if (args.length == 2)
  165. return args[0]
  166. return args;
  167. }
  168. function update_img2img_tokens(...args) {
  169. update_token_counter("img2img_token_button")
  170. if (args.length == 2)
  171. return args[0]
  172. return args;
  173. }
  174. function update_token_counter(button_id) {
  175. if (token_timeout)
  176. clearTimeout(token_timeout);
  177. token_timeout = setTimeout(() => gradioApp().getElementById(button_id)?.click(), wait_time);
  178. }
  179. function restart_reload(){
  180. document.body.innerHTML='<h1 style="font-family:monospace;margin-top:20%;color:lightgray;text-align:center;">Reloading...</h1>';
  181. setTimeout(function(){location.reload()},2000)
  182. return []
  183. }