2022-09-18 06:00:06 +00:00
|
|
|
// code related to showing and updating progressbar shown as the image is being made
|
2022-11-02 06:47:53 +00:00
|
|
|
|
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
galleries = {}
|
|
|
|
storedGallerySelections = {}
|
|
|
|
galleryObservers = {}
|
2022-09-06 16:33:51 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
function rememberGallerySelection(id_gallery){
|
|
|
|
storedGallerySelections[id_gallery] = getGallerySelectedIndex(id_gallery)
|
|
|
|
}
|
2022-09-23 17:46:02 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
function getGallerySelectedIndex(id_gallery){
|
|
|
|
let galleryButtons = gradioApp().querySelectorAll('#'+id_gallery+' .gallery-item')
|
|
|
|
let galleryBtnSelected = gradioApp().querySelector('#'+id_gallery+' .gallery-item.\\!ring-2')
|
2022-10-14 18:05:14 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
let currentlySelectedIndex = -1
|
|
|
|
galleryButtons.forEach(function(v, i){ if(v==galleryBtnSelected) { currentlySelectedIndex = i } })
|
2022-12-15 02:01:32 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
return currentlySelectedIndex
|
2022-09-23 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
// this is a workaround for https://github.com/gradio-app/gradio/issues/2984
|
2022-10-14 18:05:14 +00:00
|
|
|
function check_gallery(id_gallery){
|
|
|
|
let gallery = gradioApp().getElementById(id_gallery)
|
|
|
|
// if gallery has no change, no need to setting up observer again.
|
|
|
|
if (gallery && galleries[id_gallery] !== gallery){
|
|
|
|
galleries[id_gallery] = gallery;
|
|
|
|
if(galleryObservers[id_gallery]){
|
|
|
|
galleryObservers[id_gallery].disconnect();
|
|
|
|
}
|
2023-01-15 15:50:56 +00:00
|
|
|
|
|
|
|
storedGallerySelections[id_gallery] = -1
|
|
|
|
|
2022-10-14 18:05:14 +00:00
|
|
|
galleryObservers[id_gallery] = new MutationObserver(function (){
|
|
|
|
let galleryButtons = gradioApp().querySelectorAll('#'+id_gallery+' .gallery-item')
|
|
|
|
let galleryBtnSelected = gradioApp().querySelector('#'+id_gallery+' .gallery-item.\\!ring-2')
|
2023-01-15 15:50:56 +00:00
|
|
|
let currentlySelectedIndex = getGallerySelectedIndex(id_gallery)
|
|
|
|
prevSelectedIndex = storedGallerySelections[id_gallery]
|
|
|
|
storedGallerySelections[id_gallery] = -1
|
|
|
|
|
2022-10-15 00:14:59 +00:00
|
|
|
if (prevSelectedIndex !== -1 && galleryButtons.length>prevSelectedIndex && !galleryBtnSelected) {
|
2022-10-17 13:57:19 +00:00
|
|
|
// automatically re-open previously selected index (if exists)
|
|
|
|
activeElement = gradioApp().activeElement;
|
2022-11-30 01:11:01 +00:00
|
|
|
let scrollX = window.scrollX;
|
|
|
|
let scrollY = window.scrollY;
|
2022-10-17 13:57:19 +00:00
|
|
|
|
2022-10-15 00:14:59 +00:00
|
|
|
galleryButtons[prevSelectedIndex].click();
|
2022-10-16 14:22:56 +00:00
|
|
|
showGalleryImage();
|
2022-10-17 13:57:19 +00:00
|
|
|
|
2022-11-30 01:11:01 +00:00
|
|
|
// When the gallery button is clicked, it gains focus and scrolls itself into view
|
|
|
|
// We need to scroll back to the previous position
|
|
|
|
setTimeout(function (){
|
|
|
|
window.scrollTo(scrollX, scrollY);
|
|
|
|
}, 50);
|
|
|
|
|
2022-10-17 13:57:19 +00:00
|
|
|
if(activeElement){
|
|
|
|
// i fought this for about an hour; i don't know why the focus is lost or why this helps recover it
|
2022-11-30 01:11:01 +00:00
|
|
|
// if someone has a better solution please by all means
|
|
|
|
setTimeout(function (){
|
|
|
|
activeElement.focus({
|
|
|
|
preventScroll: true // Refocus the element that was focused before the gallery was opened without scrolling to it
|
|
|
|
})
|
|
|
|
}, 1);
|
2022-10-17 13:57:19 +00:00
|
|
|
}
|
2022-10-14 18:05:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
galleryObservers[id_gallery].observe( gallery, { childList:true, subtree:false })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:46:02 +00:00
|
|
|
onUiUpdate(function(){
|
2023-01-15 15:50:56 +00:00
|
|
|
check_gallery('txt2img_gallery')
|
|
|
|
check_gallery('img2img_gallery')
|
2022-09-18 05:51:11 +00:00
|
|
|
})
|
2022-09-23 17:46:02 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
function request(url, data, handler, errorHandler){
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var url = url;
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
var js = JSON.parse(xhr.responseText);
|
|
|
|
handler(js)
|
|
|
|
} else{
|
|
|
|
errorHandler()
|
|
|
|
}
|
2022-10-05 03:56:30 +00:00
|
|
|
}
|
2023-01-15 15:50:56 +00:00
|
|
|
};
|
|
|
|
var js = JSON.stringify(data);
|
|
|
|
xhr.send(js);
|
|
|
|
}
|
|
|
|
|
|
|
|
function pad2(x){
|
|
|
|
return x<10 ? '0'+x : x
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(secs){
|
|
|
|
if(secs > 3600){
|
|
|
|
return pad2(Math.floor(secs/60/60)) + ":" + pad2(Math.floor(secs/60)%60) + ":" + pad2(Math.floor(secs)%60)
|
|
|
|
} else if(secs > 60){
|
|
|
|
return pad2(Math.floor(secs/60)) + ":" + pad2(Math.floor(secs)%60)
|
|
|
|
} else{
|
|
|
|
return Math.floor(secs) + "s"
|
2022-09-20 18:05:25 +00:00
|
|
|
}
|
2022-09-18 06:00:06 +00:00
|
|
|
}
|
2022-09-18 08:14:42 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
function randomId(){
|
|
|
|
return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7)+")"
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
|
|
|
|
// preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
|
|
|
|
// calls onProgress every time there is a progress update
|
|
|
|
function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress){
|
|
|
|
var dateStart = new Date()
|
|
|
|
var wasEverActive = false
|
|
|
|
var parentProgressbar = progressbarContainer.parentNode
|
|
|
|
var parentGallery = gallery.parentNode
|
|
|
|
|
|
|
|
var divProgress = document.createElement('div')
|
|
|
|
divProgress.className='progressDiv'
|
|
|
|
var divInner = document.createElement('div')
|
|
|
|
divInner.className='progress'
|
|
|
|
|
|
|
|
divProgress.appendChild(divInner)
|
|
|
|
parentProgressbar.insertBefore(divProgress, progressbarContainer)
|
|
|
|
|
|
|
|
var livePreview = document.createElement('div')
|
|
|
|
livePreview.className='livePreview'
|
|
|
|
parentGallery.insertBefore(livePreview, gallery)
|
|
|
|
|
|
|
|
var removeProgressBar = function(){
|
|
|
|
parentProgressbar.removeChild(divProgress)
|
|
|
|
parentGallery.removeChild(livePreview)
|
|
|
|
atEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
var fun = function(id_task, id_live_preview){
|
|
|
|
request("/internal/progress", {"id_task": id_task, "id_live_preview": id_live_preview}, function(res){
|
|
|
|
console.log(res)
|
|
|
|
|
|
|
|
if(res.completed){
|
|
|
|
removeProgressBar()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rect = progressbarContainer.getBoundingClientRect()
|
|
|
|
|
|
|
|
if(rect.width){
|
|
|
|
divProgress.style.width = rect.width + "px";
|
|
|
|
}
|
|
|
|
|
|
|
|
progressText = ""
|
|
|
|
|
|
|
|
divInner.style.width = ((res.progress || 0) * 100.0) + '%'
|
|
|
|
|
|
|
|
if(res.progress > 0){
|
|
|
|
progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%'
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.eta){
|
|
|
|
progressText += " ETA: " + formatTime(res.eta)
|
|
|
|
} else if(res.textinfo){
|
|
|
|
progressText += " " + res.textinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
divInner.textContent = progressText
|
|
|
|
|
|
|
|
var elapsedFromStart = (new Date() - dateStart) / 1000
|
|
|
|
|
|
|
|
if(res.active) wasEverActive = true;
|
|
|
|
|
|
|
|
if(! res.active && wasEverActive){
|
|
|
|
removeProgressBar()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(elapsedFromStart > 5 && !res.queued && !res.active){
|
|
|
|
removeProgressBar()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(res.live_preview){
|
|
|
|
var img = new Image();
|
|
|
|
img.onload = function() {
|
|
|
|
var rect = gallery.getBoundingClientRect()
|
|
|
|
if(rect.width){
|
|
|
|
livePreview.style.width = rect.width + "px"
|
|
|
|
livePreview.style.height = rect.height + "px"
|
|
|
|
}
|
|
|
|
|
|
|
|
livePreview.innerHTML = ''
|
|
|
|
livePreview.appendChild(img)
|
|
|
|
if(livePreview.childElementCount > 2){
|
|
|
|
livePreview.removeChild(livePreview.firstElementChild)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
img.src = res.live_preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(onProgress){
|
|
|
|
onProgress(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
fun(id_task, res.id_live_preview);
|
|
|
|
}, 500)
|
|
|
|
}, function(){
|
|
|
|
removeProgressBar()
|
|
|
|
})
|
|
|
|
}
|
2022-09-18 08:14:42 +00:00
|
|
|
|
2023-01-15 15:50:56 +00:00
|
|
|
fun(id_task, 0)
|
2022-09-18 08:14:42 +00:00
|
|
|
}
|