styles.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* jshint esnext:true */
  2. import { isNumeric, hexToHsl } from './helpers';
  3. const styles = ( api, $ ) => {
  4. 'use strict';
  5. const prepare_background = to => {
  6. if ( to['background-image'] !== '' ) {
  7. to['background-image'] = 'url(' + to['background-image'] + ')';
  8. }
  9. return to;
  10. };
  11. api( 'vamtam_theme[top-nav-background]', value => {
  12. value.bind( to => {
  13. $( '#top-nav-wrapper, #top-nav-wrapper-filler' ).css( prepare_background( to ) );
  14. } );
  15. } );
  16. {
  17. const compiler_options = VAMTAM_CUSTOMIZE_PREVIEW.compiler_options;
  18. const real_id = function( id ) {
  19. return id.replace( /vamtam_theme\[([^\]]+)]/, '$1' );
  20. };
  21. const change_handler_by_type = {
  22. number: function( to ) {
  23. let id = real_id( this.id );
  24. if ( VAMTAM_CUSTOMIZE_PREVIEW.percentages.indexOf( id ) !== -1 ) {
  25. to += '%';
  26. } else if ( VAMTAM_CUSTOMIZE_PREVIEW.numbers.indexOf( id ) !== -1 ) {
  27. // as is
  28. } else {
  29. to += 'px';
  30. }
  31. document.documentElement.style.setProperty( `--vamtam-${id}`, to )
  32. // trigger a resize event if we change any dimension
  33. $( window ).resize();
  34. },
  35. background: function( to ) {
  36. let id = real_id( this.id );
  37. to = prepare_background( to );
  38. for ( let prop in to ) {
  39. document.documentElement.style.setProperty( `--vamtam-${id}-${prop}`, to[ prop ] );
  40. }
  41. },
  42. radio: function( to ) {
  43. let id = real_id( this.id );
  44. if ( isNumeric( to ) ) {
  45. change_handler_by_type.number.call( this, to );
  46. } else {
  47. document.documentElement.style.setProperty( `--vamtam-${id}`, to )
  48. }
  49. },
  50. select: function( to ) {
  51. let id = real_id( this.id );
  52. change_handler_by_type.radio.call( this, to );
  53. },
  54. typography: function( to, from ) {
  55. let id = real_id( this.id );
  56. let variant = to.variant;
  57. to['font-weight'] = 'normal';
  58. to['font-style'] = 'normal';
  59. to.variant = to.variant.split( ' ' );
  60. if ( to.variant.length === 2 ) {
  61. to['font-weight'] = to.variant[0];
  62. to['font-style'] = to.variant[1];
  63. } else if ( to.variant[0] === 'italic' ) {
  64. to['font-style'] = 'italic';
  65. } else {
  66. to['font-weight'] = to.variant[0];
  67. }
  68. delete to.variant;
  69. for ( let prop in to ) {
  70. document.documentElement.style.setProperty( `--vamtam-${id}-${prop}`, to[ prop ] );
  71. }
  72. // if the font-family is changed - we need to load the new font stylesheet
  73. if ( to['font-family'] !== from['font-family'] ) {
  74. let new_font = window.top.VAMTAM_ALL_FONTS[ to['font-family'] ];
  75. if ( new_font.gf ) {
  76. let family = encodeURIComponent( to['font-family'] ) + ':bold,' + variant.replace( ' ', '' );
  77. let subset = ''; // no subset support here, only newer browser can preview Google Fonts
  78. let link = document.createElement("link");
  79. link.href = 'https://fonts.googleapis.com/css?family=' + family + '&subset=' + subset;
  80. link.type = 'text/css';
  81. link.rel = 'stylesheet';
  82. document.getElementsByTagName( 'head' )[0].appendChild(link);
  83. }
  84. }
  85. },
  86. 'color-row': function( to ) {
  87. let id = real_id( this.id );
  88. for ( let prop in to ) {
  89. document.documentElement.style.setProperty( `--vamtam-${id}-${prop}`, to[ prop ] );
  90. }
  91. if ( id === 'accent-color' ) {
  92. // accents need readable colors
  93. for ( let i = 1; i <= 8; i++ ) {
  94. let hex = to[ i ];
  95. let hsl = hexToHsl( hex );
  96. let readable = '';
  97. let hc = '';
  98. if ( hsl[2] > 80 ) {
  99. readable = `hsl(${hsl[0]}, ${hsl[1]}%, ${ Math.max( 0, hsl[2] - 50 ) }%)`;// $color->darken( 50 );
  100. hc = '#000000';
  101. } else {
  102. readable = `hsl(${hsl[0]}, ${hsl[1]}%, ${ Math.min( 0, hsl[2] + 50 ) }%)`;// $color->lighten( 50 );
  103. hc = '#ffffff';
  104. }
  105. document.documentElement.style.setProperty( `--vamtam-accent-color-${i}-readable`, readable );
  106. document.documentElement.style.setProperty( `--vamtam-accent-color-${i}-hc`, hc );
  107. document.documentElement.style.setProperty( `--vamtam-accent-color-${i}-transparent`, `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%, 0)` )
  108. }
  109. }
  110. },
  111. color: function( to ) {
  112. let id = real_id( this.id );
  113. document.documentElement.style.setProperty( `--vamtam-${id}`, to )
  114. },
  115. };
  116. // const compiler_option_handler = ;
  117. for ( let opt_name in compiler_options ) {
  118. api( opt_name, function( setting ) {
  119. const type = compiler_options[ opt_name ];
  120. if ( type in change_handler_by_type ) {
  121. setting.bind( change_handler_by_type[ type ] );
  122. } else {
  123. console.error( `VamTam Customzier: Missing handler for option type ${type} - option ${opt_name}` );
  124. window.wpvval = setting;
  125. }
  126. } );
  127. }
  128. }
  129. api( 'vamtam_theme[page-title-background-hide-lowres]', value => {
  130. value.bind( to => {
  131. $( 'header.page-header' ).toggleClass( 'vamtam-hide-bg-lowres', to );
  132. } );
  133. } );
  134. api( 'vamtam_theme[main-background-hide-lowres]', value => {
  135. value.bind( to => {
  136. $( '.vamtam-main' ).toggleClass( 'vamtam-hide-bg-lowres', to );
  137. } );
  138. } );
  139. };
  140. export default styles;