debian-mirror-gitlab/app/assets/javascripts/droplab/keyboard.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable */
import { ACTIVE_CLASS } from './constants';
2021-03-08 18:12:59 +05:30
const Keyboard = function () {
2017-08-17 22:00:37 +05:30
var currentKey;
var currentFocus;
var isUpArrow = false;
var isDownArrow = false;
var removeHighlight = function removeHighlight(list) {
2018-12-13 13:39:08 +05:30
var itemElements = Array.prototype.slice.call(
list.list.querySelectorAll('li:not(.divider):not(.hidden)'),
0,
);
2017-08-17 22:00:37 +05:30
var listItems = [];
2018-12-13 13:39:08 +05:30
for (var i = 0; i < itemElements.length; i++) {
2017-08-17 22:00:37 +05:30
var listItem = itemElements[i];
listItem.classList.remove(ACTIVE_CLASS);
if (listItem.style.display !== 'none') {
listItems.push(listItem);
}
}
return listItems;
};
var setMenuForArrows = function setMenuForArrows(list) {
var listItems = removeHighlight(list);
2018-12-13 13:39:08 +05:30
if (list.currentIndex > 0) {
if (!listItems[list.currentIndex - 1]) {
list.currentIndex = list.currentIndex - 1;
2017-08-17 22:00:37 +05:30
}
2018-12-13 13:39:08 +05:30
if (listItems[list.currentIndex - 1]) {
var el = listItems[list.currentIndex - 1];
2017-08-17 22:00:37 +05:30
var filterDropdownEl = el.closest('.filter-dropdown');
el.classList.add(ACTIVE_CLASS);
if (filterDropdownEl) {
var filterDropdownBottom = filterDropdownEl.offsetHeight;
var elOffsetTop = el.offsetTop - 30;
if (elOffsetTop > filterDropdownBottom) {
filterDropdownEl.scrollTop = elOffsetTop - filterDropdownBottom;
}
}
}
}
};
var mousedown = function mousedown(e) {
var list = e.detail.hook.list;
removeHighlight(list);
list.show();
list.currentIndex = 0;
isUpArrow = false;
isDownArrow = false;
};
var selectItem = function selectItem(list) {
var listItems = removeHighlight(list);
2018-12-13 13:39:08 +05:30
var currentItem = listItems[list.currentIndex - 1];
2017-08-17 22:00:37 +05:30
var listEvent = new CustomEvent('click.dl', {
detail: {
list: list,
selected: currentItem,
data: currentItem.dataset,
},
});
list.list.dispatchEvent(listEvent);
list.hide();
2018-12-13 13:39:08 +05:30
};
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
var keydown = function keydown(e) {
2017-08-17 22:00:37 +05:30
var typedOn = e.target;
var list = e.detail.hook.list;
var currentIndex = list.currentIndex;
isUpArrow = false;
isDownArrow = false;
2018-12-13 13:39:08 +05:30
if (e.detail.which) {
2017-08-17 22:00:37 +05:30
currentKey = e.detail.which;
2018-12-13 13:39:08 +05:30
if (currentKey === 13) {
2017-08-17 22:00:37 +05:30
selectItem(e.detail.hook.list);
return;
}
2018-12-13 13:39:08 +05:30
if (currentKey === 38) {
2017-08-17 22:00:37 +05:30
isUpArrow = true;
}
2018-12-13 13:39:08 +05:30
if (currentKey === 40) {
2017-08-17 22:00:37 +05:30
isDownArrow = true;
}
2018-12-13 13:39:08 +05:30
} else if (e.detail.key) {
2017-08-17 22:00:37 +05:30
currentKey = e.detail.key;
2018-12-13 13:39:08 +05:30
if (currentKey === 'Enter') {
2017-08-17 22:00:37 +05:30
selectItem(e.detail.hook.list);
return;
}
2018-12-13 13:39:08 +05:30
if (currentKey === 'ArrowUp') {
2017-08-17 22:00:37 +05:30
isUpArrow = true;
}
2018-12-13 13:39:08 +05:30
if (currentKey === 'ArrowDown') {
2017-08-17 22:00:37 +05:30
isDownArrow = true;
}
}
2018-12-13 13:39:08 +05:30
if (isUpArrow) {
currentIndex--;
}
if (isDownArrow) {
currentIndex++;
}
if (currentIndex < 0) {
currentIndex = 0;
}
2017-08-17 22:00:37 +05:30
list.currentIndex = currentIndex;
setMenuForArrows(e.detail.hook.list);
};
document.addEventListener('mousedown.dl', mousedown);
document.addEventListener('keydown.dl', keydown);
};
export default Keyboard;