cleanup code in pr #75
JS formatting, fix the visibility toggle to make slightly more sense, reinstate a few standardized labels
This commit is contained in:
+46
-56
@@ -1,63 +1,53 @@
|
||||
(function(){
|
||||
var accordions = {};
|
||||
var enabled = {};
|
||||
onUiUpdate(() => {
|
||||
var accordion_id_prefix = "#dynthres_";
|
||||
var extension_checkbox_class = ".dynthres-enabled";
|
||||
let accordions = {};
|
||||
let enabled = {};
|
||||
onUiUpdate(() => {
|
||||
let accordion_id_prefix = "#dynthres_";
|
||||
let extension_checkbox_class = ".dynthres-enabled";
|
||||
|
||||
function triggerEvent(element, eventName) {
|
||||
var event = document.createEvent("HTMLEvents");
|
||||
event.initEvent(eventName, false, true);
|
||||
element.dispatchEvent(event);
|
||||
}
|
||||
function attachEnabledButtonListener(checkbox, accordion) {
|
||||
let span = accordion.querySelector('.label-wrap span');
|
||||
let badge = document.createElement('input');
|
||||
badge.type = "checkbox";
|
||||
badge.checked = checkbox.checked;
|
||||
badge.addEventListener('click', (e) => {
|
||||
checkbox.checked = !checkbox.checked;
|
||||
checkbox.dispatchEvent(new Event('change'));
|
||||
e.stopPropagation();
|
||||
});
|
||||
|
||||
function updateActiveState(checkbox, accordion) {
|
||||
// change checkbox state
|
||||
const badge = accordion.querySelector('.label-wrap span input');
|
||||
badge.checked = checkbox.checked;
|
||||
}
|
||||
badge.className = checkbox.className;
|
||||
badge.classList.add('primary');
|
||||
span.insertBefore(badge, span.firstChild);
|
||||
let space = document.createElement('span');
|
||||
space.innerHTML = " ";
|
||||
span.insertBefore(space, badge.nextSibling);
|
||||
|
||||
function attachEnabledButtonListener(checkbox, accordion) {
|
||||
// add checkbox
|
||||
const span = accordion.querySelector('.label-wrap span');
|
||||
const badge = document.createElement('input');
|
||||
badge.type = "checkbox";
|
||||
badge.checked = checkbox.checked;
|
||||
badge.addEventListener('click', (e) => {
|
||||
checkbox.checked = !checkbox.checked;
|
||||
triggerEvent(checkbox, 'change');
|
||||
e.stopPropagation();
|
||||
});
|
||||
checkbox.addEventListener('click', () => {
|
||||
let badge = accordion.querySelector('.label-wrap span input');
|
||||
badge.checked = checkbox.checked;
|
||||
});
|
||||
checkbox.parentNode.style.display = "none";
|
||||
}
|
||||
|
||||
badge.className = checkbox.className;
|
||||
badge.classList.add('primary');
|
||||
span.insertBefore(badge, span.firstChild);
|
||||
var space = document.createElement('span');
|
||||
space.innerHTML = " ";
|
||||
span.insertBefore(space, badge.nextSibling);
|
||||
if (Object.keys(accordions).length < 2) {
|
||||
let accordion = gradioApp().querySelector(accordion_id_prefix + 'txt2img');
|
||||
if (accordion) {
|
||||
accordions.txt2img = accordion;
|
||||
}
|
||||
accordion = gradioApp().querySelector(accordion_id_prefix + 'img2img');
|
||||
if (accordion) {
|
||||
accordions.img2img = accordion;
|
||||
}
|
||||
}
|
||||
|
||||
checkbox.addEventListener('click', () => {
|
||||
updateActiveState(checkbox, accordion);
|
||||
});
|
||||
checkbox.parentNode.style.display = "none";
|
||||
}
|
||||
|
||||
if (Object.keys(accordions).length < 2) {
|
||||
var accordion = gradioApp().querySelector(accordion_id_prefix + 'txt2img');
|
||||
if (accordion)
|
||||
accordions.txt2img = accordion;
|
||||
accordion = gradioApp().querySelector(accordion_id_prefix + 'img2img');
|
||||
if (accordion)
|
||||
accordions.img2img = accordion;
|
||||
}
|
||||
|
||||
if (Object.keys(accordions).length > 0 && accordions.txt2img && !enabled.txt2img) {
|
||||
enabled.txt2img = accordions.txt2img.querySelector(extension_checkbox_class + ' input');
|
||||
attachEnabledButtonListener(enabled.txt2img, accordions.txt2img);
|
||||
}
|
||||
if (Object.keys(accordions).length > 0 && accordions.img2img && !enabled.img2img) {
|
||||
enabled.img2img = accordions.img2img.querySelector(extension_checkbox_class + ' input');
|
||||
attachEnabledButtonListener(enabled.img2img, accordions.img2img);
|
||||
}
|
||||
});
|
||||
if (Object.keys(accordions).length > 0 && accordions.txt2img && !enabled.txt2img) {
|
||||
enabled.txt2img = accordions.txt2img.querySelector(extension_checkbox_class + ' input');
|
||||
attachEnabledButtonListener(enabled.txt2img, accordions.txt2img);
|
||||
}
|
||||
if (Object.keys(accordions).length > 0 && accordions.img2img && !enabled.img2img) {
|
||||
enabled.img2img = accordions.img2img.querySelector(extension_checkbox_class + ' input');
|
||||
attachEnabledButtonListener(enabled.img2img, accordions.img2img);
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user