2024-03-26 18:11:01 +00:00
|
|
|
// Prevent eslint errors on functions defined in other files.
|
|
|
|
/*global
|
|
|
|
ExtraNetworksClusterizeTreeList,
|
|
|
|
ExtraNetworksClusterizeCardsList,
|
|
|
|
*/
|
|
|
|
/*eslint no-undef: "error"*/
|
|
|
|
|
2024-03-25 14:26:29 +00:00
|
|
|
const SEARCH_INPUT_DEBOUNCE_TIME_MS = 250;
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
const re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/;
|
|
|
|
const re_extranet_g = /<([^:^>]+:[^:]+):[\d.]+>/g;
|
|
|
|
const re_extranet_neg = /\(([^:^>]+:[\d.]+)\)/;
|
|
|
|
const re_extranet_g_neg = /\(([^:^>]+:[\d.]+)\)/g;
|
|
|
|
const activePromptTextarea = {};
|
|
|
|
const clusterizers = {};
|
2024-03-20 18:01:38 +00:00
|
|
|
var globalPopup = null;
|
|
|
|
var globalPopupInner = null;
|
|
|
|
const storedPopupIds = {};
|
|
|
|
const extraPageUserMetadataEditors = {};
|
|
|
|
// A flag used by the `waitForBool` promise to determine when we first load Ui Options.
|
2024-03-26 17:58:21 +00:00
|
|
|
const initialUiOptionsLoaded = {state: false};
|
2024-03-13 21:11:44 +00:00
|
|
|
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Helper functions for checking types and simplifying logging. */
|
|
|
|
|
|
|
|
const isString = x => typeof x === "string" || x instanceof String;
|
|
|
|
const isStringLogError = x => {
|
|
|
|
if (isString(x)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
console.error("expected string, got:", typeof x);
|
|
|
|
return false;
|
|
|
|
};
|
2024-03-26 18:11:01 +00:00
|
|
|
const isNull = x => x === null;
|
2024-03-25 17:50:44 +00:00
|
|
|
const isUndefined = x => typeof x === "undefined" || x === undefined;
|
|
|
|
// checks both null and undefined for simplicity sake.
|
|
|
|
const isNullOrUndefined = x => isNull(x) || isUndefined(x);
|
|
|
|
const isNullOrUndefinedLogError = x => {
|
|
|
|
if (isNullOrUndefined(x)) {
|
|
|
|
console.error("Variable is null/undefined.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isElement = x => x instanceof Element;
|
|
|
|
const isElementLogError = x => {
|
|
|
|
if (isElement(x)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
console.error("expected element type, got:", typeof x);
|
|
|
|
return false;
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-25 17:50:44 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
const isFunction = x => typeof x === "function";
|
|
|
|
const isFunctionLogError = x => {
|
|
|
|
if (isFunction(x)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
console.error("expected function type, got:", typeof x);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-25 17:50:44 +00:00
|
|
|
const getElementByIdLogError = selector => {
|
|
|
|
let elem = gradioApp().getElementById(selector);
|
|
|
|
isElementLogError(elem);
|
|
|
|
return elem;
|
|
|
|
};
|
|
|
|
|
|
|
|
const querySelectorLogError = selector => {
|
|
|
|
let elem = gradioApp().querySelector(selector);
|
|
|
|
isElementLogError(elem);
|
|
|
|
return elem;
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-25 17:50:44 +00:00
|
|
|
|
2024-03-22 20:16:45 +00:00
|
|
|
const debounce = (handler, timeout_ms) => {
|
|
|
|
/** Debounces a function call.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-22 20:16:45 +00:00
|
|
|
* NOTE: This will NOT work if called from within a class.
|
|
|
|
* It will drop `this` from scope.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-22 20:16:45 +00:00
|
|
|
* Repeated calls to the debounce handler will not call the handler until there are
|
|
|
|
* no new calls to the debounce handler for timeout_ms time.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-22 20:16:45 +00:00
|
|
|
* Example:
|
|
|
|
* function add(x, y) { return x + y; }
|
|
|
|
* let debounce_handler = debounce(add, 5000);
|
|
|
|
* let res;
|
|
|
|
* for (let i = 0; i < 10; i++) {
|
|
|
|
* res = debounce_handler(i, 100);
|
|
|
|
* }
|
|
|
|
* console.log("Result:", res);
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-22 20:16:45 +00:00
|
|
|
* This example will print "Result: 109".
|
|
|
|
*/
|
|
|
|
let timer = null;
|
|
|
|
return (...args) => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => handler(...args), timeout_ms);
|
|
|
|
};
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-22 20:16:45 +00:00
|
|
|
|
2024-03-25 17:50:44 +00:00
|
|
|
const waitForElement = selector => {
|
2024-03-13 21:11:44 +00:00
|
|
|
/** Promise that waits for an element to exist in DOM. */
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (document.querySelector(selector)) {
|
|
|
|
return resolve(document.querySelector(selector));
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
if (document.querySelector(selector)) {
|
|
|
|
observer.disconnect();
|
|
|
|
resolve(document.querySelector(selector));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe(document.documentElement, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
});
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-13 21:11:44 +00:00
|
|
|
|
2024-03-25 17:50:44 +00:00
|
|
|
const waitForBool = o => {
|
2024-03-20 18:01:38 +00:00
|
|
|
/** Promise that waits for a boolean to be true.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* `o` must be an Object of the form:
|
|
|
|
* { state: <bool value> }
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* Resolves when (state === true)
|
|
|
|
*/
|
2024-03-15 18:31:58 +00:00
|
|
|
return new Promise(resolve => {
|
2024-03-19 01:59:41 +00:00
|
|
|
(function _waitForBool() {
|
2024-03-20 18:01:38 +00:00
|
|
|
if (o.state) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2024-03-19 01:59:41 +00:00
|
|
|
setTimeout(_waitForBool, 100);
|
|
|
|
})();
|
|
|
|
});
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-19 01:59:41 +00:00
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
const waitForKeyInObject = o => {
|
2024-03-20 18:01:38 +00:00
|
|
|
/** Promise that waits for a key to exist in an object.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* `o` must be an Object of the form:
|
|
|
|
* {
|
|
|
|
* obj: <object to watch for key>,
|
2024-03-26 17:58:21 +00:00
|
|
|
* k: <key to watch for>,
|
2024-03-20 18:01:38 +00:00
|
|
|
* }
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* Resolves when (k in obj)
|
|
|
|
*/
|
2024-03-19 01:59:41 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
(function _waitForKeyInObject() {
|
2024-03-20 18:01:38 +00:00
|
|
|
if (o.k in o.obj) {
|
2024-03-19 01:59:41 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
setTimeout(_waitForKeyInObject, 100);
|
|
|
|
})();
|
|
|
|
});
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-19 01:59:41 +00:00
|
|
|
|
2024-03-25 17:50:44 +00:00
|
|
|
const waitForValueInObject = o => {
|
2024-03-20 18:01:38 +00:00
|
|
|
/** Promise that waits for a key value pair in an Object.
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* `o` must be an Object of the form:
|
|
|
|
* {
|
|
|
|
* obj: <object containing value>,
|
|
|
|
* k: <key in object>,
|
|
|
|
* v: <value at key for comparison>
|
|
|
|
* }
|
2024-03-26 17:58:21 +00:00
|
|
|
*
|
2024-03-20 18:01:38 +00:00
|
|
|
* Resolves when obj[k] == v
|
|
|
|
*/
|
2024-03-19 01:59:41 +00:00
|
|
|
return new Promise(resolve => {
|
2024-03-26 17:58:21 +00:00
|
|
|
waitForKeyInObject({k: o.k, obj: o.obj}).then(() => {
|
2024-03-20 18:01:38 +00:00
|
|
|
(function _waitForValueInObject() {
|
|
|
|
|
|
|
|
if (o.k in o.obj && o.obj[o.k] == o.v) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
setTimeout(_waitForValueInObject, 100);
|
|
|
|
})();
|
|
|
|
});
|
2024-03-15 18:31:58 +00:00
|
|
|
});
|
2024-03-26 17:58:21 +00:00
|
|
|
};
|
2024-03-15 18:31:58 +00:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksRefreshTab(tabname_full) {
|
2024-03-19 01:59:41 +00:00
|
|
|
// Reapply controls since they don't change on refresh.
|
2024-03-27 20:11:13 +00:00
|
|
|
const controls = gradioApp().getElementById(`${tabname_full}_controls`);
|
|
|
|
let btn_dirs_view = controls.querySelector(".extra-network-control--dirs-view");
|
|
|
|
let btn_tree_view = controls.querySelector(".extra-network-control--tree-view");
|
2024-03-19 01:59:41 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
const pane = gradioApp().getElementById(`${tabname_full}_pane`);
|
|
|
|
let div_dirs = pane.querySelector(".extra-network-content--dirs-view");
|
|
|
|
let div_tree = pane.querySelector(`.extra-network-content.resize-handle-col:has(> #${tabname_full}_tree_list_scroll_area)`);
|
2024-03-19 01:59:41 +00:00
|
|
|
|
|
|
|
// Remove "hidden" class if button is enabled, otherwise add it.
|
2024-03-25 20:43:29 +00:00
|
|
|
div_dirs.classList.toggle("hidden", !("selected" in btn_dirs_view.dataset));
|
|
|
|
div_tree.classList.toggle("hidden", !("selected" in btn_tree_view.dataset));
|
2024-03-19 01:59:41 +00:00
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
waitForKeyInObject({k: tabname_full, obj: clusterizers})
|
2024-03-29 16:21:02 +00:00
|
|
|
.then(() => {
|
|
|
|
// We want to reload all tabs when refresh is clicked, but we only want to
|
|
|
|
// enable the tab on which the refresh button was clicked.
|
|
|
|
for (const _tabname_full of Object.keys(clusterizers)) {
|
|
|
|
let selected = _tabname_full === tabname_full;
|
|
|
|
extraNetworkClusterizersLoadTab({
|
|
|
|
tabname_full:_tabname_full,
|
|
|
|
selected: selected,
|
|
|
|
fetch_data: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
function extraNetworksRegisterPromptForTab(tabname, id) {
|
|
|
|
var textarea = gradioApp().querySelector(`#${id} > label > textarea`);
|
2023-11-05 07:12:50 +00:00
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
if (!activePromptTextarea[tabname]) {
|
|
|
|
activePromptTextarea[tabname] = textarea;
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
2024-01-14 18:39:01 +00:00
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
textarea.addEventListener("focus", function() {
|
2024-03-19 01:59:41 +00:00
|
|
|
activePromptTextarea[tabname] = textarea;
|
|
|
|
});
|
|
|
|
}
|
2024-03-13 21:11:44 +00:00
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
function extraNetworksSetupTabContent(tabname, pane, controls_div) {
|
2024-03-25 17:50:44 +00:00
|
|
|
const tabname_full = pane.id;
|
2024-03-29 16:21:02 +00:00
|
|
|
const extra_networks_tabname = tabname_full.replace(`${tabname}_`, "");
|
2024-03-27 20:11:13 +00:00
|
|
|
let controls;
|
2024-03-19 01:59:41 +00:00
|
|
|
|
|
|
|
Promise.all([
|
2024-03-27 20:11:13 +00:00
|
|
|
waitForElement(`#${tabname_full}_pane .extra-network-controls`).then(elem => controls = elem),
|
|
|
|
waitForElement(`#${tabname_full}_pane .extra-network-content--dirs-view`),
|
2024-03-19 01:59:41 +00:00
|
|
|
waitForElement(`#${tabname_full}_tree_list_scroll_area > #${tabname_full}_tree_list_content_area`),
|
|
|
|
waitForElement(`#${tabname_full}_cards_list_scroll_area > #${tabname_full}_cards_list_content_area`),
|
|
|
|
]).then(() => {
|
2024-03-27 20:11:13 +00:00
|
|
|
// Insert the controls into the page.
|
|
|
|
// add an ID since we will be moving this element elsewhere.
|
|
|
|
controls.id = `${tabname_full}_controls`;
|
|
|
|
controls_div.insertBefore(controls, null);
|
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
// Now that we have our elements in DOM, we create the clusterize lists.
|
|
|
|
clusterizers[tabname_full] = {
|
|
|
|
tree_list: new ExtraNetworksClusterizeTreeList({
|
2024-03-29 16:21:02 +00:00
|
|
|
tabname: tabname,
|
|
|
|
extra_networks_tabname: extra_networks_tabname,
|
2024-03-19 01:59:41 +00:00
|
|
|
scroll_id: `${tabname_full}_tree_list_scroll_area`,
|
|
|
|
content_id: `${tabname_full}_tree_list_content_area`,
|
2024-03-29 16:21:02 +00:00
|
|
|
data_request_callback: extraNetworksRequestListData,
|
2024-03-19 01:59:41 +00:00
|
|
|
}),
|
2024-03-21 20:03:27 +00:00
|
|
|
cards_list: new ExtraNetworksClusterizeCardsList({
|
2024-03-29 16:21:02 +00:00
|
|
|
tabname: tabname,
|
|
|
|
extra_networks_tabname: extra_networks_tabname,
|
2024-03-21 20:03:27 +00:00
|
|
|
scroll_id: `${tabname_full}_cards_list_scroll_area`,
|
|
|
|
content_id: `${tabname_full}_cards_list_content_area`,
|
2024-03-29 16:21:02 +00:00
|
|
|
data_request_callback: extraNetworksRequestListData,
|
2024-03-21 20:03:27 +00:00
|
|
|
}),
|
2024-03-13 21:11:44 +00:00
|
|
|
};
|
2024-03-08 14:09:11 +00:00
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
if (pane.style.display != "none") {
|
|
|
|
extraNetworksShowControlsForPage(tabname, tabname_full);
|
|
|
|
}
|
2024-03-29 16:21:02 +00:00
|
|
|
(async() => {
|
|
|
|
await extraNetworkClusterizersLoadTab({
|
|
|
|
tabname_full: tabname_full,
|
|
|
|
selected: false,
|
|
|
|
fetch_data: true
|
|
|
|
});
|
|
|
|
})();
|
2024-03-22 20:16:45 +00:00
|
|
|
});
|
2024-03-19 01:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksSetupTab(tabname) {
|
|
|
|
let this_tab;
|
|
|
|
let tab_nav;
|
|
|
|
let controls_div;
|
|
|
|
Promise.all([
|
|
|
|
waitForElement(`#${tabname}_extra_tabs`).then((elem) => this_tab = elem),
|
|
|
|
waitForElement(`#${tabname}_extra_tabs > div.tab-nav`).then((elem) => tab_nav = elem),
|
|
|
|
]).then(() => {
|
|
|
|
controls_div = document.createElement("div");
|
|
|
|
controls_div.classList.add("extra-network-controls-div");
|
|
|
|
tab_nav.appendChild(controls_div);
|
|
|
|
tab_nav.insertBefore(controls_div, null);
|
|
|
|
this_tab.querySelectorAll(`:scope > .tabitem[id^="${tabname}_"]`).forEach((elem) => {
|
|
|
|
extraNetworksSetupTabContent(tabname, elem, controls_div);
|
|
|
|
});
|
|
|
|
extraNetworksRegisterPromptForTab(tabname, `${tabname}_prompt`);
|
|
|
|
extraNetworksRegisterPromptForTab(tabname, `${tabname}_neg_prompt`);
|
|
|
|
});
|
2023-05-08 08:33:45 +00:00
|
|
|
}
|
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
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
var promptContainer = gradioApp().getElementById(`${tabname}_prompt_container`);
|
|
|
|
var prompt = gradioApp().getElementById(`${tabname}_prompt_row`);
|
|
|
|
var negPrompt = gradioApp().getElementById(`${tabname}_neg_prompt_row`);
|
2023-11-05 16:19:55 +00:00
|
|
|
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-03-19 01:59:41 +00:00
|
|
|
gradioApp().querySelectorAll(`#${tabname}_extra_tabs .extra-network-controls-div > div`).forEach((elem) => {
|
2024-03-27 20:11:13 +00:00
|
|
|
let show = `${tabname_full}_controls` === elem.id;
|
|
|
|
elem.classList.toggle("hidden", !show);
|
2024-01-22 20:20:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2024-03-13 21:11:44 +00:00
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
waitForKeyInObject({k: tabname_full, obj: clusterizers})
|
2024-03-29 16:21:02 +00:00
|
|
|
.then(() => {
|
|
|
|
extraNetworkClusterizersLoadTab({
|
|
|
|
tabname_full: tabname_full,
|
|
|
|
selected: true,
|
|
|
|
fetch_data: false,
|
|
|
|
});
|
|
|
|
});
|
2024-03-19 01:59:41 +00:00
|
|
|
}
|
2024-03-13 21:11:44 +00:00
|
|
|
|
2024-03-19 01:59:41 +00:00
|
|
|
function extraNetworksApplyFilter(tabname_full) {
|
|
|
|
if (!(tabname_full in clusterizers)) {
|
|
|
|
console.error(`${tabname_full} not in clusterizers.`);
|
|
|
|
return;
|
2024-03-13 21:11:44 +00:00
|
|
|
}
|
2023-11-05 16:19:55 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
const pane = gradioApp().getElementById(`${tabname_full}_pane`);
|
|
|
|
const txt_search = gradioApp().querySelector(`#${tabname_full}_controls .extra-network-control--search-text`);
|
2024-03-25 17:50:44 +00:00
|
|
|
if (!isElementLogError(txt_search)) {
|
2024-03-22 20:16:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
// We only want to filter/sort the cards list.
|
|
|
|
clusterizers[tabname_full].cards_list.applyFilter(txt_search.value.toLowerCase());
|
2024-03-29 16:21:02 +00:00
|
|
|
clusterizers[tabname_full].cards_list.update();
|
2024-03-27 20:11:13 +00:00
|
|
|
|
|
|
|
// If the search input has changed since selecting a button to populate it
|
|
|
|
// then we want to disable the button that previously populated the search input.
|
2024-03-22 20:16:45 +00:00
|
|
|
// tree view buttons
|
2024-03-27 20:11:13 +00:00
|
|
|
let btn = pane.querySelector(".tree-list-item[data-selected='']");
|
2024-03-25 17:50:44 +00:00
|
|
|
if (isElement(btn) && btn.dataset.path !== txt_search.value && "selected" in btn.dataset) {
|
|
|
|
clusterizers[tabname_full].tree_list.onRowSelected(btn.dataset.divId, btn, false);
|
2024-03-22 20:16:45 +00:00
|
|
|
}
|
|
|
|
// dirs view buttons
|
2024-03-27 20:11:13 +00:00
|
|
|
btn = pane.querySelector(".extra-network-dirs-view-button[data-selected='']");
|
2024-03-25 17:50:44 +00:00
|
|
|
if (isElement(btn) && btn.textContent.trim() !== txt_search.value) {
|
2024-03-22 20:16:45 +00:00
|
|
|
delete btn.dataset.selected;
|
|
|
|
}
|
2023-01-21 05:36:07 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-21 20:03:27 +00:00
|
|
|
function extraNetworksClusterizersEnable(tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Enables the selected tab's clusterize lists and disables all others. */
|
2024-03-21 20:03:27 +00:00
|
|
|
// iterate over tabnames
|
|
|
|
for (const [_tabname_full, tab_clusterizers] of Object.entries(clusterizers)) {
|
|
|
|
// iterate over clusterizers in tab
|
|
|
|
for (const v of Object.values(tab_clusterizers)) {
|
|
|
|
v.enable(_tabname_full === tabname_full);
|
|
|
|
}
|
2024-03-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function extraNetworkClusterizersLoadTab({
|
|
|
|
tabname_full = "",
|
|
|
|
selected = false,
|
|
|
|
fetch_data = false,
|
|
|
|
}={}) {
|
|
|
|
/** Loads clusterize data for a tab.
|
|
|
|
*
|
|
|
|
* Args:
|
|
|
|
* tabname_full [str]: The clusterize tab to load. Does not need to be the active
|
|
|
|
* tab however if it isn't the active tab then `selected` should be set to
|
|
|
|
* `false` to prevent oddities caused by the tab not being visible in the page.
|
|
|
|
* selected [bool]: Whether the tab is selected. This controls whether the
|
|
|
|
* clusterize list will be enabled which affects its operations.
|
|
|
|
* fetch_data [bool]: Whether to fetch new data for the clusterize list.
|
|
|
|
*/
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-03-22 20:16:45 +00:00
|
|
|
if (!(tabname_full in clusterizers)) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2024-03-26 17:58:21 +00:00
|
|
|
|
|
|
|
(async() => {
|
2024-03-29 16:21:02 +00:00
|
|
|
if (selected) {
|
|
|
|
extraNetworksClusterizersEnable(tabname_full);
|
|
|
|
}
|
2024-03-22 20:16:45 +00:00
|
|
|
for (const v of Object.values(clusterizers[tabname_full])) {
|
2024-03-29 16:21:02 +00:00
|
|
|
if (fetch_data) {
|
|
|
|
await v.setup();
|
|
|
|
} else {
|
|
|
|
await v.load();
|
|
|
|
}
|
2024-03-22 20:16:45 +00:00
|
|
|
}
|
|
|
|
})().then(() => {
|
|
|
|
return resolve();
|
2024-03-29 16:21:02 +00:00
|
|
|
}).catch(error => {
|
|
|
|
console.error("Error loading tab:", error);
|
|
|
|
return reject(error);
|
2024-03-22 20:16:45 +00:00
|
|
|
});
|
|
|
|
});
|
2024-03-21 20:03:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
function extraNetworksAutoSetTreeWidth(pane) {
|
|
|
|
if (!isElementLogError(pane)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-15 18:31:58 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
const tabname_full = pane.dataset.tabnameFull;
|
|
|
|
|
|
|
|
// This event is only applied to the currently selected tab if has clusterize lists.
|
|
|
|
if (!(tabname_full in clusterizers)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const row = pane.querySelector(".resize-handle-row");
|
|
|
|
if (!isElementLogError(row)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-15 18:31:58 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
const left_col = row.firstElementChild;
|
|
|
|
if (!isElementLogError(left_col)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left_col.classList.contains("hidden")) {
|
|
|
|
// If the left column is hidden then we don't want to do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pad = parseFloat(row.style.gridTemplateColumns.split(" ")[1]);
|
|
|
|
const min_left_col_width = parseFloat(left_col.style.flexBasis.slice(0, -2));
|
|
|
|
// We know that the tree list is the left column. That is the only one we want to resize.
|
|
|
|
let max_width = clusterizers[tabname_full].tree_list.getMaxRowWidth();
|
|
|
|
// Add the resize handle's padding to the result and default to minLeftColWidth if necessary.
|
|
|
|
max_width = Math.max(max_width + pad, min_left_col_width);
|
|
|
|
|
|
|
|
// Mimicks resizeHandle.js::setLeftColGridTemplate().
|
|
|
|
row.style.gridTemplateColumns = `${max_width}px ${pad}px 1fr`;
|
|
|
|
}
|
2024-03-15 18:31:58 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
function extraNetworksSetupEventDelegators() {
|
|
|
|
/** Sets up event delegators for all extraNetworks tabs.
|
|
|
|
*
|
|
|
|
* These event handlers are not tied to any specific elements on the page.
|
|
|
|
* We do this because elements within each tab may be removed and replaced
|
|
|
|
* which would break references to elements in DOM and thus prevent any event
|
|
|
|
* listeners from firing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
window.addEventListener("resizeHandleDblClick", event => {
|
|
|
|
// See resizeHandle.js::onDoubleClick() for event detail.
|
|
|
|
event.stopPropagation();
|
|
|
|
extraNetworksAutoSetTreeWidth(event.target.closest(".extra-network-pane"));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update search filter whenever the search input's clear button is pressed.
|
|
|
|
window.addEventListener("extra-network-control--search-clear", event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
extraNetworksApplyFilter(event.detail.tabname_full);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Debounce search text input. This way we only search after user is done typing.
|
|
|
|
const search_input_debounce = debounce((tabname_full) => {
|
|
|
|
extraNetworksApplyFilter(tabname_full);
|
|
|
|
}, SEARCH_INPUT_DEBOUNCE_TIME_MS);
|
|
|
|
|
|
|
|
window.addEventListener("keyup", event => {
|
|
|
|
const controls = event.target.closest(".extra-network-controls");
|
|
|
|
if (isElement(controls)) {
|
|
|
|
const tabname_full = controls.dataset.tabnameFull;
|
|
|
|
const target = event.target.closest(".extra-network-control--search-text");
|
|
|
|
if (isElement(target)) {
|
|
|
|
search_input_debounce.call(target, tabname_full);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-15 18:31:58 +00:00
|
|
|
function setupExtraNetworks() {
|
2024-03-20 18:01:38 +00:00
|
|
|
waitForBool(initialUiOptionsLoaded).then(() => {
|
2024-03-19 01:59:41 +00:00
|
|
|
extraNetworksSetupTab('txt2img');
|
|
|
|
extraNetworksSetupTab('img2img');
|
2024-03-27 20:11:13 +00:00
|
|
|
extraNetworksSetupEventDelegators();
|
2024-03-19 01:59:41 +00:00
|
|
|
});
|
2024-03-15 18:31:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 20:52:27 +00:00
|
|
|
function tryToRemoveExtraNetworkFromPrompt(textarea, text, isNeg) {
|
|
|
|
var m = text.match(isNeg ? re_extranet_neg : re_extranet);
|
2023-01-28 20:31:48 +00:00
|
|
|
var replaced = false;
|
2023-05-17 02:20:11 +00:00
|
|
|
var newTextareaText;
|
2024-01-18 03:38:51 +00:00
|
|
|
var extraTextBeforeNet = opts.extra_networks_add_text_separator;
|
2023-05-17 02:20:11 +00:00
|
|
|
if (m) {
|
2023-07-15 21:56:53 +00:00
|
|
|
var extraTextAfterNet = m[2];
|
2023-05-17 02:20:11 +00:00
|
|
|
var partToSearch = m[1];
|
2023-07-15 21:56:53 +00:00
|
|
|
var foundAtPosition = -1;
|
2024-03-26 17:58:21 +00:00
|
|
|
newTextareaText = textarea.value.replaceAll(isNeg ? re_extranet_g_neg : re_extranet_g, function(found, net, pos) {
|
2023-12-30 20:52:27 +00:00
|
|
|
m = found.match(isNeg ? re_extranet_neg : re_extranet);
|
2023-05-17 02:20:11 +00:00
|
|
|
if (m[1] == partToSearch) {
|
|
|
|
replaced = true;
|
2023-07-15 21:56:53 +00:00
|
|
|
foundAtPosition = pos;
|
2023-05-17 02:20:11 +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 02:20:11 +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 02:20:11 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-28 20:31:48 +00:00
|
|
|
if (replaced) {
|
|
|
|
textarea.value = newTextareaText;
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-28 20:31:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
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-01-28 20:31:48 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-12-30 20:52:27 +00:00
|
|
|
updateInput(textArea);
|
|
|
|
}
|
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function extraNetworksCardOnClick(event, tabname) {
|
|
|
|
const elem = event.currentTarget;
|
|
|
|
const prompt_elem = gradioApp().querySelector(`#${tabname}_prompt > label > textarea`);
|
|
|
|
const neg_prompt_elem = gradioApp().querySelector(`#${tabname}_neg_prompt > label > textarea`);
|
|
|
|
if ("negPrompt" in elem.dataset){
|
|
|
|
updatePromptArea(elem.dataset.prompt, prompt_elem);
|
|
|
|
updatePromptArea(elem.dataset.negPrompt, neg_prompt_elem);
|
|
|
|
} else if ("allowNeg" in elem.dataset) {
|
|
|
|
updatePromptArea(elem.dataset.prompt, activePromptTextarea[tabname]);
|
2023-12-30 20:52:27 +00:00
|
|
|
} else {
|
2024-03-29 16:21:02 +00:00
|
|
|
updatePromptArea(elem.dataset.prompt, prompt_elem);
|
2023-12-30 20:52:27 +00:00
|
|
|
}
|
2023-01-21 05:36:07 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-21 05:36:07 +00:00
|
|
|
function saveCardPreview(event, tabname, filename) {
|
2024-03-13 21:11:44 +00:00
|
|
|
var textarea = gradioApp().querySelector(`#${tabname}_preview_filename > label > textarea`);
|
|
|
|
var button = gradioApp().getElementById(`${tabname}_save_preview`);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-21 05:36:07 +00:00
|
|
|
textarea.value = filename;
|
|
|
|
updateInput(textarea);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-21 05:36:07 +00:00
|
|
|
button.click();
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-21 05:36:07 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksTreeProcessFileClick(event, btn, tabname_full) {
|
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-03-13 21:11:44 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param btn The clicked `tree-list-item` button.
|
|
|
|
* @param tabname_full The full active tabname.
|
|
|
|
* i.e. txt2img_lora, img2img_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-03-13 21:11:44 +00:00
|
|
|
function extraNetworksTreeProcessDirectoryClick(event, btn, tabname_full) {
|
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-03-13 21:11:44 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param btn The clicked `tree-list-item` button.
|
|
|
|
* @param tabname_full The full active tabname.
|
|
|
|
* i.e. txt2img_lora, img2img_checkpoints, etc.
|
2024-01-13 18:16:39 +00:00
|
|
|
*/
|
|
|
|
// 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;
|
2024-03-13 21:11:44 +00:00
|
|
|
const div_id = btn.dataset.divId;
|
2024-01-13 18:16:39 +00:00
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function _updateSearch(_search_text) {
|
2024-01-14 18:39:01 +00:00
|
|
|
// Update search input with select button's path.
|
2024-03-27 20:11:13 +00:00
|
|
|
const txt_search = gradioApp().querySelector(`#${tabname_full}_controls .extra-network-control--search-text`);
|
|
|
|
txt_search.value = _search_text;
|
|
|
|
updateInput(txt_search);
|
2024-03-19 01:59:41 +00:00
|
|
|
extraNetworksApplyFilter(tabname_full);
|
2024-01-14 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 18:40:47 +00:00
|
|
|
if (true_targ.matches(".tree-list-item-action--leading, .tree-list-item-action-chevron")) {
|
2024-03-25 14:26:29 +00:00
|
|
|
// If user clicks on the chevron, then we do not select the folder.
|
|
|
|
let prev_selected_elem = gradioApp().querySelector(".tree-list-item[data-selected='']");
|
|
|
|
clusterizers[tabname_full].tree_list.onRowExpandClick(div_id, btn);
|
|
|
|
let selected_elem = gradioApp().querySelector(".tree-list-item[data-selected='']");
|
2024-03-25 17:50:44 +00:00
|
|
|
if (isElement(prev_selected_elem) && !isElement(selected_elem)) {
|
2024-03-25 14:26:29 +00:00
|
|
|
// if a selected element was removed, clear filter.
|
2024-03-13 21:11:44 +00:00
|
|
|
_updateSearch("");
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
2024-03-25 14:26:29 +00:00
|
|
|
} else {
|
|
|
|
// user clicked anywhere else on row.
|
|
|
|
clusterizers[tabname_full].tree_list.onRowSelected(div_id, btn);
|
|
|
|
_updateSearch("selected" in btn.dataset ? btn.dataset.path : "");
|
2024-01-13 18:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksTreeOnClick(event, tabname_full) {
|
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-03-13 21:11:44 +00:00
|
|
|
* @param event The generated event.
|
|
|
|
* @param tabname_full The full active tabname.
|
|
|
|
* i.e. txt2img_lora, img2img_checkpoints, etc.
|
2024-01-13 18:16:39 +00:00
|
|
|
*/
|
2024-03-20 18:01:38 +00:00
|
|
|
let btn = event.target.closest(".tree-list-item");
|
|
|
|
if (btn.dataset.treeEntryType === "file") {
|
|
|
|
extraNetworksTreeProcessFileClick(event, btn, tabname_full);
|
|
|
|
} else {
|
|
|
|
extraNetworksTreeProcessDirectoryClick(event, btn, tabname_full);
|
|
|
|
}
|
|
|
|
event.stopPropagation();
|
2024-03-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
function extraNetworksDirsOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Handles `onclick` events for buttons in the directory view. */
|
2024-03-27 20:11:13 +00:00
|
|
|
const txt_search = gradioApp().querySelector(`#${tabname_full}_controls .extra-network-control--search-text`);
|
2024-03-15 23:11:30 +00:00
|
|
|
function _deselect_all() {
|
|
|
|
// deselect all buttons
|
|
|
|
gradioApp().querySelectorAll(".extra-network-dirs-view-button").forEach((elem) => {
|
|
|
|
delete elem.dataset.selected;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _select_button(elem) {
|
|
|
|
_deselect_all();
|
|
|
|
// Update search input with select button's path.
|
|
|
|
elem.dataset.selected = "";
|
|
|
|
txt_search.value = elem.textContent.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _deselect_button(elem) {
|
|
|
|
delete elem.dataset.selected;
|
|
|
|
txt_search.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ("selected" in event.target.dataset) {
|
|
|
|
_deselect_button(event.target);
|
|
|
|
} else {
|
|
|
|
_select_button(event.target);
|
|
|
|
}
|
2024-03-20 18:01:38 +00:00
|
|
|
|
2024-03-15 23:11:30 +00:00
|
|
|
updateInput(txt_search);
|
2024-03-19 01:59:41 +00:00
|
|
|
extraNetworksApplyFilter(tabname_full);
|
2024-03-15 18:31:58 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksControlSearchClearOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Dispatches custom event when the `clear` button in a search input is clicked. */
|
2024-03-13 21:11:44 +00:00
|
|
|
let clear_btn = event.target.closest(".extra-network-control--search-clear");
|
|
|
|
let txt_search = clear_btn.previousElementSibling;
|
|
|
|
txt_search.value = "";
|
2024-03-25 14:26:29 +00:00
|
|
|
txt_search.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
"extra-network-control--search-clear",
|
2024-03-27 20:11:13 +00:00
|
|
|
{
|
|
|
|
bubbles: true,
|
|
|
|
detail: {tabname_full: tabname_full}
|
|
|
|
},
|
2024-03-25 14:26:29 +00:00
|
|
|
)
|
|
|
|
);
|
2023-03-14 06:10:26 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksControlSortModeOnClick(event, tabname_full) {
|
2024-03-09 05:25:01 +00:00
|
|
|
/** Handles `onclick` events for Sort Mode buttons. */
|
2024-03-25 20:43:29 +00:00
|
|
|
event.currentTarget.parentElement.querySelectorAll('.extra-network-control--sort-mode').forEach(elem => {
|
|
|
|
delete elem.dataset.selected;
|
2024-03-09 05:25:01 +00:00
|
|
|
});
|
|
|
|
|
2024-03-25 20:43:29 +00:00
|
|
|
event.currentTarget.dataset.selected = "";
|
2024-03-09 04:24:25 +00:00
|
|
|
|
2024-03-14 20:51:52 +00:00
|
|
|
if (tabname_full in clusterizers) {
|
2024-03-23 17:05:03 +00:00
|
|
|
clusterizers[tabname_full].cards_list.setSortMode(
|
|
|
|
event.currentTarget.dataset.sortMode.toLowerCase()
|
|
|
|
);
|
2024-03-19 01:59:41 +00:00
|
|
|
extraNetworksApplyFilter(tabname_full);
|
2024-03-14 20:51:52 +00:00
|
|
|
}
|
2024-01-15 22:34:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksControlSortDirOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Handles `onclick` events for the Sort Direction button.
|
2024-01-16 18:35:01 +00:00
|
|
|
*
|
|
|
|
* Modifies the data attributes of the Sort Direction button to cycle between
|
|
|
|
* ascending and descending sort directions.
|
|
|
|
*/
|
2024-03-13 21:11:44 +00:00
|
|
|
if (event.currentTarget.dataset.sortDir.toLowerCase() == "ascending") {
|
|
|
|
event.currentTarget.dataset.sortDir = "descending";
|
2024-01-15 22:34:44 +00:00
|
|
|
event.currentTarget.setAttribute("title", "Sort descending");
|
|
|
|
} else {
|
2024-03-13 21:11:44 +00:00
|
|
|
event.currentTarget.dataset.sortDir = "ascending";
|
2024-01-15 22:34:44 +00:00
|
|
|
event.currentTarget.setAttribute("title", "Sort ascending");
|
|
|
|
}
|
2024-03-14 20:51:52 +00:00
|
|
|
|
|
|
|
if (tabname_full in clusterizers) {
|
2024-03-25 14:26:29 +00:00
|
|
|
clusterizers[tabname_full].cards_list.setSortDir(event.currentTarget.dataset.sortDir.toLowerCase());
|
2024-03-19 01:59:41 +00:00
|
|
|
extraNetworksApplyFilter(tabname_full);
|
2024-03-14 20:51:52 +00:00
|
|
|
}
|
2024-01-15 22:34:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksControlTreeViewOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Handles `onclick` events for the Tree View button.
|
2024-01-20 16:43:45 +00:00
|
|
|
*
|
|
|
|
* Toggles the tree view in the extra networks pane.
|
|
|
|
*/
|
2024-03-25 20:43:29 +00:00
|
|
|
let show;
|
|
|
|
if ("selected" in event.currentTarget.dataset) {
|
|
|
|
delete event.currentTarget.dataset.selected;
|
|
|
|
show = false;
|
|
|
|
} else {
|
|
|
|
event.currentTarget.dataset.selected = "";
|
|
|
|
show = true;
|
|
|
|
}
|
2024-03-08 06:52:25 +00:00
|
|
|
|
2024-03-22 20:16:45 +00:00
|
|
|
gradioApp().getElementById(`${tabname_full}_tree_list_scroll_area`).parentElement.classList.toggle("hidden", !show);
|
|
|
|
clusterizers[tabname_full].tree_list.enable(show);
|
2024-03-19 01:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksControlDirsViewOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Handles `onclick` events for the Dirs View button.
|
2024-03-19 01:59:41 +00:00
|
|
|
*
|
|
|
|
* Toggles the directory view in the extra networks pane.
|
|
|
|
*/
|
2024-03-25 20:43:29 +00:00
|
|
|
let show;
|
|
|
|
if ("selected" in event.currentTarget.dataset) {
|
|
|
|
delete event.currentTarget.dataset.selected;
|
|
|
|
show = false;
|
|
|
|
} else {
|
|
|
|
event.currentTarget.dataset.selected = "";
|
|
|
|
show = true;
|
|
|
|
}
|
2024-03-14 20:51:52 +00:00
|
|
|
|
2024-03-27 20:11:13 +00:00
|
|
|
const pane = gradioApp().getElementById(`${tabname_full}_pane`);
|
|
|
|
pane.querySelector(".extra-network-content--dirs-view").classList.toggle("hidden", !show);
|
2024-01-20 16:43:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
function extraNetworksControlRefreshOnClick(event, tabname_full) {
|
2024-03-25 17:50:44 +00:00
|
|
|
/** Handles `onclick` events for the Refresh Page button.
|
2024-01-16 18:35:01 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-03-22 20:16:45 +00:00
|
|
|
// reset states
|
2024-03-20 18:01:38 +00:00
|
|
|
initialUiOptionsLoaded.state = false;
|
2024-03-29 16:21:02 +00:00
|
|
|
|
|
|
|
// We want to reset all clusterizers on refresh click so that the viewing area
|
|
|
|
// shows that it is loading new data.
|
|
|
|
for (const _tabname_full of Object.keys(clusterizers)) {
|
|
|
|
for (const v of Object.values(clusterizers[_tabname_full])) {
|
|
|
|
v.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:16:45 +00:00
|
|
|
// Fire an event for this button click.
|
|
|
|
gradioApp().getElementById(`${tabname_full}_extra_refresh_internal`).dispatchEvent(new Event("click"));
|
2024-01-15 22:34:44 +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-03-14 06:10:26 +00:00
|
|
|
function popup(contents) {
|
|
|
|
if (!globalPopup) {
|
|
|
|
globalPopup = document.createElement('div');
|
|
|
|
globalPopup.classList.add('global-popup');
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-03-14 06:10:26 +00:00
|
|
|
var close = document.createElement('div');
|
|
|
|
close.classList.add('global-popup-close');
|
2023-10-03 04:22:15 +00:00
|
|
|
close.addEventListener("click", closePopup);
|
2023-03-14 06:10:26 +00:00
|
|
|
close.title = "Close";
|
|
|
|
globalPopup.appendChild(close);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-03-14 06:10:26 +00:00
|
|
|
globalPopupInner = document.createElement('div');
|
|
|
|
globalPopupInner.classList.add('global-popup-inner');
|
|
|
|
globalPopup.appendChild(globalPopupInner);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-07-17 15:56:14 +00:00
|
|
|
gradioApp().querySelector('.main').appendChild(globalPopup);
|
2023-03-14 06:10:26 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-03-14 06:10:26 +00:00
|
|
|
globalPopupInner.innerHTML = '';
|
|
|
|
globalPopupInner.appendChild(contents);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-03-14 06:10:26 +00:00
|
|
|
globalPopup.style.display = "flex";
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-08-27 06:39:37 +00:00
|
|
|
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-03-25 09:10:03 +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) {
|
2024-03-14 20:51:52 +00:00
|
|
|
console.error(error);
|
2024-03-04 23:46:25 +00:00
|
|
|
}
|
2024-03-04 23:33:22 +00:00
|
|
|
|
2023-04-30 19:08:52 +00:00
|
|
|
var elem = document.createElement('pre');
|
2023-03-14 06:10:26 +00:00
|
|
|
elem.classList.add('popup-metadata');
|
|
|
|
elem.textContent = text;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-03-14 06:10:26 +00:00
|
|
|
popup(elem);
|
2024-03-04 23:33:22 +00:00
|
|
|
return;
|
2023-03-14 06:10:26 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function requestGetPromise(url, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
let args = Object.keys(data).map(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 {
|
|
|
|
resolve(xhr.responseText);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject({status: this.status, statusText: xhr.statusText});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send(JSON.stringify(data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-25 07:11:04 +00:00
|
|
|
function requestGet(url, data, handler, errorHandler) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
2024-03-26 17:58:21 +00:00
|
|
|
var args = Object.keys(data).map(function(k) {
|
2023-03-25 07:11:04 +00:00
|
|
|
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
|
|
|
|
}).join('&');
|
|
|
|
xhr.open("GET", url + "?" + args, true);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
2023-03-25 07:11:04 +00:00
|
|
|
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 12:46:58 +00:00
|
|
|
}
|
2023-03-25 07:11:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
var js = JSON.stringify(data);
|
|
|
|
xhr.send(js);
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-14 20:51:52 +00:00
|
|
|
function extraNetworksCopyPathToClipboard(event, path) {
|
2024-01-08 19:10:03 +00:00
|
|
|
navigator.clipboard.writeText(path);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
async function extraNetworksRequestListData(tabname, extra_networks_tabname, class_name) {
|
|
|
|
return await requestGetPromise(
|
|
|
|
"./sd_extra_networks/get-list-data",
|
|
|
|
{
|
|
|
|
tabname: tabname,
|
|
|
|
extra_networks_tabname: extra_networks_tabname,
|
|
|
|
list_type: class_name,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function extraNetworksRequestMetadata(extra_networks_tabname, card_name) {
|
2024-03-26 17:58:21 +00:00
|
|
|
var showError = function() {
|
2023-04-30 19:08:52 +00:00
|
|
|
extraNetworksShowMetadata("there was an error getting metadata");
|
|
|
|
};
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
requestGet("./sd_extra_networks/metadata", {page: extra_networks_tabname, item: card_name}, function(data) {
|
2023-03-25 07:11:04 +00:00
|
|
|
if (data && data.metadata) {
|
|
|
|
extraNetworksShowMetadata(data.metadata);
|
|
|
|
} else {
|
|
|
|
showError();
|
|
|
|
}
|
|
|
|
}, showError);
|
2024-03-29 16:21:02 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function extraNetworksMetadataButtonOnClick(event, extra_networks_tabname, card_name) {
|
|
|
|
extraNetworksRequestMetadata(extra_networks_tabname, card_name);
|
2023-03-25 09:10:03 +00:00
|
|
|
event.stopPropagation();
|
2023-03-25 07:11:04 +00:00
|
|
|
}
|
2023-07-15 17:39:04 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function extraNetworksEditUserMetadata(tabname_full, card_name) {
|
|
|
|
const id = `${tabname_full}_edit_user_metadata`;
|
|
|
|
let editor = extraPageUserMetadataEditors[id];
|
2023-07-15 18:05:33 +00:00
|
|
|
if (!editor) {
|
2023-07-15 17:39:04 +00:00
|
|
|
editor = {};
|
|
|
|
editor.page = gradioApp().getElementById(id);
|
2024-03-29 16:21:02 +00:00
|
|
|
editor.nameTextarea = gradioApp().querySelector(`#${id}_name textarea`);
|
|
|
|
editor.button = gradioApp().querySelector(`#${id}_button`);
|
2023-07-15 17:39:04 +00:00
|
|
|
extraPageUserMetadataEditors[id] = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
editor.nameTextarea.value = card_name;
|
2023-07-15 17:39:04 +00:00
|
|
|
updateInput(editor.nameTextarea);
|
|
|
|
|
|
|
|
editor.button.click();
|
|
|
|
|
|
|
|
popup(editor.page);
|
2024-03-29 16:21:02 +00:00
|
|
|
}
|
2023-07-15 17:39:04 +00:00
|
|
|
|
2024-03-29 16:21:02 +00:00
|
|
|
function extraNetworksEditItemOnClick(event, tabname_full, card_name) {
|
|
|
|
extraNetworksEditUserMetadata(tabname_full, card_name);
|
2023-07-15 17:39:04 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2023-07-15 21:56:53 +00:00
|
|
|
|
2023-07-16 05:38:23 +00:00
|
|
|
function extraNetworksRefreshSingleCard(page, tabname, name) {
|
2024-03-26 17:58:21 +00:00
|
|
|
requestGet("./sd_extra_networks/get-single-card", {page: page, tabname: tabname, name: name}, function(data) {
|
2023-07-16 05:38:23 +00:00
|
|
|
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
|
|
|
|
2024-03-26 17:58:21 +00:00
|
|
|
window.addEventListener("keydown", function(event) {
|
2023-12-01 05:36:12 +00:00
|
|
|
if (event.key == "Escape") {
|
|
|
|
closePopup();
|
|
|
|
}
|
|
|
|
});
|
2024-01-13 18:16:39 +00:00
|
|
|
|
2024-03-13 21:11:44 +00:00
|
|
|
onUiLoaded(setupExtraNetworks);
|
2024-03-26 17:58:21 +00:00
|
|
|
onOptionsChanged(() => initialUiOptionsLoaded.state = true);
|