[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/themes/twentyfifteen/js/ -> functions.js (source)

   1  /* global screenReaderText */
   2  /**
   3   * Theme functions file.
   4   *
   5   * Contains handlers for navigation and widget area.
   6   */
   7  
   8  ( function( $ ) {
   9      var $body, $window, $sidebar, adminbarOffset, top = false,
  10          bottom = false, windowWidth, windowHeight, lastWindowPos = 0,
  11          topOffset = 0, bodyHeight, sidebarHeight, resizeTimer,
  12          secondary, button;
  13  
  14  	function initMainNavigation( container ) {
  15          // Add dropdown toggle that display child menu items.
  16          container.find( '.menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
  17  
  18          // Toggle buttons and submenu items with active children menu items.
  19          container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
  20          container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
  21  
  22          container.find( '.dropdown-toggle' ).click( function( e ) {
  23              var _this = $( this );
  24              e.preventDefault();
  25              _this.toggleClass( 'toggle-on' );
  26              _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
  27              _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
  28              _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
  29          } );
  30      }
  31      initMainNavigation( $( '.main-navigation' ) );
  32  
  33      // Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states.
  34      $( document ).on( 'customize-preview-menu-refreshed', function( e, params ) {
  35          if ( 'primary' === params.wpNavMenuArgs.theme_location ) {
  36              initMainNavigation( params.newContainer );
  37  
  38              // Re-sync expanded states from oldContainer.
  39              params.oldContainer.find( '.dropdown-toggle.toggle-on' ).each(function() {
  40                  var containerId = $( this ).parent().prop( 'id' );
  41                  $( params.newContainer ).find( '#' + containerId + ' > .dropdown-toggle' ).triggerHandler( 'click' );
  42              });
  43          }
  44      });
  45  
  46      secondary = $( '#secondary' );
  47      button = $( '.site-branding' ).find( '.secondary-toggle' );
  48  
  49      // Enable menu toggle for small screens.
  50      ( function() {
  51          var menu, widgets, social;
  52          if ( ! secondary.length || ! button.length ) {
  53              return;
  54          }
  55  
  56          // Hide button if there are no widgets and the menus are missing or empty.
  57          menu    = secondary.find( '.nav-menu' );
  58          widgets = secondary.find( '#widget-area' );
  59          social  = secondary.find( '#social-navigation' );
  60          if ( ! widgets.length && ! social.length && ( ! menu.length || ! menu.children().length ) ) {
  61              button.hide();
  62              return;
  63          }
  64  
  65          button.on( 'click.twentyfifteen', function() {
  66              secondary.toggleClass( 'toggled-on' );
  67              secondary.trigger( 'resize' );
  68              $( this ).toggleClass( 'toggled-on' );
  69              if ( $( this, secondary ).hasClass( 'toggled-on' ) ) {
  70                  $( this ).attr( 'aria-expanded', 'true' );
  71                  secondary.attr( 'aria-expanded', 'true' );
  72              } else {
  73                  $( this ).attr( 'aria-expanded', 'false' );
  74                  secondary.attr( 'aria-expanded', 'false' );
  75              }
  76          } );
  77      } )();
  78  
  79      /**
  80       * Add or remove ARIA attributes.
  81       *
  82       * Uses jQuery's width() function to determine the size of the window and add
  83       * the default ARIA attributes for the menu toggle if it's visible.
  84       * @since Twenty Fifteen 1.1
  85       */
  86  	function onResizeARIA() {
  87          if ( 955 > $window.width() ) {
  88              button.attr( 'aria-expanded', 'false' );
  89              secondary.attr( 'aria-expanded', 'false' );
  90              button.attr( 'aria-controls', 'secondary' );
  91          } else {
  92              button.removeAttr( 'aria-expanded' );
  93              secondary.removeAttr( 'aria-expanded' );
  94              button.removeAttr( 'aria-controls' );
  95          }
  96      }
  97  
  98      // Sidebar scrolling.
  99  	function resize() {
 100          windowWidth = $window.width();
 101  
 102          if ( 955 > windowWidth ) {
 103              top = bottom = false;
 104              $sidebar.removeAttr( 'style' );
 105          }
 106      }
 107  
 108  	function scroll() {
 109          var windowPos = $window.scrollTop();
 110  
 111          if ( 955 > windowWidth ) {
 112              return;
 113          }
 114  
 115          sidebarHeight = $sidebar.height();
 116          windowHeight  = $window.height();
 117          bodyHeight    = $body.height();
 118  
 119          if ( sidebarHeight + adminbarOffset > windowHeight ) {
 120              if ( windowPos > lastWindowPos ) {
 121                  if ( top ) {
 122                      top = false;
 123                      topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
 124                      $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
 125                  } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
 126                      bottom = true;
 127                      $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
 128                  }
 129              } else if ( windowPos < lastWindowPos ) {
 130                  if ( bottom ) {
 131                      bottom = false;
 132                      topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
 133                      $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
 134                  } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
 135                      top = true;
 136                      $sidebar.attr( 'style', 'position: fixed;' );
 137                  }
 138              } else {
 139                  top = bottom = false;
 140                  topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
 141                  $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
 142              }
 143          } else if ( ! top ) {
 144              top = true;
 145              $sidebar.attr( 'style', 'position: fixed;' );
 146          }
 147  
 148          lastWindowPos = windowPos;
 149      }
 150  
 151  	function resizeAndScroll() {
 152          resize();
 153          scroll();
 154      }
 155  
 156      $( document ).ready( function() {
 157          $body          = $( document.body );
 158          $window        = $( window );
 159          $sidebar       = $( '#sidebar' ).first();
 160          adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
 161  
 162          $window
 163              .on( 'scroll.twentyfifteen', scroll )
 164              .on( 'load.twentyfifteen', onResizeARIA )
 165              .on( 'resize.twentyfifteen', function() {
 166                  clearTimeout( resizeTimer );
 167                  resizeTimer = setTimeout( resizeAndScroll, 500 );
 168                  onResizeARIA();
 169              } );
 170          $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
 171  
 172          resizeAndScroll();
 173  
 174          for ( var i = 1; i < 6; i++ ) {
 175              setTimeout( resizeAndScroll, 100 * i );
 176          }
 177      } );
 178  
 179  } )( jQuery );


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1