stable-diffusion-webui/javascript/images_history.js

207 lines
7.7 KiB
JavaScript
Raw Normal View History

2022-10-12 15:47:55 +03:00
var images_history_click_image = function(){
2022-10-11 15:17:27 +03:00
if (!this.classList.contains("transform")){
2022-10-12 15:47:55 +03:00
var gallery = images_history_get_parent_by_class(this, "images_history_cantainor");
var buttons = gallery.querySelectorAll(".gallery-item");
var i = 0;
var hidden_list = [];
2022-10-11 15:17:27 +03:00
buttons.forEach(function(e){
if (e.style.display == "none"){
2022-10-12 15:47:55 +03:00
hidden_list.push(i);
2022-10-11 15:17:27 +03:00
}
2022-10-12 15:47:55 +03:00
i += 1;
2022-10-11 15:17:27 +03:00
})
if (hidden_list.length > 0){
2022-10-12 15:47:55 +03:00
setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
}
2022-10-11 15:17:27 +03:00
}
2022-10-12 15:47:55 +03:00
images_history_set_image_info(this);
}
2022-10-11 15:17:27 +03:00
2022-10-12 15:47:55 +03:00
var images_history_click_tab = function(){
2022-10-12 19:19:34 +03:00
var tabs_box = gradioApp().getElementById("images_history_tab");
if (!tabs_box.classList.contains(this.getAttribute("tabname"))) {
gradioApp().getElementById(this.getAttribute("tabname") + "_images_history_renew_page").click();
tabs_box.classList.add(this.getAttribute("tabname"))
}
2022-10-10 10:39:39 +03:00
}
2022-10-12 15:47:55 +03:00
2022-10-12 19:19:34 +03:00
function images_history_disabled_del(){
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.setAttribute('disabled','disabled');
});
2022-10-12 15:47:55 +03:00
}
function images_history_get_parent_by_class(item, class_name){
2022-10-12 19:19:34 +03:00
var parent = item.parentElement;
2022-10-12 15:47:55 +03:00
while(!parent.classList.contains(class_name)){
parent = parent.parentElement;
2022-10-10 10:39:39 +03:00
}
2022-10-12 15:47:55 +03:00
return parent;
}
function images_history_get_parent_by_tagname(item, tagname){
2022-10-12 19:19:34 +03:00
var parent = item.parentElement;
tagname = tagname.toUpperCase()
2022-10-12 15:47:55 +03:00
while(parent.tagName != tagname){
2022-10-12 19:19:34 +03:00
console.log(parent.tagName, tagname)
2022-10-12 15:47:55 +03:00
parent = parent.parentElement;
}
return parent;
}
2022-10-12 19:19:34 +03:00
2022-10-11 15:17:27 +03:00
function images_history_hide_buttons(hidden_list, gallery){
2022-10-12 15:47:55 +03:00
var buttons = gallery.querySelectorAll(".gallery-item");
var num = 0;
2022-10-11 15:17:27 +03:00
buttons.forEach(function(e){
if (e.style.display == "none"){
2022-10-12 15:47:55 +03:00
num += 1;
2022-10-11 15:17:27 +03:00
}
2022-10-12 19:19:34 +03:00
});
2022-10-11 15:17:27 +03:00
if (num == hidden_list.length){
2022-10-12 15:47:55 +03:00
setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
2022-10-11 15:17:27 +03:00
}
for( i in hidden_list){
2022-10-12 15:47:55 +03:00
buttons[hidden_list[i]].style.display = "none";
2022-10-11 15:17:27 +03:00
}
}
2022-10-10 10:39:39 +03:00
2022-10-11 15:17:27 +03:00
function images_history_set_image_info(button){
2022-10-12 15:47:55 +03:00
var buttons = images_history_get_parent_by_tagname(button, "DIV").querySelectorAll(".gallery-item");
var index = -1;
var i = 0;
2022-10-11 15:17:27 +03:00
buttons.forEach(function(e){
2022-10-12 15:47:55 +03:00
if(e == button){
index = i;
}
2022-10-11 15:17:27 +03:00
if(e.style.display != "none"){
2022-10-12 15:47:55 +03:00
i += 1;
2022-10-11 15:17:27 +03:00
}
2022-10-12 19:19:34 +03:00
});
2022-10-12 15:47:55 +03:00
var gallery = images_history_get_parent_by_class(button, "images_history_cantainor");
var set_btn = gallery.querySelector(".images_history_set_index");
2022-10-12 19:19:34 +03:00
var curr_idx = set_btn.getAttribute("img_index", index);
if (curr_idx != index) {
set_btn.setAttribute("img_index", index);
images_history_disabled_del();
}
2022-10-12 15:47:55 +03:00
set_btn.click();
2022-10-10 10:39:39 +03:00
}
2022-10-11 15:17:27 +03:00
function images_history_get_current_img(tabname, image_path, files){
2022-10-12 15:47:55 +03:00
return [
gradioApp().getElementById(tabname + '_images_history_set_index').getAttribute("img_index"),
image_path,
files
];
2022-10-10 10:39:39 +03:00
}
2022-10-11 15:17:27 +03:00
function images_history_delete(del_num, tabname, img_path, img_file_name, page_index, filenames, image_index){
2022-10-12 15:47:55 +03:00
image_index = parseInt(image_index);
var tab = gradioApp().getElementById(tabname + '_images_history');
var set_btn = tab.querySelector(".images_history_set_index");
var buttons = [];
2022-10-11 15:17:27 +03:00
tab.querySelectorAll(".gallery-item").forEach(function(e){
if (e.style.display != 'none'){
2022-10-12 15:47:55 +03:00
buttons.push(e);
2022-10-11 15:17:27 +03:00
}
2022-10-12 19:19:34 +03:00
});
2022-10-12 15:47:55 +03:00
var img_num = buttons.length / 2;
if (img_num <= del_num){
2022-10-12 15:47:55 +03:00
setTimeout(function(tabname){
gradioApp().getElementById(tabname + '_images_history_renew_page').click();
}, 30, tabname);
} else {
var next_img
for (var i = 0; i < del_num; i++){
if (image_index + i < image_index + img_num){
buttons[image_index + i].style.display = 'none';
buttons[image_index + img_num + 1].style.display = 'none';
next_img = image_index + i + 1
}
}
2022-10-12 15:47:55 +03:00
var bnt;
if (next_img >= img_num){
btn = buttons[image_index - del_num];
2022-10-12 19:19:34 +03:00
} else {
btn = buttons[next_img];
2022-10-12 19:19:34 +03:00
}
2022-10-12 15:47:55 +03:00
setTimeout(function(btn){btn.click()}, 30, btn);
2022-10-12 19:19:34 +03:00
}
images_history_disabled_del();
return [del_num, tabname, img_path, img_file_name, page_index, filenames, image_index];
2022-10-10 10:39:39 +03:00
}
2022-10-11 15:17:27 +03:00
function images_history_turnpage(img_path, page_index, image_index, tabname){
2022-10-12 15:47:55 +03:00
var buttons = gradioApp().getElementById(tabname + '_images_history').querySelectorAll(".gallery-item");
2022-10-11 15:17:27 +03:00
buttons.forEach(function(elem) {
2022-10-12 15:47:55 +03:00
elem.style.display = 'block';
2022-10-11 15:17:27 +03:00
})
2022-10-12 15:47:55 +03:00
return [img_path, page_index, image_index, tabname];
2022-10-11 15:17:27 +03:00
}
2022-10-12 15:47:55 +03:00
function images_history_enable_del_buttons(){
2022-10-12 19:19:34 +03:00
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.removeAttribute('disabled');
2022-10-12 15:47:55 +03:00
})
}
function images_history_init(){
2022-10-12 19:19:34 +03:00
var load_txt2img_button = gradioApp().getElementById('txt2img_images_history_renew_page')
if (load_txt2img_button){
2022-10-12 15:47:55 +03:00
for (var i in images_history_tab_list ){
tab = images_history_tab_list[i];
gradioApp().getElementById(tab + '_images_history').classList.add("images_history_cantainor");
gradioApp().getElementById(tab + '_images_history_set_index').classList.add("images_history_set_index");
gradioApp().getElementById(tab + '_images_history_del_button').classList.add("images_history_del_button");
2022-10-12 19:19:34 +03:00
gradioApp().getElementById(tab + '_images_history_gallery').classList.add("images_history_gallery");
2022-10-12 15:47:55 +03:00
}
var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div");
2022-10-12 19:19:34 +03:00
tabs_box.setAttribute("id", "images_history_tab");
var tab_btns = tabs_box.querySelectorAll("button");
for (var i in images_history_tab_list){
var tabname = images_history_tab_list[i]
tab_btns[i].setAttribute("tabname", tabname);
// this refreshes history upon tab switch
// until the history is known to work well, which is not the case now, we do not do this at startup
//tab_btns[i].addEventListener('click', images_history_click_tab);
2022-10-12 19:19:34 +03:00
}
tabs_box.classList.add(images_history_tab_list[0]);
// same as above, at page load
//load_txt2img_button.click();
2022-10-12 19:19:34 +03:00
} else {
setTimeout(images_history_init, 500);
}
2022-10-12 15:47:55 +03:00
}
var images_history_tab_list = ["txt2img", "img2img", "extras"];
2022-10-12 19:27:45 +03:00
setTimeout(images_history_init, 500);
2022-10-12 19:19:34 +03:00
document.addEventListener("DOMContentLoaded", function() {
var mutationObserver = new MutationObserver(function(m){
for (var i in images_history_tab_list ){
2022-10-14 02:13:38 +03:00
let tabname = images_history_tab_list[i]
var buttons = gradioApp().querySelectorAll('#' + tabname + '_images_history .gallery-item');
2022-10-12 19:19:34 +03:00
buttons.forEach(function(bnt){
bnt.addEventListener('click', images_history_click_image, true);
});
// same as load_txt2img_button.click() above
/*
2022-10-14 02:13:38 +03:00
var cls_btn = gradioApp().getElementById(tabname + '_images_history_gallery').querySelector("svg");
if (cls_btn){
cls_btn.addEventListener('click', function(){
gradioApp().getElementById(tabname + '_images_history_renew_page').click();
}, false);
}*/
2022-10-14 02:13:38 +03:00
2022-10-12 19:19:34 +03:00
}
});
mutationObserver.observe( gradioApp(), { childList:true, subtree:true });
});
2022-10-12 15:47:55 +03:00