<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
*   @constructor PopupMenuItem
*   based on: https://www.w3.org/TR/wai-aria-practices/examples/menu-button/js/MenuItemAction.js
*
*   @desc
*       Wrapper object for a simple menu item in a popup menu
*       Handles key events for navigating on the list and menu actions
*
*   @param domNode
*       Element that represents a list item usually a &lt;li&gt;
*   @param popupMenuObj
*       Instance of PopupMenuContainer
*   @param eventHelper
*       Instance of JSLib/EventHelper to handle key events
*/

const PopupMenuItem = function (domNode, popupMenuObj, eventHelper) {
    const module = {
        domNode: domNode,
        popupMenu: popupMenuObj
    };

    module.init = function () {
        this.domNode.tabIndex = -1;
        this._clickableElement = this.domNode.querySelector('a, input, button, [onclick]');

        if (!this.domNode.getAttribute('role')) {
            this.domNode.setAttribute('role', 'menuitem');
        }

        this.domNode.addEventListener('keydown', this._handleKeydown.bind(this));
    };

    module._handleKeydown = function (event) {
        const char = event.key;

        if (eventHelper.isNavigationEvent(event) || eventHelper.isSpaceEvent(event)) {
            event.stopPropagation();
            event.preventDefault();
        }

        if (event.shiftKey &amp;&amp; !eventHelper.isTabEvent(event)) {
            if (this._isPrintableCharacter(char)) {
                this.popupMenu.setFocusByFirstCharacter(this, char);
            }
        } else {
            if (eventHelper.isTabEvent(event) 
                || eventHelper.isShiftTabEvent(event)) {
                this._tabAction(event)
            } else if (eventHelper.isSpaceEvent(event) 
                || eventHelper.isEnterEvent(event)) {
                this._enterSpaceAction(event);
            } else if (eventHelper.isEscEvent(event)) {
                this._escAction(event);
            } else if (eventHelper.isUpArrowEvent(event)) {
                this._upArrowAction(this);
            } else if (eventHelper.isDownArrowEvent(event)) {
                this._downArrowAction(this);
            } else if (eventHelper.isHomeKeyEvent(event)) {
                this._homeKeyAction();
            } else if (eventHelper.isEndKeyEvent(event)) {
                this._endKeyAction();
            } else {
                if (this._isPrintableCharacter(char)) {
                    this.popupMenu.setFocusByFirstCharacter(this, char);
                }
            }
        }
    }

    module._isPrintableCharacter = function (str) {
        return str.length === 1 &amp;&amp; str.match(/\S/);
    }

    module._downArrowAction = function (popupMenuItem) {
        this.popupMenu.setFocusToNextItem(popupMenuItem);
    }

    module._upArrowAction = function (popupMenuItem) {
        this.popupMenu.setFocusToPreviousItem(popupMenuItem);
    }

    module._enterSpaceAction = function (event) {
        if (this._clickableElement) {
            this._clickableElement.dispatchEvent(eventHelper.createClickEvent());
        }
        this._closeAction(event);
    }

    module._endKeyAction = function () {
        this.popupMenu.setFocusToLastItem();
    }

    module._homeKeyAction = function () {
        this.popupMenu.setFocusToFirstItem();
    }

    module._escAction = function (event) {
        this._closeAction(event);
    }

    module._tabAction = function (event) {
        this._closeAction(event);
    }

    module._closeAction = function (event) {
        this.popupMenu.setFocusToController();
        this.popupMenu.close(event);
    }

    return module;
};</pre></body></html>