libretto.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * libretto.js
  3. *
  4. * Handles custom functions, primarily for images
  5. */
  6. ( function( $ ) {
  7. /* This calculates the size of each image in the entry content,
  8. * then gives it a class to overlap the content area if it's wide enough.
  9. * Certain images are ignored-—mostly those in galleries or video previews
  10. */
  11. function formatImages() {
  12. $( 'article img' ).each( function() {
  13. // Never give overlap class to gallery images or video, unless you want things to implode
  14. // Leave comment images alone, as well.
  15. // "Medium" and "large" images are also excluded for logical reasons, as well as smileys
  16. if ( ! $( this ).hasClass( 'attachment-gallery' ) && ! $( this ).hasClass( 'videopress-poster' )
  17. && ! $( this ).parents( '.tiled-gallery-item' ).length && ! $( this ).parents( '.gallery' ).length
  18. && ! $( this ).parents( '.comment-content' ).length
  19. && ! $( this ).hasClass( 'size-thumbnail' ) && ! $( this ).hasClass( 'size-medium' ) && ! $( this ).hasClass( 'wp-smiley' ) ) {
  20. // Determine actual, rather than computed, width of image, by creating a new image instance
  21. var computedImage = $( this );
  22. var actualImage = new Image();
  23. actualImage.src = computedImage.attr( 'src' )
  24. var imageWidth = actualImage.width;
  25. // If it's big enough, give it the oversized class for the overlap, and remove height & width attributes
  26. if ( imageWidth > 895 ) {
  27. // If we're inside a caption, the oversized class should instead be added to our caption.
  28. // We'll also remove the width property
  29. if ( $( this ).parents( 'figure' ).length ) {
  30. $( this ).parents( 'figure' ).addClass( 'libretto-oversized' );
  31. $( this ).parents( 'figure' ).css( "width", "" );
  32. } else {
  33. $( this ).addClass( 'libretto-oversized' );
  34. }
  35. $( this ).removeAttr( 'width' );
  36. $( this ).removeAttr( 'height' );
  37. }
  38. }
  39. });
  40. // Remove height & width attributes from featured images
  41. $( '.featured-image' ).each( function() {
  42. $( this ).removeAttr( 'width' );
  43. $( this ).removeAttr( 'height' );
  44. });
  45. }
  46. // Format images on each subsequent Infinite Scroll load, as well
  47. $( document ).on( 'post-load', function() {
  48. formatImages();
  49. });
  50. // Ensure that header images are no taller than the viewport height
  51. function fitHeader() {
  52. var windowHeight = $( window ).height();
  53. var windowWidth = $( window ).width();
  54. var navBarHeight = $( '.nav-bar' ).height();
  55. var wpAdminBar = $( '#wpadminbar' ).height() || 0;
  56. var effectiveViewportHeight = windowHeight - navBarHeight - wpAdminBar;
  57. var mastheadImageHeight = $( '#masthead' ).data('image-height');
  58. // we should only resize for larger devices (tablets and up)
  59. if ( windowWidth > 640 ) {
  60. if ( mastheadImageHeight >= effectiveViewportHeight ) {
  61. $( '#masthead' ).css( 'height', effectiveViewportHeight );
  62. } else {
  63. $( '#masthead' ).css( 'height', mastheadImageHeight );
  64. }
  65. }
  66. };
  67. // Wait for images to be loaded before calculating sizes
  68. $( window ).on( 'load', function() {
  69. formatImages();
  70. });
  71. $( document ).ready( function() {
  72. fitHeader();
  73. // Pull out the search bar when clicked
  74. $( '#site-navigation .search-form label' ).on( 'click', function(event) {
  75. var $form = $( this ).parent( 'form' );
  76. var $input = $form.find( '.search-field' );
  77. /*
  78. * Only submit the form if we actually clicked on the label itself.
  79. * There's an input inside the label (for entering your search term)
  80. * and it's confusing if clicking that submits the form!
  81. */
  82. if ( $form.hasClass( 'libretto-open' ) && $( event.target ).prop('tagName') === 'LABEL') {
  83. // Submit our search if there's a search term entered
  84. if ( $input.val() ) {
  85. $form.submit();
  86. // Otherwise, close the search box
  87. } else {
  88. $form.removeClass( 'libretto-open' );
  89. return false;
  90. }
  91. } else {
  92. $form.addClass( 'libretto-open' );
  93. $input.focus();
  94. return false;
  95. }
  96. });
  97. // And hide when it loses focus, too!
  98. $( '#site-navigation .search-form input[type="search"]' ).on( 'blur', function(event) {
  99. if ( event.target.attr('type') === 'search' ) {
  100. return;
  101. }
  102. $( '#site-navigation .search-form' ).removeClass( 'libretto-open' );
  103. });
  104. });
  105. } )( jQuery );