Guest User

Untitled

a guest
Jul 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. ( function ( document, $, undefined ) {
  2.  
  3. $( 'body' ).addClass( 'js' );
  4.  
  5. 'use strict';
  6.  
  7. var interior = {},
  8. mainMenuButtonClass = 'menu-toggle',
  9. subMenuButtonClass = 'sub-menu-toggle';
  10.  
  11. interior.init = function() {
  12. var toggleButtons = {
  13. menu : $( '<button />', {
  14. 'class' : mainMenuButtonClass,
  15. 'aria-expanded' : false,
  16. 'aria-pressed' : false,
  17. 'role' : 'button'
  18. } )
  19. .append( interior.params.mainMenu ),
  20. submenu : $( '<button />', {
  21. 'class' : subMenuButtonClass,
  22. 'aria-expanded' : false,
  23. 'aria-pressed' : false,
  24. 'role' : 'button'
  25. } )
  26. .append( $( '<span />', {
  27. 'class' : 'screen-reader-text',
  28. text : interior.params.subMenu
  29. } ) )
  30. };
  31. $( '.nav-primary' ).before( toggleButtons.menu ); // add the main nav buttons
  32. $( 'nav .sub-menu' ).before( toggleButtons.submenu ); // add the submenu nav buttons
  33. $( '.' + mainMenuButtonClass ).each( _addClassID );
  34. $( '.' + mainMenuButtonClass ).addClass('ion-navicon-round');
  35. $( '.' + subMenuButtonClass ).addClass('ion-chevron-down');
  36. $( window ).on( 'resize.interior', _doResize ).triggerHandler( 'resize.interior' );
  37. $( '.' + mainMenuButtonClass ).on( 'click.interior-mainbutton', _mainmenuToggle );
  38. $( '.' + subMenuButtonClass ).on( 'click.interior-subbutton', _submenuToggle );
  39. };
  40.  
  41. // add nav class and ID to related button
  42. function _addClassID() {
  43. var $this = $( this ),
  44. nav = $this.next( 'nav' ),
  45. id = 'class';
  46. $this.addClass( $( nav ).attr( 'class' ) );
  47. if ( $( nav ).attr( 'id' ) ) {
  48. id = 'id';
  49. }
  50. $this.attr( 'id', 'mobile-' + $( nav ).attr( id ) );
  51. }
  52.  
  53. // Change Skiplinks and Superfish
  54. function _doResize() {
  55. var buttons = $( 'button[id^="mobile-"]' ).attr( 'id' );
  56. if ( typeof buttons === 'undefined' ) {
  57. return;
  58. }
  59. _superfishToggle( buttons );
  60. _changeSkipLink( buttons );
  61. _maybeClose( buttons );
  62. }
  63.  
  64. /**
  65. * action to happen when the main menu button is clicked
  66. */
  67. function _mainmenuToggle() {
  68. var $this = $( this );
  69. _toggleAria( $this, 'aria-pressed' );
  70. _toggleAria( $this, 'aria-expanded' );
  71. $this.toggleClass( 'activated' );
  72. $( 'nav.nav-primary' ).slideToggle( 'fast' ); //changed to .nav-primary since we're not toggling .nav-secondary
  73. }
  74.  
  75. /**
  76. * action for submenu toggles
  77. */
  78. function _submenuToggle() {
  79.  
  80. var $this = $( this ),
  81. others = $this.closest( '.menu-item' ).siblings();
  82. _toggleAria( $this, 'aria-pressed' );
  83. _toggleAria( $this, 'aria-expanded' );
  84. $this.toggleClass( 'activated' );
  85. $this.next( '.sub-menu' ).slideToggle( 'fast' );
  86.  
  87. others.find( '.' + subMenuButtonClass ).removeClass( 'activated' ).attr( 'aria-pressed', 'false' );
  88. others.find( '.sub-menu' ).slideUp( 'fast' );
  89.  
  90. }
  91.  
  92. /**
  93. * activate/deactivate superfish
  94. */
  95. function _superfishToggle( buttons ) {
  96. if ( typeof $( '.js-superfish' ).superfish !== 'function' ) {
  97. return;
  98. }
  99. if ( 'none' === _getDisplayValue( buttons ) ) {
  100. $( '.js-superfish' ).superfish( {
  101. 'delay': 100,
  102. 'animation': {'opacity': 'show', 'height': 'show'},
  103. 'dropShadows': false
  104. });
  105. } else {
  106. $( '.js-superfish' ).superfish( 'destroy' );
  107. }
  108. }
  109.  
  110. /**
  111. * modify skip links to match mobile buttons
  112. */
  113. function _changeSkipLink( buttons ) {
  114. var startLink = 'genesis-nav',
  115. endLink = 'mobile-genesis-nav';
  116. if ( 'none' === _getDisplayValue( buttons ) ) {
  117. startLink = 'mobile-genesis-nav';
  118. endLink = 'genesis-nav';
  119. }
  120. $( '.genesis-skip-link a[href^="#' + startLink + '"]' ).each( function() {
  121. var link = $( this ).attr( 'href' );
  122. link = link.replace( startLink, endLink );
  123. $( this ).attr( 'href', link );
  124. });
  125. }
  126.  
  127. function _maybeClose( buttons ) {
  128. if ( 'none' !== _getDisplayValue( buttons ) ) {
  129. return;
  130. }
  131. $( '.menu-toggle, .sub-menu-toggle' )
  132. .removeClass( 'activated' )
  133. .attr( 'aria-expanded', false )
  134. .attr( 'aria-pressed', false );
  135. $( 'nav, .sub-menu' )
  136. .attr( 'style', '' );
  137. }
  138.  
  139. /**
  140. * generic function to get the display value of an element
  141. * @param {id} $id ID to check
  142. * @return {string} CSS value of display property
  143. */
  144. function _getDisplayValue( $id ) {
  145. var element = document.getElementById( $id ),
  146. style = window.getComputedStyle( element );
  147. return style.getPropertyValue( 'display' );
  148. }
  149.  
  150. /**
  151. * Toggle aria attributes
  152. * @param {button} $this passed through
  153. * @param {aria-xx} attribute aria attribute to toggle
  154. * @return {bool} from _ariaReturn
  155. */
  156. function _toggleAria( $this, attribute ) {
  157. $this.attr( attribute, function( index, value ) {
  158. return 'false' === value;
  159. });
  160. }
  161.  
  162. $(document).ready(function () {
  163.  
  164. interior.params = typeof InteriorL10n === 'undefined' ? '' : InteriorL10n;
  165.  
  166. if ( typeof interior.params !== 'undefined' ) {
  167. interior.init();
  168. }
  169.  
  170. });
  171.  
  172. })( document, jQuery );
Add Comment
Please, Sign In to add comment