navigation.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ( function( $ ) {
  2. var debounce = function( func, wait ) {
  3. var timeout, args, context, timestamp;
  4. return function() {
  5. context = this;
  6. args = [].slice.call( arguments, 0 );
  7. timestamp = new Date();
  8. var later = function() {
  9. var last = ( new Date() ) - timestamp;
  10. if ( last < wait ) {
  11. timeout = setTimeout( later, wait - last );
  12. } else {
  13. timeout = null;
  14. func.apply( context, args );
  15. }
  16. };
  17. if ( ! timeout ) {
  18. timeout = setTimeout( later, wait );
  19. }
  20. };
  21. };
  22. function menuDropdownToggle() {
  23. $( '.main-navigation .page_item_has_children > a, .main-navigation .menu-item-has-children > a, .widget_nav_menu .page_item_has_children > a, .widget_nav_menu .menu-item-has-children > a' ).each( function() {
  24. if ( ! $( this ).find( '.dropdown-toggle' ).length ) {
  25. $( this ).append( '<button class="dropdown-toggle" aria-expanded="false"/>' );
  26. }
  27. } );
  28. if ( $( window ).width() > 839 ) {
  29. $( '.main-navigation .dropdown-toggle' ).remove();
  30. }
  31. }
  32. $( window ).load( menuDropdownToggle ).resize( debounce( menuDropdownToggle, 500 ) );
  33. $( window ).load( function() {
  34. $( '.dropdown-toggle' ).click( function( event ) {
  35. event.preventDefault();
  36. $( this ).toggleClass( 'toggled' );
  37. $( this ).parent().next( '.children, .sub-menu' ).toggleClass( 'toggled' );
  38. $( this ).attr( 'aria-expanded', $( this ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
  39. } );
  40. } );
  41. } )( jQuery );
  42. ( function() {
  43. var container, button, menu;
  44. container = document.getElementById( 'site-navigation' );
  45. if ( ! container ) {
  46. return;
  47. }
  48. button = container.getElementsByTagName( 'button' )[0];
  49. if ( 'undefined' === typeof button ) {
  50. return;
  51. }
  52. menu = container.getElementsByTagName( 'ul' )[0];
  53. if ( 'undefined' === typeof menu ) {
  54. button.style.display = 'none';
  55. return;
  56. }
  57. menu.setAttribute( 'aria-expanded', 'false' );
  58. if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
  59. menu.className += ' nav-menu';
  60. }
  61. button.onclick = function() {
  62. if ( -1 !== container.className.indexOf( 'toggled' ) ) {
  63. container.className = container.className.replace( ' toggled', '' );
  64. button.setAttribute( 'aria-expanded', 'false' );
  65. menu.setAttribute( 'aria-expanded', 'false' );
  66. } else {
  67. container.className += ' toggled';
  68. button.setAttribute( 'aria-expanded', 'true' );
  69. menu.setAttribute( 'aria-expanded', 'true' );
  70. }
  71. };
  72. } )();