color-scheme-control.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* global colorScheme, Color */
  2. /**
  3. * Add a listener to the Color Scheme control to update other color controls to new values/defaults.
  4. * Also trigger an update of the Color Scheme CSS when a color is changed.
  5. */
  6. ( function( api ) {
  7. var cssTemplate = wp.template( 'twentyfifteen-color-scheme' ),
  8. colorSchemeKeys = [
  9. 'background_color',
  10. 'header_background_color',
  11. 'box_background_color',
  12. 'textcolor',
  13. 'sidebar_textcolor',
  14. 'meta_box_background_color'
  15. ],
  16. colorSettings = [
  17. 'background_color',
  18. 'header_background_color',
  19. 'sidebar_textcolor'
  20. ];
  21. api.controlConstructor.select = api.Control.extend( {
  22. ready: function() {
  23. if ( 'color_scheme' === this.id ) {
  24. this.setting.bind( 'change', function( value ) {
  25. // Update Background Color.
  26. api( 'background_color' ).set( colorScheme[value].colors[0] );
  27. api.control( 'background_color' ).container.find( '.color-picker-hex' )
  28. .data( 'data-default-color', colorScheme[value].colors[0] )
  29. .wpColorPicker( 'defaultColor', colorScheme[value].colors[0] );
  30. // Update Header/Sidebar Background Color.
  31. api( 'header_background_color' ).set( colorScheme[value].colors[1] );
  32. api.control( 'header_background_color' ).container.find( '.color-picker-hex' )
  33. .data( 'data-default-color', colorScheme[value].colors[1] )
  34. .wpColorPicker( 'defaultColor', colorScheme[value].colors[1] );
  35. // Update Header/Sidebar Text Color.
  36. api( 'sidebar_textcolor' ).set( colorScheme[value].colors[4] );
  37. api.control( 'sidebar_textcolor' ).container.find( '.color-picker-hex' )
  38. .data( 'data-default-color', colorScheme[value].colors[4] )
  39. .wpColorPicker( 'defaultColor', colorScheme[value].colors[4] );
  40. } );
  41. }
  42. }
  43. } );
  44. // Generate the CSS for the current Color Scheme.
  45. function updateCSS() {
  46. var scheme = api( 'color_scheme' )(), css,
  47. colors = _.object( colorSchemeKeys, colorScheme[ scheme ].colors );
  48. // Merge in color scheme overrides.
  49. _.each( colorSettings, function( setting ) {
  50. colors[ setting ] = api( setting )();
  51. });
  52. // Add additional colors.
  53. colors.secondary_textcolor = Color( colors.textcolor ).toCSS( 'rgba', 0.7 );
  54. colors.border_color = Color( colors.textcolor ).toCSS( 'rgba', 0.1 );
  55. colors.border_focus_color = Color( colors.textcolor ).toCSS( 'rgba', 0.3 );
  56. colors.secondary_sidebar_textcolor = Color( colors.sidebar_textcolor ).toCSS( 'rgba', 0.7 );
  57. colors.sidebar_border_color = Color( colors.sidebar_textcolor ).toCSS( 'rgba', 0.1 );
  58. colors.sidebar_border_focus_color = Color( colors.sidebar_textcolor ).toCSS( 'rgba', 0.3 );
  59. css = cssTemplate( colors );
  60. api.previewer.send( 'update-color-scheme-css', css );
  61. }
  62. // Update the CSS whenever a color setting is changed.
  63. _.each( colorSettings, function( setting ) {
  64. api( setting, function( setting ) {
  65. setting.bind( updateCSS );
  66. } );
  67. } );
  68. } )( wp.customize );