[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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' ).on( '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       *
  83       * @since Twenty Fifteen 1.1
  84       */
  85  	function onResizeARIA() {
  86          if ( 955 > $window.width() ) {
  87              button.attr( 'aria-expanded', 'false' );
  88              secondary.attr( 'aria-expanded', 'false' );
  89              button.attr( 'aria-controls', 'secondary' );
  90          } else {
  91              button.removeAttr( 'aria-expanded' );
  92              secondary.removeAttr( 'aria-expanded' );
  93              button.removeAttr( 'aria-controls' );
  94          }
  95      }
  96  
  97      // Sidebar scrolling.
  98  	function resizeAndScroll() {
  99          var windowPos = $window.scrollTop(),
 100              windowHeight = $window.height(),
 101              sidebarHeight = $sidebar.height(),
 102              pageHeight = $( '#page' ).height();
 103  
 104          if ( 955 < $window.width() && pageHeight > sidebarHeight && ( windowPos + windowHeight ) >= sidebarHeight ) {
 105              $sidebar.css({
 106                  position: 'fixed',
 107                  bottom: sidebarHeight > windowHeight ? 0 : 'auto'
 108              });
 109          } else {
 110              $sidebar.css('position', 'relative');
 111          }
 112      }
 113  
 114      $( function() {
 115          $body          = $( document.body );
 116          $window        = $( window );
 117          $sidebar       = $( '#sidebar' ).first();
 118  
 119          $window
 120              .on( 'scroll.twentyfifteen', resizeAndScroll )
 121              .on( 'load.twentyfifteen', onResizeARIA )
 122              .on( 'resize.twentyfifteen', function() {
 123                  clearTimeout( resizeTimer );
 124                  resizeTimer = setTimeout( resizeAndScroll, 500 );
 125                  onResizeARIA();
 126              } );
 127          $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
 128  
 129          for ( var i = 0; i < 6; i++ ) {
 130              setTimeout( resizeAndScroll, 100 * i );
 131          }
 132      } );
 133  
 134  } )( jQuery );


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref