Select.prototype.bindKeys = function()

in source/console/app/lib/aws-ui/AWS-UI-Components/components.js [4273:4356]


    Select.prototype.bindKeys = function () {
        var select = this;
        select.target.addEventListener('keypress', function (e) {
            if (e.charCode === 0) {
                return;
            }
            if (e.keyCode === SPACE) {
                e.preventDefault();
            }
            clearTimeout(select.searchTextTimeout);
            select.searchTextTimeout = setTimeout(function () {
                select.searchText = '';
            }, 500);
            select.searchText += String.fromCharCode(e.charCode);
            var options = select.findOptionsByPrefix(select.searchText);
            if (options.length === 1) {
                // We have an exact match, choose it
                select.selectOption(options[0]);
            }
            if (select.searchText.length > 1 && isRepeatedChar(select.searchText)) {
                // They hit the same char over and over, maybe they want to cycle through
                // the options that start with that char
                var repeatedOptions = select.findOptionsByPrefix(select.searchText[0]);
                if (repeatedOptions.length) {
                    var selected = repeatedOptions.indexOf(select.getChosen());
                    // Pick the next thing (if something with this prefix wasen't selected
                    // we'll end up with the first option)
                    selected += 1;
                    selected = selected % repeatedOptions.length;
                    select.selectOption(repeatedOptions[selected]);
                    return;
                }
            }
            if (options.length) {
                // We have multiple things that start with this prefix.  Based on the
                // behavior of native select, this is considered after the repeated case.
                select.selectOption(options[0]);
                return;
            }
            // No match at all, do nothing
        });
        select.target.addEventListener('keydown', function (e) {
            // We consider this independently of the keypress handler so we can intercept
            // keys that have built-in functions.
            // Since we can't use a relatedTarget on blur events (not supported in
            // Firefox, causes problems when contained in an element with tabindex),
            // we need to have a different way of figuring out when to close the
            // the dropdown.
            // This code checks after a tab key has been pressed whether the current
            // select is still the active element. If not, it closes the select.
            // activeElement is updated after the event fires, so we need to wrap
            // this in a setTimeout.
            if (e.keyCode === TAB) {
                setTimeout(function () {
                    if (document.activeElement !== select.target) {
                        select.close();
                    }
                });
            }
            if ([UP, DOWN, ESCAPE].indexOf(e.keyCode) >= 0) {
                e.preventDefault();
            }
            if (select.isOpen()) {
                switch (e.keyCode) {
                    case UP:
                    case DOWN:
                        select.moveHighlight(e.keyCode);
                        break;
                    case ENTER:
                    case SPACE:
                        select.selectHighlightedOption();
                        break;
                    case ESCAPE:
                        select.close();
                        select.target.focus();
                }
            }
            else {
                if ([UP, DOWN, SPACE].indexOf(e.keyCode) >= 0) {
                    select.open();
                }
            }
        });
    };