/* global blogging_hub_l10n */
/**
 * Theme functions file.
 *
 * Contains handlers for navigation and widget area.
 */

// Menus 
function blogging_hub_menu_open_nav() {
    jQuery(".sidenav").addClass('show');
  }
  function blogging_hub_menu_close_nav() {
    jQuery(".sidenav").removeClass('show');
  }
  
  ( function( window, document ) {
    function blogging_hub_keepFocusInMenu() {
      document.addEventListener( 'keydown', function( e ) {
        const blogging_hub_nav = document.querySelector( '.sidenav' );
  
        if ( ! blogging_hub_nav || ! blogging_hub_nav.classList.contains( 'show' ) ) {
          return;
        }
        const elements = [...blogging_hub_nav.querySelectorAll( 'input, a, button' )],
          blogging_hub_lastEl = elements[ elements.length - 1 ],
          blogging_hub_firstEl = elements[0],
          blogging_hub_activeEl = document.activeElement,
          tabKey = e.keyCode === 9,
          shiftKey = e.shiftKey;
  
        if ( ! shiftKey && tabKey && blogging_hub_lastEl === blogging_hub_activeEl ) {
          e.preventDefault();
          blogging_hub_firstEl.focus();
        }
  
        if ( shiftKey && tabKey && blogging_hub_firstEl === blogging_hub_activeEl ) {
          e.preventDefault();
          blogging_hub_lastEl.focus();
        }
      } );
    }
    blogging_hub_keepFocusInMenu();
  } )( window, document );