2019-08-24 13:26:28 -04:00
|
|
|
import {onSelect} from "../services/dom";
|
|
|
|
|
2017-08-06 13:01:49 -04:00
|
|
|
/**
|
|
|
|
* Dropdown
|
|
|
|
* Provides some simple logic to create simple dropdown menus.
|
2020-06-24 15:38:08 -04:00
|
|
|
* @extends {Component}
|
2017-08-06 13:01:49 -04:00
|
|
|
*/
|
|
|
|
class DropDown {
|
|
|
|
|
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;
|
2017-08-06 13:01:49 -04:00
|
|
|
this.setupListeners();
|
2020-12-31 12:25:20 -05:00
|
|
|
this.hide = this.hide.bind(this);
|
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
|
|
|
|
|
|
|
if (this.moveMenu) {
|
|
|
|
// Move to body to prevent being trapped within scrollable sections
|
|
|
|
this.rect = this.menu.getBoundingClientRect();
|
|
|
|
this.body.appendChild(this.menu);
|
|
|
|
this.menu.style.position = 'fixed';
|
2020-04-05 08:07:19 -04:00
|
|
|
if (this.direction === 'right') {
|
|
|
|
this.menu.style.right = `${(this.rect.right - this.rect.width)}px`;
|
|
|
|
} else {
|
|
|
|
this.menu.style.left = `${this.rect.left}px`;
|
|
|
|
}
|
2019-05-05 09:43:26 -04:00
|
|
|
this.menu.style.top = `${this.rect.top}px`;
|
|
|
|
this.menu.style.width = `${this.rect.width}px`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set listener to hide on mouse leave or window click
|
|
|
|
this.menu.addEventListener('mouseleave', this.hide.bind(this));
|
|
|
|
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() {
|
|
|
|
for (let dropdown of window.components.dropdown) {
|
|
|
|
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');
|
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.top = '';
|
|
|
|
this.menu.style.width = '';
|
|
|
|
this.container.appendChild(this.menu);
|
|
|
|
}
|
2019-08-24 13:26:28 -04:00
|
|
|
this.showing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFocusable() {
|
|
|
|
return Array.from(this.menu.querySelectorAll('[tabindex],[href],button,input:not([type=hidden])'));
|
|
|
|
}
|
|
|
|
|
|
|
|
focusNext() {
|
|
|
|
const focusable = this.getFocusable();
|
|
|
|
const currentIndex = focusable.indexOf(document.activeElement);
|
|
|
|
let newIndex = currentIndex + 1;
|
|
|
|
if (newIndex >= focusable.length) {
|
|
|
|
newIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
focusable[newIndex].focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
focusPrevious() {
|
|
|
|
const focusable = this.getFocusable();
|
|
|
|
const currentIndex = focusable.indexOf(document.activeElement);
|
|
|
|
let newIndex = currentIndex - 1;
|
|
|
|
if (newIndex < 0) {
|
|
|
|
newIndex = focusable.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
focusable[newIndex].focus();
|
2017-08-06 13:01:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
// 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) {
|
|
|
|
this.focusNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-26 07:47:04 -04:00
|
|
|
// Keyboard navigation
|
|
|
|
const keyboardNavigation = event => {
|
2019-08-24 13:26:28 -04:00
|
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
|
|
|
|
this.focusNext();
|
|
|
|
event.preventDefault();
|
|
|
|
} else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
|
|
|
|
this.focusPrevious();
|
|
|
|
event.preventDefault();
|
|
|
|
} else if (event.key === 'Escape') {
|
|
|
|
this.hide();
|
2019-08-26 07:47:04 -04:00
|
|
|
this.toggle.focus();
|
2021-04-19 16:41:13 -04:00
|
|
|
if (!this.bubbleEscapes) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2019-08-24 13:26:28 -04:00
|
|
|
}
|
2019-08-26 07:47:04 -04:00
|
|
|
};
|
|
|
|
this.container.addEventListener('keydown', keyboardNavigation);
|
|
|
|
if (this.moveMenu) {
|
|
|
|
this.menu.addEventListener('keydown', keyboardNavigation);
|
|
|
|
}
|
2019-08-24 13:26:28 -04:00
|
|
|
|
|
|
|
// Hide menu on enter press or escape
|
|
|
|
this.menu.addEventListener('keydown ', event => {
|
|
|
|
if (event.key === 'Enter') {
|
2017-08-06 13:01:49 -04:00
|
|
|
event.preventDefault();
|
2019-08-24 13:26:28 -04:00
|
|
|
event.stopPropagation();
|
2017-08-06 13:01:49 -04:00
|
|
|
this.hide();
|
2019-08-24 13:26:28 -04:00
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default DropDown;
|