functions.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ( function( $ ) {
  2. var $body = $( document.body ),
  3. $window = $( window ),
  4. hero = $( '.hero' ),
  5. header = $( '#masthead' ),
  6. sidebar = $( '#secondary' ),
  7. searchSubmit = $( '.search-submit' ),
  8. resizeTimer;
  9. // Add dropdown toggle that display child menu items.
  10. $( '.main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
  11. // Toggle buttons and submenu items with active children menu items.
  12. $( '.main-navigation .current-menu-ancestor > a' ).addClass( 'toggled' );
  13. $( '.main-navigation .current-menu-ancestor > button' ).addClass( 'toggled' );
  14. $( '.main-navigation .current-menu-ancestor > .sub-menu' ).addClass( 'toggled' );
  15. // Click on dropdown toggle.
  16. $( '.dropdown-toggle' ).click( function( e ) {
  17. var _this = $( this );
  18. e.preventDefault();
  19. _this.toggleClass( 'toggled' );
  20. _this.prev( 'a' ).toggleClass( 'toggled' );
  21. _this.next( '.children, .sub-menu' ).toggleClass( 'toggled' );
  22. _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
  23. _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
  24. $window.resize();
  25. } );
  26. function searchAddClass() {
  27. $( this ).closest( '.search-form' ).addClass( 'hover' );
  28. }
  29. function searchRemoveClass() {
  30. $( this ).closest( '.search-form' ).removeClass( 'hover' );
  31. }
  32. searchSubmit.hover( searchAddClass, searchRemoveClass );
  33. searchSubmit.focusin( searchAddClass );
  34. searchSubmit.focusout( searchRemoveClass );
  35. // Make Featured image full-screen.
  36. function heroFullScreen() {
  37. var toolbarHeight, heroHeight, headerHeight, windowWidth, hentryInnerCalc;
  38. if ( ! hero.length ) {
  39. return;
  40. }
  41. toolbarHeight = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
  42. heroHeight = $window.height();
  43. headerHeight = header.outerHeight() + toolbarHeight;
  44. heroHeightNoHeader = heroHeight - headerHeight;
  45. windowWidth = $window.innerWidth();
  46. hentryInnerCalc = hero.find( '.hentry-inner' ).height() + headerHeight;
  47. if ( $window.height() < hentryInnerCalc ) {
  48. heroHeight = hentryInnerCalc;
  49. heroHeightNoHeader = hentryInnerCalc - headerHeight;
  50. }
  51. if ( windowWidth >= 768 ) {
  52. hero.css( {
  53. 'height': heroHeight + 'px',
  54. 'margin-top': '-' + headerHeight + 'px',
  55. 'padding-top': headerHeight + 'px',
  56. } );
  57. } else if ( windowWidth >= 540 ) {
  58. hero.css( {
  59. 'height': heroHeight + 'px',
  60. 'margin-top': '-' + headerHeight + 'px',
  61. 'padding-top': headerHeight + 'px',
  62. } );
  63. } else {
  64. hero.css( {
  65. 'height': '',
  66. 'margin-top': '-' + headerHeight + 'px',
  67. 'padding-top': headerHeight + 'px',
  68. } );
  69. }
  70. if ( ! sidebar.length ) {
  71. return;
  72. }
  73. if ( windowWidth >= 1056 ) {
  74. sidebar.css( {
  75. 'margin-top': heroHeightNoHeader + 'px'
  76. } );
  77. } else {
  78. sidebar.removeAttr( 'style' );
  79. }
  80. }
  81. function contentHeight() {
  82. var toolbarHeight = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0,
  83. screenHeight = $window.height() - toolbarHeight,
  84. elementsHeight = header.outerHeight() + $( '#colophon' ).outerHeight();
  85. $( '#content' ).css( {
  86. 'min-height': screenHeight - elementsHeight + 'px'
  87. } );
  88. }
  89. function moveSidebar() {
  90. //First check if sidebar 1 or 2 exist at all.
  91. if ( ! $( '#sidebar-1' ).length && ! $( '#sidebar-2' ).length ) {
  92. return;
  93. }
  94. // Check if we're on desktop or mobile. The sidebars are initially rendered in the mobile location.
  95. if ( $window.width() >= 1056 ) {
  96. // Check if sidebar 2 exists in the mobile location, if so then move.
  97. if ( $( '#tertiary #sidebar-2' ).length ) {
  98. $( "#sidebar-2" ).prependTo( $( "#secondary" ) );
  99. }
  100. // Check if sidebar 1 exists in the mobile location, if so then move.
  101. if ( $( '#tertiary #sidebar-1' ).length ) {
  102. $( "#sidebar-1" ).prependTo( $( "#secondary" ) );
  103. }
  104. } else {
  105. // Check if sidebar 2 exists in the desktop location, if so then move.
  106. if ( $( '#secondary #sidebar-2' ).length ) {
  107. $( "#sidebar-2" ).prependTo( $( "#tertiary" ) );
  108. }
  109. // Check if sidebar 1 exists in the desktop location, if so then move.
  110. if ( $( '#secondary #sidebar-1' ).length ) {
  111. $( "#sidebar-1" ).prependTo( $( "#tertiary" ) );
  112. }
  113. }
  114. }
  115. $window.load( function() {
  116. $( 'html' ).addClass( 'js' );
  117. // Move Sharedaddy & Related Posts.
  118. var sharedaddy = $( '.sd-sharing-enabled:not(#jp-post-flair), .sd-like.jetpack-likes-widget-wrapper, .sd-rating' ),
  119. relatedPosts = $( '#jp-relatedposts' );
  120. if ( sharedaddy.length ) {
  121. sharedaddy.appendTo( '.entry-footer' );
  122. }
  123. if ( relatedPosts.length ) {
  124. $( "#jp-post-flair" ).insertAfter( '.entry-footer' );
  125. }
  126. // Make sure tables don't overflow in Entry Content.
  127. $( '.entry-content' ).find( 'table' ).each( function() {
  128. if ( $( this ).width() > $( this ).parent().width() ) {
  129. $( this ).css( 'table-layout', 'fixed' );
  130. }
  131. } );
  132. // Remove box-shadow from linked images.
  133. $( '.entry-content a, .comment-content a' ).each( function() {
  134. $( this ).has( 'img' ).addClass( 'is-image' );
  135. } );
  136. $window
  137. .on( 'resize.publication', function() {
  138. clearTimeout( resizeTimer );
  139. resizeTimer = setTimeout( function() {
  140. heroFullScreen();
  141. contentHeight();
  142. }, 300 );
  143. moveSidebar();
  144. } );
  145. heroFullScreen();
  146. contentHeight();
  147. for ( var i = 1; i < 4; i++ ) {
  148. setTimeout( heroFullScreen, 100 * i );
  149. setTimeout( contentHeight, 100 * i );
  150. }
  151. } );
  152. // After DOM is ready
  153. $( document ).ready( function() {
  154. moveSidebar();
  155. } );
  156. } )( jQuery );