2019-08-24 13:26:28 -04:00
|
|
|
import {onSelect} from "../services/dom";
|
2022-11-21 12:35:19 -05:00
|
|
|
import {KeyboardNavigationHandler} from "../services/keyboard-navigation";
|
2022-11-15 06:24:31 -05:00
|
|
|
import {Component} from "./component";
|
2019-08-24 13:26:28 -04:00
|
|
|
|
2017-08-06 13:01:49 -04:00
|
|
|
/**
|
|
|
|
* Dropdown
|
|
|
|
* Provides some simple logic to create simple dropdown menus.
|
|
|
|
*/
|
2022-11-15 06:24:31 -05:00
|
|
|
export class Dropdown extends Component {
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.menu = this.$refs.menu;
|
|
|
|
this.toggle = this.$refs.toggle;
|
|
|
|
this.moveMenu = this.$opts.moveMenu;
|
2021-04-19 16:41:13 -04:00
|
|
|
this.bubbleEscapes = this.$opts.bubbleEscapes === 'true';
|
2020-06-24 15:38:08 -04:00
|
|
|
|
2020-04-05 08:07:19 -04:00
|
|
|
this.direction = (document.dir === 'rtl') ? 'right' : 'left';
|
2019-05-05 09:43:26 -04:00
|
|
|
this.body = document.body;
|
2019-08-24 13:26:28 -04:00
|
|
|
this.showing = false;
|
2022-11-21 12:35:19 -05:00
|
|
|
|
2020-12-31 12:25:20 -05:00
|
|
|
this.hide = this.hide.bind(this);
|
2022-11-21 12:35:19 -05:00
|
|
|
this.setupListeners();
|
2017-08-06 13:01:49 -04:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
show(event = null) {
|
2019-05-06 12:59:17 -04:00
|
|
|
this.hideAll();
|
2019-05-05 09:43:26 -04:00
|
|
|
|
2017-08-06 13:01:49 -04:00
|
|
|
this.menu.style.display = 'block';
|
|
|
|
this.menu.classList.add('anim', 'menuIn');
|
2019-08-24 13:26:28 -04:00
|
|
|
this.toggle.setAttribute('aria-expanded', 'true');
|
2019-05-05 09:43:26 -04:00
|
|
|
|
2022-05-13 12:12:45 -04:00
|
|
|
const menuOriginalRect = this.menu.getBoundingClientRect();
|
|
|
|
let heightOffset = 0;
|
|
|
|
const toggleHeight = this.toggle.getBoundingClientRect().height;
|
|
|
|
const dropUpwards = menuOriginalRect.bottom > window.innerHeight;
|
|
|
|
|
|
|
|
// If enabled, Move to body to prevent being trapped within scrollable sections
|
2019-05-05 09:43:26 -04:00
|
|
|
if (this.moveMenu) {
|
|
|
|
this.body.appendChild(this.menu);
|
|
|
|
this.menu.style.position = 'fixed';
|
2022-05-13 12:12:45 -04:00
|
|
|
this.menu.style.width = `${menuOriginalRect.width}px`;
|
2022-09-06 16:30:28 -04:00
|
|
|
this.menu.style.left = `${menuOriginalRect.left}px`;
|
2022-05-13 12:12:45 -04:00
|
|
|
heightOffset = dropUpwards ? (window.innerHeight - menuOriginalRect.top - toggleHeight / 2) : menuOriginalRect.top;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust menu to display upwards if near the bottom of the screen
|
|
|
|
if (dropUpwards) {
|
|
|
|
this.menu.style.top = 'initial';
|
|
|
|
this.menu.style.bottom = `${heightOffset}px`;
|
|
|
|
} else {
|
|
|
|
this.menu.style.top = `${heightOffset}px`;
|
|
|
|
this.menu.style.bottom = 'initial';
|
2019-05-05 09:43:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set listener to hide on mouse leave or window click
|
2022-11-21 12:35:19 -05:00
|
|
|
this.menu.addEventListener('mouseleave', this.hide);
|
2019-05-05 09:43:26 -04:00
|
|
|
window.addEventListener('click', event => {
|
|
|
|
if (!this.menu.contains(event.target)) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
});
|
2017-08-06 13:01:49 -04:00
|
|
|
|
|
|
|
// Focus on first input if existing
|
2019-08-24 13:26:28 -04:00
|
|
|
const input = this.menu.querySelector('input');
|
2017-08-06 13:01:49 -04:00
|
|
|
if (input !== null) input.focus();
|
2019-05-05 09:43:26 -04:00
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
this.showing = true;
|
|
|
|
|
|
|
|
const showEvent = new Event('show');
|
|
|
|
this.container.dispatchEvent(showEvent);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
}
|
|
|
|
|
2019-05-06 12:59:17 -04:00
|
|
|
hideAll() {
|
2022-11-14 18:19:02 -05:00
|
|
|
for (let dropdown of window.$components.get('dropdown')) {
|
2019-05-06 12:59:17 -04:00
|
|
|
dropdown.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 13:01:49 -04:00
|
|
|
hide() {
|
|
|
|
this.menu.style.display = 'none';
|
|
|
|
this.menu.classList.remove('anim', 'menuIn');
|
2019-08-24 13:26:28 -04:00
|
|
|
this.toggle.setAttribute('aria-expanded', 'false');
|
2022-05-13 12:12:45 -04:00
|
|
|
this.menu.style.top = '';
|
|
|
|
this.menu.style.bottom = '';
|
|
|
|
|
2019-05-05 09:43:26 -04:00
|
|
|
if (this.moveMenu) {
|
|
|
|
this.menu.style.position = '';
|
2020-04-05 08:07:19 -04:00
|
|
|
this.menu.style[this.direction] = '';
|
2019-05-05 09:43:26 -04:00
|
|
|
this.menu.style.width = '';
|
2022-09-06 16:30:28 -04:00
|
|
|
this.menu.style.left = '';
|
2019-05-05 09:43:26 -04:00
|
|
|
this.container.appendChild(this.menu);
|
|
|
|
}
|
2022-05-13 12:12:45 -04:00
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
this.showing = false;
|
|
|
|
}
|
|
|
|
|
2022-11-21 12:35:19 -05:00
|
|
|
setupListeners() {
|
|
|
|
const keyboardNavHandler = new KeyboardNavigationHandler(this.container, (event) => {
|
|
|
|
this.hide();
|
|
|
|
this.toggle.focus();
|
|
|
|
if (!this.bubbleEscapes) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}, (event) => {
|
|
|
|
if (event.target.nodeName === 'INPUT') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
this.hide();
|
|
|
|
});
|
2019-08-24 13:26:28 -04:00
|
|
|
|
2022-11-21 12:35:19 -05:00
|
|
|
if (this.moveMenu) {
|
|
|
|
keyboardNavHandler.shareHandlingToEl(this.menu);
|
2019-08-24 13:26:28 -04:00
|
|
|
}
|
|
|
|
|
2017-08-06 13:01:49 -04:00
|
|
|
// Hide menu on option click
|
|
|
|
this.container.addEventListener('click', event => {
|
2019-08-24 13:26:28 -04:00
|
|
|
const possibleChildren = Array.from(this.menu.querySelectorAll('a'));
|
|
|
|
if (possibleChildren.includes(event.target)) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onSelect(this.toggle, event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
this.show(event);
|
|
|
|
if (event instanceof KeyboardEvent) {
|
2022-11-21 12:35:19 -05:00
|
|
|
keyboardNavHandler.focusNext();
|
2019-08-24 13:26:28 -04:00
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|