stable-diffusion-webui/javascript/images_history.js

193 lines
7.2 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(){
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
var images_history_close_full_view = function(){
var box = images_history_get_parent_by_class(this, "images_history_cantainor");
box.querySelector(".images_history_del_button").setAttribute("disabled", "disabled");
}
function images_history_get_parent_by_class(item, class_name){
var parent = item.parentElement;
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){
var parent = item.parentElement;
tagname = tagname.toUpperCase()
while(parent.tagName != tagname){
console.log(parent.tagName, tagname)
parent = parent.parentElement;
}
return parent;
}
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
}
})
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 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");
set_btn.setAttribute("img_index", index);
set_btn.click();
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.setAttribute('disabled','disabled');
})
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(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 15:47:55 +03:00
});
2022-10-11 15:17:27 +03:00
2022-10-12 15:47:55 +03:00
var img_num = buttons.length / 2;
if (img_num === 1){
setTimeout(function(tabname){
gradioApp().getElementById(tabname + '_images_history_renew_page').click();
}, 30, tabname);
2022-10-11 15:17:27 +03:00
} else {
2022-10-12 15:47:55 +03:00
buttons[image_index].style.display = 'none';
buttons[image_index + img_num].style.display = 'none';
var bnt;
if (image_index >= img_num - 1){
btn = buttons[img_num - 2];
2022-10-11 15:17:27 +03:00
} else {
2022-10-12 15:47:55 +03:00
btn = buttons[image_index + 1] ;
2022-10-11 15:17:27 +03:00
}
2022-10-12 15:47:55 +03:00
setTimeout(function(btn){btn.click()}, 30, btn);
2022-10-11 15:37:03 +03:00
}
2022-10-12 15:47:55 +03:00
return [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(){
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.removeAttribute('disabled');
})
}
function images_history_init(){
if (gradioApp().getElementById('txt2img_images_history_renew_page') == null) {
setTimeout(images_history_init, 500);
} else {
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");
gradioApp().getElementById(tab + '_images_history_gallery').classList.add("images_history_gallery");
}
var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div");
tabs_box.setAttribute("id", "images_history_tab");
tabs_box.classList.add(images_history_tab_list[0]);
gradioApp().getElementById("txt2img_images_history_renew_page").click();
}
}
var images_history_tab_list = ["txt2img", "img2img", "extras"];
var images_history_start_flag = false;
onUiUpdate(function(){
var tab = gradioApp().getElementById("images_history_tab");
if (tab) {
if (!images_history_start_flag){
images_history_init();
images_history_start_flag = true;
}
var tab_btns = gradioApp().getElementById("images_history_tab").querySelectorAll("button");
for (var i in images_history_tab_list ){
var buttons = gradioApp().querySelectorAll('#' + images_history_tab_list[i] + '_images_history .gallery-item');
buttons.forEach(function(bnt){
bnt.addEventListener('click', images_history_click_image, true);
});
var tabname = images_history_tab_list[i]
tab_btns[i].setAttribute("tabname", tabname);
tab_btns[i].addEventListener('click', images_history_click_tab, true);
// var cls_btn = gradioApp().getElementById(tabname + '_images_history_gallery').querySelector("svg");
// if (cls_btn){
// cls_btn.addEventListener('click', images_history_close_full_view, false);
// }
// console.log(cls_btn, cls_btn.parentElement.parentElement)
// if (cls_btn) {
// cls_btn = images_history_get_parent_by_tagname(cls_btn, "BUTTON");
// cls_btn.addEventListener('click', images_history_close_full_view, true);
// }
}
}
});