Recount-Server/web/static/dropdowns.js

76 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2024-01-20 15:05:06 -08:00
import {
computePosition,
flip,
shift,
} from 'https://cdn.jsdelivr.net/npm/@floating-ui/dom@1.5.4/+esm';
function update_position(button, dropdown_div) {
const placement = button.dataset.dropdownDirection;
computePosition(button, dropdown_div, {
placement: placement,
middleware: [
flip(),
shift()
],
}).then(({x, y}) => {
Object.assign(dropdown_div.style, {
left: `${x}px`,
top: `${y}px`,
});
})
}
function setup_dropdown(button) {
const dropdown_div = document.querySelector("#" + button.dataset.dropdownTarget);
update_position(button, dropdown_div);
2024-01-20 21:51:10 -08:00
dropdown_div.style.display = "none";
dropdown_div.style.maxHeight = "0px";
2024-01-20 15:05:06 -08:00
return function () {
if (dropdown_div.style.display == "none") {
2024-01-20 21:51:10 -08:00
update_position(button, dropdown_div);
button.style.borderBottomLeftRadius = "0px";
button.style.borderBottomRightRadius = "0px";
2024-01-20 15:05:06 -08:00
2024-01-20 21:51:10 -08:00
dropdown_div.style.overflow = "hidden";
dropdown_div.style.display = "block";
dropdown_div.style.maxHeight = "0px";
2024-01-20 15:05:06 -08:00
setTimeout(function () {
2024-01-20 21:51:10 -08:00
dropdown_div.style.maxHeight = "150px";
dropdown_div.style.removeProperty("overflow");
}, 200);
2024-01-20 15:05:06 -08:00
} else {
2024-01-20 21:51:10 -08:00
dropdown_div.style.overflow = "hidden";
dropdown_div.style.maxHeight = "0px";
2024-01-20 15:05:06 -08:00
setTimeout(function () {
2024-01-20 21:51:10 -08:00
button.style.removeProperty("border-bottom-left-radius");
button.style.removeProperty("border-bottom-right-radius");
2024-01-20 15:05:06 -08:00
dropdown_div.style.display = "none";
2024-01-20 21:51:10 -08:00
dropdown_div.style.removeProperty("overflow");
2024-01-20 15:05:06 -08:00
}, 200);
}
};
}
2024-01-21 20:46:11 -08:00
function select_value_change(el) {
return function () {
var enable_div = el.options[el.selectedIndex].dataset.selectDiv;
el.parentNode.querySelectorAll("div").forEach(function (div_el) {
div_el.style.display = "none";
});
el.parentNode.querySelector("#" + enable_div).style.removeProperty("display");
}
}
2024-01-20 15:05:06 -08:00
function register_dropdowns() {
document.querySelectorAll(".dropdown-button").forEach(function (el) {
el.addEventListener("click", setup_dropdown(el));
});
2024-01-21 20:46:11 -08:00
document.querySelectorAll(".select-swapout").forEach(function (el) {
el.addEventListener("change", select_value_change(el));
});
2024-01-20 15:05:06 -08:00
}
export {register_dropdowns}