[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> admin-bar.js (source)

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


Generated: Tue Feb 18 01:00:03 2020 Cross-referenced by PHPXref 0.7.1