admin-bar.js 11 KB

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