customize-preview.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * File customize-preview.js.
  3. *
  4. * Instantly live-update customizer settings in the preview for improved user experience.
  5. */
  6. (function( $ ) {
  7. // Collect information from customize-controls.js about which panels are opening.
  8. wp.customize.bind( 'preview-ready', function() {
  9. // Initially hide the theme option placeholders on load
  10. $( '.panel-placeholder' ).hide();
  11. wp.customize.preview.bind( 'section-highlight', function( data ) {
  12. // Only on the front page.
  13. if ( ! $( 'body' ).hasClass( 'twentyseventeen-front-page' ) ) {
  14. return;
  15. }
  16. // When the section is expanded, show and scroll to the content placeholders, exposing the edit links.
  17. if ( true === data.expanded ) {
  18. $( 'body' ).addClass( 'highlight-front-sections' );
  19. $( '.panel-placeholder' ).slideDown( 200, function() {
  20. $.scrollTo( $( '#panel1' ), {
  21. duration: 600,
  22. offset: { 'top': -70 } // Account for sticky menu.
  23. });
  24. });
  25. // If we've left the panel, hide the placeholders and scroll back to the top.
  26. } else {
  27. $( 'body' ).removeClass( 'highlight-front-sections' );
  28. // Don't change scroll when leaving - it's likely to have unintended consequences.
  29. $( '.panel-placeholder' ).slideUp( 200 );
  30. }
  31. });
  32. });
  33. // Site title and description.
  34. wp.customize( 'blogname', function( value ) {
  35. value.bind( function( to ) {
  36. $( '.site-title a' ).text( to );
  37. });
  38. });
  39. wp.customize( 'blogdescription', function( value ) {
  40. value.bind( function( to ) {
  41. $( '.site-description' ).text( to );
  42. });
  43. });
  44. // Header text color.
  45. wp.customize( 'header_textcolor', function( value ) {
  46. value.bind( function( to ) {
  47. if ( 'blank' === to ) {
  48. $( '.site-title, .site-description' ).css({
  49. clip: 'rect(1px, 1px, 1px, 1px)',
  50. position: 'absolute'
  51. });
  52. // Add class for different logo styles if title and description are hidden.
  53. $( 'body' ).addClass( 'title-tagline-hidden' );
  54. } else {
  55. // Check if the text color has been removed and use default colors in theme stylesheet.
  56. if ( ! to.length ) {
  57. $( '#twentyseventeen-custom-header-styles' ).remove();
  58. }
  59. $( '.site-title, .site-description' ).css({
  60. clip: 'auto',
  61. position: 'relative'
  62. });
  63. $( '.site-branding, .site-branding a, .site-description, .site-description a' ).css({
  64. color: to
  65. });
  66. // Add class for different logo styles if title and description are visible.
  67. $( 'body' ).removeClass( 'title-tagline-hidden' );
  68. }
  69. });
  70. });
  71. // Color scheme.
  72. wp.customize( 'colorscheme', function( value ) {
  73. value.bind( function( to ) {
  74. // Update color body class.
  75. $( 'body' )
  76. .removeClass( 'colors-light colors-dark colors-custom' )
  77. .addClass( 'colors-' + to );
  78. });
  79. });
  80. // Custom color hue.
  81. wp.customize( 'colorscheme_hue', function( value ) {
  82. value.bind( function( to ) {
  83. // Update custom color CSS.
  84. var style = $( '#custom-theme-colors' ),
  85. hue = style.data( 'hue' ),
  86. css = style.html();
  87. // Equivalent to css.replaceAll, with hue followed by comma to prevent values with units from being changed.
  88. css = css.split( hue + ',' ).join( to + ',' );
  89. style.html( css ).data( 'hue', to );
  90. });
  91. });
  92. // Page layouts.
  93. wp.customize( 'page_layout', function( value ) {
  94. value.bind( function( to ) {
  95. if ( 'one-column' === to ) {
  96. $( 'body' ).addClass( 'page-one-column' ).removeClass( 'page-two-column' );
  97. } else {
  98. $( 'body' ).removeClass( 'page-one-column' ).addClass( 'page-two-column' );
  99. }
  100. } );
  101. } );
  102. // Whether a header image is available.
  103. function hasHeaderImage() {
  104. var image = wp.customize( 'header_image' )();
  105. return '' !== image && 'remove-header' !== image;
  106. }
  107. // Whether a header video is available.
  108. function hasHeaderVideo() {
  109. var externalVideo = wp.customize( 'external_header_video' )(),
  110. video = wp.customize( 'header_video' )();
  111. return '' !== externalVideo || ( 0 !== video && '' !== video );
  112. }
  113. // Toggle a body class if a custom header exists.
  114. $.each( [ 'external_header_video', 'header_image', 'header_video' ], function( index, settingId ) {
  115. wp.customize( settingId, function( setting ) {
  116. setting.bind(function() {
  117. if ( hasHeaderImage() ) {
  118. $( document.body ).addClass( 'has-header-image' );
  119. } else {
  120. $( document.body ).removeClass( 'has-header-image' );
  121. }
  122. if ( ! hasHeaderVideo() ) {
  123. $( document.body ).removeClass( 'has-header-video' );
  124. }
  125. } );
  126. } );
  127. } );
  128. } )( jQuery );