Edit File by line
/home/barbar84/public_h.../wp-inclu.../js
File: admin-bar.js
/**
[0] Fix | Delete
* @output wp-includes/js/admin-bar.js
[1] Fix | Delete
*/
[2] Fix | Delete
/**
[3] Fix | Delete
* Admin bar with Vanilla JS, no external dependencies.
[4] Fix | Delete
*
[5] Fix | Delete
* @since 5.3.1
[6] Fix | Delete
*
[7] Fix | Delete
* @param {Object} document The document object.
[8] Fix | Delete
* @param {Object} window The window object.
[9] Fix | Delete
* @param {Object} navigator The navigator object.
[10] Fix | Delete
*
[11] Fix | Delete
* @return {void}
[12] Fix | Delete
*/
[13] Fix | Delete
( function( document, window, navigator ) {
[14] Fix | Delete
document.addEventListener( 'DOMContentLoaded', function() {
[15] Fix | Delete
var adminBar = document.getElementById( 'wpadminbar' ),
[16] Fix | Delete
topMenuItems,
[17] Fix | Delete
allMenuItems,
[18] Fix | Delete
adminBarLogout,
[19] Fix | Delete
adminBarSearchForm,
[20] Fix | Delete
shortlink,
[21] Fix | Delete
skipLink,
[22] Fix | Delete
mobileEvent,
[23] Fix | Delete
adminBarSearchInput,
[24] Fix | Delete
i;
[25] Fix | Delete
[26] Fix | Delete
if ( ! adminBar || ! ( 'querySelectorAll' in adminBar ) ) {
[27] Fix | Delete
return;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
topMenuItems = adminBar.querySelectorAll( 'li.menupop' );
[31] Fix | Delete
allMenuItems = adminBar.querySelectorAll( '.ab-item' );
[32] Fix | Delete
adminBarLogout = document.getElementById( 'wp-admin-bar-logout' );
[33] Fix | Delete
adminBarSearchForm = document.getElementById( 'adminbarsearch' );
[34] Fix | Delete
shortlink = document.getElementById( 'wp-admin-bar-get-shortlink' );
[35] Fix | Delete
skipLink = adminBar.querySelector( '.screen-reader-shortcut' );
[36] Fix | Delete
mobileEvent = /Mobile\/.+Safari/.test( navigator.userAgent ) ? 'touchstart' : 'click';
[37] Fix | Delete
[38] Fix | Delete
// Remove nojs class after the DOM is loaded.
[39] Fix | Delete
removeClass( adminBar, 'nojs' );
[40] Fix | Delete
[41] Fix | Delete
if ( 'ontouchstart' in window ) {
[42] Fix | Delete
// Remove hover class when the user touches outside the menu items.
[43] Fix | Delete
document.body.addEventListener( mobileEvent, function( e ) {
[44] Fix | Delete
if ( ! getClosest( e.target, 'li.menupop' ) ) {
[45] Fix | Delete
removeAllHoverClass( topMenuItems );
[46] Fix | Delete
}
[47] Fix | Delete
} );
[48] Fix | Delete
[49] Fix | Delete
// Add listener for menu items to toggle hover class by touches.
[50] Fix | Delete
// Remove the callback later for better performance.
[51] Fix | Delete
adminBar.addEventListener( 'touchstart', function bindMobileEvents() {
[52] Fix | Delete
for ( var i = 0; i < topMenuItems.length; i++ ) {
[53] Fix | Delete
topMenuItems[i].addEventListener( 'click', mobileHover.bind( null, topMenuItems ) );
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
adminBar.removeEventListener( 'touchstart', bindMobileEvents );
[57] Fix | Delete
} );
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
// Scroll page to top when clicking on the admin bar.
[61] Fix | Delete
adminBar.addEventListener( 'click', scrollToTop );
[62] Fix | Delete
[63] Fix | Delete
for ( i = 0; i < topMenuItems.length; i++ ) {
[64] Fix | Delete
// Adds or removes the hover class based on the hover intent.
[65] Fix | Delete
window.hoverintent(
[66] Fix | Delete
topMenuItems[i],
[67] Fix | Delete
addClass.bind( null, topMenuItems[i], 'hover' ),
[68] Fix | Delete
removeClass.bind( null, topMenuItems[i], 'hover' )
[69] Fix | Delete
).options( {
[70] Fix | Delete
timeout: 180
[71] Fix | Delete
} );
[72] Fix | Delete
[73] Fix | Delete
// Toggle hover class if the enter key is pressed.
[74] Fix | Delete
topMenuItems[i].addEventListener( 'keydown', toggleHoverIfEnter );
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
// Remove hover class if the escape key is pressed.
[78] Fix | Delete
for ( i = 0; i < allMenuItems.length; i++ ) {
[79] Fix | Delete
allMenuItems[i].addEventListener( 'keydown', removeHoverIfEscape );
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
if ( adminBarSearchForm ) {
[83] Fix | Delete
adminBarSearchInput = document.getElementById( 'adminbar-search' );
[84] Fix | Delete
[85] Fix | Delete
// Adds the adminbar-focused class on focus.
[86] Fix | Delete
adminBarSearchInput.addEventListener( 'focus', function() {
[87] Fix | Delete
addClass( adminBarSearchForm, 'adminbar-focused' );
[88] Fix | Delete
} );
[89] Fix | Delete
[90] Fix | Delete
// Removes the adminbar-focused class on blur.
[91] Fix | Delete
adminBarSearchInput.addEventListener( 'blur', function() {
[92] Fix | Delete
removeClass( adminBarSearchForm, 'adminbar-focused' );
[93] Fix | Delete
} );
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
if ( skipLink ) {
[97] Fix | Delete
// Focus the target of skip link after pressing Enter.
[98] Fix | Delete
skipLink.addEventListener( 'keydown', focusTargetAfterEnter );
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
if ( shortlink ) {
[102] Fix | Delete
shortlink.addEventListener( 'click', clickShortlink );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
// Prevents the toolbar from covering up content when a hash is present in the URL.
[106] Fix | Delete
if ( window.location.hash ) {
[107] Fix | Delete
window.scrollBy( 0, -32 );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
// Clear sessionStorage on logging out.
[111] Fix | Delete
if ( adminBarLogout ) {
[112] Fix | Delete
adminBarLogout.addEventListener( 'click', emptySessionStorage );
[113] Fix | Delete
}
[114] Fix | Delete
} );
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Remove hover class for top level menu item when escape is pressed.
[118] Fix | Delete
*
[119] Fix | Delete
* @since 5.3.1
[120] Fix | Delete
*
[121] Fix | Delete
* @param {Event} event The keydown event.
[122] Fix | Delete
*/
[123] Fix | Delete
function removeHoverIfEscape( event ) {
[124] Fix | Delete
var wrapper;
[125] Fix | Delete
[126] Fix | Delete
if ( event.which !== 27 ) {
[127] Fix | Delete
return;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
wrapper = getClosest( event.target, '.menupop' );
[131] Fix | Delete
[132] Fix | Delete
if ( ! wrapper ) {
[133] Fix | Delete
return;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
wrapper.querySelector( '.menupop > .ab-item' ).focus();
[137] Fix | Delete
removeClass( wrapper, 'hover' );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* Toggle hover class for top level menu item when enter is pressed.
[142] Fix | Delete
*
[143] Fix | Delete
* @since 5.3.1
[144] Fix | Delete
*
[145] Fix | Delete
* @param {Event} event The keydown event.
[146] Fix | Delete
*/
[147] Fix | Delete
function toggleHoverIfEnter( event ) {
[148] Fix | Delete
var wrapper;
[149] Fix | Delete
[150] Fix | Delete
if ( event.which !== 13 ) {
[151] Fix | Delete
return;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
if ( !! getClosest( event.target, '.ab-sub-wrapper' ) ) {
[155] Fix | Delete
return;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
wrapper = getClosest( event.target, '.menupop' );
[159] Fix | Delete
[160] Fix | Delete
if ( ! wrapper ) {
[161] Fix | Delete
return;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
event.preventDefault();
[165] Fix | Delete
[166] Fix | Delete
if ( hasClass( wrapper, 'hover' ) ) {
[167] Fix | Delete
removeClass( wrapper, 'hover' );
[168] Fix | Delete
} else {
[169] Fix | Delete
addClass( wrapper, 'hover' );
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Focus the target of skip link after pressing Enter.
[175] Fix | Delete
*
[176] Fix | Delete
* @since 5.3.1
[177] Fix | Delete
*
[178] Fix | Delete
* @param {Event} event The keydown event.
[179] Fix | Delete
*/
[180] Fix | Delete
function focusTargetAfterEnter( event ) {
[181] Fix | Delete
var id, userAgent;
[182] Fix | Delete
[183] Fix | Delete
if ( event.which !== 13 ) {
[184] Fix | Delete
return;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
id = event.target.getAttribute( 'href' );
[188] Fix | Delete
userAgent = navigator.userAgent.toLowerCase();
[189] Fix | Delete
[190] Fix | Delete
if ( userAgent.indexOf( 'applewebkit' ) > -1 && id && id.charAt( 0 ) === '#' ) {
[191] Fix | Delete
setTimeout( function() {
[192] Fix | Delete
var target = document.getElementById( id.replace( '#', '' ) );
[193] Fix | Delete
[194] Fix | Delete
if ( target ) {
[195] Fix | Delete
target.setAttribute( 'tabIndex', '0' );
[196] Fix | Delete
target.focus();
[197] Fix | Delete
}
[198] Fix | Delete
}, 100 );
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Toogle hover class for mobile devices.
[204] Fix | Delete
*
[205] Fix | Delete
* @since 5.3.1
[206] Fix | Delete
*
[207] Fix | Delete
* @param {NodeList} topMenuItems All menu items.
[208] Fix | Delete
* @param {Event} event The click event.
[209] Fix | Delete
*/
[210] Fix | Delete
function mobileHover( topMenuItems, event ) {
[211] Fix | Delete
var wrapper;
[212] Fix | Delete
[213] Fix | Delete
if ( !! getClosest( event.target, '.ab-sub-wrapper' ) ) {
[214] Fix | Delete
return;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
event.preventDefault();
[218] Fix | Delete
[219] Fix | Delete
wrapper = getClosest( event.target, '.menupop' );
[220] Fix | Delete
[221] Fix | Delete
if ( ! wrapper ) {
[222] Fix | Delete
return;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
if ( hasClass( wrapper, 'hover' ) ) {
[226] Fix | Delete
removeClass( wrapper, 'hover' );
[227] Fix | Delete
} else {
[228] Fix | Delete
removeAllHoverClass( topMenuItems );
[229] Fix | Delete
addClass( wrapper, 'hover' );
[230] Fix | Delete
}
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Handles the click on the Shortlink link in the adminbar.
[235] Fix | Delete
*
[236] Fix | Delete
* @since 3.1.0
[237] Fix | Delete
* @since 5.3.1 Use querySelector to clean up the function.
[238] Fix | Delete
*
[239] Fix | Delete
* @param {Event} event The click event.
[240] Fix | Delete
* @return {boolean} Returns false to prevent default click behavior.
[241] Fix | Delete
*/
[242] Fix | Delete
function clickShortlink( event ) {
[243] Fix | Delete
var wrapper = event.target.parentNode,
[244] Fix | Delete
input;
[245] Fix | Delete
[246] Fix | Delete
if ( wrapper ) {
[247] Fix | Delete
input = wrapper.querySelector( '.shortlink-input' );
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
if ( ! input ) {
[251] Fix | Delete
return;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
// (Old) IE doesn't support preventDefault, and does support returnValue.
[255] Fix | Delete
if ( event.preventDefault ) {
[256] Fix | Delete
event.preventDefault();
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
event.returnValue = false;
[260] Fix | Delete
[261] Fix | Delete
addClass( wrapper, 'selected' );
[262] Fix | Delete
[263] Fix | Delete
input.focus();
[264] Fix | Delete
input.select();
[265] Fix | Delete
input.onblur = function() {
[266] Fix | Delete
removeClass( wrapper, 'selected' );
[267] Fix | Delete
};
[268] Fix | Delete
[269] Fix | Delete
return false;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
/**
[273] Fix | Delete
* Clear sessionStorage on logging out.
[274] Fix | Delete
*
[275] Fix | Delete
* @since 5.3.1
[276] Fix | Delete
*/
[277] Fix | Delete
function emptySessionStorage() {
[278] Fix | Delete
if ( 'sessionStorage' in window ) {
[279] Fix | Delete
try {
[280] Fix | Delete
for ( var key in sessionStorage ) {
[281] Fix | Delete
if ( key.indexOf( 'wp-autosave-' ) > -1 ) {
[282] Fix | Delete
sessionStorage.removeItem( key );
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
} catch ( er ) {}
[286] Fix | Delete
}
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* Check if element has class.
[291] Fix | Delete
*
[292] Fix | Delete
* @since 5.3.1
[293] Fix | Delete
*
[294] Fix | Delete
* @param {HTMLElement} element The HTML element.
[295] Fix | Delete
* @param {string} className The class name.
[296] Fix | Delete
* @return {boolean} Whether the element has the className.
[297] Fix | Delete
*/
[298] Fix | Delete
function hasClass( element, className ) {
[299] Fix | Delete
var classNames;
[300] Fix | Delete
[301] Fix | Delete
if ( ! element ) {
[302] Fix | Delete
return false;
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
if ( element.classList && element.classList.contains ) {
[306] Fix | Delete
return element.classList.contains( className );
[307] Fix | Delete
} else if ( element.className ) {
[308] Fix | Delete
classNames = element.className.split( ' ' );
[309] Fix | Delete
return classNames.indexOf( className ) > -1;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
return false;
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Add class to an element.
[317] Fix | Delete
*
[318] Fix | Delete
* @since 5.3.1
[319] Fix | Delete
*
[320] Fix | Delete
* @param {HTMLElement} element The HTML element.
[321] Fix | Delete
* @param {string} className The class name.
[322] Fix | Delete
*/
[323] Fix | Delete
function addClass( element, className ) {
[324] Fix | Delete
if ( ! element ) {
[325] Fix | Delete
return;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
if ( element.classList && element.classList.add ) {
[329] Fix | Delete
element.classList.add( className );
[330] Fix | Delete
} else if ( ! hasClass( element, className ) ) {
[331] Fix | Delete
if ( element.className ) {
[332] Fix | Delete
element.className += ' ';
[333] Fix | Delete
}
[334] Fix | Delete
[335] Fix | Delete
element.className += className;
[336] Fix | Delete
}
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
/**
[340] Fix | Delete
* Remove class from an element.
[341] Fix | Delete
*
[342] Fix | Delete
* @since 5.3.1
[343] Fix | Delete
*
[344] Fix | Delete
* @param {HTMLElement} element The HTML element.
[345] Fix | Delete
* @param {string} className The class name.
[346] Fix | Delete
*/
[347] Fix | Delete
function removeClass( element, className ) {
[348] Fix | Delete
var testName,
[349] Fix | Delete
classes;
[350] Fix | Delete
[351] Fix | Delete
if ( ! element || ! hasClass( element, className ) ) {
[352] Fix | Delete
return;
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
if ( element.classList && element.classList.remove ) {
[356] Fix | Delete
element.classList.remove( className );
[357] Fix | Delete
} else {
[358] Fix | Delete
testName = ' ' + className + ' ';
[359] Fix | Delete
classes = ' ' + element.className + ' ';
[360] Fix | Delete
[361] Fix | Delete
while ( classes.indexOf( testName ) > -1 ) {
[362] Fix | Delete
classes = classes.replace( testName, '' );
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
element.className = classes.replace( /^[\s]+|[\s]+$/g, '' );
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
/**
[370] Fix | Delete
* Remove hover class for all menu items.
[371] Fix | Delete
*
[372] Fix | Delete
* @since 5.3.1
[373] Fix | Delete
*
[374] Fix | Delete
* @param {NodeList} topMenuItems All menu items.
[375] Fix | Delete
*/
[376] Fix | Delete
function removeAllHoverClass( topMenuItems ) {
[377] Fix | Delete
if ( topMenuItems && topMenuItems.length ) {
[378] Fix | Delete
for ( var i = 0; i < topMenuItems.length; i++ ) {
[379] Fix | Delete
removeClass( topMenuItems[i], 'hover' );
[380] Fix | Delete
}
[381] Fix | Delete
}
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
/**
[385] Fix | Delete
* Scrolls to the top of the page.
[386] Fix | Delete
*
[387] Fix | Delete
* @since 3.4.0
[388] Fix | Delete
*
[389] Fix | Delete
* @param {Event} event The Click event.
[390] Fix | Delete
*
[391] Fix | Delete
* @return {void}
[392] Fix | Delete
*/
[393] Fix | Delete
function scrollToTop( event ) {
[394] Fix | Delete
// Only scroll when clicking on the wpadminbar, not on menus or submenus.
[395] Fix | Delete
if (
[396] Fix | Delete
event.target &&
[397] Fix | Delete
event.target.id !== 'wpadminbar' &&
[398] Fix | Delete
event.target.id !== 'wp-admin-bar-top-secondary'
[399] Fix | Delete
) {
[400] Fix | Delete
return;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
try {
[404] Fix | Delete
window.scrollTo( {
[405] Fix | Delete
top: -32,
[406] Fix | Delete
left: 0,
[407] Fix | Delete
behavior: 'smooth'
[408] Fix | Delete
} );
[409] Fix | Delete
} catch ( er ) {
[410] Fix | Delete
window.scrollTo( 0, -32 );
[411] Fix | Delete
}
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
/**
[415] Fix | Delete
* Get closest Element.
[416] Fix | Delete
*
[417] Fix | Delete
* @since 5.3.1
[418] Fix | Delete
*
[419] Fix | Delete
* @param {HTMLElement} el Element to get parent.
[420] Fix | Delete
* @param {string} selector CSS selector to match.
[421] Fix | Delete
*/
[422] Fix | Delete
function getClosest( el, selector ) {
[423] Fix | Delete
if ( ! window.Element.prototype.matches ) {
[424] Fix | Delete
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/API/Element/matches.
[425] Fix | Delete
window.Element.prototype.matches =
[426] Fix | Delete
window.Element.prototype.matchesSelector ||
[427] Fix | Delete
window.Element.prototype.mozMatchesSelector ||
[428] Fix | Delete
window.Element.prototype.msMatchesSelector ||
[429] Fix | Delete
window.Element.prototype.oMatchesSelector ||
[430] Fix | Delete
window.Element.prototype.webkitMatchesSelector ||
[431] Fix | Delete
function( s ) {
[432] Fix | Delete
var matches = ( this.document || this.ownerDocument ).querySelectorAll( s ),
[433] Fix | Delete
i = matches.length;
[434] Fix | Delete
[435] Fix | Delete
while ( --i >= 0 && matches.item( i ) !== this ) { }
[436] Fix | Delete
[437] Fix | Delete
return i > -1;
[438] Fix | Delete
};
[439] Fix | Delete
}
[440] Fix | Delete
[441] Fix | Delete
// Get the closest matching elent.
[442] Fix | Delete
for ( ; el && el !== document; el = el.parentNode ) {
[443] Fix | Delete
if ( el.matches( selector ) ) {
[444] Fix | Delete
return el;
[445] Fix | Delete
}
[446] Fix | Delete
}
[447] Fix | Delete
[448] Fix | Delete
return null;
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
} )( document, window, navigator );
[452] Fix | Delete
[453] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function