customize-preview.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* global twentyTwentyBgColors, twentyTwentyPreviewEls, jQuery, _, wp */
  2. /**
  3. * Customizer enhancements for a better user experience.
  4. *
  5. * Contains handlers to make Theme Customizer preview reload changes asynchronously.
  6. *
  7. * @since Twenty Twenty 1.0
  8. */
  9. ( function( $, api, _ ) {
  10. /**
  11. * Return a value for our partial refresh.
  12. *
  13. * @param {Object} partial Current partial.
  14. *
  15. * @return {jQuery.Promise} Resolved promise.
  16. */
  17. function returnDeferred( partial ) {
  18. var deferred = new $.Deferred();
  19. deferred.resolveWith( partial, _.map( partial.placements(), function() {
  20. return '';
  21. } ) );
  22. return deferred.promise();
  23. }
  24. // Selective refresh for "Fixed Background Image".
  25. api.selectiveRefresh.partialConstructor.cover_fixed = api.selectiveRefresh.Partial.extend( {
  26. /**
  27. * Override the refresh method.
  28. *
  29. * @return {jQuery.Promise} Resolved promise.
  30. */
  31. refresh: function() {
  32. var partial, cover, params;
  33. partial = this;
  34. params = partial.params;
  35. cover = $( params.selector );
  36. if ( cover.length && cover.hasClass( 'bg-image' ) ) {
  37. cover.toggleClass( 'bg-attachment-fixed' );
  38. }
  39. return returnDeferred( partial );
  40. }
  41. } );
  42. // Selective refresh for "Image Overlay Opacity".
  43. api.selectiveRefresh.partialConstructor.cover_opacity = api.selectiveRefresh.Partial.extend( {
  44. /**
  45. * Input attributes.
  46. *
  47. * @type {Object}
  48. */
  49. attrs: {},
  50. /**
  51. * Override the refresh method.
  52. *
  53. * @return {jQuery.Promise} Resolved promise.
  54. */
  55. refresh: function() {
  56. var partial, ranges, attrs, setting, params, cover, className, classNames;
  57. partial = this;
  58. attrs = partial.attrs;
  59. ranges = _.range( attrs.min, attrs.max + attrs.step, attrs.step );
  60. params = partial.params;
  61. setting = api( params.primarySetting );
  62. cover = $( params.selector );
  63. if ( cover.length ) {
  64. classNames = _.map( ranges, function( val ) {
  65. return 'opacity-' + val;
  66. } );
  67. className = classNames[ ranges.indexOf( parseInt( setting.get(), 10 ) ) ];
  68. cover.removeClass( classNames.join( ' ' ) );
  69. cover.addClass( className );
  70. }
  71. return returnDeferred( partial );
  72. }
  73. } );
  74. // Add listener for the "header_footer_background_color" control.
  75. api( 'header_footer_background_color', function( value ) {
  76. value.bind( function( to ) {
  77. // Add background color to header and footer wrappers.
  78. $( 'body:not(.overlay-header)#site-header, #site-footer' ).css( 'background-color', to );
  79. // Change body classes if this is the same background-color as the content background.
  80. if ( to.toLowerCase() === api( 'background_color' ).get().toLowerCase() ) {
  81. $( 'body' ).addClass( 'reduced-spacing' );
  82. } else {
  83. $( 'body' ).removeClass( 'reduced-spacing' );
  84. }
  85. } );
  86. } );
  87. // Add listener for the "background_color" control.
  88. api( 'background_color', function( value ) {
  89. value.bind( function( to ) {
  90. // Change body classes if this is the same background-color as the header/footer background.
  91. if ( to.toLowerCase() === api( 'header_footer_background_color' ).get().toLowerCase() ) {
  92. $( 'body' ).addClass( 'reduced-spacing' );
  93. } else {
  94. $( 'body' ).removeClass( 'reduced-spacing' );
  95. }
  96. } );
  97. } );
  98. // Add listener for the accent color.
  99. api( 'accent_hue', function( value ) {
  100. value.bind( function() {
  101. // Generate the styles.
  102. // Add a small delay to be sure the accessible colors were generated.
  103. setTimeout( function() {
  104. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  105. twentyTwentyGenerateColorA11yPreviewStyles( context );
  106. } );
  107. }, 50 );
  108. } );
  109. } );
  110. // Add listeners for background-color settings.
  111. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  112. wp.customize( twentyTwentyBgColors[ context ].setting, function( value ) {
  113. value.bind( function() {
  114. // Generate the styles.
  115. // Add a small delay to be sure the accessible colors were generated.
  116. setTimeout( function() {
  117. twentyTwentyGenerateColorA11yPreviewStyles( context );
  118. }, 50 );
  119. } );
  120. } );
  121. } );
  122. /**
  123. * Add styles to elements in the preview pane.
  124. *
  125. * @since Twenty Twenty 1.0
  126. *
  127. * @param {string} context The area for which we want to generate styles. Can be for example "content", "header" etc.
  128. *
  129. * @return {void}
  130. */
  131. function twentyTwentyGenerateColorA11yPreviewStyles( context ) {
  132. // Get the accessible colors option.
  133. var a11yColors = window.parent.wp.customize( 'accent_accessible_colors' ).get(),
  134. stylesheedID = 'twentytwenty-customizer-styles-' + context,
  135. stylesheet = $( '#' + stylesheedID ),
  136. styles = '';
  137. // If the stylesheet doesn't exist, create it and append it to <head>.
  138. if ( ! stylesheet.length ) {
  139. $( '#twentytwenty-style-inline-css' ).after( '<style id="' + stylesheedID + '"></style>' );
  140. stylesheet = $( '#' + stylesheedID );
  141. }
  142. if ( ! _.isUndefined( a11yColors[ context ] ) ) {
  143. // Check if we have elements defined.
  144. if ( twentyTwentyPreviewEls[ context ] ) {
  145. _.each( twentyTwentyPreviewEls[ context ], function( items, setting ) {
  146. _.each( items, function( elements, property ) {
  147. if ( ! _.isUndefined( a11yColors[ context ][ setting ] ) ) {
  148. styles += elements.join( ',' ) + '{' + property + ':' + a11yColors[ context ][ setting ] + ';}';
  149. }
  150. } );
  151. } );
  152. }
  153. }
  154. // Add styles.
  155. stylesheet.html( styles );
  156. }
  157. // Generate styles on load. Handles page-changes on the preview pane.
  158. $( document ).ready( function() {
  159. twentyTwentyGenerateColorA11yPreviewStyles( 'content' );
  160. twentyTwentyGenerateColorA11yPreviewStyles( 'header-footer' );
  161. } );
  162. }( jQuery, wp.customize, _ ) );