PageSourceSearch

https://www.todos-math.org/wp-content/themes/generatepress/assets/js/dropdown-click.js?ver=3.6.1

js todos-math.org collected 2026-10-01 11:57:16 UTC 5,919 bytes, 170 lines download raw bytes

1( function() {
2	'use strict';
3
4	if ( 'querySelector' in document && 'addEventListener' in window ) {
5		var body = document.body,
6			i;
7
8		/**
9		 * Dropdown click
10		 *
11		 * @param {Object} e     The event.
12		 * @param {Object} _this The clicked item.
13		 */
14		var dropdownClick = function( e, _this ) {
15			e.preventDefault();
16			e.stopPropagation();
17
18			if ( ! _this ) {
19				_this = this;
20			}
21
22			var closestLi = _this.closest( 'li' );
23
24			// Close other sub-menus
25			var openedSubMenus = _this.closest( 'nav' ).querySelectorAll( 'ul.toggled-on' );
26			if ( openedSubMenus && ! _this.closest( 'ul' ).classList.contains( 'toggled-on' ) && ! _this.closest( 'li' ).classList.contains( 'sfHover' ) ) {
27				for ( var o = 0; o < openedSubMenus.length; o++ ) {
28					openedSubMenus[ o ].classList.remove( 'toggled-on' );
29					openedSubMenus[ o ].closest( 'li' ).classList.remove( 'sfHover' );
30				}
31			}
32
33			// Add sfHover class to parent li
34			closestLi.classList.toggle( 'sfHover' );
35
36			if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
37				// Set aria-expanded on arrow
38				var dropdownToggle = closestLi.querySelector( '.dropdown-menu-toggle' );
39				if ( 'false' === dropdownToggle.getAttribute( 'aria-expanded' ) || ! dropdownToggle.getAttribute( 'aria-expanded' ) ) {
40					dropdownToggle.setAttribute( 'aria-expanded', 'true' );
41				} else {
42					dropdownToggle.setAttribute( 'aria-expanded', 'false' );
43				}
44			}
45
46			if ( body.classList.contains( 'dropdown-click-menu-item' ) && _this.tagName && 'A' === _this.tagName.toUpperCase() ) {
47				if ( 'false' === _this.getAttribute( 'aria-expanded' ) || ! _this.getAttribute( 'aria-expanded' ) ) {
48					_this.setAttribute( 'aria-expanded', 'true' );
49					_this.setAttribute( 'aria-label', generatepressDropdownClick.closeSubMenuLabel );
50				} else {
51					_this.setAttribute( 'aria-expanded', 'false' );
52					_this.setAttribute( 'aria-label', generatepressDropdownClick.openSubMenuLabel );
53				}
54			}
55
56			var subMenuSelector = '.children';
57
58			if ( closestLi.querySelector( '.sub-menu' ) ) {
59				subMenuSelector = '.sub-menu';
60			}
61
62			// Open the sub-menu
63			if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
64				_this.parentNode.querySelector( subMenuSelector ).classList.toggle( 'toggled-on' );
65			} else if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
66				closestLi.querySelector( subMenuSelector ).classList.toggle( 'toggled-on' );
67			}
68		};
69
70		// Do stuff if click dropdown if enabled
71		var parentElementLinks = document.querySelectorAll( '.main-nav .menu-item-has-children > a' );
72
73		// Open the sub-menu by clicking on the entire link element
74		if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
75			for ( i = 0; i < parentElementLinks.length; i++ ) {
76				parentElementLinks[ i ].addEventListener( 'click', dropdownClick, true );
77
78				parentElementLinks[ i ].addEventListener( 'keydown', function( e ) {
79					var _this = this;
80
81					if ( 'Enter' === e.key || ' ' === e.key ) {
82						e.preventDefault();
83						dropdownClick( e, _this );
84					}
85				}, false );
86			}
87		}
88
89		// Open the sub-menu by clicking on a dropdown arrow
90		if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
91			// Add a class to sub-menu items that are set to #
92			for ( i = 0; i < parentElementLinks.length; i++ ) {
93				if ( '#' === parentElementLinks[ i ].getAttribute( 'href' ) ) {
94					parentElementLinks[ i ].classList.add( 'menu-item-dropdown-click' );
95				}
96			}
97
98			var dropdownToggleLinks = document.querySelectorAll( '.main-nav .menu-item-has-children > a .dropdown-menu-toggle' );
99			for ( i = 0; i < dropdownToggleLinks.length; i++ ) {
100				dropdownToggleLinks[ i ].addEventListener( 'click', dropdownClick, false );
101
102				dropdownToggleLinks[ i ].addEventListener( 'keydown', function( e ) {
103					var _this = this;
104
105					if ( 'Enter' === e.key || ' ' === e.key ) {
106						e.preventDefault();
107						dropdownClick( e, _this );
108					}
109				}, false );
110			}
111
112			const menuItemDropdownClick = document.querySelectorAll( '.main-nav .menu-item-has-children > a.menu-item-dropdown-click' );
113
114			for ( i = 0; i < menuItemDropdownClick.length; i++ ) {
115				menuItemDropdownClick[ i ].addEventListener( 'click', dropdownClick, false );
116
117				menuItemDropdownClick[ i ].addEventListener( 'keydown', function( e ) {
118					var _this = this;
119
120					if ( 'Enter' === e.key || ' ' === e.key ) {
121						e.preventDefault();
122						dropdownClick( e, _this );
123					}
124				}, false );
125			}
126		}
127
128		var closeSubMenus = function() {
129			if ( document.querySelector( 'nav ul .toggled-on' ) ) {
130				var activeSubMenus = document.querySelectorAll( 'nav ul .toggled-on' );
131
132				for ( i = 0; i < activeSubMenus.length; i++ ) {
133					activeSubMenus[ i ].classList.remove( 'toggled-on' );
134					activeSubMenus[ i ].closest( '.sfHover' ).classList.remove( 'sfHover' );
135				}
136
137				if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
138					var activeDropdownToggles = document.querySelectorAll( 'nav .dropdown-menu-toggle' );
139
140					for ( i = 0; i < activeDropdownToggles.length; i++ ) {
141						activeDropdownToggles[ i ].setAttribute( 'aria-expanded', 'false' );
142					}
143				}
144
145				if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
146					var activeDropdownLinks = document.querySelectorAll( 'nav .menu-item-has-children > a' );
147
148					for ( i = 0; i < activeDropdownLinks.length; i++ ) {
149						activeDropdownLinks[ i ].setAttribute( 'aria-expanded', 'false' );
150						activeDropdownLinks[ i ].setAttribute( 'aria-label', generatepressDropdownClick.openSubMenuLabel );
151					}
152				}
153			}
154		};
155
156		// Close sub-menus when clicking elsewhere
157		document.addEventListener( 'click', function( event ) {
158			if ( ! event.target.closest( '.sfHover' ) ) {
159				closeSubMenus();
160			}
161		}, false );
162
163		// Close sub-menus on escape key
164		document.addEventListener( 'keydown', function( e ) {
165			if ( 'Escape' === e.key ) { // 27 is esc
166				closeSubMenus();
167			}
168		}, false );
169	}
170}() );

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.