PageSourceSearch

https://www.salvador-dali.org/wp-content/themes/salvadordali-fundacio/js/navigation.js?ver=1.19-1783605312

js salvador-dali.org collected 2026-09-24 09:03:07 UTC 7,914 bytes, 300 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' );
9
10	// Return early if the navigation doesn't exist.
11	if ( ! siteNavigation ) {
12		return;
13	}
14
15	const button = siteNavigation.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	} );
44
45	// Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation.
46	document.addEventListener( 'click', function( event ) {
47		const isClickInside = siteNavigation.contains( event.target );
48
49		if ( ! isClickInside ) {
50			siteNavigation.classList.remove( 'toggled' );
51			button.setAttribute( 'aria-expanded', 'false' );
52		}
53	} );
54
55	// Get all the link elements within the menu.
56	const links = menu.getElementsByTagName( 'a' );
57
58	// Get all the link elements with children within the menu.
59	const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
60
61	// Toggle focus each time a menu link is focused or blurred.
62	for ( const link of links ) {
63		link.addEventListener( 'focus', toggleFocus, true );
64		link.addEventListener( 'blur', toggleFocus, true );
65	}
66
67	// Toggle focus each time a menu link with children receive a touch event.
68	for ( const link of linksWithChildren ) {
69		link.addEventListener( 'touchstart', toggleFocus, false );
70	}
71
72	/**
73	 * Sets or removes .focus class on an element.
74	 */
75	function toggleFocus() {
76		if ( event.type === 'focus' || event.type === 'blur' ) {
77			let self = this;
78			// Move up through the ancestors of the current link until we hit .nav-menu.
79			while ( ! self.classList.contains( 'nav-menu' ) ) {
80				// On li elements toggle the class .focus.
81				if ( 'li' === self.tagName.toLowerCase() ) {
82					self.classList.toggle( 'focus' );
83				}
84				self = self.parentNode;
85			}
86		}
87
88		if ( event.type === 'touchstart' ) {
89			const menuItem = this.parentNode;
90			event.preventDefault();
91			for ( const link of menuItem.parentNode.children ) {
92				if ( menuItem !== link ) {
93					link.classList.remove( 'focus' );
94				}
95			}
96			menuItem.classList.toggle( 'focus' );
97		}
98	}
99
100
101
102
103
104}() );
105
106function disableMain() {
107	jQuery("main").attr("inert","1");
108	jQuery("footer").attr("inert","1");
109	//jQuery(".menu-primer-block").attr("inert","1");
110}
111function enableMain() {
112	jQuery("main").removeAttr("inert");
113	jQuery("footer").removeAttr("inert");
114	//jQuery(".menu-primer-block").removeAttr("inert");
115}
116
117
118
119jQuery(document).ready(function(){
120	const $mainMenu = jQuery('.menu-complet');
121	const $mainMenuItems = jQuery('.menu-items');
122	const $mainMenuButton = jQuery('.maxi-menu-icon');
123	const $languageMenu = jQuery('.menu-idiomes');
124	const $languageMenuButton = jQuery('.lang-world-icon');
125	const $mainMenuContainer = $mainMenu.parent();
126	const $languageMenuContainer = $languageMenu.parent();
127
128	function setMainMenuState(isOpen) {
129		if (typeof menuTexts !== 'undefined') {
130			$mainMenuButton.attr('aria-label', isOpen ? menuTexts.close : menuTexts.open);
131		}
132
133		$mainMenuButton.attr('aria-expanded', isOpen ? 'true' : 'false');
134	}
135
136	function unbindMainMenuFocusTrap() {
137		$mainMenuContainer.off('keydown.focusTrap');
138	}
139
140	function bindMainMenuFocusTrap() {
141		const $focusable = $mainMenuContainer.find('input, .maxi-menu-icon, .menu-complet a, .menu-complet button').filter(':visible');
142		const first = $focusable[0];
143		const last = $focusable[$focusable.length - 1];
144
145		unbindMainMenuFocusTrap();
146
147		setTimeout(function() {
148			if ($focusable[1]) {
149				jQuery($focusable[1]).focus();
150			}
151		}, 500);
152
153		$mainMenuContainer.on('keydown.focusTrap', function(e) {
154			if (e.key === 'Tab') {
155				if (e.shiftKey) {
156					if (document.activeElement === first) {
157						e.preventDefault();
158						jQuery(last).focus();
159					}
160				} else if (document.activeElement === last) {
161					e.preventDefault();
162					jQuery(first).focus();
163				}
164			}
165
166			if (e.key === 'Escape') {
167				closeMainMenu();
168			}
169		});
170	}
171
172	function closeMainMenu() {
173		$mainMenu.removeClass('fs');
174		$mainMenuItems.removeClass('fs');
175		$mainMenuButton.removeClass('fs');
176		setMainMenuState(false);
177		unbindMainMenuFocusTrap();
178		document.body.classList.remove('menu-open');
179	}
180
181	function openMainMenu() {
182		closeLanguageMenu(false);
183		$mainMenu.addClass('fs');
184		$mainMenuItems.addClass('fs');
185		$mainMenuButton.addClass('fs');
186		setMainMenuState(true);
187		bindMainMenuFocusTrap();
188		document.body.classList.add('menu-open');
189	}
190
191	function setLanguageMenuState(isOpen) {
192		const labelOpen = $languageMenuButton.attr('data-label-open');
193		const labelClose = $languageMenuButton.attr('data-label-close');
194
195		$languageMenuButton.attr('aria-expanded', isOpen ? 'true' : 'false');
196
197		if (labelOpen && labelClose) {
198			$languageMenuButton.attr('aria-label', isOpen ? labelClose : labelOpen);
199		}
200	}
201
202	function unbindLanguageMenuFocusTrap() {
203		$languageMenuContainer.off('keydown.langFocusTrap');
204	}
205
206	function bindLanguageMenuFocusTrap() {
207		const $focusable = $languageMenuContainer.find('input, .lang-world-icon, .menu-idiomes a, .menu-idiomes button').filter(':visible');
208		const first = $focusable[0];
209		const last = $focusable[$focusable.length - 1];
210
211		unbindLanguageMenuFocusTrap();
212
213		$languageMenuContainer.on('keydown.langFocusTrap', function(e) {
214			if (e.key === 'Tab') {
215				if (e.shiftKey) {
216					if (document.activeElement === first) {
217						e.preventDefault();
218						jQuery(last).focus();
219					}
220				} else if (document.activeElement === last) {
221					e.preventDefault();
222					jQuery(first).focus();
223				}
224			}
225
226			if (e.key === 'Escape') {
227				closeLanguageMenu(true);
228			}
229		});
230	}
231
232	function closeLanguageMenu(shouldFocusTrigger) {
233		$languageMenu.removeClass('fs');
234		$languageMenuButton.removeClass('fs');
235		setLanguageMenuState(false);
236		unbindLanguageMenuFocusTrap();
237		document.body.classList.remove('menu-open');
238
239		if (shouldFocusTrigger) {
240			$languageMenuButton.focus();
241		}
242	}
243
244	function openLanguageMenu() {
245		closeMainMenu();
246		$languageMenu.addClass('fs');
247		$languageMenuButton.addClass('fs');
248		setLanguageMenuState(true);
249		bindLanguageMenuFocusTrap();
250		document.body.classList.add('menu-open');
251
252
253		setTimeout(function() {
254			const firstLink = $languageMenu.find('a').first();
255			if (firstLink.length) {
256				firstLink.focus();
257			}
258		}, 300);
259	}
260
261	setLanguageMenuState($languageMenu.hasClass('fs'));
262
263	// Menú Hamburguesa
264	jQuery('.maxi-menu-icon').on('click', function() {
265		if ($mainMenu.hasClass('fs')) {
266			closeMainMenu();
267		} else {
268			openMainMenu();
269		}
270
271		/*
272		if(jQuery('.menu-complet').hasClass('fs')){
273			jQuery('.menu-complet').removeAttr("inert");
274			disableMain();
275			
276		} else {
277			
278			jQuery('.menu-complet').attr("inert","0");
279			enableMain();
280		}
281		*/
282	});
283
284
285	// Menú selector d'idiomes (world icon)
286	jQuery('.lang-world-icon').on('click', function() {
287		if ($languageMenu.hasClass('fs')) {
288			closeLanguageMenu(false);
289		} else {
290			openLanguageMenu();
291		}
292	});
293
294	jQuery(document).on('keydown.langMenu', function(e) {
295		if (e.key === 'Escape' && $languageMenu.hasClass('fs')) {
296			closeLanguageMenu(true);
297		}
298	});
299
300});

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.