[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7