PageSourceSearch

https://passionaustralia.blog/_static/??wp-content/themes/gateway/…/js/skip-link-focus-fix.js?m=1438358024

js passionaustralia.blog collected 2026-10-04 21:38:08 UTC 3,836 bytes, 131 lines download raw bytes

1/**
2 * navigation.js
3 *
4 * Handles toggling the navigation menu for small screens and enables tab
5 * support for dropdown menus.
6 */
7( function() {
8	var container, button, menu, links, subMenus;
9
10	container = document.getElementById( 'site-navigation' );
11	if ( ! container ) {
12		return;
13	}
14
15	button = container.getElementsByTagName( 'button' )[0];
16	if ( 'undefined' === typeof button ) {
17		return;
18	}
19
20	menu = container.getElementsByTagName( 'ul' )[0];
21
22	// Hide menu toggle button if menu is empty and return early.
23	if ( 'undefined' === typeof menu ) {
24		button.style.display = 'none';
25		return;
26	}
27
28	menu.setAttribute( 'aria-expanded', 'false' );
29	if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
30		menu.className += ' nav-menu';
31	}
32
33	button.onclick = function() {
34		if ( -1 !== container.className.indexOf( 'toggled' ) ) {
35			container.className = container.className.replace( ' toggled', '' );
36			button.setAttribute( 'aria-expanded', 'false' );
37			menu.setAttribute( 'aria-expanded', 'false' );
38		} else {
39			container.className += ' toggled';
40			button.setAttribute( 'aria-expanded', 'true' );
41			menu.setAttribute( 'aria-expanded', 'true' );
42		}
43	};
44
45	// Get all the link elements within the menu.
46	links    = menu.getElementsByTagName( 'a' );
47	subMenus = menu.getElementsByTagName( 'ul' );
48
49	// Set menu items with submenus to aria-haspopup="true".
50	for ( var i = 0, len = subMenus.length; i < len; i++ ) {
51		subMenus[i].parentNode.setAttribute( 'aria-haspopup', 'true' );
52	}
53
54	// Each time a menu link is focused or blurred, toggle focus.
55	for ( i = 0, len = links.length; i < len; i++ ) {
56		links[i].addEventListener( 'focus', toggleFocus, true );
57		links[i].addEventListener( 'blur', toggleFocus, true );
58	}
59
60	/**
61	 * Sets or removes .focus class on an element.
62	 */
63	function toggleFocus() {
64		var self = this;
65
66		// Move up through the ancestors of the current link until we hit .nav-menu.
67		while ( -1 === self.className.indexOf( 'nav-menu' ) ) {
68
69			// On li elements toggle the class .focus.
70			if ( 'li' === self.tagName.toLowerCase() ) {
71				if ( -1 !== self.className.indexOf( 'focus' ) ) {
72					self.className = self.className.replace( ' focus', '' );
73				} else {
74					self.className += ' focus';
75				}
76			}
77
78			self = self.parentElement;
79		}
80	}
81
82	// Fix child menus for touch devices.
83	function fixMenuTouchTaps( container ) {
84		var touchStartFn,
85		    parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
86
87		if ( 'ontouchstart' in window ) {
88			touchStartFn = function( e ) {
89				var menuItem = this.parentNode;
90
91				if ( ! menuItem.classList.contains( 'focus' ) ) {
92					e.preventDefault();
93					for( var i = 0; i < menuItem.parentNode.children.length; ++i ) {
94						if ( menuItem === menuItem.parentNode.children[i] ) {
95							continue;
96						}
97						menuItem.parentNode.children[i].classList.remove( 'focus' );
98					}
99					menuItem.classList.add( 'focus' );
100				} else {
101					menuItem.classList.remove( 'focus' );
102				}
103			};
104
105			for ( var i = 0; i < parentLink.length; ++i ) {
106				parentLink[i].addEventListener( 'touchstart', touchStartFn, false )
107			}
108		}
109	}
110
111	fixMenuTouchTaps( container );
112} )();;
113( function() {
114	var is_webkit = navigator.userAgent.toLowerCase().indexOf( 'webkit' ) > -1,
115	    is_opera  = navigator.userAgent.toLowerCase().indexOf( 'opera' )  > -1,
116	    is_ie     = navigator.userAgent.toLowerCase().indexOf( 'msie' )   > -1;
117
118	if ( ( is_webkit || is_opera || is_ie ) && document.getElementById && window.addEventListener ) {
119		window.addEventListener( 'hashchange', function() {
120			var element = document.getElementById( location.hash.substring( 1 ) );
121
122			if ( element ) {
123				if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) )
124					element.tabIndex = -1;
125
126				element.focus();
127			}
128		}, false );
129	}
130})();
131;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.