mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
import ApplicationController from "./application_controller";
|
|
|
|
export default class extends ApplicationController {
|
|
connect() {
|
|
super.connect();
|
|
window.addEventListener("click", this.closeOnClickOutside);
|
|
this.computeItemsHeight();
|
|
}
|
|
|
|
disconnect() {
|
|
super.disconnect();
|
|
window.removeEventListener("click", this.closeOnClickOutside);
|
|
}
|
|
|
|
initialize() {
|
|
this.close();
|
|
}
|
|
|
|
afterReflex() {
|
|
this.computeItemsHeight();
|
|
}
|
|
|
|
toggle = (event) => {
|
|
event.preventDefault();
|
|
this.element.querySelector(".selector").classList.toggle("selector-close");
|
|
};
|
|
|
|
// Private
|
|
closeOnClickOutside = (event) => {
|
|
if (
|
|
!this.element.contains(event.target) &&
|
|
this.isVisible(this.element.querySelector(".selector-wrapper"))
|
|
) {
|
|
this.close();
|
|
}
|
|
};
|
|
|
|
computeItemsHeight = () => {
|
|
const items = this.element.querySelector(".selector-items");
|
|
const rect = items.getBoundingClientRect();
|
|
items.style.maxHeight = `calc(100vh - ${rect.height}px)`;
|
|
};
|
|
|
|
isVisible = (element) => {
|
|
const style = window.getComputedStyle(element);
|
|
return style.display !== "none" && style.visibility !== "hidden";
|
|
};
|
|
|
|
close = () => {
|
|
this.element.querySelector(".selector").classList.add("selector-close");
|
|
};
|
|
}
|