2018-06-27 22:30:15 -04:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/admin-bar.js
|
|
|
|
*/
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Admin bar with Vanilla JS, no external dependencies.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
|
|
|
*
|
2019-12-09 20:03:02 -05:00
|
|
|
* @param {Object} document The document object.
|
|
|
|
* @param {Object} window The window object.
|
|
|
|
* @param {Object} navigator The navigator object.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
( function( document, window, navigator ) {
|
|
|
|
document.addEventListener( 'DOMContentLoaded', function() {
|
|
|
|
var adminBar = document.getElementById( 'wpadminbar' ),
|
2019-12-11 13:56:03 -05:00
|
|
|
topMenuItems,
|
|
|
|
allMenuItems,
|
|
|
|
adminBarLogout,
|
|
|
|
adminBarSearchForm,
|
|
|
|
shortlink,
|
|
|
|
skipLink,
|
|
|
|
mobileEvent,
|
2019-12-09 20:03:02 -05:00
|
|
|
adminBarSearchInput,
|
|
|
|
i;
|
2012-10-19 14:31:21 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( ! adminBar || ! ( 'querySelectorAll' in adminBar ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
topMenuItems = adminBar.querySelectorAll( 'li.menupop' );
|
|
|
|
allMenuItems = adminBar.querySelectorAll( '.ab-item' );
|
|
|
|
adminBarLogout = document.getElementById( 'wp-admin-bar-logout' );
|
|
|
|
adminBarSearchForm = document.getElementById( 'adminbarsearch' );
|
|
|
|
shortlink = document.getElementById( 'wp-admin-bar-get-shortlink' );
|
|
|
|
skipLink = adminBar.querySelector( '.screen-reader-shortcut' );
|
|
|
|
mobileEvent = /Mobile\/.+Safari/.test( navigator.userAgent ) ? 'touchstart' : 'click';
|
|
|
|
|
|
|
|
// Remove nojs class after the DOM is loaded.
|
|
|
|
removeClass( adminBar, 'nojs' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( 'ontouchstart' in window ) {
|
2019-12-11 13:56:03 -05:00
|
|
|
// Remove hover class when the user touches outside the menu items.
|
2019-12-09 20:03:02 -05:00
|
|
|
document.body.addEventListener( mobileEvent, function( e ) {
|
|
|
|
if ( ! getClosest( e.target, 'li.menupop' ) ) {
|
|
|
|
removeAllHoverClass( topMenuItems );
|
2012-10-19 14:31:21 -04:00
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
} );
|
2012-11-17 15:14:17 -05:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Add listener for menu items to toggle hover class by touches.
|
|
|
|
// Remove the callback later for better performance.
|
2019-12-09 20:03:02 -05:00
|
|
|
adminBar.addEventListener( 'touchstart', function bindMobileEvents() {
|
|
|
|
for ( var i = 0; i < topMenuItems.length; i++ ) {
|
|
|
|
topMenuItems[i].addEventListener( 'click', mobileHover.bind( null, topMenuItems ) );
|
2012-11-17 15:14:17 -05:00
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
|
|
|
|
adminBar.removeEventListener( 'touchstart', bindMobileEvents );
|
|
|
|
} );
|
|
|
|
}
|
2012-11-17 15:14:17 -05:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Scroll page to top when clicking on the admin bar.
|
2019-12-09 20:03:02 -05:00
|
|
|
adminBar.addEventListener( 'click', scrollToTop );
|
2012-11-17 15:14:17 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
for ( i = 0; i < topMenuItems.length; i++ ) {
|
2019-12-11 13:56:03 -05:00
|
|
|
// Adds or removes the hover class based on the hover intent.
|
|
|
|
window.hoverintent(
|
2019-12-09 20:03:02 -05:00
|
|
|
topMenuItems[i],
|
2019-12-11 13:56:03 -05:00
|
|
|
addClass.bind( null, topMenuItems[i], 'hover' ),
|
|
|
|
removeClass.bind( null, topMenuItems[i], 'hover' )
|
2019-12-09 20:03:02 -05:00
|
|
|
).options( {
|
2019-12-09 20:34:01 -05:00
|
|
|
timeout: 180
|
2019-12-09 20:03:02 -05:00
|
|
|
} );
|
2012-11-17 15:14:17 -05:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Toggle hover class if the enter key is pressed.
|
2019-12-09 20:03:02 -05:00
|
|
|
topMenuItems[i].addEventListener( 'keydown', toggleHoverIfEnter );
|
2012-10-19 14:31:21 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Remove hover class if the escape key is pressed.
|
2019-12-09 20:03:02 -05:00
|
|
|
for ( i = 0; i < allMenuItems.length; i++ ) {
|
|
|
|
allMenuItems[i].addEventListener( 'keydown', removeHoverIfEscape );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( adminBarSearchForm ) {
|
|
|
|
adminBarSearchInput = document.getElementById( 'adminbar-search' );
|
2015-01-09 04:51:22 -05:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Adds the adminbar-focused class on focus.
|
2019-12-09 20:03:02 -05:00
|
|
|
adminBarSearchInput.addEventListener( 'focus', function() {
|
2019-12-11 13:56:03 -05:00
|
|
|
addClass( adminBarSearchForm, 'adminbar-focused' );
|
2019-12-09 20:03:02 -05:00
|
|
|
} );
|
2015-01-09 04:51:22 -05:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Removes the adminbar-focused class on blur.
|
2019-12-09 20:03:02 -05:00
|
|
|
adminBarSearchInput.addEventListener( 'blur', function() {
|
2019-12-11 13:56:03 -05:00
|
|
|
removeClass( adminBarSearchForm, 'adminbar-focused' );
|
2019-12-09 20:03:02 -05:00
|
|
|
} );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( skipLink ) {
|
|
|
|
// Focus the target of skip link after pressing Enter.
|
|
|
|
skipLink.addEventListener( 'keydown', focusTargetAfterEnter );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( shortlink ) {
|
|
|
|
shortlink.addEventListener( 'click', clickShortlink );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Prevents the toolbar from covering up content when a hash is present in the URL.
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( window.location.hash ) {
|
|
|
|
window.scrollBy( 0, -32 );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
// Clear sessionStorage on logging out.
|
|
|
|
if ( adminBarLogout ) {
|
|
|
|
adminBarLogout.addEventListener( 'click', emptySessionStorage );
|
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
} );
|
2013-11-14 00:51:10 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Remove hover class for top level menu item when escape is pressed.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The keydown event.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function removeHoverIfEscape( event ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
var wrapper;
|
2012-10-16 17:26:59 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( event.which !== 27 ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
wrapper = getClosest( event.target, '.menupop' );
|
2015-04-08 12:08:28 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
2013-12-08 21:41:10 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
wrapper.querySelector( '.menupop > .ab-item' ).focus();
|
2019-12-11 13:56:03 -05:00
|
|
|
removeClass( wrapper, 'hover' );
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2013-12-08 21:41:10 -05:00
|
|
|
|
2018-06-27 22:23:53 -04:00
|
|
|
/**
|
2019-12-09 20:03:02 -05:00
|
|
|
* Toggle hover class for top level menu item when enter is pressed.
|
2018-06-27 22:23:53 -04:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2018-06-27 22:23:53 -04:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The keydown event.
|
2018-06-27 22:23:53 -04:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function toggleHoverIfEnter( event ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
var wrapper;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( event.which !== 13 ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( !! getClosest( event.target, '.ab-sub-wrapper' ) ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
wrapper = getClosest( event.target, '.menupop' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if ( hasClass( wrapper, 'hover' ) ) {
|
|
|
|
removeClass( wrapper, 'hover' );
|
2019-12-09 20:03:02 -05:00
|
|
|
} else {
|
2019-12-11 13:56:03 -05:00
|
|
|
addClass( wrapper, 'hover' );
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Focus the target of skip link after pressing Enter.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The keydown event.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function focusTargetAfterEnter( event ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
var id, userAgent;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( event.which !== 13 ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
id = event.target.getAttribute( 'href' );
|
2019-12-09 20:03:02 -05:00
|
|
|
userAgent = navigator.userAgent.toLowerCase();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( userAgent.indexOf( 'applewebkit' ) > -1 && id && id.charAt( 0 ) === '#' ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
setTimeout( function() {
|
|
|
|
var target = document.getElementById( id.replace( '#', '' ) );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( target ) {
|
|
|
|
target.setAttribute( 'tabIndex', '0' );
|
|
|
|
target.focus();
|
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
}, 100 );
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Toogle hover class for mobile devices.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
|
|
|
* @param {NodeList} topMenuItems All menu items.
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The click event.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function mobileHover( topMenuItems, event ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
var wrapper;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( !! getClosest( event.target, '.ab-sub-wrapper' ) ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
event.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
wrapper = getClosest( event.target, '.menupop' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( hasClass( wrapper, 'hover' ) ) {
|
|
|
|
removeClass( wrapper, 'hover' );
|
2019-12-09 20:03:02 -05:00
|
|
|
} else {
|
|
|
|
removeAllHoverClass( topMenuItems );
|
2019-12-11 13:56:03 -05:00
|
|
|
addClass( wrapper, 'hover' );
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Handles the click on the Shortlink link in the adminbar.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1 Use querySelector to clean up the function.
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The click event.
|
2019-12-09 20:03:02 -05:00
|
|
|
* @return {boolean} Returns false to prevent default click behavior.
|
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function clickShortlink( event ) {
|
|
|
|
var wrapper = event.target.parentNode,
|
|
|
|
input;
|
|
|
|
|
|
|
|
if ( wrapper ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
input = wrapper.querySelector( '.shortlink-input' );
|
2019-12-11 13:56:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! input ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// (Old) IE doesn't support preventDefault, and does support returnValue.
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( event.preventDefault ) {
|
|
|
|
event.preventDefault();
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-11 13:56:03 -05:00
|
|
|
event.returnValue = false;
|
|
|
|
|
|
|
|
addClass( wrapper, 'selected' );
|
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
input.focus();
|
|
|
|
input.select();
|
|
|
|
input.onblur = function() {
|
2019-12-11 13:56:03 -05:00
|
|
|
removeClass( wrapper, 'selected' );
|
2019-12-09 20:03:02 -05:00
|
|
|
};
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Clear sessionStorage on logging out.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
|
|
|
function emptySessionStorage() {
|
|
|
|
if ( 'sessionStorage' in window ) {
|
|
|
|
try {
|
|
|
|
for ( var key in sessionStorage ) {
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( key.indexOf( 'wp-autosave-' ) > -1 ) {
|
2019-12-09 20:03:02 -05:00
|
|
|
sessionStorage.removeItem( key );
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 13:56:03 -05:00
|
|
|
} catch ( er ) {}
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
2019-12-11 13:56:03 -05:00
|
|
|
* Check if element has class.
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {HTMLElement} element The HTML element.
|
|
|
|
* @param {String} className The class name.
|
|
|
|
* @return {bool} Whether the element has the className.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function hasClass( element, className ) {
|
|
|
|
var classNames;
|
|
|
|
|
|
|
|
if ( ! element ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( element.classList && element.classList.contains ) {
|
|
|
|
return element.classList.contains( className );
|
|
|
|
} else if ( element.className ) {
|
|
|
|
classNames = element.className.split( ' ' );
|
|
|
|
return classNames.indexOf( className ) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
2019-12-11 13:56:03 -05:00
|
|
|
* Add class to an element.
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {HTMLElement} element The HTML element.
|
|
|
|
* @param {String} className The class name.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function addClass( element, className ) {
|
|
|
|
if ( ! element ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( element.classList && element.classList.add ) {
|
|
|
|
element.classList.add( className );
|
|
|
|
} else if ( ! hasClass( element, className ) ) {
|
|
|
|
if ( element.className ) {
|
|
|
|
element.className += ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
element.className += className;
|
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
2019-12-11 13:56:03 -05:00
|
|
|
* Remove class from an element.
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {HTMLElement} element The HTML element.
|
|
|
|
* @param {String} className The class name.
|
2019-12-09 20:03:02 -05:00
|
|
|
*/
|
2019-12-11 13:56:03 -05:00
|
|
|
function removeClass( element, className ) {
|
|
|
|
var testName,
|
|
|
|
classes;
|
|
|
|
|
|
|
|
if ( ! element || ! hasClass( element, className ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( element.classList && element.classList.remove ) {
|
|
|
|
element.classList.remove( className );
|
|
|
|
} else {
|
|
|
|
testName = ' ' + className + ' ';
|
|
|
|
classes = ' ' + element.className + ' ';
|
|
|
|
|
|
|
|
while ( classes.indexOf( testName ) > -1 ) {
|
|
|
|
classes = classes.replace( testName, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
element.className = classes.replace( /^[\s]+|[\s]+$/g, '' );
|
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Remove hover class for all menu items.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
|
|
|
* @param {NodeList} topMenuItems All menu items.
|
|
|
|
*/
|
|
|
|
function removeAllHoverClass( topMenuItems ) {
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( topMenuItems && topMenuItems.length ) {
|
|
|
|
for ( var i = 0; i < topMenuItems.length; i++ ) {
|
|
|
|
removeClass( topMenuItems[i], 'hover' );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls to the top of the page.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @param {Event} event The Click event.
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function scrollToTop( event ) {
|
|
|
|
// Only scroll when clicking on the wpadminbar, not on menus or submenus.
|
|
|
|
if (
|
|
|
|
event.target &&
|
2019-12-11 13:56:03 -05:00
|
|
|
event.target.id !== 'wpadminbar' &&
|
|
|
|
event.target.id !== 'wp-admin-bar-top-secondary'
|
2019-12-09 20:03:02 -05:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
try {
|
|
|
|
window.scrollTo( {
|
|
|
|
top: -32,
|
|
|
|
left: 0,
|
|
|
|
behavior: 'smooth'
|
|
|
|
} );
|
|
|
|
} catch ( er ) {
|
|
|
|
window.scrollTo( 0, -32 );
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 21:41:10 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
/**
|
|
|
|
* Get closest Element.
|
|
|
|
*
|
2019-12-11 13:56:03 -05:00
|
|
|
* @since 5.3.1
|
2019-12-09 20:03:02 -05:00
|
|
|
*
|
|
|
|
* @param {HTMLElement} el Element to get parent.
|
|
|
|
* @param {string} selector CSS selector to match.
|
|
|
|
*/
|
|
|
|
function getClosest( el, selector ) {
|
2019-12-11 13:56:03 -05:00
|
|
|
if ( ! window.Element.prototype.matches ) {
|
|
|
|
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/API/Element/matches.
|
|
|
|
window.Element.prototype.matches =
|
|
|
|
window.Element.prototype.matchesSelector ||
|
|
|
|
window.Element.prototype.mozMatchesSelector ||
|
|
|
|
window.Element.prototype.msMatchesSelector ||
|
|
|
|
window.Element.prototype.oMatchesSelector ||
|
|
|
|
window.Element.prototype.webkitMatchesSelector ||
|
2019-12-09 20:03:02 -05:00
|
|
|
function( s ) {
|
|
|
|
var matches = ( this.document || this.ownerDocument ).querySelectorAll( s ),
|
|
|
|
i = matches.length;
|
2019-12-11 13:56:03 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
while ( --i >= 0 && matches.item( i ) !== this ) { }
|
2019-12-11 13:56:03 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
return i > -1;
|
|
|
|
};
|
|
|
|
}
|
2013-12-08 21:41:10 -05:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Get the closest matching elent.
|
2019-12-09 20:03:02 -05:00
|
|
|
for ( ; el && el !== document; el = el.parentNode ) {
|
|
|
|
if ( el.matches( selector ) ) {
|
|
|
|
return el;
|
2013-12-08 21:41:10 -05:00
|
|
|
}
|
2019-12-09 20:03:02 -05:00
|
|
|
}
|
2019-12-11 13:56:03 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
return null;
|
|
|
|
}
|
2019-12-11 13:56:03 -05:00
|
|
|
|
2019-12-09 20:03:02 -05:00
|
|
|
} )( document, window, navigator );
|