2023-08-05 06:15:18 +00: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-05-17 12:46:58 +00:00
|
|
|
function setupExtraNetworksForTab(tabname) {
|
2024-01-13 18:16:39 +00:00
|
|
|
function registerPrompt(tabname, id) {
|
|
|
|
var textarea = gradioApp().querySelector("#" + id + " > label > textarea");
|
2023-11-05 07:12:50 +00:00
|
|
|
|
2024-01-13 18:16:39 +00:00
|
|
|
if (!activePromptTextarea[tabname]) {
|
|
|
|
activePromptTextarea[tabname] = textarea;
|
2023-06-02 04:12:08 +00:00
|
|
|
}
|
2023-06-02 04:08:45 +00:00
|
|
|
|
2024-01-13 18:16:39 +00:00
|
|
|
textarea.addEventListener("focus", function() {
|
|
|
|
activePromptTextarea[tabname] = textarea;
|
2023-06-02 04:12:08 +00:00
|
|
|
});
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
2024-01-14 18:39:01 +00:00
|
|
|
|
2024-01-22 20:20:30 +00:00
|
|
|
var tabnav = gradioApp().querySelector('#' + tabname + '_extra_tabs > div.tab-nav');
|
|
|
|
var controlsDiv = document.createElement('DIV');
|
|
|
|
controlsDiv.classList.add('extra-networks-controls-div');
|
|
|
|
tabnav.appendChild(controlsDiv);
|
|
|
|
tabnav.insertBefore(controlsDiv, null);
|
|
|
|
|
2024-01-14 18:39:01 +00:00
|
|
|
var this_tab = gradioApp().querySelector('#' + tabname + '_extra_tabs');
|
2024-01-13 18:16:39 +00:00
|
|
|
this_tab.querySelectorAll(":scope > [id^='" + tabname + "_']").forEach(function(elem) {
|
2024-01-16 19:54:07 +00:00
|
|
|
// tabname_full = {tabname}_{extra_networks_tabname}
|
|
|
|
var tabname_full = elem.id;
|
|
|
|
var search = gradioApp().querySelector("#" + tabname_full + "_extra_search");
|
|
|
|
var sort_mode = gradioApp().querySelector("#" + tabname_full + "_extra_sort");
|
|
|
|
var sort_dir = gradioApp().querySelector("#" + tabname_full + "_extra_sort_dir");
|
|
|
|
var refresh = gradioApp().querySelector("#" + tabname_full + "_extra_refresh");
|
2024-01-16 18:35:01 +00:00
|
|
|
|
|
|
|
// If any of the buttons above don't exist, we want to skip this iteration of the loop.
|
|
|
|
if (!search || !sort_mode || !sort_dir || !refresh) {
|
|
|
|
return; // `return` is equivalent of `continue` but for forEach loops.
|
2024-01-15 22:34:44 +00:00
|
|
|
}
|
2024-01-14 18:39:01 +00:00
|
|
|
|
2024-02-02 17:51:54 +00:00
|
|
|
var applyFilter = function(force) {
|
2024-01-13 18:16:39 +00:00
|
|
|
var searchTerm = search.value.toLowerCase();
|
|
|
|
gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card').forEach(function(elem) {
|
|
|
|
var searchOnly = elem.querySelector('.search_only');
|
2024-01-14 18:49:39 +00:00
|
|
|
var text = Array.prototype.map.call(elem.querySelectorAll('.search_terms'), function(t) {
|
|
|
|
return t.textContent.toLowerCase();
|
|
|
|
}).join(" ");
|
|
|
|
|
2024-01-13 18:16:39 +00:00
|
|
|
var visible = text.indexOf(searchTerm) != -1;
|
|
|
|
if (searchOnly && searchTerm.length < 4) {
|
|
|
|
visible = false;
|
|
|
|
}
|
2024-01-18 17:13:33 +00:00
|
|
|
if (visible) {
|
|
|
|
elem.classList.remove("hidden");
|
|
|
|
} else {
|
|
|
|
elem.classList.add("hidden");
|
|
|
|
}
|
2024-01-13 18:16:39 +00:00
|
|
|
});
|
2024-01-14 18:49:39 +00:00
|
|
|
|
2024-02-02 17:51:54 +00:00
|
|
|
applySort(force);
|
2024-01-13 18:16:39 +00:00
|
|
|
};
|
2024-01-14 18:49:39 +00:00
|
|
|
|
2024-02-02 17:51:54 +00:00
|
|
|
var applySort = function(force) {
|
2024-01-13 18:16:39 +00:00
|
|
|
var cards = gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card');
|
2024-01-15 22:34:44 +00:00
|
|
|
var reverse = sort_dir.dataset.sortdir == "Descending";
|
2024-01-16 18:35:01 +00:00
|
|
|
var sortKey = sort_mode.dataset.sortmode.toLowerCase().replace("sort", "").replaceAll(" ", "_").replace(/_+$/, "").trim() || "name";
|
2024-01-13 18:16:39 +00:00
|
|
|
sortKey = "sort" + sortKey.charAt(0).toUpperCase() + sortKey.slice(1);
|
|
|
|
var sortKeyStore = sortKey + "-" + (reverse ? "Descending" : "Ascending") + "-" + cards.length;
|
2024-01-14 18:49:39 +00:00
|
|
|
|
2024-02-02 17:51:54 +00:00
|
|
|
if (sortKeyStore == sort_mode.dataset.sortkey && !force) {
|
2024-01-13 18:16:39 +00:00
|
|
|
return;
|
2023-06-02 04:12:08 +00:00
|
|
|
}
|
2024-01-16 18:35:01 +00:00
|
|
|
sort_mode.dataset.sortkey = sortKeyStore;
|
2024-01-14 18:49:39 +00:00
|
|
|
|
2024-01-13 18:16:39 +00: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-14 18:49:39 +00:00
|
|
|
|
2024-01-13 18:16:39 +00: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-14 18:49:39 +00:00
|
|
|
|
2024-01-13 18:16:39 +00:00
|
|
|
search.addEventListener("input", applyFilter);
|
2024-01-15 22:34:44 +00:00
|
|
|
applySort();
|
2024-01-13 18:16:39 +00:00
|
|
|
applyFilter();
|
2024-01-16 19:54:07 +00:00
|
|
|
extraNetworksApplySort[tabname_full] = applySort;
|
|
|
|
extraNetworksApplyFilter[tabname_full] = applyFilter;
|
2024-01-22 20:20:30 +00:00
|
|
|
|
|
|
|
var controls = gradioApp().querySelector("#" + tabname_full + "_controls");
|
|
|
|
controlsDiv.insertBefore(controls, null);
|
2024-02-17 06:45:48 +00:00
|
|
|
|
|
|
|
if (elem.style.display != "none") {
|
|
|
|
extraNetworksShowControlsForPage(tabname, tabname_full);
|
|
|
|
}
|
2023-06-02 04:08:45 +00:00
|
|
|
});
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-01-14 18:39:01 +00:00
|
|
|
registerPrompt(tabname, tabname + "_prompt");
|
|
|
|
registerPrompt(tabname, tabname + "_neg_prompt");
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 16:19:55 +00: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 11:58:47 +00:00
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
elem.classList.toggle('extra-page-prompts-active', showNegativePrompt || showPrompt);
|
|
|
|
}
|
2023-11-05 16:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-22 21:36:31 +00:00
|
|
|
function extraNetworksShowControlsForPage(tabname, tabname_full) {
|
2024-01-22 20:20:30 +00:00
|
|
|
gradioApp().querySelectorAll('#' + tabname + '_extra_tabs .extra-networks-controls-div > div').forEach(function(elem) {
|
2024-01-22 21:36:31 +00:00
|
|
|
var targetId = tabname_full + "_controls";
|
2024-01-22 20:20:30 +00:00
|
|
|
elem.style.display = elem.id == targetId ? "" : "none";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-11 20:06:57 +00:00
|
|
|
function extraNetworksUnrelatedTabSelected(tabname) { // called from python when user selects an unrelated tab (generate)
|
2023-11-05 16:19:55 +00:00
|
|
|
extraNetworksMovePromptToTab(tabname, '', false, false);
|
2024-01-22 20:20:30 +00:00
|
|
|
|
|
|
|
extraNetworksShowControlsForPage(tabname, null);
|
2023-11-05 16:19:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 20:20:30 +00:00
|
|
|
function extraNetworksTabSelected(tabname, id, showPrompt, showNegativePrompt, tabname_full) { // called from python when user selects an extra networks tab
|
2023-11-05 16:19:55 +00:00
|
|
|
extraNetworksMovePromptToTab(tabname, id, showPrompt, showNegativePrompt);
|
2024-01-22 20:20:30 +00:00
|
|
|
|
|
|
|
extraNetworksShowControlsForPage(tabname, tabname_full);
|
2023-11-05 16:19:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 19:54:07 +00:00
|
|
|
function applyExtraNetworkFilter(tabname_full) {
|
2024-02-02 17:51:54 +00:00
|
|
|
var doFilter = function() {
|
2024-02-17 06:46:04 +00:00
|
|
|
var applyFunction = extraNetworksApplyFilter[tabname_full];
|
|
|
|
|
|
|
|
if (applyFunction) {
|
|
|
|
applyFunction(true);
|
|
|
|
}
|
2024-02-02 17:51:54 +00:00
|
|
|
};
|
|
|
|
setTimeout(doFilter, 1);
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 19:54:07 +00:00
|
|
|
function applyExtraNetworkSort(tabname_full) {
|
2024-02-02 17:51:54 +00:00
|
|
|
var doSort = function() {
|
|
|
|
extraNetworksApplySort[tabname_full](true);
|
|
|
|
};
|
|
|
|
setTimeout(doSort, 1);
|
2023-11-05 07:12:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
var extraNetworksApplyFilter = {};
|
2023-11-05 07:12:50 +00:00
|
|
|
var extraNetworksApplySort = {};
|
2023-05-17 12:46:58 +00:00
|
|
|
var activePromptTextarea = {};
|
|
|
|
|
|
|
|
function setupExtraNetworks() {
|
|
|
|
setupExtraNetworksForTab('txt2img');
|
|
|
|
setupExtraNetworksForTab('img2img');
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:26:46 +00:00
|
|
|
var re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/;
|
2023-09-10 00:22:10 +00:00
|
|
|
var re_extranet_g = /<([^:^>]+:[^:]+):[\d.]+>/g;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-12-30 20:52:27 +00: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-05-17 12:46:58 +00:00
|
|
|
var replaced = false;
|
|
|
|
var newTextareaText;
|
2024-01-18 03:38:51 +00:00
|
|
|
var extraTextBeforeNet = opts.extra_networks_add_text_separator;
|
2023-05-17 12:46:58 +00:00
|
|
|
if (m) {
|
2023-07-15 21:56:53 +00:00
|
|
|
var extraTextAfterNet = m[2];
|
2023-05-17 12:46:58 +00:00
|
|
|
var partToSearch = m[1];
|
2023-07-15 21:56:53 +00:00
|
|
|
var foundAtPosition = -1;
|
2023-12-30 20:52:27 +00: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 12:46:58 +00:00
|
|
|
if (m[1] == partToSearch) {
|
|
|
|
replaced = true;
|
2023-07-15 21:56:53 +00:00
|
|
|
foundAtPosition = pos;
|
2023-05-17 12:46:58 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
});
|
2023-09-10 00:22:10 +00:00
|
|
|
if (foundAtPosition >= 0) {
|
2023-12-30 20:52:27 +00:00
|
|
|
if (extraTextAfterNet && newTextareaText.substr(foundAtPosition, extraTextAfterNet.length) == extraTextAfterNet) {
|
2023-09-10 00:22:10 +00: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-15 21:56:53 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
} else {
|
2024-01-18 03:38:51 +00:00
|
|
|
newTextareaText = textarea.value.replaceAll(new RegExp(`((?:${extraTextBeforeNet})?${text})`, "g"), "");
|
|
|
|
replaced = (newTextareaText != textarea.value);
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (replaced) {
|
|
|
|
textarea.value = newTextareaText;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-30 20:52:27 +00:00
|
|
|
function updatePromptArea(text, textArea, isNeg) {
|
|
|
|
if (!tryToRemoveExtraNetworkFromPrompt(textArea, text, isNeg)) {
|
|
|
|
textArea.value = textArea.value + opts.extra_networks_add_text_separator + text;
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 20:52:27 +00:00
|
|
|
updateInput(textArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cardClicked(tabname, textToAdd, textToAddNegative, allowNegativePrompt) {
|
|
|
|
if (textToAddNegative.length > 0) {
|
2023-12-30 21:16:51 +00:00
|
|
|
updatePromptArea(textToAdd, gradioApp().querySelector("#" + tabname + "_prompt > label > textarea"));
|
|
|
|
updatePromptArea(textToAddNegative, gradioApp().querySelector("#" + tabname + "_neg_prompt > label > textarea"), true);
|
2023-12-30 20:52:27 +00:00
|
|
|
} else {
|
|
|
|
var textarea = allowNegativePrompt ? activePromptTextarea[tabname] : gradioApp().querySelector("#" + tabname + "_prompt > label > textarea");
|
2023-12-30 21:16:51 +00:00
|
|
|
updatePromptArea(textToAdd, textarea);
|
2023-12-30 20:52:27 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveCardPreview(event, tabname, filename) {
|
|
|
|
var textarea = gradioApp().querySelector("#" + tabname + '_preview_filename > label > textarea');
|
|
|
|
var button = gradioApp().getElementById(tabname + '_save_preview');
|
|
|
|
|
|
|
|
textarea.value = filename;
|
|
|
|
updateInput(textarea);
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:35:01 +00:00
|
|
|
function extraNetworksTreeProcessFileClick(event, btn, tabname, extra_networks_tabname) {
|
2024-01-13 18:16:39 +00:00
|
|
|
/**
|
|
|
|
* Processes `onclick` events when user clicks on files in tree.
|
2024-01-14 18:49:39 +00:00
|
|
|
*
|
2024-01-16 18:35:01 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param btn The clicked `tree-list-item` button.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
2024-01-13 18:16:39 +00:00
|
|
|
*/
|
2024-01-16 18:35:01 +00:00
|
|
|
// NOTE: Currently unused.
|
|
|
|
return;
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:35:01 +00:00
|
|
|
function extraNetworksTreeProcessDirectoryClick(event, btn, tabname, extra_networks_tabname) {
|
2024-01-13 18:16:39 +00:00
|
|
|
/**
|
|
|
|
* Processes `onclick` events when user clicks on directories in tree.
|
2024-01-14 18:49:39 +00:00
|
|
|
*
|
2024-01-13 18:16:39 +00:00
|
|
|
* Here is how the tree reacts to clicks for various states:
|
2024-03-04 06:37:23 +00:00
|
|
|
* unselected unopened directory: Directory is selected and expanded.
|
2024-01-13 18:16:39 +00:00
|
|
|
* 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-14 18:49:39 +00:00
|
|
|
*
|
2024-01-16 18:35:01 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param btn The clicked `tree-list-item` button.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
2024-01-13 18:16:39 +00: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.
|
2024-01-16 18:35:01 +00:00
|
|
|
if (_ul.hasAttribute("hidden")) {
|
|
|
|
_ul.removeAttribute("hidden");
|
|
|
|
_btn.dataset.expanded = "";
|
2024-01-13 18:16:39 +00:00
|
|
|
} else {
|
2024-01-16 18:35:01 +00:00
|
|
|
_ul.setAttribute("hidden", "");
|
|
|
|
delete _btn.dataset.expanded;
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _remove_selected_from_all() {
|
|
|
|
// Removes the `selected` attribute from all buttons.
|
2024-01-15 22:34:44 +00:00
|
|
|
var sels = document.querySelectorAll("div.tree-list-content");
|
2024-01-13 18:16:39 +00:00
|
|
|
[...sels].forEach(el => {
|
2024-01-16 18:35:01 +00:00
|
|
|
delete el.dataset.selected;
|
2024-01-14 18:49:39 +00:00
|
|
|
});
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _select_button(_btn) {
|
2024-01-16 18:35:01 +00:00
|
|
|
// Removes `data-selected` attribute from all buttons then adds to passed button.
|
2024-01-13 18:16:39 +00:00
|
|
|
_remove_selected_from_all();
|
2024-01-16 18:35:01 +00:00
|
|
|
_btn.dataset.selected = "";
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:35:01 +00:00
|
|
|
function _update_search(_tabname, _extra_networks_tabname, _search_text) {
|
2024-01-14 18:39:01 +00:00
|
|
|
// Update search input with select button's path.
|
2024-01-16 18:35:01 +00:00
|
|
|
var search_input_elem = gradioApp().querySelector("#" + tabname + "_" + extra_networks_tabname + "_extra_search");
|
2024-01-14 18:39:01 +00:00
|
|
|
search_input_elem.value = _search_text;
|
|
|
|
updateInput(search_input_elem);
|
|
|
|
}
|
|
|
|
|
2024-01-14 18:49:39 +00:00
|
|
|
|
2024-01-13 18:16:39 +00:00
|
|
|
// If user clicks on the chevron, then we do not select the folder.
|
2024-01-15 18:40:47 +00:00
|
|
|
if (true_targ.matches(".tree-list-item-action--leading, .tree-list-item-action-chevron")) {
|
2024-01-13 18:16:39 +00:00
|
|
|
_expand_or_collapse(ul, btn);
|
|
|
|
} else {
|
|
|
|
// User clicked anywhere else on the button.
|
2024-01-16 18:35:01 +00:00
|
|
|
if ("selected" in btn.dataset && !(ul.hasAttribute("hidden"))) {
|
2024-01-13 18:16:39 +00:00
|
|
|
// If folder is select and open, collapse and deselect button.
|
|
|
|
_expand_or_collapse(ul, btn);
|
2024-01-16 18:35:01 +00:00
|
|
|
delete btn.dataset.selected;
|
|
|
|
_update_search(tabname, extra_networks_tabname, "");
|
|
|
|
} else if (!(!("selected" in btn.dataset) && !(ul.hasAttribute("hidden")))) {
|
2024-01-13 18:16:39 +00:00
|
|
|
// 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-16 18:35:01 +00:00
|
|
|
_select_button(btn, tabname, extra_networks_tabname);
|
|
|
|
_update_search(tabname, extra_networks_tabname, btn.dataset.path);
|
2024-01-13 18:16:39 +00:00
|
|
|
} else {
|
|
|
|
// All other cases, just select the button.
|
2024-01-16 18:35:01 +00:00
|
|
|
_select_button(btn, tabname, extra_networks_tabname);
|
|
|
|
_update_search(tabname, extra_networks_tabname, btn.dataset.path);
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:35:01 +00:00
|
|
|
function extraNetworksTreeOnClick(event, tabname, extra_networks_tabname) {
|
2024-01-13 18:16:39 +00:00
|
|
|
/**
|
2024-01-15 18:40:47 +00:00
|
|
|
* Handles `onclick` events for buttons within an `extra-network-tree .tree-list--tree`.
|
2024-01-14 18:49:39 +00:00
|
|
|
*
|
2024-01-13 18:16:39 +00:00
|
|
|
* Determines whether the clicked button in the tree is for a file entry or a directory
|
|
|
|
* then calls the appropriate function.
|
2024-01-14 18:49:39 +00:00
|
|
|
*
|
2024-01-16 18:35:01 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
2024-01-13 18:16:39 +00:00
|
|
|
*/
|
|
|
|
var btn = event.currentTarget;
|
|
|
|
var par = btn.parentElement;
|
2024-01-16 18:35:01 +00:00
|
|
|
if (par.dataset.treeEntryType === "file") {
|
|
|
|
extraNetworksTreeProcessFileClick(event, btn, tabname, extra_networks_tabname);
|
2024-01-13 18:16:39 +00:00
|
|
|
} else {
|
2024-01-16 18:35:01 +00:00
|
|
|
extraNetworksTreeProcessDirectoryClick(event, btn, tabname, extra_networks_tabname);
|
2024-01-11 20:06:57 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 16:43:45 +00:00
|
|
|
function extraNetworksControlSortOnClick(event, tabname, extra_networks_tabname) {
|
2024-01-16 18:35:01 +00:00
|
|
|
/**
|
|
|
|
* Handles `onclick` events for the Sort Mode button.
|
|
|
|
*
|
|
|
|
* Modifies the data attributes of the Sort Mode button to cycle between
|
|
|
|
* various sorting modes.
|
|
|
|
*
|
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
|
|
|
*/
|
2024-01-15 22:34:44 +00:00
|
|
|
var curr_mode = event.currentTarget.dataset.sortmode;
|
2024-01-16 18:35:01 +00:00
|
|
|
var el_sort_dir = gradioApp().querySelector("#" + tabname + "_" + extra_networks_tabname + "_extra_sort_dir");
|
2024-01-15 22:34:44 +00:00
|
|
|
var sort_dir = el_sort_dir.dataset.sortdir;
|
|
|
|
if (curr_mode == "path") {
|
|
|
|
event.currentTarget.dataset.sortmode = "name";
|
|
|
|
event.currentTarget.dataset.sortkey = "sortName-" + sort_dir + "-640";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort by filename");
|
|
|
|
} else if (curr_mode == "name") {
|
|
|
|
event.currentTarget.dataset.sortmode = "date_created";
|
|
|
|
event.currentTarget.dataset.sortkey = "sortDate_created-" + sort_dir + "-640";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort by date created");
|
|
|
|
} else if (curr_mode == "date_created") {
|
|
|
|
event.currentTarget.dataset.sortmode = "date_modified";
|
|
|
|
event.currentTarget.dataset.sortkey = "sortDate_modified-" + sort_dir + "-640";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort by date modified");
|
|
|
|
} else {
|
|
|
|
event.currentTarget.dataset.sortmode = "path";
|
|
|
|
event.currentTarget.dataset.sortkey = "sortPath-" + sort_dir + "-640";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort by path");
|
|
|
|
}
|
2024-01-16 18:35:01 +00:00
|
|
|
applyExtraNetworkSort(tabname + "_" + extra_networks_tabname);
|
2024-01-15 22:34:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 16:43:45 +00:00
|
|
|
function extraNetworksControlSortDirOnClick(event, tabname, extra_networks_tabname) {
|
2024-01-16 18:35:01 +00:00
|
|
|
/**
|
|
|
|
* Handles `onclick` events for the Sort Direction button.
|
|
|
|
*
|
|
|
|
* Modifies the data attributes of the Sort Direction button to cycle between
|
|
|
|
* ascending and descending sort directions.
|
|
|
|
*
|
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
|
|
|
*/
|
|
|
|
if (event.currentTarget.dataset.sortdir == "Ascending") {
|
2024-01-15 22:34:44 +00:00
|
|
|
event.currentTarget.dataset.sortdir = "Descending";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort descending");
|
|
|
|
} else {
|
|
|
|
event.currentTarget.dataset.sortdir = "Ascending";
|
|
|
|
event.currentTarget.setAttribute("title", "Sort ascending");
|
|
|
|
}
|
2024-01-16 18:35:01 +00:00
|
|
|
applyExtraNetworkSort(tabname + "_" + extra_networks_tabname);
|
2024-01-15 22:34:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 16:43:45 +00:00
|
|
|
function extraNetworksControlTreeViewOnClick(event, tabname, extra_networks_tabname) {
|
|
|
|
/**
|
|
|
|
* Handles `onclick` events for the Tree View button.
|
|
|
|
*
|
|
|
|
* Toggles the tree view in the extra networks pane.
|
|
|
|
*
|
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
|
|
|
*/
|
2024-02-27 14:31:36 +00:00
|
|
|
const tree = gradioApp().getElementById(tabname + "_" + extra_networks_tabname + "_tree");
|
|
|
|
const parent = tree.parentElement;
|
|
|
|
let resizeHandle = parent.querySelector('.resize-handle');
|
|
|
|
tree.classList.toggle("hidden");
|
|
|
|
|
2024-02-27 14:38:38 +00:00
|
|
|
if (tree.classList.contains("hidden")) {
|
2024-02-27 14:31:36 +00:00
|
|
|
tree.style.display = 'none';
|
|
|
|
parent.style.display = 'flex';
|
2024-02-27 19:17:52 +00:00
|
|
|
if (resizeHandle) {
|
|
|
|
resizeHandle.style.display = 'none';
|
|
|
|
}
|
2024-02-27 14:31:36 +00:00
|
|
|
} else {
|
|
|
|
tree.style.display = 'block';
|
2024-02-27 19:17:52 +00:00
|
|
|
parent.style.display = 'grid';
|
2024-02-27 14:31:36 +00:00
|
|
|
if (!resizeHandle) {
|
|
|
|
setupResizeHandle(parent);
|
|
|
|
resizeHandle = parent.querySelector('.resize-handle');
|
|
|
|
}
|
|
|
|
resizeHandle.style.display = 'block';
|
|
|
|
}
|
2024-01-20 16:43:45 +00:00
|
|
|
event.currentTarget.classList.toggle("extra-network-control--enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksControlRefreshOnClick(event, tabname, extra_networks_tabname) {
|
2024-01-16 18:35:01 +00:00
|
|
|
/**
|
|
|
|
* Handles `onclick` events for the Refresh Page button.
|
|
|
|
*
|
|
|
|
* In order to actually call the python functions in `ui_extra_networks.py`
|
|
|
|
* to refresh the page, we created an empty gradio button in that file with an
|
|
|
|
* event handler that refreshes the page. So what this function here does
|
|
|
|
* is it manually raises a `click` event on that button.
|
|
|
|
*
|
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname The name of the active tab in the sd webui. Ex: txt2img, img2img, etc.
|
|
|
|
* @param extra_networks_tabname The id of the active extraNetworks tab. Ex: lora, checkpoints, etc.
|
|
|
|
*/
|
2024-02-02 16:30:59 +00:00
|
|
|
var btn_refresh_internal = gradioApp().getElementById(tabname + "_" + extra_networks_tabname + "_extra_refresh_internal");
|
2024-01-15 22:34:44 +00:00
|
|
|
btn_refresh_internal.dispatchEvent(new Event("click"));
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
var globalPopup = null;
|
|
|
|
var globalPopupInner = null;
|
2023-10-03 04:22:15 +00:00
|
|
|
|
2023-07-15 18:05:33 +00:00
|
|
|
function closePopup() {
|
2023-07-15 17:39:04 +00:00
|
|
|
if (!globalPopup) return;
|
|
|
|
globalPopup.style.display = "none";
|
|
|
|
}
|
2023-10-03 04:22:15 +00:00
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
function popup(contents) {
|
|
|
|
if (!globalPopup) {
|
|
|
|
globalPopup = document.createElement('div');
|
|
|
|
globalPopup.classList.add('global-popup');
|
|
|
|
|
|
|
|
var close = document.createElement('div');
|
|
|
|
close.classList.add('global-popup-close');
|
2023-10-03 04:22:15 +00:00
|
|
|
close.addEventListener("click", closePopup);
|
2023-05-17 12:46:58 +00:00
|
|
|
close.title = "Close";
|
|
|
|
globalPopup.appendChild(close);
|
|
|
|
|
|
|
|
globalPopupInner = document.createElement('div');
|
|
|
|
globalPopupInner.classList.add('global-popup-inner');
|
|
|
|
globalPopup.appendChild(globalPopupInner);
|
|
|
|
|
2023-07-17 15:56:14 +00:00
|
|
|
gradioApp().querySelector('.main').appendChild(globalPopup);
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
globalPopupInner.innerHTML = '';
|
|
|
|
globalPopupInner.appendChild(contents);
|
|
|
|
|
|
|
|
globalPopup.style.display = "flex";
|
|
|
|
}
|
|
|
|
|
2023-08-27 06:39:37 +00:00
|
|
|
var storedPopupIds = {};
|
|
|
|
function popupId(id) {
|
2023-08-27 07:11:14 +00:00
|
|
|
if (!storedPopupIds[id]) {
|
2023-08-27 06:39:37 +00:00
|
|
|
storedPopupIds[id] = gradioApp().getElementById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
popup(storedPopupIds[id]);
|
|
|
|
}
|
|
|
|
|
2024-03-04 23:33:22 +00:00
|
|
|
function extraNetworksFlattenMetadata(obj) {
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
// Convert any stringified JSON objects to actual objects
|
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
if (typeof obj[key] === 'string') {
|
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(obj[key]);
|
|
|
|
if (parsed && typeof parsed === 'object') {
|
|
|
|
obj[key] = parsed;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flatten the object
|
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
|
|
const nested = extraNetworksFlattenMetadata(obj[key]);
|
|
|
|
for (const nestedKey of Object.keys(nested)) {
|
|
|
|
result[`${key}/${nestedKey}`] = nested[nestedKey];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for handling modelspec keys
|
|
|
|
for (const key of Object.keys(result)) {
|
|
|
|
if (key.startsWith("modelspec.")) {
|
|
|
|
result[key.replaceAll(".", "/")] = result[key];
|
|
|
|
delete result[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add empty keys to designate hierarchy
|
|
|
|
for (const key of Object.keys(result)) {
|
|
|
|
const parts = key.split("/");
|
|
|
|
for (let i = 1; i < parts.length; i++) {
|
|
|
|
const parent = parts.slice(0, i).join("/");
|
|
|
|
if (!result[parent]) {
|
|
|
|
result[parent] = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
function extraNetworksShowMetadata(text) {
|
2024-03-04 23:33:22 +00:00
|
|
|
try {
|
|
|
|
let parsed = JSON.parse(text);
|
|
|
|
if (parsed && typeof parsed === 'object') {
|
|
|
|
parsed = extraNetworksFlattenMetadata(parsed);
|
|
|
|
const table = createVisualizationTable(parsed, 0);
|
|
|
|
popup(table);
|
|
|
|
return;
|
|
|
|
}
|
2024-03-04 23:46:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.eror(error);
|
|
|
|
}
|
2024-03-04 23:33:22 +00:00
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
var elem = document.createElement('pre');
|
|
|
|
elem.classList.add('popup-metadata');
|
|
|
|
elem.textContent = text;
|
|
|
|
|
|
|
|
popup(elem);
|
2024-03-04 23:33:22 +00:00
|
|
|
return;
|
2023-05-17 12:46:58 +00: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);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var js = JSON.stringify(data);
|
|
|
|
xhr.send(js);
|
|
|
|
}
|
|
|
|
|
2024-01-08 19:10:03 +00:00
|
|
|
function extraNetworksCopyCardPath(event, path) {
|
|
|
|
navigator.clipboard.writeText(path);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:46:58 +00:00
|
|
|
function extraNetworksRequestMetadata(event, extraPage, cardName) {
|
|
|
|
var showError = function() {
|
|
|
|
extraNetworksShowMetadata("there was an error getting metadata");
|
|
|
|
};
|
|
|
|
|
|
|
|
requestGet("./sd_extra_networks/metadata", {page: extraPage, item: cardName}, function(data) {
|
|
|
|
if (data && data.metadata) {
|
|
|
|
extraNetworksShowMetadata(data.metadata);
|
|
|
|
} else {
|
|
|
|
showError();
|
|
|
|
}
|
|
|
|
}, showError);
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2023-07-15 17:39:04 +00:00
|
|
|
|
2023-07-15 18:05:33 +00:00
|
|
|
var extraPageUserMetadataEditors = {};
|
2023-07-15 17:39:04 +00:00
|
|
|
|
|
|
|
function extraNetworksEditUserMetadata(event, tabname, extraPage, cardName) {
|
|
|
|
var id = tabname + '_' + extraPage + '_edit_user_metadata';
|
|
|
|
|
2023-07-15 18:05:33 +00:00
|
|
|
var editor = extraPageUserMetadataEditors[id];
|
|
|
|
if (!editor) {
|
2023-07-15 17:39:04 +00: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-15 21:56:53 +00:00
|
|
|
|
2023-07-16 05:38:23 +00: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 03:08:55 +00:00
|
|
|
var card = gradioApp().querySelector(`#${tabname}_${page.replace(" ", "_")}_cards > .card[data-name="${name}"]`);
|
2023-07-16 05:38:23 +00:00
|
|
|
|
|
|
|
var newDiv = document.createElement('DIV');
|
|
|
|
newDiv.innerHTML = data.html;
|
|
|
|
var newCard = newDiv.firstElementChild;
|
|
|
|
|
2023-08-14 16:27:04 +00:00
|
|
|
newCard.style.display = '';
|
2023-07-16 05:38:23 +00:00
|
|
|
card.parentElement.insertBefore(newCard, card);
|
|
|
|
card.parentElement.removeChild(card);
|
|
|
|
}
|
|
|
|
});
|
2023-07-15 21:56:53 +00:00
|
|
|
}
|
2023-12-01 05:36:12 +00:00
|
|
|
|
|
|
|
window.addEventListener("keydown", function(event) {
|
|
|
|
if (event.key == "Escape") {
|
|
|
|
closePopup();
|
|
|
|
}
|
|
|
|
});
|
2024-01-13 18:16:39 +00:00
|
|
|
|
2024-01-14 18:39:01 +00: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-14 18:49:39 +00:00
|
|
|
*
|
2024-01-14 18:39:01 +00:00
|
|
|
*/
|
|
|
|
|
2024-01-14 18:49:39 +00:00
|
|
|
var uiAfterScriptsCallbacks = [];
|
2024-01-14 18:39:01 +00:00
|
|
|
var uiAfterScriptsTimeout = null;
|
|
|
|
var executedAfterScripts = false;
|
|
|
|
|
|
|
|
function scheduleAfterScriptsCallbacks() {
|
|
|
|
clearTimeout(uiAfterScriptsTimeout);
|
|
|
|
uiAfterScriptsTimeout = setTimeout(function() {
|
|
|
|
executeCallbacks(uiAfterScriptsCallbacks);
|
|
|
|
}, 200);
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 06:45:03 +00:00
|
|
|
onUiLoaded(function() {
|
2024-01-14 18:39:01 +00:00
|
|
|
var mutationObserver = new MutationObserver(function(m) {
|
2024-02-17 06:45:03 +00:00
|
|
|
let existingSearchfields = gradioApp().querySelectorAll("[id$='_extra_search']").length;
|
|
|
|
let neededSearchfields = gradioApp().querySelectorAll("[id$='_extra_tabs'] > .tab-nav > button").length - 2;
|
|
|
|
|
|
|
|
if (!executedAfterScripts && existingSearchfields >= neededSearchfields) {
|
|
|
|
mutationObserver.disconnect();
|
2024-01-14 18:39:01 +00:00
|
|
|
executedAfterScripts = true;
|
|
|
|
scheduleAfterScriptsCallbacks();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
|
|
|
|
});
|
|
|
|
|
2024-01-14 18:49:39 +00:00
|
|
|
uiAfterScriptsCallbacks.push(setupExtraNetworks);
|