Sin descripción

customize-controls.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* global twentyTwentyBgColors, twentyTwentyColor, jQuery, wp, _ */
  2. /**
  3. * Customizer enhancements for a better user experience.
  4. *
  5. * Contains extra logic for our Customizer controls & settings.
  6. *
  7. * @since Twenty Twenty 1.0
  8. */
  9. ( function() {
  10. // Wait until the customizer has finished loading.
  11. wp.customize.bind( 'ready', function() {
  12. // Add a listener for accent-color changes.
  13. wp.customize( 'accent_hue', function( value ) {
  14. value.bind( function( to ) {
  15. // Update the value for our accessible colors for all areas.
  16. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  17. var backgroundColorValue;
  18. if ( twentyTwentyBgColors[ context ].color ) {
  19. backgroundColorValue = twentyTwentyBgColors[ context ].color;
  20. } else {
  21. backgroundColorValue = wp.customize( twentyTwentyBgColors[ context ].setting ).get();
  22. }
  23. twentyTwentySetAccessibleColorsValue( context, backgroundColorValue, to );
  24. } );
  25. } );
  26. } );
  27. // Add a listener for background-color changes.
  28. Object.keys( twentyTwentyBgColors ).forEach( function( context ) {
  29. wp.customize( twentyTwentyBgColors[ context ].setting, function( value ) {
  30. value.bind( function( to ) {
  31. // Update the value for our accessible colors for this area.
  32. twentyTwentySetAccessibleColorsValue( context, to, wp.customize( 'accent_hue' ).get(), to );
  33. } );
  34. } );
  35. } );
  36. // Show or hide retina_logo setting on the first load.
  37. twentyTwentySetRetineLogoVisibility( !! wp.customize( 'custom_logo' )() );
  38. // Add a listener for custom_logo changes.
  39. wp.customize( 'custom_logo', function( value ) {
  40. value.bind( function( to ) {
  41. // Show or hide retina_logo setting on changing custom_logo.
  42. twentyTwentySetRetineLogoVisibility( !! to );
  43. } );
  44. } );
  45. } );
  46. /**
  47. * Updates the value of the "accent_accessible_colors" setting.
  48. *
  49. * @since Twenty Twenty 1.0
  50. *
  51. * @param {string} context The area for which we want to get colors. Can be for example "content", "header" etc.
  52. * @param {string} backgroundColor The background color (HEX value).
  53. * @param {number} accentHue Numeric representation of the selected hue (0 - 359).
  54. *
  55. * @return {void}
  56. */
  57. function twentyTwentySetAccessibleColorsValue( context, backgroundColor, accentHue ) {
  58. var value, colors;
  59. // Get the current value for our accessible colors, and make sure it's an object.
  60. value = wp.customize( 'accent_accessible_colors' ).get();
  61. value = ( _.isObject( value ) && ! _.isArray( value ) ) ? value : {};
  62. // Get accessible colors for the defined background-color and hue.
  63. colors = twentyTwentyColor( backgroundColor, accentHue );
  64. // Sanity check.
  65. if ( colors.getAccentColor() && 'function' === typeof colors.getAccentColor().toCSS ) {
  66. // Update the value for this context.
  67. value[ context ] = {
  68. text: colors.getTextColor(),
  69. accent: colors.getAccentColor().toCSS(),
  70. background: backgroundColor
  71. };
  72. // Get borders color.
  73. value[ context ].borders = colors.bgColorObj
  74. .clone()
  75. .getReadableContrastingColor( colors.bgColorObj, 1.36 )
  76. .toCSS();
  77. // Get secondary color.
  78. value[ context ].secondary = colors.bgColorObj
  79. .clone()
  80. .getReadableContrastingColor( colors.bgColorObj )
  81. .s( colors.bgColorObj.s() / 2 )
  82. .toCSS();
  83. }
  84. // Change the value.
  85. wp.customize( 'accent_accessible_colors' ).set( value );
  86. // Small hack to save the option.
  87. wp.customize( 'accent_accessible_colors' )._dirty = true;
  88. }
  89. /**
  90. * Shows or hides the "retina_logo" setting based on the given value.
  91. *
  92. * @since Twenty Twenty 1.3
  93. *
  94. * @param {boolean} visible The visible value.
  95. *
  96. * @return {void}
  97. */
  98. function twentyTwentySetRetineLogoVisibility( visible ) {
  99. wp.customize.control( 'retina_logo' ).container.toggle( visible );
  100. }
  101. }( jQuery ) );