2019-01-23 04:33:34 -07:00
|
|
|
define(['dom', 'focusManager'], function (dom, focusManager) {
|
2019-01-10 05:39:37 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inputDisplayElement;
|
|
|
|
var currentDisplayText = '';
|
|
|
|
var currentDisplayTextContainer;
|
2018-10-22 15:05:09 -07:00
|
|
|
|
|
|
|
function onKeyDown(e) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
if (e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!!e.shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var key = e.key;
|
|
|
|
var chr = key ? alphanumeric(key) : null;
|
|
|
|
|
|
|
|
if (chr) {
|
|
|
|
|
|
|
|
chr = chr.toString().toUpperCase();
|
|
|
|
|
|
|
|
if (chr.length === 1) {
|
|
|
|
currentDisplayTextContainer = this.options.itemsContainer;
|
|
|
|
onAlphanumericKeyPress(e, chr);
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function alphanumeric(value) {
|
|
|
|
var letterNumber = /^[0-9a-zA-Z]+$/;
|
2019-01-10 05:39:37 -07:00
|
|
|
return value.match(letterNumber);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ensureInputDisplayElement() {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (!inputDisplayElement) {
|
|
|
|
inputDisplayElement = document.createElement('div');
|
|
|
|
inputDisplayElement.classList.add('alphanumeric-shortcut');
|
|
|
|
inputDisplayElement.classList.add('hide');
|
|
|
|
|
|
|
|
document.body.appendChild(inputDisplayElement);
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var alpanumericShortcutTimeout;
|
2018-10-22 15:05:09 -07:00
|
|
|
function clearAlphaNumericShortcutTimeout() {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (alpanumericShortcutTimeout) {
|
|
|
|
clearTimeout(alpanumericShortcutTimeout);
|
|
|
|
alpanumericShortcutTimeout = null;
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
function resetAlphaNumericShortcutTimeout() {
|
2019-01-10 05:39:37 -07:00
|
|
|
clearAlphaNumericShortcutTimeout();
|
|
|
|
alpanumericShortcutTimeout = setTimeout(onAlphanumericShortcutTimeout, 2000);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAlphanumericKeyPress(e, chr) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (currentDisplayText.length >= 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ensureInputDisplayElement();
|
|
|
|
currentDisplayText += chr;
|
|
|
|
inputDisplayElement.innerHTML = currentDisplayText;
|
|
|
|
inputDisplayElement.classList.remove('hide');
|
|
|
|
resetAlphaNumericShortcutTimeout();
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAlphanumericShortcutTimeout() {
|
2019-01-10 05:39:37 -07:00
|
|
|
var value = currentDisplayText;
|
|
|
|
var container = currentDisplayTextContainer;
|
|
|
|
|
|
|
|
currentDisplayText = '';
|
|
|
|
currentDisplayTextContainer = null;
|
|
|
|
inputDisplayElement.innerHTML = '';
|
|
|
|
inputDisplayElement.classList.add('hide');
|
|
|
|
clearAlphaNumericShortcutTimeout();
|
|
|
|
selectByShortcutValue(container, value);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectByShortcutValue(container, value) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
value = value.toUpperCase();
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var focusElem;
|
2019-01-10 05:39:37 -07:00
|
|
|
if (value === '#') {
|
|
|
|
|
|
|
|
focusElem = container.querySelector('*[data-prefix]');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!focusElem) {
|
|
|
|
focusElem = container.querySelector('*[data-prefix^=\'' + value + '\']');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusElem) {
|
|
|
|
focusManager.focus(focusElem);
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function AlphaNumericShortcuts(options) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
this.options = options;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var keyDownHandler = onKeyDown.bind(this);
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
dom.addEventListener(window, 'keydown', keyDownHandler, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.keyDownHandler = keyDownHandler;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
AlphaNumericShortcuts.prototype.destroy = function () {
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var keyDownHandler = this.keyDownHandler;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
if (keyDownHandler) {
|
|
|
|
dom.removeEventListener(window, 'keydown', keyDownHandler, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
this.keyDownHandler = null;
|
|
|
|
}
|
|
|
|
this.options = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
return AlphaNumericShortcuts;
|
2020-02-22 09:47:03 -07:00
|
|
|
});
|