Changeset 43904 for branches/5.0/src/wp-content/themes/twentynineteen/js/touch-keyboard-navigation.js
- Timestamp:
- 11/16/2018 12:13:32 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0/src/wp-content/themes/twentynineteen/js/touch-keyboard-navigation.js
r43892 r43904 7 7 (function() { 8 8 9 // Debounce 9 /** 10 * Debounce 11 * 12 * @param {Function} func 13 * @param {number} wait 14 * @param {boolean} immediate 15 */ 10 16 function debounce(func, wait, immediate) { 11 17 'use strict'; … … 37 43 } 38 44 39 // Add Class 45 /** 46 * Add class 47 * 48 * @param {Object} el 49 * @param {string} cls 50 */ 40 51 function addClass(el, cls) { 41 52 if ( ! el.className.match( '(?:^|\\s)' + cls + '(?!\\S)') ) { … … 44 55 } 45 56 46 // Delete Class 57 /** 58 * Delete class 59 * 60 * @param {Object} el 61 * @param {string} cls 62 */ 47 63 function deleteClass(el, cls) { 48 64 el.className = el.className.replace( new RegExp( '(?:^|\\s)' + cls + '(?!\\S)' ),'' ); 49 65 } 50 66 51 // Has Class? 67 /** 68 * Has class? 69 * 70 * @param {Object} el 71 * @param {string} cls 72 * 73 * @returns {boolean} Has class 74 */ 52 75 function hasClass(el, cls) { 53 76 … … 57 80 } 58 81 59 // Toggle Aria Expanded state for screenreaders 82 /** 83 * Toggle Aria Expanded state for screenreaders 84 * 85 * @param {Object} ariaItem 86 */ 60 87 function toggleAriaExpandedState( ariaItem ) { 61 88 'use strict'; … … 72 99 } 73 100 74 // Open sub-menu 101 /** 102 * Open sub-menu 103 * 104 * @param {Object} currentSubMenu 105 */ 75 106 function openSubMenu( currentSubMenu ) { 76 107 'use strict'; … … 85 116 } 86 117 87 // Close sub-menu 118 /** 119 * Close sub-menu 120 * 121 * @param {Object} currentSubMenu 122 */ 88 123 function closeSubMenu( currentSubMenu ) { 89 124 'use strict'; … … 117 152 } 118 153 119 // Find first ancestor of an element by selector 154 /** 155 * Find first ancestor of an element by selector 156 * 157 * @param {Object} child 158 * @param {String} selector 159 * @param {String} stopSelector 160 */ 120 161 function getCurrentParent( child, selector, stopSelector ) { 121 162 … … 140 181 } 141 182 142 // Remove all off-canvas states 183 /** 184 * Remove all off-canvas states 185 */ 143 186 function removeAllFocusStates() { 144 187 'use strict'; 145 188 146 var getFocusedElements = document.querySelectorAll(':hover, :focus, :focus-within'); 189 var siteBranding = document.getElementsByClassName( 'site-branding' )[0]; 190 var getFocusedElements = siteBranding.querySelectorAll(':hover, :focus, :focus-within'); 191 var getFocusedClassElements = siteBranding.querySelectorAll('.is-focused'); 147 192 var i; 193 148 194 149 195 for ( i = 0; i < getFocusedElements.length; i++) { 150 196 getFocusedElements[i].blur(); 151 197 } 152 } 153 154 // Matches polyfill for IE11 198 199 for ( o = 0; o < getFocusedClassElements.length; o++) { 200 deleteClass( getFocusedClassElements[o], 'is-focused' ); 201 } 202 } 203 204 /** 205 * Matches polyfill for IE11 206 */ 155 207 if (!Element.prototype.matches) { 156 208 Element.prototype.matches = Element.prototype.msMatchesSelector; 157 209 } 158 210 159 // Toggle `focus` class to allow sub-menu access on touch screens. 211 /** 212 * Toggle `focus` class to allow sub-menu access on touch screens. 213 */ 160 214 function toggleSubmenuDisplay() { 161 215 … … 211 265 // Prevent default mouse events 212 266 event.preventDefault(); 213 } 214 215 // Prevent default mouse events 216 event.preventDefault(); 267 } else if ( 268 event.target.matches('.submenu-expand') || 269 null != getCurrentParent( event.target, '.submenu-expand' ) && 270 getCurrentParent( event.target, '.submenu-expand' ).matches( '.submenu-expand' ) || 271 event.target.matches('.menu-item-link-return') || 272 null != getCurrentParent( event.target, '.menu-item-link-return' ) && 273 getCurrentParent( event.target, '.menu-item-link-return' ).matches( '.menu-item-link-return' ) ) { 274 // Prevent default mouse events 275 event.preventDefault(); 276 } 217 277 218 278 // Prevent default mouse/focus events … … 223 283 document.addEventListener('focus', function(event) { 224 284 225 if ( event.target.matches('.main-navigation > div > ul > li >a') ) {226 227 // Remove Focuse elements in sibling div228 var currentDiv = getCurrentParent( event.target, 'div' );285 if ( event.target.matches('.main-navigation > div > ul > li a') ) { 286 287 // Remove Focuse elements in sibling div 288 var currentDiv = getCurrentParent( event.target, 'div' ); 229 289 var currentDivSibling = currentDiv.previousElementSibling === null ? currentDiv.nextElementSibling : currentDiv.previousElementSibling; 230 290 var focusedElement = currentDivSibling.querySelector( '.is-focused' ); 231 291 var focusedClass = 'is-focused'; 232 var prevLi = event.target.parentNode.previousElementSibling;233 var nextLi = event.target.parentNode.nextElementSibling;292 var prevLi = .previousElementSibling; 293 var nextLi = .nextElementSibling; 234 294 235 295 if ( null !== focusedElement && null !== hasClass( focusedElement, focusedClass ) ) { … … 237 297 } 238 298 239 // Add .is-focused class to top-level ul240 if ( event.target.parentNode.querySelector( '.main-navigation ul ul') ) {241 addClass( event.target.parentNode, focusedClass );299 // Add .is-focused class to top-level 300 if ( ) ) { 301 addClass( , focusedClass ); 242 302 } 243 303 … … 252 312 } 253 313 } 314 254 315 }, true); 255 } 256 257 // Run our sub-menu function as soon as the document is `ready` 316 317 document.addEventListener('click', function(event) { 318 319 // Remove all focused menu states when clicking outside site branding 320 if ( event.target !== document.getElementsByClassName( 'site-branding' )[0] ) { 321 removeAllFocusStates(); 322 } else { 323 // nothing 324 } 325 326 }, false); 327 } 328 329 /** 330 * Run our sub-menu function as soon as the document is `ready` 331 */ 258 332 document.addEventListener( 'DOMContentLoaded', function() { 259 333 toggleSubmenuDisplay(); 260 334 }); 261 // Annnnnd also every time the window resizes 335 336 /** 337 * Run our sub-menu function on selective refresh in the customizer 338 */ 339 document.addEventListener( 'customize-preview-menu-refreshed', function( e, params ) { 340 if ( 'menu-1' === params.wpNavMenuArgs.theme_location ) { 341 toggleSubmenuDisplay(); 342 } 343 }); 344 345 /** 346 * Run our sub-menu function every time the window resizes 347 */ 262 348 var isResizing = false; 263 window.addEventListener( 'resize', 349 window.addEventListener( 'resize', function() { 350 isResizing = true; 264 351 debounce( function() { 265 352 if ( isResizing ) { … … 267 354 } 268 355 269 isResizing = true; 270 setTimeout( function() { 271 toggleSubmenuDisplay(); 272 isResizing = false; 273 }, 150 ); 274 } ) 275 ); 356 toggleSubmenuDisplay(); 357 isResizing = false; 358 359 }, 150 ); 360 } ); 276 361 277 362 })();
Note: See TracChangeset
for help on using the changeset viewer.