2018-06-28 04:30:15 +02:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/admin-bar.js
|
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Admin bar with Vanilla JS, no external dependencies.
|
|
|
|
*
|
|
|
|
* @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' ),
|
|
|
|
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',
|
|
|
|
adminBarSearchInput,
|
|
|
|
i;
|
2012-10-19 20:31:21 +02:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Remove nojs class after the DOM is loaded.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
adminBar.classList.remove( 'nojs' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( 'ontouchstart' in window ) {
|
|
|
|
/**
|
|
|
|
* Remove hover class when the user touches outside the menu items.
|
|
|
|
*/
|
|
|
|
document.body.addEventListener( mobileEvent, function( e ) {
|
|
|
|
if ( ! getClosest( e.target, 'li.menupop' ) ) {
|
|
|
|
removeAllHoverClass( topMenuItems );
|
2012-10-19 20:31:21 +02:00
|
|
|
}
|
2019-12-10 02:03:02 +01:00
|
|
|
} );
|
2012-11-17 21:14:17 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Add listener for menu items to toggle hover class by touches.
|
|
|
|
* Remove the callback later for better performance.
|
|
|
|
*/
|
|
|
|
adminBar.addEventListener( 'touchstart', function bindMobileEvents() {
|
|
|
|
for ( var i = 0; i < topMenuItems.length; i++ ) {
|
|
|
|
topMenuItems[i].addEventListener( 'click', mobileHover.bind( null, topMenuItems ) );
|
2012-11-17 21:14:17 +01:00
|
|
|
}
|
2019-12-10 02:03:02 +01:00
|
|
|
|
|
|
|
adminBar.removeEventListener( 'touchstart', bindMobileEvents );
|
|
|
|
} );
|
|
|
|
}
|
2012-11-17 21:14:17 +01:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Scroll page to top when clicking on the admin bar.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
adminBar.addEventListener( 'click', scrollToTop );
|
2012-11-17 21:14:17 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
for ( i = 0; i < topMenuItems.length; i++ ) {
|
|
|
|
/**
|
|
|
|
* Adds or removes the hover class based on the hover intent.
|
|
|
|
*/
|
|
|
|
hoverintent(
|
|
|
|
topMenuItems[i],
|
|
|
|
addHoverClass.bind( null, topMenuItems[i] ),
|
|
|
|
removeHoverClass.bind( null, topMenuItems[i] )
|
|
|
|
).options( {
|
|
|
|
timeout: 180,
|
|
|
|
} );
|
2012-11-17 21:14:17 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Toggle hover class if the enter key is pressed.
|
|
|
|
*/
|
|
|
|
topMenuItems[i].addEventListener( 'keydown', toggleHoverIfEnter );
|
2012-10-19 20:31:21 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Remove hover class if the escape key is pressed.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
for ( i = 0; i < allMenuItems.length; i++ ) {
|
|
|
|
allMenuItems[i].addEventListener( 'keydown', removeHoverIfEscape );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( adminBarSearchForm ) {
|
|
|
|
adminBarSearchInput = document.getElementById( 'adminbar-search' );
|
2015-01-09 10:51:22 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Adds the adminbar-focused class on focus.
|
|
|
|
*/
|
|
|
|
adminBarSearchInput.addEventListener( 'focus', function() {
|
|
|
|
adminBarSearchForm.classList.add( 'adminbar-focused' );
|
|
|
|
} );
|
2015-01-09 10:51:22 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Removes the adminbar-focused class on blur.
|
|
|
|
*/
|
|
|
|
adminBarSearchInput.addEventListener( 'blur', function() {
|
|
|
|
adminBarSearchForm.classList.remove( 'adminbar-focused' );
|
|
|
|
} );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Focus the target of skip link after pressing Enter.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
skipLink.addEventListener( 'keydown', focusTargetAfterEnter );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( shortlink ) {
|
|
|
|
shortlink.addEventListener( 'click', clickShortlink );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Prevents the toolbar from covering up content when a hash is present
|
|
|
|
* in the URL.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( window.location.hash ) {
|
|
|
|
window.scrollBy( 0, -32 );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Add no-font-face class to body if needed.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( navigator.userAgent && document.body.className.indexOf( 'no-font-face' ) === -1 &&
|
|
|
|
/Android (1.0|1.1|1.5|1.6|2.0|2.1)|Nokia|Opera Mini|w(eb)?OSBrowser|webOS|UCWEB|Windows Phone OS 7|XBLWP7|ZuneWP7|MSIE 7/.test( navigator.userAgent ) ) {
|
|
|
|
document.body.className += ' no-font-face';
|
|
|
|
}
|
2012-10-16 23:26:59 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Clear sessionStorage on logging out.
|
|
|
|
*/
|
|
|
|
adminBarLogout.addEventListener( 'click', emptySessionStorage );
|
|
|
|
} );
|
2013-11-14 06:51:10 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Remove hover class for top level menu item when escape is pressed.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {Event} e The keydown event.
|
|
|
|
*/
|
|
|
|
function removeHoverIfEscape( e ) {
|
|
|
|
var wrapper;
|
2012-10-16 23:26:59 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( e.which != 27 ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-13 11:08:16 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
wrapper = getClosest( e.target, '.menupop' );
|
2015-04-08 18:08:28 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
2013-03-13 11:08:16 +01:00
|
|
|
}
|
2013-12-09 03:41:10 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
wrapper.querySelector( '.menupop > .ab-item' ).focus();
|
|
|
|
removeHoverClass( wrapper );
|
|
|
|
}
|
2013-12-09 03:41:10 +01:00
|
|
|
|
2018-06-28 04:23:53 +02:00
|
|
|
/**
|
2019-12-10 02:03:02 +01:00
|
|
|
* Toggle hover class for top level menu item when enter is pressed.
|
2018-06-28 04:23:53 +02:00
|
|
|
*
|
2019-12-10 02:03:02 +01:00
|
|
|
* @since 5.3.0
|
2018-06-28 04:23:53 +02:00
|
|
|
*
|
2019-12-10 02:03:02 +01:00
|
|
|
* @param {Event} e The keydown event.
|
2018-06-28 04:23:53 +02:00
|
|
|
*/
|
2019-12-10 02:03:02 +01:00
|
|
|
function toggleHoverIfEnter( e ) {
|
|
|
|
var wrapper;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( e.which != 13 ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( !! getClosest( e.target, '.ab-sub-wrapper' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
wrapper = getClosest( e.target, '.menupop' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
e.preventDefault();
|
|
|
|
if ( hasHoverClass( wrapper ) ) {
|
|
|
|
removeHoverClass( wrapper );
|
|
|
|
} else {
|
|
|
|
addHoverClass( wrapper );
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Focus the target of skip link after pressing Enter.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {Event} e The keydown event.
|
|
|
|
*/
|
|
|
|
function focusTargetAfterEnter( e ) {
|
|
|
|
var id, userAgent;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( 13 !== e.which ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
id = e.target.getAttribute( 'href' );
|
|
|
|
userAgent = navigator.userAgent.toLowerCase();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( userAgent.indexOf( 'applewebkit' ) != -1 && id && id.charAt( 0 ) == '#' ) {
|
|
|
|
setTimeout( function() {
|
|
|
|
var target = document.getElementById( id.replace( '#', '' ) );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
target.setAttribute( 'tabIndex', '0' );
|
|
|
|
target.focus();
|
|
|
|
}, 100 );
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Toogle hover class for mobile devices.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {NodeList} topMenuItems All menu items.
|
|
|
|
* @param {Event} e The click event.
|
|
|
|
*/
|
|
|
|
function mobileHover( topMenuItems, e ) {
|
|
|
|
var wrapper;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( !! getClosest( e.target, '.ab-sub-wrapper' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
e.preventDefault();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
wrapper = getClosest( e.target, '.menupop' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( ! wrapper ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
if ( hasHoverClass( wrapper ) ) {
|
|
|
|
removeHoverClass( wrapper );
|
|
|
|
} else {
|
|
|
|
removeAllHoverClass( topMenuItems );
|
|
|
|
addHoverClass( wrapper );
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Handles the click on the Shortlink link in the adminbar.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @since 5.3.0 Use querySelector to clean up the function.
|
|
|
|
*
|
|
|
|
* @param {Event} e The click event.
|
|
|
|
*
|
|
|
|
* @return {boolean} Returns false to prevent default click behavior.
|
|
|
|
*/
|
|
|
|
function clickShortlink( e ) {
|
|
|
|
var wrapper = e.target.parentNode,
|
|
|
|
input = wrapper.querySelector( '.shortlink-input' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
// IE doesn't support preventDefault, and does support returnValue
|
|
|
|
if ( e.preventDefault ) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
e.returnValue = false;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
wrapper.classList.add( 'selected' );
|
|
|
|
input.focus();
|
|
|
|
input.select();
|
|
|
|
input.onblur = function() {
|
|
|
|
wrapper.classList.remove( 'selected' );
|
|
|
|
};
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Clear sessionStorage on logging out.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*/
|
|
|
|
function emptySessionStorage() {
|
|
|
|
if ( 'sessionStorage' in window ) {
|
|
|
|
try {
|
|
|
|
for ( var key in sessionStorage ) {
|
|
|
|
if ( key.indexOf( 'wp-autosave-' ) != -1 ) {
|
|
|
|
sessionStorage.removeItem( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( e ) {}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Check if menu item has hover class.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} item Menu item Element.
|
|
|
|
*/
|
|
|
|
function hasHoverClass( item ) {
|
|
|
|
return item.classList.contains( 'hover' );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Add hover class for menu item.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} item Menu item Element.
|
|
|
|
*/
|
|
|
|
function addHoverClass( item ) {
|
|
|
|
item.classList.add( 'hover' );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Remove hover class for menu item.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} item Menu item Element.
|
|
|
|
*/
|
|
|
|
function removeHoverClass( item ) {
|
|
|
|
item.classList.remove( 'hover' );
|
|
|
|
}
|
2013-03-13 11:08:16 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Remove hover class for all menu items.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {NodeList} topMenuItems All menu items.
|
|
|
|
*/
|
|
|
|
function removeAllHoverClass( topMenuItems ) {
|
|
|
|
for ( var i = 0; i < topMenuItems.length; i++ ) {
|
|
|
|
if ( hasHoverClass( topMenuItems[i] ) ) {
|
|
|
|
removeHoverClass( topMenuItems[i] );
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2019-12-10 02:03:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls to the top of the page.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param {Event} e The Click event.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function scrollToTop( event ) {
|
|
|
|
// Only scroll when clicking on the wpadminbar, not on menus or submenus.
|
|
|
|
if (
|
|
|
|
event.target &&
|
|
|
|
event.target.id &&
|
|
|
|
event.target.id != 'wpadminbar' &&
|
|
|
|
event.target.id != 'wp-admin-bar-top-secondary'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
try {
|
|
|
|
window.scrollTo( {
|
|
|
|
top: -32,
|
|
|
|
left: 0,
|
|
|
|
behavior: 'smooth'
|
|
|
|
} );
|
|
|
|
} catch ( er ) {
|
|
|
|
window.scrollTo( 0, -32 );
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 03:41:10 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
/**
|
|
|
|
* Get closest Element.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} el Element to get parent.
|
|
|
|
* @param {string} selector CSS selector to match.
|
|
|
|
*/
|
|
|
|
function getClosest( el, selector ) {
|
|
|
|
if ( ! Element.prototype.matches ) {
|
|
|
|
Element.prototype.matches =
|
|
|
|
Element.prototype.matchesSelector ||
|
|
|
|
Element.prototype.mozMatchesSelector ||
|
|
|
|
Element.prototype.msMatchesSelector ||
|
|
|
|
Element.prototype.oMatchesSelector ||
|
|
|
|
Element.prototype.webkitMatchesSelector ||
|
|
|
|
function( s ) {
|
|
|
|
var matches = ( this.document || this.ownerDocument ).querySelectorAll( s ),
|
|
|
|
i = matches.length;
|
|
|
|
while ( --i >= 0 && matches.item( i ) !== this ) { }
|
|
|
|
return i > -1;
|
|
|
|
};
|
|
|
|
}
|
2013-12-09 03:41:10 +01:00
|
|
|
|
2019-12-10 02:03:02 +01:00
|
|
|
// Get the closest matching elent
|
|
|
|
for ( ; el && el !== document; el = el.parentNode ) {
|
|
|
|
if ( el.matches( selector ) ) {
|
|
|
|
return el;
|
2013-12-09 03:41:10 +01:00
|
|
|
}
|
2019-12-10 02:03:02 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} )( document, window, navigator );
|