customizer-preview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. ( function( $, api ) {
  2. var $document = $( document );
  3. wp.customize.bind( 'preview-ready', function() {
  4. var defaultTarget = window.parent === window ? null : window.parent;
  5. $document.on(
  6. 'click',
  7. '.site-header-focus-item .item-customizer-focus, .builder-item-focus .edit-row-action',
  8. function(e) {
  9. e.preventDefault();
  10. e.stopPropagation();
  11. var p = $( this ).closest( '.site-header-focus-item' );
  12. var section_id = p.attr( 'data-section' ) || '';
  13. if ( section_id ) {
  14. if ( defaultTarget.wp.customize.section( section_id ) ) {
  15. defaultTarget.wp.customize.section( section_id ).focus();
  16. }
  17. }
  18. }
  19. );
  20. $document.on(
  21. 'click',
  22. '.site-footer-focus-item .item-customizer-focus',
  23. function(e) {
  24. e.preventDefault();
  25. e.stopPropagation();
  26. var p = $( this ).closest( '.site-footer-focus-item' );
  27. var section_id = p.attr( 'data-section' ) || '';
  28. if ( section_id ) {
  29. if ( defaultTarget.wp.customize.section( section_id ) ) {
  30. defaultTarget.wp.customize.section( section_id ).focus();
  31. }
  32. }
  33. }
  34. );
  35. /**
  36. * Register partial refresh events at once asynchronously.
  37. */
  38. wp.customize.preview.bind( 'active', function() {
  39. var partials = $.extend({}, astraCustomizer.dynamic_partial_options), key;
  40. var register_partial = async function () {
  41. for ( key in partials) {
  42. wp.customize.selectiveRefresh.partial.add(
  43. new wp.customize.selectiveRefresh.Partial(
  44. key,
  45. _.extend({params: partials[key]}, partials[key])
  46. )
  47. );
  48. await null;
  49. }
  50. }
  51. register_partial();
  52. });
  53. } );
  54. /**
  55. * Inline logo and title css only.
  56. */
  57. wp.customize( 'astra-settings[logo-title-inline]', function( value ) {
  58. value.bind( function( is_checked ) {
  59. jQuery('#masthead').toggleClass( 'ast-logo-title-inline', is_checked );
  60. } );
  61. } );
  62. } )( jQuery, wp );
  63. /**
  64. * Apply Advanced CSS for the element
  65. *
  66. * @param string section Section ID.
  67. * @param string selector Base Selector.
  68. */
  69. function astra_builder_advanced_css( section, selector ) {
  70. var tablet_break_point = astraBuilderPreview.tablet_break_point || 768,
  71. mobile_break_point = astraBuilderPreview.mobile_break_point || 544;
  72. // Padding.
  73. wp.customize( 'astra-settings[' + section + '-padding]', function( value ) {
  74. value.bind( function( padding ) {
  75. if( ! padding.hasOwnProperty('desktop') ) {
  76. return
  77. }
  78. if(
  79. padding.desktop.bottom != '' || padding.desktop.top != '' || padding.desktop.left != '' || padding.desktop.right != '' ||
  80. padding.tablet.bottom != '' || padding.tablet.top != '' || padding.tablet.left != '' || padding.tablet.right != '' ||
  81. padding.mobile.bottom != '' || padding.mobile.top != '' || padding.mobile.left != '' || padding.mobile.right != ''
  82. ) {
  83. var dynamicStyle = '';
  84. dynamicStyle += selector + ' {';
  85. dynamicStyle += 'padding-left: ' + padding['desktop']['left'] + padding['desktop-unit'] + ';';
  86. dynamicStyle += 'padding-right: ' + padding['desktop']['right'] + padding['desktop-unit'] + ';';
  87. dynamicStyle += 'padding-top: ' + padding['desktop']['top'] + padding['desktop-unit'] + ';';
  88. dynamicStyle += 'padding-bottom: ' + padding['desktop']['bottom'] + padding['desktop-unit'] + ';';
  89. dynamicStyle += '} ';
  90. dynamicStyle += '@media (max-width: ' + tablet_break_point + 'px) {';
  91. dynamicStyle += selector + ' {';
  92. dynamicStyle += 'padding-left: ' + padding['tablet']['left'] + padding['tablet-unit'] + ';';
  93. dynamicStyle += 'padding-right: ' + padding['tablet']['right'] + padding['tablet-unit'] + ';';
  94. dynamicStyle += 'padding-top: ' + padding['tablet']['top'] + padding['tablet-unit'] + ';';
  95. dynamicStyle += 'padding-bottom: ' + padding['tablet']['bottom'] + padding['tablet-unit'] + ';';
  96. dynamicStyle += '} ';
  97. dynamicStyle += '} ';
  98. dynamicStyle += '@media (max-width: ' + mobile_break_point + 'px) {';
  99. dynamicStyle += selector + ' {';
  100. dynamicStyle += 'padding-left: ' + padding['mobile']['left'] + padding['mobile-unit'] + ';';
  101. dynamicStyle += 'padding-right: ' + padding['mobile']['right'] + padding['mobile-unit'] + ';';
  102. dynamicStyle += 'padding-top: ' + padding['mobile']['top'] + padding['mobile-unit'] + ';';
  103. dynamicStyle += 'padding-bottom: ' + padding['mobile']['bottom'] + padding['mobile-unit'] + ';';
  104. dynamicStyle += '} ';
  105. dynamicStyle += '} ';
  106. astra_add_dynamic_css( section + '-padding-toggle-button', dynamicStyle );
  107. } else {
  108. astra_add_dynamic_css( section + '-padding-toggle-button', '' );
  109. }
  110. } );
  111. } );
  112. // Margin.
  113. wp.customize( 'astra-settings[' + section + '-margin]', function( value ) {
  114. value.bind( function( margin ) {
  115. if( ! margin.hasOwnProperty('desktop') ) {
  116. return
  117. }
  118. if(
  119. margin.desktop.bottom != '' || margin.desktop.top != '' || margin.desktop.left != '' || margin.desktop.right != '' ||
  120. margin.tablet.bottom != '' || margin.tablet.top != '' || margin.tablet.left != '' || margin.tablet.right != '' ||
  121. margin.mobile.bottom != '' || margin.mobile.top != '' || margin.mobile.left != '' || margin.mobile.right != ''
  122. ) {
  123. var dynamicStyle = '';
  124. dynamicStyle += selector + ' {';
  125. dynamicStyle += 'margin-left: ' + margin['desktop']['left'] + margin['desktop-unit'] + ';';
  126. dynamicStyle += 'margin-right: ' + margin['desktop']['right'] + margin['desktop-unit'] + ';';
  127. dynamicStyle += 'margin-top: ' + margin['desktop']['top'] + margin['desktop-unit'] + ';';
  128. dynamicStyle += 'margin-bottom: ' + margin['desktop']['bottom'] + margin['desktop-unit'] + ';';
  129. dynamicStyle += '} ';
  130. dynamicStyle += '@media (max-width: ' + tablet_break_point + 'px) {';
  131. dynamicStyle += selector + ' {';
  132. dynamicStyle += 'margin-left: ' + margin['tablet']['left'] + margin['tablet-unit'] + ';';
  133. dynamicStyle += 'margin-right: ' + margin['tablet']['right'] + margin['tablet-unit'] + ';';
  134. dynamicStyle += 'margin-top: ' + margin['tablet']['top'] + margin['tablet-unit'] + ';';
  135. dynamicStyle += 'margin-bottom: ' + margin['tablet']['bottom'] + margin['tablet-unit'] + ';';
  136. dynamicStyle += '} ';
  137. dynamicStyle += '} ';
  138. dynamicStyle += '@media (max-width: ' + mobile_break_point + 'px) {';
  139. dynamicStyle += selector + ' {';
  140. dynamicStyle += 'margin-left: ' + margin['mobile']['left'] + margin['mobile-unit'] + ';';
  141. dynamicStyle += 'margin-right: ' + margin['mobile']['right'] + margin['mobile-unit'] + ';';
  142. dynamicStyle += 'margin-top: ' + margin['mobile']['top'] + margin['mobile-unit'] + ';';
  143. dynamicStyle += 'margin-bottom: ' + margin['mobile']['bottom'] + margin['mobile-unit'] + ';';
  144. dynamicStyle += '} ';
  145. dynamicStyle += '} ';
  146. astra_add_dynamic_css( section + '-margin-toggle-button', dynamicStyle );
  147. } else {
  148. astra_add_dynamic_css( section + '-margin-toggle-button', '' );
  149. }
  150. } );
  151. } );
  152. }
  153. // Single Post Content Width
  154. wp.customize( 'astra-settings[blog-single-width]', function( value ) {
  155. value.bind( function( value ) {
  156. var single_post_max_width = wp.customize('astra-settings[blog-single-max-width]').get();
  157. var dynamicStyle = '';
  158. if ( 'custom' === value ) {
  159. dynamicStyle += '.single-post .site-content > .ast-container {';
  160. dynamicStyle += 'max-width: ' + single_post_max_width + 'px;';
  161. dynamicStyle += '} ';
  162. }
  163. astra_add_dynamic_css( 'blog-single-width', dynamicStyle );
  164. } );
  165. } );
  166. // Blog Post Content Width
  167. wp.customize( 'astra-settings[blog-width]', function( value ) {
  168. value.bind( function( value ) {
  169. var blog_max_width = wp.customize('astra-settings[blog-max-width]').get();
  170. var dynamicStyle = '';
  171. if ( 'custom' === value ) {
  172. dynamicStyle += '.blog .site-content > .ast-container, .archive .site-content > .ast-container, .search .site-content > .ast-container {';
  173. dynamicStyle += 'max-width: ' + blog_max_width + 'px;';
  174. dynamicStyle += '} ';
  175. }
  176. astra_add_dynamic_css( 'blog-width', dynamicStyle );
  177. } );
  178. } );
  179. // Blog Post Content Width
  180. wp.customize( 'astra-settings[edd-archive-grids]', function( value ) {
  181. value.bind( function( value ) {
  182. for ( var i = 1; i < 7; i++ ) {
  183. jQuery('body').removeClass( 'columns-' + i );
  184. jQuery('body').removeClass( 'tablet-columns-' + i );
  185. jQuery('body').removeClass( 'mobile-columns-' + i );
  186. }
  187. if ( jQuery('body').hasClass( 'ast-edd-archive-page' ) ) {
  188. jQuery('body').addClass( 'columns-' + value['desktop'] );
  189. jQuery('body').addClass( 'tablet-columns-' + value['tablet'] );
  190. jQuery('body').addClass( 'mobile-columns-' + value['mobile'] );
  191. }
  192. } );
  193. } );
  194. // Blog Post Content Width
  195. wp.customize( 'astra-settings[edd-archive-width]', function( value ) {
  196. value.bind( function( value ) {
  197. var edd_archive_max_width = wp.customize('astra-settings[edd-archive-max-width]').get();
  198. edd_archive_max_width = ( 'custom' === value ) ? edd_archive_max_width : edd_archive_max_width + 40;
  199. var dynamicStyle = '';
  200. dynamicStyle += '.ast-edd-archive-page .site-content > .ast-container {';
  201. dynamicStyle += 'max-width: ' + edd_archive_max_width + 'px;';
  202. dynamicStyle += '} ';
  203. astra_add_dynamic_css( 'edd-archive-width', dynamicStyle );
  204. } );
  205. } );
  206. // Global Typography Refresh - START
  207. const bodyFontFamily = [
  208. 'body-font-family',
  209. 'body-font-variant',
  210. 'font-size-body',
  211. 'body-font-weight',
  212. 'body-text-transform',
  213. 'body-line-height',
  214. 'headings-font-family',
  215. 'headings-font-variant',
  216. 'headings-font-weight',
  217. 'headings-text-transform',
  218. 'headings-line-height'
  219. ];
  220. bodyFontFamily.forEach(element => {
  221. // Body Font Family
  222. wp.customize( 'astra-settings['+element+']', function( value ) {
  223. value.bind( function( value ) {
  224. wp.customize.preview.send( 'refresh' );
  225. } );
  226. } );
  227. });
  228. // Global Typography Refresh - END