From 4a6a3d0338affa7019044ea7d09e90c70e07d640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Wed, 9 Feb 2022 16:28:12 +0000 Subject: [PATCH 1/4] Twenty Sixteen: reimplement frontend JS w/o jQuery --- .../themes/twentysixteen/functions.php | 4 +- .../themes/twentysixteen/js/functions.js | 357 +++++++++++------- .../js/keyboard-image-navigation.js | 30 +- 3 files changed, 239 insertions(+), 152 deletions(-) diff --git a/src/wp-content/themes/twentysixteen/functions.php b/src/wp-content/themes/twentysixteen/functions.php index d098a1ebde927..6053fe6c03d22 100644 --- a/src/wp-content/themes/twentysixteen/functions.php +++ b/src/wp-content/themes/twentysixteen/functions.php @@ -412,10 +412,10 @@ function twentysixteen_scripts() { } if ( is_singular() && wp_attachment_is_image() ) { - wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20170530' ); + wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array(), '20220209', true ); } - wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20211130', true ); + wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array(), '20220209', true ); wp_localize_script( 'twentysixteen-script', diff --git a/src/wp-content/themes/twentysixteen/js/functions.js b/src/wp-content/themes/twentysixteen/js/functions.js index 4c3b97d0329f7..57d640e1f585e 100644 --- a/src/wp-content/themes/twentysixteen/js/functions.js +++ b/src/wp-content/themes/twentysixteen/js/functions.js @@ -4,210 +4,293 @@ * * Contains handlers for navigation and widget area. */ + ( function () { + 'use strict'; -( function( $ ) { - var body, masthead, menuToggle, siteNavigation, socialNavigation, siteHeaderMenu, resizeTimer; + var masthead, menuToggle, siteNavigation, socialNavigation, siteHeaderMenu, resizeTimer; + + function matches( el, sel ) { + if ( Element.prototype.matches ) { + return el.matches( sel ); + } + + if ( Element.prototype.msMatchesSelector ) { + return el.msMatchesSelector( sel ); + } + } + + // Based on https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill + function closest( el, sel ) { + if ( el.closest ) { + return el.closest( sel ); + } + + var current = el; + + do { + if ( matches( el, sel ) ) { + return current; + } + current = current.parentElement || current.parentNode; + } while ( current !== null && current.nodeType === 1 ); + + return null; + } + + function forEachNode( parent, selector, fn ) { + var nodes = parent.querySelectorAll( selector ); + for ( var i = 0; i < nodes.length; i++ ) { + fn( nodes[ i ] ); + } + } function initMainNavigation( container ) { + forEachNode( container, '.menu-item-has-children > a', function ( el ) { + // Add dropdown toggle that displays child menu items. + var dropdownToggle = document.createElement( 'button' ); + dropdownToggle.className = 'dropdown-toggle'; + dropdownToggle.setAttribute( 'aria-expanded', 'false' ); - // Add dropdown toggle that displays child menu items. - var dropdownToggle = $( '