Geen omschrijving

customize-preview.js 5.6KB

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