wp/wp-content/themes/twentyseventeen/assets/js/customize-preview.js
changeset 8 c7c34916027a
parent 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
7:cf61fcea0001 8:c7c34916027a
     1 /**
       
     2  * File customize-preview.js.
       
     3  *
       
     4  * Instantly live-update customizer settings in the preview for improved user experience.
       
     5  */
       
     6 
       
     7 (function( $ ) {
       
     8 
       
     9 	// Collect information from customize-controls.js about which panels are opening.
       
    10 	wp.customize.bind( 'preview-ready', function() {
       
    11 
       
    12 		// Initially hide the theme option placeholders on load
       
    13 		$( '.panel-placeholder' ).hide();
       
    14 
       
    15 		wp.customize.preview.bind( 'section-highlight', function( data ) {
       
    16 
       
    17 			// Only on the front page.
       
    18 			if ( ! $( 'body' ).hasClass( 'twentyseventeen-front-page' ) ) {
       
    19 				return;
       
    20 			}
       
    21 
       
    22 			// When the section is expanded, show and scroll to the content placeholders, exposing the edit links.
       
    23 			if ( true === data.expanded ) {
       
    24 				$( 'body' ).addClass( 'highlight-front-sections' );
       
    25 				$( '.panel-placeholder' ).slideDown( 200, function() {
       
    26 					$.scrollTo( $( '#panel1' ), {
       
    27 						duration: 600,
       
    28 						offset: { 'top': -70 } // Account for sticky menu.
       
    29 					});
       
    30 				});
       
    31 
       
    32 			// If we've left the panel, hide the placeholders and scroll back to the top.
       
    33 			} else {
       
    34 				$( 'body' ).removeClass( 'highlight-front-sections' );
       
    35 				// Don't change scroll when leaving - it's likely to have unintended consequences.
       
    36 				$( '.panel-placeholder' ).slideUp( 200 );
       
    37 			}
       
    38 		});
       
    39 	});
       
    40 
       
    41 	// Site title and description.
       
    42 	wp.customize( 'blogname', function( value ) {
       
    43 		value.bind( function( to ) {
       
    44 			$( '.site-title a' ).text( to );
       
    45 		});
       
    46 	});
       
    47 	wp.customize( 'blogdescription', function( value ) {
       
    48 		value.bind( function( to ) {
       
    49 			$( '.site-description' ).text( to );
       
    50 		});
       
    51 	});
       
    52 
       
    53 	// Header text color.
       
    54 	wp.customize( 'header_textcolor', function( value ) {
       
    55 		value.bind( function( to ) {
       
    56 			if ( 'blank' === to ) {
       
    57 				$( '.site-title, .site-description' ).css({
       
    58 					clip: 'rect(1px, 1px, 1px, 1px)',
       
    59 					position: 'absolute'
       
    60 				});
       
    61 				// Add class for different logo styles if title and description are hidden.
       
    62 				$( 'body' ).addClass( 'title-tagline-hidden' );
       
    63 			} else {
       
    64 
       
    65 				// Check if the text color has been removed and use default colors in theme stylesheet.
       
    66 				if ( ! to.length ) {
       
    67 					$( '#twentyseventeen-custom-header-styles' ).remove();
       
    68 				}
       
    69 				$( '.site-title, .site-description' ).css({
       
    70 					clip: 'auto',
       
    71 					position: 'relative'
       
    72 				});
       
    73 				$( '.site-branding, .site-branding a, .site-description, .site-description a' ).css({
       
    74 					color: to
       
    75 				});
       
    76 				// Add class for different logo styles if title and description are visible.
       
    77 				$( 'body' ).removeClass( 'title-tagline-hidden' );
       
    78 			}
       
    79 		});
       
    80 	});
       
    81 
       
    82 	// Color scheme.
       
    83 	wp.customize( 'colorscheme', function( value ) {
       
    84 		value.bind( function( to ) {
       
    85 
       
    86 			// Update color body class.
       
    87 			$( 'body' )
       
    88 				.removeClass( 'colors-light colors-dark colors-custom' )
       
    89 				.addClass( 'colors-' + to );
       
    90 		});
       
    91 	});
       
    92 
       
    93 	// Custom color hue.
       
    94 	wp.customize( 'colorscheme_hue', function( value ) {
       
    95 		value.bind( function( to ) {
       
    96 
       
    97 			// Update custom color CSS.
       
    98 			var style = $( '#custom-theme-colors' ),
       
    99 				hue = style.data( 'hue' ),
       
   100 				css = style.html();
       
   101 
       
   102 			// Equivalent to css.replaceAll, with hue followed by comma to prevent values with units from being changed.
       
   103 			css = css.split( hue + ',' ).join( to + ',' );
       
   104 			style.html( css ).data( 'hue', to );
       
   105 		});
       
   106 	});
       
   107 
       
   108 	// Page layouts.
       
   109 	wp.customize( 'page_layout', function( value ) {
       
   110 		value.bind( function( to ) {
       
   111 			if ( 'one-column' === to ) {
       
   112 				$( 'body' ).addClass( 'page-one-column' ).removeClass( 'page-two-column' );
       
   113 			} else {
       
   114 				$( 'body' ).removeClass( 'page-one-column' ).addClass( 'page-two-column' );
       
   115 			}
       
   116 		} );
       
   117 	} );
       
   118 
       
   119 	// Whether a header image is available.
       
   120 	function hasHeaderImage() {
       
   121 		var image = wp.customize( 'header_image' )();
       
   122 		return '' !== image && 'remove-header' !== image;
       
   123 	}
       
   124 
       
   125 	// Whether a header video is available.
       
   126 	function hasHeaderVideo() {
       
   127 		var externalVideo = wp.customize( 'external_header_video' )(),
       
   128 			video = wp.customize( 'header_video' )();
       
   129 
       
   130 		return '' !== externalVideo || ( 0 !== video && '' !== video );
       
   131 	}
       
   132 
       
   133 	// Toggle a body class if a custom header exists.
       
   134 	$.each( [ 'external_header_video', 'header_image', 'header_video' ], function( index, settingId ) {
       
   135 		wp.customize( settingId, function( setting ) {
       
   136 			setting.bind(function() {
       
   137 				if ( hasHeaderImage() ) {
       
   138 					$( document.body ).addClass( 'has-header-image' );
       
   139 				} else {
       
   140 					$( document.body ).removeClass( 'has-header-image' );
       
   141 				}
       
   142 
       
   143 				if ( ! hasHeaderVideo() ) {
       
   144 					$( document.body ).removeClass( 'has-header-video' );
       
   145 				}
       
   146 			} );
       
   147 		} );
       
   148 	} );
       
   149 
       
   150 } )( jQuery );