2023-11-05 05:55:54 +00:00
|
|
|
function inputAccordionChecked(id, checked) {
|
|
|
|
var accordion = gradioApp().getElementById(id);
|
|
|
|
accordion.visibleCheckbox.checked = checked;
|
|
|
|
accordion.onVisibleCheckboxChange();
|
|
|
|
}
|
2023-08-10 08:20:46 +00:00
|
|
|
|
2023-11-05 06:20:05 +00:00
|
|
|
function setupAccordion(accordion) {
|
2023-11-05 05:55:54 +00:00
|
|
|
var labelWrap = accordion.querySelector('.label-wrap');
|
|
|
|
var gradioCheckbox = gradioApp().querySelector('#' + accordion.id + "-checkbox input");
|
|
|
|
var extra = gradioApp().querySelector('#' + accordion.id + "-extra");
|
|
|
|
var span = labelWrap.querySelector('span');
|
|
|
|
var linked = true;
|
2023-08-10 08:20:46 +00:00
|
|
|
|
2023-11-05 06:20:05 +00:00
|
|
|
var isOpen = function() {
|
|
|
|
return labelWrap.classList.contains('open');
|
|
|
|
};
|
2023-08-10 08:20:46 +00:00
|
|
|
|
2023-11-05 05:55:54 +00:00
|
|
|
var observerAccordionOpen = new MutationObserver(function(mutations) {
|
|
|
|
mutations.forEach(function(mutationRecord) {
|
|
|
|
accordion.classList.toggle('input-accordion-open', isOpen());
|
|
|
|
|
2023-11-05 06:20:05 +00:00
|
|
|
if (linked) {
|
2023-11-05 05:55:54 +00:00
|
|
|
accordion.visibleCheckbox.checked = isOpen();
|
|
|
|
accordion.onVisibleCheckboxChange();
|
|
|
|
}
|
|
|
|
});
|
2023-08-10 08:20:46 +00:00
|
|
|
});
|
2023-11-05 05:55:54 +00:00
|
|
|
observerAccordionOpen.observe(labelWrap, {attributes: true, attributeFilter: ['class']});
|
2023-08-10 08:20:46 +00:00
|
|
|
|
2023-11-05 05:55:54 +00:00
|
|
|
if (extra) {
|
|
|
|
labelWrap.insertBefore(extra, labelWrap.lastElementChild);
|
|
|
|
}
|
|
|
|
|
2023-11-05 06:20:05 +00:00
|
|
|
accordion.onChecked = function(checked) {
|
2023-11-05 05:55:54 +00:00
|
|
|
if (isOpen() != checked) {
|
|
|
|
labelWrap.click();
|
|
|
|
}
|
2023-11-05 06:20:05 +00:00
|
|
|
};
|
2023-11-05 05:55:54 +00:00
|
|
|
|
|
|
|
var visibleCheckbox = document.createElement('INPUT');
|
|
|
|
visibleCheckbox.type = 'checkbox';
|
|
|
|
visibleCheckbox.checked = isOpen();
|
2023-11-05 06:20:05 +00:00
|
|
|
visibleCheckbox.id = accordion.id + "-visible-checkbox";
|
2023-11-05 05:55:54 +00:00
|
|
|
visibleCheckbox.className = gradioCheckbox.className + " input-accordion-checkbox";
|
|
|
|
span.insertBefore(visibleCheckbox, span.firstChild);
|
|
|
|
|
|
|
|
accordion.visibleCheckbox = visibleCheckbox;
|
2023-11-05 06:20:05 +00:00
|
|
|
accordion.onVisibleCheckboxChange = function() {
|
|
|
|
if (linked && isOpen() != visibleCheckbox.checked) {
|
2023-11-05 05:55:54 +00:00
|
|
|
labelWrap.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
gradioCheckbox.checked = visibleCheckbox.checked;
|
|
|
|
updateInput(gradioCheckbox);
|
|
|
|
};
|
|
|
|
|
2023-11-05 06:20:05 +00:00
|
|
|
visibleCheckbox.addEventListener('click', function(event) {
|
2023-11-05 05:55:54 +00:00
|
|
|
linked = false;
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
visibleCheckbox.addEventListener('input', accordion.onVisibleCheckboxChange);
|
2023-08-10 08:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onUiLoaded(function() {
|
|
|
|
for (var accordion of gradioApp().querySelectorAll('.input-accordion')) {
|
2023-11-05 05:55:54 +00:00
|
|
|
setupAccordion(accordion);
|
2023-08-10 08:20:46 +00:00
|
|
|
}
|
|
|
|
});
|