2023-08-05 14:15:18 +08:00
|
|
|
function toggleCss(key, css, enable) {
|
|
|
|
var style = document.getElementById(key);
|
|
|
|
if (enable && !style) {
|
|
|
|
style = document.createElement('style');
|
|
|
|
style.id = key;
|
|
|
|
style.type = 'text/css';
|
|
|
|
document.head.appendChild(style);
|
|
|
|
}
|
|
|
|
if (style && !enable) {
|
|
|
|
document.head.removeChild(style);
|
|
|
|
}
|
|
|
|
if (style) {
|
|
|
|
style.innerHTML == '';
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
function setupExtraNetworksForTab(tabname) {
|
2024-01-14 02:16:39 +08:00
|
|
|
function registerPrompt(tabname, id) {
|
|
|
|
var textarea = gradioApp().querySelector("#" + id + " > label > textarea");
|
2023-11-05 15:12:50 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
if (!activePromptTextarea[tabname]) {
|
|
|
|
activePromptTextarea[tabname] = textarea;
|
2023-06-02 12:12:08 +08:00
|
|
|
}
|
2023-06-02 12:08:45 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
textarea.addEventListener("focus", function() {
|
|
|
|
activePromptTextarea[tabname] = textarea;
|
2023-06-02 12:12:08 +08:00
|
|
|
});
|
2024-01-14 02:16:39 +08:00
|
|
|
}
|
2024-01-15 02:39:01 +08:00
|
|
|
|
|
|
|
var this_tab = gradioApp().querySelector('#' + tabname + '_extra_tabs');
|
|
|
|
this_tab.classList.add('extra-networks');
|
2024-01-14 02:16:39 +08:00
|
|
|
this_tab.querySelectorAll(":scope > [id^='" + tabname + "_']").forEach(function(elem) {
|
|
|
|
var tab_id = elem.getAttribute("id");
|
2024-01-15 02:39:01 +08:00
|
|
|
var search = gradioApp().querySelector("#" + tab_id + "_extra_search");
|
|
|
|
if (!search) {
|
|
|
|
return; // `continue` doesn't work in `forEach` loops. This is equivalent.
|
|
|
|
}
|
2024-01-14 02:16:39 +08:00
|
|
|
|
|
|
|
var tabs = gradioApp().querySelector('#' + tabname + '_extra_tabs > div');
|
|
|
|
var sort = gradioApp().getElementById(tabname + '_extra_sort');
|
|
|
|
var sortOrder = gradioApp().getElementById(tabname + '_extra_sortorder');
|
|
|
|
var refresh = gradioApp().getElementById(tabname + '_extra_refresh');
|
|
|
|
var promptContainer = gradioApp().querySelector('.prompt-container-compact#' + tabname + '_prompt_container');
|
|
|
|
var negativePrompt = gradioApp().querySelector('#' + tabname + '_neg_prompt');
|
|
|
|
tabs.appendChild(sort);
|
|
|
|
tabs.appendChild(sortOrder);
|
|
|
|
tabs.appendChild(refresh);
|
2024-01-15 02:39:01 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
var applyFilter = function() {
|
|
|
|
var searchTerm = search.value.toLowerCase();
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card').forEach(function(elem) {
|
|
|
|
var searchOnly = elem.querySelector('.search_only');
|
2024-01-15 02:49:39 +08:00
|
|
|
|
|
|
|
var text = Array.prototype.map.call(elem.querySelectorAll('.search_terms'), function(t) {
|
|
|
|
return t.textContent.toLowerCase();
|
|
|
|
}).join(" ");
|
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
var visible = text.indexOf(searchTerm) != -1;
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
if (searchOnly && searchTerm.length < 4) {
|
|
|
|
visible = false;
|
|
|
|
}
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
elem.style.display = visible ? "" : "none";
|
|
|
|
});
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
applySort();
|
|
|
|
};
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
var applySort = function() {
|
|
|
|
var cards = gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card');
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
var reverse = sortOrder.classList.contains("sortReverse");
|
|
|
|
var sortKey = sort.querySelector("input").value.toLowerCase().replace("sort", "").replaceAll(" ", "_").replace(/_+$/, "").trim() || "name";
|
|
|
|
sortKey = "sort" + sortKey.charAt(0).toUpperCase() + sortKey.slice(1);
|
|
|
|
var sortKeyStore = sortKey + "-" + (reverse ? "Descending" : "Ascending") + "-" + cards.length;
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
if (sortKeyStore == sort.dataset.sortkey) {
|
|
|
|
return;
|
2023-06-02 12:12:08 +08:00
|
|
|
}
|
2024-01-14 02:16:39 +08:00
|
|
|
sort.dataset.sortkey = sortKeyStore;
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
cards.forEach(function(card) {
|
|
|
|
card.originalParentElement = card.parentElement;
|
|
|
|
});
|
|
|
|
var sortedCards = Array.from(cards);
|
|
|
|
sortedCards.sort(function(cardA, cardB) {
|
|
|
|
var a = cardA.dataset[sortKey];
|
|
|
|
var b = cardB.dataset[sortKey];
|
|
|
|
if (!isNaN(a) && !isNaN(b)) {
|
|
|
|
return parseInt(a) - parseInt(b);
|
|
|
|
}
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
return (a < b ? -1 : (a > b ? 1 : 0));
|
|
|
|
});
|
|
|
|
if (reverse) {
|
|
|
|
sortedCards.reverse();
|
|
|
|
}
|
|
|
|
cards.forEach(function(card) {
|
|
|
|
card.remove();
|
|
|
|
});
|
|
|
|
sortedCards.forEach(function(card) {
|
|
|
|
card.originalParentElement.appendChild(card);
|
|
|
|
});
|
|
|
|
};
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
search.addEventListener("input", applyFilter);
|
|
|
|
sortOrder.addEventListener("click", function() {
|
|
|
|
sortOrder.classList.toggle("sortReverse");
|
|
|
|
applySort();
|
2023-06-02 12:12:08 +08:00
|
|
|
});
|
2024-01-14 02:16:39 +08:00
|
|
|
applyFilter();
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
extraNetworksApplySort[tab_id] = applySort;
|
|
|
|
extraNetworksApplyFilter[tab_id] = applyFilter;
|
2023-06-02 12:08:45 +08:00
|
|
|
});
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2024-01-15 02:39:01 +08:00
|
|
|
registerPrompt(tabname, tabname + "_prompt");
|
|
|
|
registerPrompt(tabname, tabname + "_neg_prompt");
|
2023-05-08 16:33:45 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-11-06 00:19:55 +08:00
|
|
|
function extraNetworksMovePromptToTab(tabname, id, showPrompt, showNegativePrompt) {
|
|
|
|
if (!gradioApp().querySelector('.toprow-compact-tools')) return; // only applicable for compact prompt layout
|
|
|
|
|
|
|
|
var promptContainer = gradioApp().getElementById(tabname + '_prompt_container');
|
|
|
|
var prompt = gradioApp().getElementById(tabname + '_prompt_row');
|
|
|
|
var negPrompt = gradioApp().getElementById(tabname + '_neg_prompt_row');
|
|
|
|
var elem = id ? gradioApp().getElementById(id) : null;
|
|
|
|
|
|
|
|
if (showNegativePrompt && elem) {
|
|
|
|
elem.insertBefore(negPrompt, elem.firstChild);
|
|
|
|
} else {
|
|
|
|
promptContainer.insertBefore(negPrompt, promptContainer.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showPrompt && elem) {
|
|
|
|
elem.insertBefore(prompt, elem.firstChild);
|
|
|
|
} else {
|
|
|
|
promptContainer.insertBefore(prompt, promptContainer.firstChild);
|
|
|
|
}
|
2023-11-26 19:58:47 +08:00
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
elem.classList.toggle('extra-page-prompts-active', showNegativePrompt || showPrompt);
|
|
|
|
}
|
2023-11-06 00:19:55 +08:00
|
|
|
}
|
|
|
|
|
2024-01-12 04:06:57 +08:00
|
|
|
function clearSearch(tabname) {
|
|
|
|
// Clear search box.
|
|
|
|
var tab_id = tabname + "_extra_search";
|
|
|
|
var searchTextarea = gradioApp().querySelector("#" + tab_id + ' > label > textarea');
|
|
|
|
searchTextarea.value = "";
|
|
|
|
updateInput(searchTextarea);
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:19:55 +08:00
|
|
|
|
2024-01-12 04:06:57 +08:00
|
|
|
function extraNetworksUnrelatedTabSelected(tabname) { // called from python when user selects an unrelated tab (generate)
|
2023-11-06 00:19:55 +08:00
|
|
|
extraNetworksMovePromptToTab(tabname, '', false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksTabSelected(tabname, id, showPrompt, showNegativePrompt) { // called from python when user selects an extra networks tab
|
|
|
|
extraNetworksMovePromptToTab(tabname, id, showPrompt, showNegativePrompt);
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
function applyExtraNetworkFilter(tabname) {
|
|
|
|
setTimeout(extraNetworksApplyFilter[tabname], 1);
|
2023-01-21 13:36:07 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-11-05 15:12:50 +08:00
|
|
|
function applyExtraNetworkSort(tabname) {
|
|
|
|
setTimeout(extraNetworksApplySort[tabname], 1);
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
var extraNetworksApplyFilter = {};
|
2023-11-05 15:12:50 +08:00
|
|
|
var extraNetworksApplySort = {};
|
2023-01-21 14:48:38 +08:00
|
|
|
var activePromptTextarea = {};
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
function setupExtraNetworks() {
|
|
|
|
setupExtraNetworksForTab('txt2img');
|
|
|
|
setupExtraNetworksForTab('img2img');
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-09-10 08:26:46 +08:00
|
|
|
var re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/;
|
2023-09-10 08:22:10 +08:00
|
|
|
var re_extranet_g = /<([^:^>]+:[^:]+):[\d.]+>/g;
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-12-31 04:52:27 +08:00
|
|
|
var re_extranet_neg = /\(([^:^>]+:[\d.]+)\)/;
|
|
|
|
var re_extranet_g_neg = /\(([^:^>]+:[\d.]+)\)/g;
|
|
|
|
function tryToRemoveExtraNetworkFromPrompt(textarea, text, isNeg) {
|
|
|
|
var m = text.match(isNeg ? re_extranet_neg : re_extranet);
|
2023-01-29 04:31:48 +08:00
|
|
|
var replaced = false;
|
2023-05-17 10:20:11 +08:00
|
|
|
var newTextareaText;
|
|
|
|
if (m) {
|
2023-09-10 08:22:10 +08:00
|
|
|
var extraTextBeforeNet = opts.extra_networks_add_text_separator;
|
2023-07-16 05:56:53 +08:00
|
|
|
var extraTextAfterNet = m[2];
|
2023-05-17 10:20:11 +08:00
|
|
|
var partToSearch = m[1];
|
2023-07-16 05:56:53 +08:00
|
|
|
var foundAtPosition = -1;
|
2023-12-31 04:52:27 +08:00
|
|
|
newTextareaText = textarea.value.replaceAll(isNeg ? re_extranet_g_neg : re_extranet_g, function(found, net, pos) {
|
|
|
|
m = found.match(isNeg ? re_extranet_neg : re_extranet);
|
2023-05-17 10:20:11 +08:00
|
|
|
if (m[1] == partToSearch) {
|
|
|
|
replaced = true;
|
2023-07-16 05:56:53 +08:00
|
|
|
foundAtPosition = pos;
|
2023-05-17 10:20:11 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
});
|
2023-07-16 05:56:53 +08:00
|
|
|
|
2023-09-10 08:22:10 +08:00
|
|
|
if (foundAtPosition >= 0) {
|
2023-12-31 04:52:27 +08:00
|
|
|
if (extraTextAfterNet && newTextareaText.substr(foundAtPosition, extraTextAfterNet.length) == extraTextAfterNet) {
|
2023-09-10 08:22:10 +08:00
|
|
|
newTextareaText = newTextareaText.substr(0, foundAtPosition) + newTextareaText.substr(foundAtPosition + extraTextAfterNet.length);
|
|
|
|
}
|
|
|
|
if (newTextareaText.substr(foundAtPosition - extraTextBeforeNet.length, extraTextBeforeNet.length) == extraTextBeforeNet) {
|
|
|
|
newTextareaText = newTextareaText.substr(0, foundAtPosition - extraTextBeforeNet.length) + newTextareaText.substr(foundAtPosition);
|
|
|
|
}
|
2023-07-16 05:56:53 +08:00
|
|
|
}
|
2023-05-17 10:20:11 +08:00
|
|
|
} else {
|
|
|
|
newTextareaText = textarea.value.replaceAll(new RegExp(text, "g"), function(found) {
|
|
|
|
if (found == text) {
|
|
|
|
replaced = true;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
});
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-29 04:31:48 +08:00
|
|
|
if (replaced) {
|
|
|
|
textarea.value = newTextareaText;
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-29 04:31:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-12-31 04:52:27 +08:00
|
|
|
function updatePromptArea(text, textArea, isNeg) {
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-12-31 04:52:27 +08:00
|
|
|
if (!tryToRemoveExtraNetworkFromPrompt(textArea, text, isNeg)) {
|
|
|
|
textArea.value = textArea.value + opts.extra_networks_add_text_separator + text;
|
2023-01-29 04:31:48 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-12-31 04:52:27 +08:00
|
|
|
updateInput(textArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cardClicked(tabname, textToAdd, textToAddNegative, allowNegativePrompt) {
|
|
|
|
if (textToAddNegative.length > 0) {
|
2023-12-31 05:16:51 +08:00
|
|
|
updatePromptArea(textToAdd, gradioApp().querySelector("#" + tabname + "_prompt > label > textarea"));
|
|
|
|
updatePromptArea(textToAddNegative, gradioApp().querySelector("#" + tabname + "_neg_prompt > label > textarea"), true);
|
2023-12-31 04:52:27 +08:00
|
|
|
} else {
|
|
|
|
var textarea = allowNegativePrompt ? activePromptTextarea[tabname] : gradioApp().querySelector("#" + tabname + "_prompt > label > textarea");
|
2023-12-31 05:16:51 +08:00
|
|
|
updatePromptArea(textToAdd, textarea);
|
2023-12-31 04:52:27 +08:00
|
|
|
}
|
2023-01-21 13:36:07 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
function saveCardPreview(event, tabname, filename) {
|
2023-01-21 14:48:38 +08:00
|
|
|
var textarea = gradioApp().querySelector("#" + tabname + '_preview_filename > label > textarea');
|
|
|
|
var button = gradioApp().getElementById(tabname + '_save_preview');
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
textarea.value = filename;
|
|
|
|
updateInput(textarea);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
button.click();
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
function extraNetworksTreeProcessFileClick(event, btn, tabname, tab_id) {
|
|
|
|
/**
|
|
|
|
* Processes `onclick` events when user clicks on files in tree.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-14 02:16:39 +08:00
|
|
|
* @param event The generated event.
|
2024-01-16 02:40:47 +08:00
|
|
|
* @param btn The clicked `tree-list-item` button.
|
2024-01-14 02:16:39 +08:00
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
2024-01-15 02:49:39 +08:00
|
|
|
* @param tab_id The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
2024-01-14 02:16:39 +08:00
|
|
|
*/
|
|
|
|
var par = btn.parentElement;
|
|
|
|
var search_id = tabname + "_" + tab_id + "_extra_search";
|
|
|
|
var type = par.getAttribute("data-tree-entry-type");
|
|
|
|
var path = par.getAttribute("data-path");
|
|
|
|
}
|
|
|
|
|
2024-01-15 02:39:01 +08:00
|
|
|
function extraNetworksTreeProcessDirectoryClick(event, btn, tabname, tab_id) {
|
2024-01-14 02:16:39 +08:00
|
|
|
/**
|
|
|
|
* Processes `onclick` events when user clicks on directories in tree.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-14 02:16:39 +08:00
|
|
|
* Here is how the tree reacts to clicks for various states:
|
|
|
|
* unselected unopened directory: Diretory is selected and expanded.
|
|
|
|
* unselected opened directory: Directory is selected.
|
|
|
|
* selected opened directory: Directory is collapsed and deselected.
|
|
|
|
* chevron is clicked: Directory is expanded or collapsed. Selected state unchanged.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-14 02:16:39 +08:00
|
|
|
* @param event The generated event.
|
2024-01-16 02:40:47 +08:00
|
|
|
* @param btn The clicked `tree-list-item` button.
|
2024-01-15 02:39:01 +08:00
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param tab_id The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
2024-01-14 02:16:39 +08:00
|
|
|
*/
|
|
|
|
var ul = btn.nextElementSibling;
|
|
|
|
// This is the actual target that the user clicked on within the target button.
|
|
|
|
// We use this to detect if the chevron was clicked.
|
|
|
|
var true_targ = event.target;
|
|
|
|
|
|
|
|
function _expand_or_collapse(_ul, _btn) {
|
|
|
|
// Expands <ul> if it is collapsed, collapses otherwise. Updates button attributes.
|
|
|
|
if (_ul.hasAttribute("data-hidden")) {
|
|
|
|
_ul.removeAttribute("data-hidden");
|
|
|
|
_btn.setAttribute("expanded", "true");
|
|
|
|
} else {
|
|
|
|
_ul.setAttribute("data-hidden", "");
|
|
|
|
_btn.setAttribute("expanded", "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _remove_selected_from_all() {
|
|
|
|
// Removes the `selected` attribute from all buttons.
|
2024-01-16 02:40:47 +08:00
|
|
|
var sels = document.querySelectorAll("button.tree-list-content");
|
2024-01-14 02:16:39 +08:00
|
|
|
[...sels].forEach(el => {
|
|
|
|
el.removeAttribute("selected");
|
2024-01-15 02:49:39 +08:00
|
|
|
});
|
2024-01-14 02:16:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function _select_button(_btn) {
|
|
|
|
// Removes `selected` attribute from all buttons then adds to passed button.
|
|
|
|
_remove_selected_from_all();
|
|
|
|
_btn.setAttribute("selected", "");
|
|
|
|
}
|
|
|
|
|
2024-01-15 02:39:01 +08:00
|
|
|
function _update_search(_tabname, _tab_id, _search_text) {
|
|
|
|
// Update search input with select button's path.
|
2024-01-15 02:49:39 +08:00
|
|
|
var search_input_elem = gradioApp().querySelector("#" + tabname + "_" + tab_id + "_extra_search");
|
2024-01-15 02:39:01 +08:00
|
|
|
search_input_elem.value = _search_text;
|
|
|
|
updateInput(search_input_elem);
|
|
|
|
}
|
|
|
|
|
2024-01-15 02:49:39 +08:00
|
|
|
|
2024-01-14 02:16:39 +08:00
|
|
|
// If user clicks on the chevron, then we do not select the folder.
|
2024-01-16 02:40:47 +08:00
|
|
|
if (true_targ.matches(".tree-list-item-action--leading, .tree-list-item-action-chevron")) {
|
2024-01-14 02:16:39 +08:00
|
|
|
_expand_or_collapse(ul, btn);
|
|
|
|
} else {
|
|
|
|
// User clicked anywhere else on the button.
|
|
|
|
if (btn.hasAttribute("selected") && !ul.hasAttribute("data-hidden")) {
|
|
|
|
// If folder is select and open, collapse and deselect button.
|
|
|
|
_expand_or_collapse(ul, btn);
|
|
|
|
btn.removeAttribute("selected");
|
2024-01-15 02:39:01 +08:00
|
|
|
_update_search(tabname, tab_id, "");
|
2024-01-14 02:16:39 +08:00
|
|
|
} else if (!(!btn.hasAttribute("selected") && !ul.hasAttribute("data-hidden"))) {
|
|
|
|
// If folder is open and not selected, then we don't collapse; just select.
|
|
|
|
// NOTE: Double inversion sucks but it is the clearest way to show the branching here.
|
|
|
|
_expand_or_collapse(ul, btn);
|
2024-01-15 02:39:01 +08:00
|
|
|
_select_button(btn, tabname, tab_id);
|
|
|
|
_update_search(tabname, tab_id, btn.parentElement.getAttribute("data-path"));
|
2024-01-14 02:16:39 +08:00
|
|
|
} else {
|
|
|
|
// All other cases, just select the button.
|
2024-01-15 02:39:01 +08:00
|
|
|
_select_button(btn, tabname, tab_id);
|
|
|
|
_update_search(tabname, tab_id, btn.parentElement.getAttribute("data-path"));
|
2024-01-14 02:16:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksTreeOnClick(event, tabname, tab_id) {
|
|
|
|
/**
|
2024-01-16 02:40:47 +08:00
|
|
|
* Handles `onclick` events for buttons within an `extra-network-tree .tree-list--tree`.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-14 02:16:39 +08:00
|
|
|
* Determines whether the clicked button in the tree is for a file entry or a directory
|
|
|
|
* then calls the appropriate function.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-14 02:16:39 +08:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param tab_id The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
|
|
|
*/
|
|
|
|
var btn = event.currentTarget;
|
|
|
|
var par = btn.parentElement;
|
|
|
|
if (par.getAttribute("data-tree-entry-type") === "file") {
|
|
|
|
extraNetworksTreeProcessFileClick(event, btn, tabname, tab_id);
|
|
|
|
} else {
|
2024-01-15 02:39:01 +08:00
|
|
|
extraNetworksTreeProcessDirectoryClick(event, btn, tabname, tab_id);
|
2024-01-12 04:06:57 +08:00
|
|
|
}
|
2023-03-14 14:10:26 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
var globalPopup = null;
|
|
|
|
var globalPopupInner = null;
|
2023-10-03 12:22:15 +08:00
|
|
|
|
2023-07-16 02:05:33 +08:00
|
|
|
function closePopup() {
|
2023-07-16 01:39:04 +08:00
|
|
|
if (!globalPopup) return;
|
|
|
|
globalPopup.style.display = "none";
|
|
|
|
}
|
2023-10-03 12:22:15 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
function popup(contents) {
|
|
|
|
if (!globalPopup) {
|
|
|
|
globalPopup = document.createElement('div');
|
|
|
|
globalPopup.classList.add('global-popup');
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
var close = document.createElement('div');
|
|
|
|
close.classList.add('global-popup-close');
|
2023-10-03 12:22:15 +08:00
|
|
|
close.addEventListener("click", closePopup);
|
2023-03-14 14:10:26 +08:00
|
|
|
close.title = "Close";
|
|
|
|
globalPopup.appendChild(close);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
globalPopupInner = document.createElement('div');
|
|
|
|
globalPopupInner.classList.add('global-popup-inner');
|
|
|
|
globalPopup.appendChild(globalPopupInner);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-07-17 23:56:14 +08:00
|
|
|
gradioApp().querySelector('.main').appendChild(globalPopup);
|
2023-03-14 14:10:26 +08:00
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
globalPopupInner.innerHTML = '';
|
|
|
|
globalPopupInner.appendChild(contents);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
globalPopup.style.display = "flex";
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-08-27 14:39:37 +08:00
|
|
|
var storedPopupIds = {};
|
|
|
|
function popupId(id) {
|
2023-08-27 15:11:14 +08:00
|
|
|
if (!storedPopupIds[id]) {
|
2023-08-27 14:39:37 +08:00
|
|
|
storedPopupIds[id] = gradioApp().getElementById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
popup(storedPopupIds[id]);
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:10:03 +08:00
|
|
|
function extraNetworksShowMetadata(text) {
|
2023-05-01 03:08:52 +08:00
|
|
|
var elem = document.createElement('pre');
|
2023-03-14 14:10:26 +08:00
|
|
|
elem.classList.add('popup-metadata');
|
|
|
|
elem.textContent = text;
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-14 14:10:26 +08:00
|
|
|
popup(elem);
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
function requestGet(url, data, handler, errorHandler) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var args = Object.keys(data).map(function(k) {
|
|
|
|
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
|
|
|
|
}).join('&');
|
|
|
|
xhr.open("GET", url + "?" + args, true);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
try {
|
|
|
|
var js = JSON.parse(xhr.responseText);
|
|
|
|
handler(js);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
errorHandler();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errorHandler();
|
2023-05-17 20:46:58 +08:00
|
|
|
}
|
2023-03-25 15:11:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
var js = JSON.stringify(data);
|
|
|
|
xhr.send(js);
|
|
|
|
}
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2024-01-09 03:10:03 +08:00
|
|
|
function extraNetworksCopyCardPath(event, path) {
|
|
|
|
navigator.clipboard.writeText(path);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:10:03 +08:00
|
|
|
function extraNetworksRequestMetadata(event, extraPage, cardName) {
|
2023-05-01 03:08:52 +08:00
|
|
|
var showError = function() {
|
|
|
|
extraNetworksShowMetadata("there was an error getting metadata");
|
|
|
|
};
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
requestGet("./sd_extra_networks/metadata", {page: extraPage, item: cardName}, function(data) {
|
|
|
|
if (data && data.metadata) {
|
|
|
|
extraNetworksShowMetadata(data.metadata);
|
|
|
|
} else {
|
|
|
|
showError();
|
|
|
|
}
|
|
|
|
}, showError);
|
2023-05-17 20:46:58 +08:00
|
|
|
|
2023-03-25 17:10:03 +08:00
|
|
|
event.stopPropagation();
|
2023-03-25 15:11:04 +08:00
|
|
|
}
|
2023-07-16 01:39:04 +08:00
|
|
|
|
2023-07-16 02:05:33 +08:00
|
|
|
var extraPageUserMetadataEditors = {};
|
2023-07-16 01:39:04 +08:00
|
|
|
|
|
|
|
function extraNetworksEditUserMetadata(event, tabname, extraPage, cardName) {
|
|
|
|
var id = tabname + '_' + extraPage + '_edit_user_metadata';
|
|
|
|
|
2023-07-16 02:05:33 +08:00
|
|
|
var editor = extraPageUserMetadataEditors[id];
|
|
|
|
if (!editor) {
|
2023-07-16 01:39:04 +08:00
|
|
|
editor = {};
|
|
|
|
editor.page = gradioApp().getElementById(id);
|
|
|
|
editor.nameTextarea = gradioApp().querySelector("#" + id + "_name" + ' textarea');
|
|
|
|
editor.button = gradioApp().querySelector("#" + id + "_button");
|
|
|
|
extraPageUserMetadataEditors[id] = editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.nameTextarea.value = cardName;
|
|
|
|
updateInput(editor.nameTextarea);
|
|
|
|
|
|
|
|
editor.button.click();
|
|
|
|
|
|
|
|
popup(editor.page);
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2023-07-16 05:56:53 +08:00
|
|
|
|
2023-07-16 13:38:23 +08:00
|
|
|
function extraNetworksRefreshSingleCard(page, tabname, name) {
|
|
|
|
requestGet("./sd_extra_networks/get-single-card", {page: page, tabname: tabname, name: name}, function(data) {
|
|
|
|
if (data && data.html) {
|
2023-09-27 11:08:55 +08:00
|
|
|
var card = gradioApp().querySelector(`#${tabname}_${page.replace(" ", "_")}_cards > .card[data-name="${name}"]`);
|
2023-07-16 13:38:23 +08:00
|
|
|
|
|
|
|
var newDiv = document.createElement('DIV');
|
|
|
|
newDiv.innerHTML = data.html;
|
|
|
|
var newCard = newDiv.firstElementChild;
|
|
|
|
|
2023-08-15 00:27:04 +08:00
|
|
|
newCard.style.display = '';
|
2023-07-16 13:38:23 +08:00
|
|
|
card.parentElement.insertBefore(newCard, card);
|
|
|
|
card.parentElement.removeChild(card);
|
|
|
|
}
|
|
|
|
});
|
2023-07-16 05:56:53 +08:00
|
|
|
}
|
2023-12-01 13:36:12 +08:00
|
|
|
|
|
|
|
window.addEventListener("keydown", function(event) {
|
|
|
|
if (event.key == "Escape") {
|
|
|
|
closePopup();
|
|
|
|
}
|
|
|
|
});
|
2024-01-14 02:16:39 +08:00
|
|
|
|
2024-01-15 02:39:01 +08:00
|
|
|
/**
|
|
|
|
* Setup custom loading for this script.
|
|
|
|
* We need to wait for all of our HTML to be generated in the extra networks tabs
|
|
|
|
* before we can actually run the `setupExtraNetworks` function.
|
|
|
|
* The `onUiLoaded` function actually runs before all of our extra network tabs are
|
|
|
|
* finished generating. Thus we needed this new method.
|
2024-01-15 02:49:39 +08:00
|
|
|
*
|
2024-01-15 02:39:01 +08:00
|
|
|
*/
|
|
|
|
|
2024-01-15 02:49:39 +08:00
|
|
|
var uiAfterScriptsCallbacks = [];
|
2024-01-15 02:39:01 +08:00
|
|
|
var uiAfterScriptsTimeout = null;
|
|
|
|
var executedAfterScripts = false;
|
|
|
|
|
|
|
|
function scheduleAfterScriptsCallbacks() {
|
|
|
|
clearTimeout(uiAfterScriptsTimeout);
|
|
|
|
uiAfterScriptsTimeout = setTimeout(function() {
|
|
|
|
executeCallbacks(uiAfterScriptsCallbacks);
|
|
|
|
}, 200);
|
2024-01-14 02:16:39 +08:00
|
|
|
}
|
|
|
|
|
2024-01-15 02:39:01 +08:00
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
var mutationObserver = new MutationObserver(function(m) {
|
|
|
|
if (!executedAfterScripts &&
|
|
|
|
gradioApp().querySelectorAll("[id$='_extra_search']").length == 8) {
|
|
|
|
executedAfterScripts = true;
|
|
|
|
scheduleAfterScriptsCallbacks();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
|
|
|
|
});
|
|
|
|
|
2024-01-15 02:49:39 +08:00
|
|
|
uiAfterScriptsCallbacks.push(setupExtraNetworks);
|