PageSourceSearch

https://www.kayawell.com/blog/wp-content/themes/articlewave/js/navigation.js?ver=1.0.5

js kayawell.com collected 2026-09-24 20:15:10 UTC 4,504 bytes, 138 lines download raw bytes

1/**
2 * File navigation.js.
3 *
4 * Handles toggling the navigation menu for small screens and enables TAB key
5 * navigation support for dropdown menus.
6 */
7( function($) {
8	const siteNavigation = document.getElementById( 'site-navigation' ), KEYCODE_TAB = 9;
9
10	// Return early if the navigation doesn't exist.
11	if ( ! siteNavigation ) {
12		return;
13	}
14
15	const button = document.getElementsByTagName( 'button' )[ 0 ];
16
17	// Return early if the button doesn't exist.
18	if ( 'undefined' === typeof button ) {
19		return;
20	}
21
22	const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ];
23
24	// Hide menu toggle button if menu is empty and return early.
25	if ( 'undefined' === typeof menu ) {
26		button.style.display = 'none';
27		return;
28	}
29
30	if ( ! menu.classList.contains( 'nav-menu' ) ) {
31		menu.classList.add( 'nav-menu' );
32	}
33
34	// Toggle the .toggled class and the aria-expanded value each time the button is clicked.
35	button.addEventListener( 'click', function() {
36		siteNavigation.classList.toggle( 'toggled' );
37
38		if ( button.getAttribute( 'aria-expanded' ) === 'true' ) {
39			button.setAttribute( 'aria-expanded', 'false' );
40		} else {
41			button.setAttribute( 'aria-expanded', 'true' );
42		}
43		var element = document.querySelector( '#site-navigation.toggled' );
44		if( element ) {
45			document.addEventListener('keydown', function(e) {
46				if( document.querySelector( '#site-navigation.toggled' ) ) {
47					var focusable = document.querySelector( '#site-navigation.toggled' ).querySelectorAll( 'input, a, button' );
48					focusable = Array.prototype.slice.call( focusable );
49					focusable = focusable.filter( function( focusableelement ) {
50						return null !== focusableelement.offsetParent;
51					} );
52					var firstFocusable = document.querySelector('.menu-toggle');
53					var lastFocusable = focusable[focusable.length - 1];
54					articlewave_focus_trap( firstFocusable, lastFocusable, e );
55				}
56			})
57		}
58	} );
59
60	// Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation.
61	document.addEventListener( 'click', function( event ) {
62		const isClickInside = siteNavigation.contains( event.target );
63
64		if ( ! isClickInside ) {
65			siteNavigation.classList.remove( 'toggled' );
66			button.setAttribute( 'aria-expanded', 'false' );
67		}
68		
69	} );
70
71	// Get all the link elements within the menu.
72	const links = menu.getElementsByTagName( 'a' );
73
74	// Get all the link elements with children within the menu.
75	const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
76
77	// Toggle focus each time a menu link is focused or blurred.
78	for ( const link of links ) {
79		link.addEventListener( 'focus', toggleFocus, true );
80		link.addEventListener( 'blur', toggleFocus, true );
81	}
82
83	// Toggle focus each time a menu link with children receive a touch event.
84	for ( const link of linksWithChildren ) {
85		link.addEventListener( 'touchstart', toggleFocus, false );
86	}
87
88	/**
89	 * Sets or removes .focus class on an element.
90	 */
91	function toggleFocus() {
92		if ( event.type === 'focus' || event.type === 'blur' ) {
93			let self = this;
94			// Move up through the ancestors of the current link until we hit .nav-menu.
95			while ( ! self.classList.contains( 'nav-menu' ) ) {
96				// On li elements toggle the class .focus.
97				if ( 'li' === self.tagName.toLowerCase() ) {
98					self.classList.toggle( 'focus' );
99				}
100				self = self.parentNode;
101			}
102		}
103
104		if ( event.type === 'touchstart' ) {
105			const menuItem = this.parentNode;
106			// event.preventDefault();
107			for ( const link of menuItem.parentNode.children ) {
108				if ( menuItem !== link ) {
109					link.classList.remove( 'focus' );
110				}
111			}
112			menuItem.classList.toggle( 'focus' );
113		}
114	}
115
116	/**
117	 * Responsive header menu modal
118	 * 
119	 * @returns void
120	 * @since 1.0.0
121	 */
122	function articlewave_focus_trap( firstFocusable, lastFocusable, e ) {
123        if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {
124            if ( e.shiftKey ) /* shift + tab */ {
125				if (document.activeElement === firstFocusable) {
126                    lastFocusable.focus();
127                    e.preventDefault();
128                }
129            } else /* tab */ {
130                if ( document.activeElement === lastFocusable ) {
131                    firstFocusable.focus();
132                    e.preventDefault();
133                }
134            }
135        }
136    }
137
138}(jQuery) );

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.