2023-01-21 05:36:07 +00:00
|
|
|
function setupExtraNetworksForTab(tabname){
|
|
|
|
gradioApp().querySelector('#'+tabname+'_extra_tabs').classList.add('extra-networks')
|
|
|
|
|
2023-01-21 14:20:24 +00:00
|
|
|
var tabs = gradioApp().querySelector('#'+tabname+'_extra_tabs > div')
|
|
|
|
var search = gradioApp().querySelector('#'+tabname+'_extra_search textarea')
|
|
|
|
var refresh = gradioApp().getElementById(tabname+'_extra_refresh')
|
|
|
|
|
|
|
|
search.classList.add('search')
|
|
|
|
tabs.appendChild(search)
|
|
|
|
tabs.appendChild(refresh)
|
|
|
|
|
2023-05-08 08:33:45 +00:00
|
|
|
var applyFilter = function(){
|
2023-04-30 19:08:52 +00:00
|
|
|
var searchTerm = search.value.toLowerCase()
|
2023-01-21 14:20:24 +00:00
|
|
|
|
|
|
|
gradioApp().querySelectorAll('#'+tabname+'_extra_tabs div.card').forEach(function(elem){
|
2023-05-08 08:33:45 +00:00
|
|
|
var searchOnly = elem.querySelector('.search_only')
|
2023-04-30 19:08:52 +00:00
|
|
|
var text = elem.querySelector('.name').textContent.toLowerCase() + " " + elem.querySelector('.search_term').textContent.toLowerCase()
|
2023-05-08 08:33:45 +00:00
|
|
|
|
|
|
|
var visible = text.indexOf(searchTerm) != -1
|
|
|
|
|
|
|
|
if(searchOnly && searchTerm.length < 4){
|
|
|
|
visible = false
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.style.display = visible ? "" : "none"
|
2023-01-21 14:20:24 +00:00
|
|
|
})
|
2023-05-08 08:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
search.addEventListener("input", applyFilter);
|
|
|
|
applyFilter();
|
|
|
|
|
|
|
|
extraNetworksApplyFilter[tabname] = applyFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyExtraNetworkFilter(tabname){
|
|
|
|
setTimeout(extraNetworksApplyFilter[tabname], 1);
|
2023-01-21 05:36:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:33:45 +00:00
|
|
|
var extraNetworksApplyFilter = {}
|
2023-01-21 06:48:38 +00:00
|
|
|
var activePromptTextarea = {};
|
2023-01-21 05:36:07 +00:00
|
|
|
|
|
|
|
function setupExtraNetworks(){
|
|
|
|
setupExtraNetworksForTab('txt2img')
|
|
|
|
setupExtraNetworksForTab('img2img')
|
|
|
|
|
2023-01-21 06:48:38 +00:00
|
|
|
function registerPrompt(tabname, id){
|
2023-01-21 05:36:07 +00:00
|
|
|
var textarea = gradioApp().querySelector("#" + id + " > label > textarea");
|
|
|
|
|
2023-01-21 06:48:38 +00:00
|
|
|
if (! activePromptTextarea[tabname]){
|
|
|
|
activePromptTextarea[tabname] = textarea
|
2023-01-21 05:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
textarea.addEventListener("focus", function(){
|
2023-01-21 06:48:38 +00:00
|
|
|
activePromptTextarea[tabname] = textarea;
|
2023-01-21 05:36:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-21 06:48:38 +00:00
|
|
|
registerPrompt('txt2img', 'txt2img_prompt')
|
|
|
|
registerPrompt('txt2img', 'txt2img_neg_prompt')
|
|
|
|
registerPrompt('img2img', 'img2img_prompt')
|
|
|
|
registerPrompt('img2img', 'img2img_neg_prompt')
|
2023-01-21 05:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onUiLoaded(setupExtraNetworks)
|
|
|
|
|
2023-01-28 20:31:48 +00:00
|
|
|
var re_extranet = /<([^:]+:[^:]+):[\d\.]+>/;
|
|
|
|
var re_extranet_g = /\s+<([^:]+:[^:]+):[\d\.]+>/g;
|
|
|
|
|
|
|
|
function tryToRemoveExtraNetworkFromPrompt(textarea, text){
|
|
|
|
var m = text.match(re_extranet)
|
|
|
|
if(! m) return false
|
|
|
|
|
|
|
|
var partToSearch = m[1]
|
|
|
|
var replaced = false
|
2023-04-30 19:12:24 +00:00
|
|
|
var newTextareaText = textarea.value.replaceAll(re_extranet_g, function(found){
|
2023-01-28 20:31:48 +00:00
|
|
|
m = found.match(re_extranet);
|
|
|
|
if(m[1] == partToSearch){
|
|
|
|
replaced = true;
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
})
|
|
|
|
|
|
|
|
if(replaced){
|
|
|
|
textarea.value = newTextareaText
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-21 06:48:38 +00:00
|
|
|
function cardClicked(tabname, textToAdd, allowNegativePrompt){
|
|
|
|
var textarea = allowNegativePrompt ? activePromptTextarea[tabname] : gradioApp().querySelector("#" + tabname + "_prompt > label > textarea")
|
2023-01-21 05:36:07 +00:00
|
|
|
|
2023-01-28 20:31:48 +00:00
|
|
|
if(! tryToRemoveExtraNetworkFromPrompt(textarea, textToAdd)){
|
2023-03-11 11:18:18 +00:00
|
|
|
textarea.value = textarea.value + opts.extra_networks_add_text_separator + textToAdd
|
2023-01-28 20:31:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 05:36:07 +00:00
|
|
|
updateInput(textarea)
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveCardPreview(event, tabname, filename){
|
2023-01-21 06:48:38 +00:00
|
|
|
var textarea = gradioApp().querySelector("#" + tabname + '_preview_filename > label > textarea')
|
|
|
|
var button = gradioApp().getElementById(tabname + '_save_preview')
|
2023-01-21 05:36:07 +00:00
|
|
|
|
|
|
|
textarea.value = filename
|
|
|
|
updateInput(textarea)
|
|
|
|
|
|
|
|
button.click()
|
|
|
|
|
|
|
|
event.stopPropagation()
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
2023-01-29 08:34:58 +00:00
|
|
|
|
|
|
|
function extraNetworksSearchButton(tabs_id, event){
|
2023-04-30 19:08:52 +00:00
|
|
|
var searchTextarea = gradioApp().querySelector("#" + tabs_id + ' > div > textarea')
|
|
|
|
var button = event.target
|
|
|
|
var text = button.classList.contains("search-all") ? "" : button.textContent.trim()
|
2023-01-29 08:34:58 +00:00
|
|
|
|
|
|
|
searchTextarea.value = text
|
|
|
|
updateInput(searchTextarea)
|
2023-03-14 06:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var globalPopup = null;
|
|
|
|
var globalPopupInner = null;
|
|
|
|
function popup(contents){
|
|
|
|
if(! globalPopup){
|
|
|
|
globalPopup = document.createElement('div')
|
|
|
|
globalPopup.onclick = function(){ globalPopup.style.display = "none"; };
|
|
|
|
globalPopup.classList.add('global-popup');
|
|
|
|
|
|
|
|
var close = document.createElement('div')
|
|
|
|
close.classList.add('global-popup-close');
|
|
|
|
close.onclick = function(){ globalPopup.style.display = "none"; };
|
|
|
|
close.title = "Close";
|
|
|
|
globalPopup.appendChild(close)
|
|
|
|
|
|
|
|
globalPopupInner = document.createElement('div')
|
|
|
|
globalPopupInner.onclick = function(event){ event.stopPropagation(); return false; };
|
|
|
|
globalPopupInner.classList.add('global-popup-inner');
|
|
|
|
globalPopup.appendChild(globalPopupInner)
|
|
|
|
|
|
|
|
gradioApp().appendChild(globalPopup);
|
|
|
|
}
|
|
|
|
|
|
|
|
globalPopupInner.innerHTML = '';
|
|
|
|
globalPopupInner.appendChild(contents);
|
|
|
|
|
|
|
|
globalPopup.style.display = "flex";
|
|
|
|
}
|
|
|
|
|
2023-03-25 09:10:03 +00:00
|
|
|
function extraNetworksShowMetadata(text){
|
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;
|
|
|
|
|
|
|
|
popup(elem);
|
|
|
|
}
|
2023-03-25 07:11:04 +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);
|
|
|
|
}
|
|
|
|
|
2023-03-25 09:10:03 +00:00
|
|
|
function extraNetworksRequestMetadata(event, extraPage, cardName){
|
2023-04-30 19:08:52 +00:00
|
|
|
var showError = function(){ extraNetworksShowMetadata("there was an error getting metadata"); }
|
2023-03-25 07:11:04 +00:00
|
|
|
|
|
|
|
requestGet("./sd_extra_networks/metadata", {"page": extraPage, "item": cardName}, function(data){
|
|
|
|
if(data && data.metadata){
|
|
|
|
extraNetworksShowMetadata(data.metadata)
|
|
|
|
} else{
|
|
|
|
showError()
|
|
|
|
}
|
|
|
|
}, showError)
|
2023-03-25 09:10:03 +00:00
|
|
|
|
|
|
|
event.stopPropagation()
|
2023-03-25 07:11:04 +00:00
|
|
|
}
|