1 /** |
1 /** |
2 * navigation.js |
2 * Handles toggling the navigation menu for small screens and |
3 * |
3 * accessibility for submenu items. |
4 * Handles toggling the navigation menu for small screens. |
|
5 */ |
4 */ |
6 ( function() { |
5 ( function() { |
7 var nav = document.getElementById( 'site-navigation' ), button, menu; |
6 var nav = document.getElementById( 'site-navigation' ), button, menu; |
8 if ( ! nav ) |
7 if ( ! nav ) { |
9 return; |
8 return; |
10 button = nav.getElementsByTagName( 'h3' )[0]; |
9 } |
|
10 |
|
11 button = nav.getElementsByTagName( 'button' )[0]; |
11 menu = nav.getElementsByTagName( 'ul' )[0]; |
12 menu = nav.getElementsByTagName( 'ul' )[0]; |
12 if ( ! button ) |
13 if ( ! button ) { |
13 return; |
14 return; |
|
15 } |
14 |
16 |
15 // Hide button if menu is missing or empty. |
17 // Hide button if menu is missing or empty. |
16 if ( ! menu || ! menu.childNodes.length ) { |
18 if ( ! menu || ! menu.childNodes.length ) { |
17 button.style.display = 'none'; |
19 button.style.display = 'none'; |
18 return; |
20 return; |
19 } |
21 } |
20 |
22 |
21 button.onclick = function() { |
23 button.onclick = function() { |
22 if ( -1 == menu.className.indexOf( 'nav-menu' ) ) |
24 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { |
23 menu.className = 'nav-menu'; |
25 menu.className = 'nav-menu'; |
|
26 } |
24 |
27 |
25 if ( -1 != button.className.indexOf( 'toggled-on' ) ) { |
28 if ( -1 !== button.className.indexOf( 'toggled-on' ) ) { |
26 button.className = button.className.replace( ' toggled-on', '' ); |
29 button.className = button.className.replace( ' toggled-on', '' ); |
27 menu.className = menu.className.replace( ' toggled-on', '' ); |
30 menu.className = menu.className.replace( ' toggled-on', '' ); |
28 } else { |
31 } else { |
29 button.className += ' toggled-on'; |
32 button.className += ' toggled-on'; |
30 menu.className += ' toggled-on'; |
33 menu.className += ' toggled-on'; |
31 } |
34 } |
32 }; |
35 }; |
33 } )(); |
36 } )(); |
|
37 |
|
38 // Better focus for hidden submenu items for accessibility. |
|
39 ( function( $ ) { |
|
40 $( '.main-navigation' ).find( 'a' ).on( 'focus.twentytwelve blur.twentytwelve', function() { |
|
41 $( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' ); |
|
42 } ); |
|
43 |
|
44 if ( 'ontouchstart' in window ) { |
|
45 $('body').on( 'touchstart.twentytwelve', '.menu-item-has-children > a, .page_item_has_children > a', function( e ) { |
|
46 var el = $( this ).parent( 'li' ); |
|
47 |
|
48 if ( ! el.hasClass( 'focus' ) ) { |
|
49 e.preventDefault(); |
|
50 el.toggleClass( 'focus' ); |
|
51 el.siblings( '.focus').removeClass( 'focus' ); |
|
52 } |
|
53 } ); |
|
54 } |
|
55 } )( jQuery ); |