1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/alphanumericshortcuts.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import dom from './dom';
import focusManager from '../components/focusManager';
let inputDisplayElement;
let currentDisplayText = '';
let currentDisplayTextContainer;
2018-10-23 01:05:09 +03:00
function onKeyDown(e) {
if (e.ctrlKey) {
return;
}
if (e.shiftKey) {
return;
}
if (e.altKey) {
return;
}
const key = e.key;
let chr = key ? alphanumeric(key) : null;
if (chr) {
chr = chr.toString().toUpperCase();
if (chr.length === 1) {
currentDisplayTextContainer = this.options.itemsContainer;
onAlphanumericKeyPress(e, chr);
2018-10-23 01:05:09 +03:00
}
}
}
2018-10-23 01:05:09 +03:00
function alphanumeric(value) {
const letterNumber = /^[0-9a-zA-Z]+$/;
return value.match(letterNumber);
}
2018-10-23 01:05:09 +03:00
function ensureInputDisplayElement() {
if (!inputDisplayElement) {
inputDisplayElement = document.createElement('div');
inputDisplayElement.classList.add('alphanumeric-shortcut');
inputDisplayElement.classList.add('hide');
document.body.appendChild(inputDisplayElement);
2018-10-23 01:05:09 +03:00
}
}
2018-10-23 01:05:09 +03:00
let alpanumericShortcutTimeout;
function clearAlphaNumericShortcutTimeout() {
if (alpanumericShortcutTimeout) {
clearTimeout(alpanumericShortcutTimeout);
alpanumericShortcutTimeout = null;
2018-10-23 01:05:09 +03:00
}
}
function resetAlphaNumericShortcutTimeout() {
clearAlphaNumericShortcutTimeout();
alpanumericShortcutTimeout = setTimeout(onAlphanumericShortcutTimeout, 2000);
}
function onAlphanumericKeyPress(e, chr) {
if (currentDisplayText.length >= 3) {
return;
2018-10-23 01:05:09 +03:00
}
ensureInputDisplayElement();
currentDisplayText += chr;
inputDisplayElement.innerHTML = currentDisplayText;
inputDisplayElement.classList.remove('hide');
resetAlphaNumericShortcutTimeout();
}
2018-10-23 01:05:09 +03:00
function onAlphanumericShortcutTimeout() {
const value = currentDisplayText;
const container = currentDisplayTextContainer;
currentDisplayText = '';
currentDisplayTextContainer = null;
inputDisplayElement.innerHTML = '';
inputDisplayElement.classList.add('hide');
clearAlphaNumericShortcutTimeout();
selectByShortcutValue(container, value);
}
2018-10-23 01:05:09 +03:00
function selectByShortcutValue(container, value) {
value = value.toUpperCase();
let focusElem;
if (value === '#') {
focusElem = container.querySelector('*[data-prefix]');
}
if (!focusElem) {
focusElem = container.querySelector('*[data-prefix^=\'' + value + '\']');
}
if (focusElem) {
focusManager.focus(focusElem);
2018-10-23 01:05:09 +03:00
}
}
2018-10-23 01:05:09 +03:00
class AlphaNumericShortcuts {
constructor(options) {
2018-10-23 01:05:09 +03:00
this.options = options;
const keyDownHandler = onKeyDown.bind(this);
dom.addEventListener(window, 'keydown', keyDownHandler, {
passive: true
});
this.keyDownHandler = keyDownHandler;
2018-10-23 01:05:09 +03:00
}
destroy() {
const keyDownHandler = this.keyDownHandler;
if (keyDownHandler) {
dom.removeEventListener(window, 'keydown', keyDownHandler, {
passive: true
});
this.keyDownHandler = null;
}
this.options = null;
}
}
export default AlphaNumericShortcuts;