WebUI/javascript/images_history.js

201 lines
7.6 KiB
JavaScript
Raw Normal View History

2022-10-12 12:47:55 +00:00
var images_history_click_image = function(){
2022-10-11 12:17:27 +00:00
if (!this.classList.contains("transform")){
2022-10-12 12:47:55 +00: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 12:17:27 +00:00
buttons.forEach(function(e){
if (e.style.display == "none"){
2022-10-12 12:47:55 +00:00
hidden_list.push(i);
2022-10-11 12:17:27 +00:00
}
2022-10-12 12:47:55 +00:00
i += 1;
2022-10-11 12:17:27 +00:00
})
if (hidden_list.length > 0){
2022-10-12 12:47:55 +00:00
setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
}
2022-10-11 12:17:27 +00:00
}
2022-10-12 12:47:55 +00:00
images_history_set_image_info(this);
}
2022-10-11 12:17:27 +00:00
2022-10-12 16:19:34 +00:00
function images_history_disabled_del(){
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.setAttribute('disabled','disabled');
});
2022-10-12 12:47:55 +00:00
}
function images_history_get_parent_by_class(item, class_name){
2022-10-12 16:19:34 +00:00
var parent = item.parentElement;
2022-10-12 12:47:55 +00:00
while(!parent.classList.contains(class_name)){
parent = parent.parentElement;
2022-10-10 07:39:39 +00:00
}
2022-10-12 12:47:55 +00:00
return parent;
}
function images_history_get_parent_by_tagname(item, tagname){
2022-10-12 16:19:34 +00:00
var parent = item.parentElement;
tagname = tagname.toUpperCase()
2022-10-12 12:47:55 +00:00
while(parent.tagName != tagname){
parent = parent.parentElement;
}
return parent;
}
2022-10-12 16:19:34 +00:00
2022-10-11 12:17:27 +00:00
function images_history_hide_buttons(hidden_list, gallery){
2022-10-12 12:47:55 +00:00
var buttons = gallery.querySelectorAll(".gallery-item");
var num = 0;
2022-10-11 12:17:27 +00:00
buttons.forEach(function(e){
if (e.style.display == "none"){
2022-10-12 12:47:55 +00:00
num += 1;
2022-10-11 12:17:27 +00:00
}
2022-10-12 16:19:34 +00:00
});
2022-10-11 12:17:27 +00:00
if (num == hidden_list.length){
2022-10-12 12:47:55 +00:00
setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
2022-10-11 12:17:27 +00:00
}
for( i in hidden_list){
2022-10-12 12:47:55 +00:00
buttons[hidden_list[i]].style.display = "none";
2022-10-11 12:17:27 +00:00
}
}
2022-10-10 07:39:39 +00:00
2022-10-11 12:17:27 +00:00
function images_history_set_image_info(button){
2022-10-12 12:47:55 +00:00
var buttons = images_history_get_parent_by_tagname(button, "DIV").querySelectorAll(".gallery-item");
var index = -1;
var i = 0;
2022-10-11 12:17:27 +00:00
buttons.forEach(function(e){
2022-10-12 12:47:55 +00:00
if(e == button){
index = i;
}
2022-10-11 12:17:27 +00:00
if(e.style.display != "none"){
2022-10-12 12:47:55 +00:00
i += 1;
2022-10-11 12:17:27 +00:00
}
2022-10-12 16:19:34 +00:00
});
2022-10-12 12:47:55 +00: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 16:19:34 +00: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 12:47:55 +00:00
set_btn.click();
2022-10-10 07:39:39 +00:00
}
2022-10-16 02:03:09 +00:00
function images_history_get_current_img(tabname, img_index, files){
2022-10-12 12:47:55 +00:00
return [
2022-10-16 02:03:09 +00:00
tabname,
gradioApp().getElementById(tabname + '_images_history_set_index').getAttribute("img_index"),
2022-10-12 12:47:55 +00:00
files
];
2022-10-10 07:39:39 +00:00
}
2022-10-11 12:17:27 +00:00
2022-10-16 13:22:38 +00:00
function images_history_delete(del_num, tabname, image_index){
2022-10-12 12:47:55 +00: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 12:17:27 +00:00
tab.querySelectorAll(".gallery-item").forEach(function(e){
if (e.style.display != 'none'){
2022-10-12 12:47:55 +00:00
buttons.push(e);
2022-10-11 12:17:27 +00:00
}
2022-10-12 16:19:34 +00:00
});
2022-10-12 12:47:55 +00:00
var img_num = buttons.length / 2;
2022-10-16 13:22:38 +00:00
del_num = Math.min(img_num - image_index, del_num)
if (img_num <= del_num){
2022-10-12 12:47:55 +00: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++){
2022-10-16 13:22:38 +00:00
buttons[image_index + i].style.display = 'none';
buttons[image_index + i + img_num].style.display = 'none';
next_img = image_index + i + 1
}
2022-10-12 12:47:55 +00:00
var bnt;
if (next_img >= img_num){
2022-10-16 13:22:38 +00:00
btn = buttons[image_index - 1];
2022-10-12 16:19:34 +00:00
} else {
btn = buttons[next_img];
2022-10-12 16:19:34 +00:00
}
2022-10-12 12:47:55 +00:00
setTimeout(function(btn){btn.click()}, 30, btn);
2022-10-12 16:19:34 +00:00
}
images_history_disabled_del();
2022-10-16 13:22:38 +00:00
2022-10-10 07:39:39 +00:00
}
2022-10-16 13:22:38 +00:00
function images_history_turnpage(tabname){
gradioApp().getElementById(tabname + '_images_history_del_button').setAttribute('disabled','disabled');
2022-10-12 12:47:55 +00:00
var buttons = gradioApp().getElementById(tabname + '_images_history').querySelectorAll(".gallery-item");
2022-10-11 12:17:27 +00:00
buttons.forEach(function(elem) {
2022-10-12 12:47:55 +00:00
elem.style.display = 'block';
2022-10-16 13:22:38 +00:00
})
2022-10-11 12:17:27 +00:00
}
2022-10-12 12:47:55 +00:00
function images_history_enable_del_buttons(){
2022-10-12 16:19:34 +00:00
gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
btn.removeAttribute('disabled');
2022-10-12 12:47:55 +00:00
})
}
function images_history_init(){
2022-10-19 03:27:51 +00:00
var tabnames = gradioApp().getElementById("images_history_tabnames_list")
if (tabnames){
images_history_tab_list = tabnames.querySelector("textarea").value.split(",")
2022-10-12 12:47:55 +00:00
for (var i in images_history_tab_list ){
2022-10-19 03:27:51 +00:00
var tab = images_history_tab_list[i];
2022-10-12 12:47:55 +00:00
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-19 03:27:51 +00:00
gradioApp().getElementById(tab + '_images_history_gallery').classList.add("images_history_gallery");
gradioApp().getElementById(tab + "_images_history_start").setAttribute("style","padding:20px;font-size:25px");
}
//preload
if (gradioApp().getElementById("images_history_preload").querySelector("input").checked ){
var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div");
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);
tab_btns[i].addEventListener('click', function(){
var tabs_box = gradioApp().getElementById("images_history_tab");
if (!tabs_box.classList.contains(this.getAttribute("tabname"))) {
gradioApp().getElementById(this.getAttribute("tabname") + "_images_history_start").click();
tabs_box.classList.add(this.getAttribute("tabname"))
}
});
}
tab_btns[0].click()
2022-10-12 12:47:55 +00:00
}
2022-10-12 16:19:34 +00:00
} else {
setTimeout(images_history_init, 500);
}
2022-10-12 12:47:55 +00:00
}
2022-10-19 03:27:51 +00:00
var images_history_tab_list = "";
2022-10-12 16:27:45 +00:00
setTimeout(images_history_init, 500);
2022-10-12 16:19:34 +00:00
document.addEventListener("DOMContentLoaded", function() {
var mutationObserver = new MutationObserver(function(m){
2022-10-19 03:27:51 +00:00
if (images_history_tab_list != ""){
for (var i in images_history_tab_list ){
let tabname = images_history_tab_list[i]
var buttons = gradioApp().querySelectorAll('#' + tabname + '_images_history .gallery-item');
buttons.forEach(function(bnt){
bnt.addEventListener('click', images_history_click_image, true);
});
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-12 16:19:34 +00:00
});
2022-10-16 13:22:38 +00:00
mutationObserver.observe(gradioApp(), { childList:true, subtree:true });
2022-10-12 16:19:34 +00:00
});
2022-10-12 12:47:55 +00:00