PageSourceSearch

https://www.northleach.gov.uk/wp-content/themes/edin/js/navigation.js?ver=20140718

js northleach.gov.uk collected 2026-10-02 13:39:39 UTC 5,095 bytes, 155 lines download raw bytes

1/* global screen_reader_text */
2/**
3 * navigation.js
4 *
5 * Handles the navigation menu.
6 */
7( function( $ ) {
8
9	/* Define variables */
10	var header_wrapper, navigation_wrapper, site_navigation, classic_primary, classic_secondary, menu_toggle, window_width, menu_toggle_width, site_branding, header_search;
11
12	header_wrapper     = $( '.header-wrapper' );
13	navigation_wrapper = $( '.navigation-wrapper' );
14	site_navigation    = $( '#site-navigation' );
15	classic_primary    = $( '.navigation-classic .primary-navigation' );
16	classic_secondary  = $( '.navigation-classic .secondary-navigation' );
17	menu_toggle        = $( '.menu-toggle' );
18	site_branding      = $( '.site-branding' );
19	header_search      = $( '.header-search' );
20
21	/* Add dropdown toggle to Primary Navigation items */
22	$( '.primary-navigation .menu-primary > ul > .page_item_has_children > a, .primary-navigation .menu-primary > ul > .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screen_reader_text.expand + '</button>' );
23
24
25	/* Depending on window width */
26	function responsive_navigation() {
27
28		var e = window, a = 'inner';
29
30		if ( ! ( 'innerWidth' in window ) ) {
31			a = 'client';
32			e = document.documentElement || document.body;
33		}
34
35		window_width = e[ a+'Width' ];
36		
37		menu_toggle_width = menu_toggle.outerWidth();
38
39		/* Reset menu_toggle DOM position */
40		if ( $( 'body' ).hasClass( 'rtl' ) ) {
41			menu_toggle.appendTo( site_navigation ).css( 'margin-right', '' );
42		} else {
43			menu_toggle.appendTo( site_navigation ).css( 'margin-left', '' );
44		}
45
46		if ( window_width < 1020 ) {
47			classic_secondary.appendTo( navigation_wrapper );
48			classic_primary.appendTo( navigation_wrapper );
49		} else {
50			classic_secondary.insertBefore( header_wrapper );
51			classic_primary.insertAfter( header_wrapper );
52		}
53
54		if ( window_width < 1230 ) {
55			navigation_wrapper.insertBefore( header_wrapper );
56			if ( $( 'body' ).hasClass( 'rtl' ) ) {
57				$( '.menu-secondary' ).css( 'padding-left', '' );
58				menu_toggle.css( 'margin-right', '' );
59			} else {
60				$( '.menu-secondary' ).css( 'padding-right', '' );
61				menu_toggle.css( 'margin-left', '' );
62			}
63		} else {
64			navigation_wrapper.insertAfter( menu_toggle );
65			if ( $( 'body' ).hasClass( 'rtl' ) ) {
66				$( '.navigation-default .menu-secondary' ).css( 'padding-left', menu_toggle_width + 60 );
67			} else {
68				$( '.navigation-default .menu-secondary' ).css( 'padding-right', menu_toggle_width + 60 );
69			}
70			if ( menu_toggle.hasClass( 'open' ) ) {
71				$( 'html, body' ).css( 'overflow-y', 'hidden' );
72				menu_toggle.appendTo( navigation_wrapper );
73				if ( $( 'body' ).hasClass( 'rtl' ) ) {
74					menu_toggle.css( 'margin-right', 930 / 2 - menu_toggle_width );
75				} else {
76					menu_toggle.css( 'margin-left', 930 / 2 - menu_toggle_width );
77				}
78			} else {
79				$( 'html, body' ).css( 'overflow-y', '' );
80				if ( $( 'body' ).hasClass( 'rtl' ) ) {
81					menu_toggle.css( 'margin-right', '' );
82				} else {
83					menu_toggle.css( 'margin-left', '' );
84				}
85			}
86		}
87
88		/* Add a max-width to .site-branding */
89		var gap_icons = 0;
90		if ( site_navigation.length ) {
91			gap_icons += 16;
92		}
93		if ( header_search.length ) {
94			gap_icons += 16;
95		}
96		if ( window_width < 768 ) {
97			site_branding.removeAttr( 'style' );
98		} else {
99			site_branding.css( 'max-width', header_wrapper.outerWidth() - ( site_navigation.outerWidth() + header_search.outerWidth() + gap_icons ) );
100		}
101
102		$( '.menu-primary .dropdown-toggle' ).each( function() {
103			$( this ).css( 'top', $( this ).prev( 'a' ).outerHeight() - $( this ).outerHeight() - 1 );
104		} );
105	}
106
107	/* Click toggle */
108	menu_toggle.click( function() {
109		$( 'html, body' ).scrollTop( 0 );
110		$( this ).toggleClass( 'open' );
111		$( this ).attr( 'aria-expanded', $( this ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
112		navigation_wrapper.toggle();
113		$( '.search-toggle' ).removeClass( 'open' );
114		$( '.search-toggle' ).attr( 'aria-expanded', 'false' );
115		$( '.search-wrapper' ).hide();
116		responsive_navigation();
117	} );
118
119	/* Load */
120	$( window ).load( responsive_navigation ).resize( responsive_navigation );
121
122	var container = $( '.navigation-classic .primary-navigation' );
123
124	// Fix child menus for touch devices.
125	function fixMenuTouchTaps( container ) {
126		var touchStartFn,
127		    parentLink = container.find( '.menu-item-has-children > a, .page_item_has_children > a' );
128
129		if ( 'ontouchstart' in window ) {
130			touchStartFn = function( e ) {
131				var menuItem = this.parentNode;
132
133				if ( ! menuItem.classList.contains( 'focus' ) ) {
134					e.preventDefault();
135					for( var i = 0; i < menuItem.parentNode.children.length; ++i ) {
136						if ( menuItem === menuItem.parentNode.children[i] ) {
137							continue;
138						}
139						menuItem.parentNode.children[i].classList.remove( 'focus' );
140					}
141					menuItem.classList.add( 'focus' );
142				} else {
143					menuItem.classList.remove( 'focus' );
144				}
145			};
146
147			for ( var i = 0; i < parentLink.length; ++i ) {
148				parentLink[i].addEventListener( 'touchstart', touchStartFn, false )
149			}
150		}
151	}
152
153	fixMenuTouchTaps( container );
154
155} )( 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.