wp/wp-content/themes/twentyfourteen/js/functions.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
--- a/wp/wp-content/themes/twentyfourteen/js/functions.js	Tue Jun 09 11:14:17 2015 +0000
+++ b/wp/wp-content/themes/twentyfourteen/js/functions.js	Mon Oct 14 17:39:30 2019 +0200
@@ -16,12 +16,12 @@
 
 	// Enable menu toggle for small screens.
 	( function() {
-		if ( ! nav || ! button ) {
+		if ( ! nav.length || ! button.length ) {
 			return;
 		}
 
 		// Hide button if menu is missing or empty.
-		if ( ! menu || ! menu.children().length ) {
+		if ( ! menu.length || ! menu.children().length ) {
 			button.hide();
 			return;
 		}
@@ -122,7 +122,8 @@
 	} );
 
 	/**
-	 * @summary Add or remove ARIA attributes.
+	 * Add or remove ARIA attributes.
+	 *
 	 * Uses jQuery's width() function to determine the size of the window and add
 	 * the default ARIA attributes for the menu toggle if it's visible.
 	 * @since Twenty Fourteen 1.4
@@ -146,9 +147,13 @@
 	} );
 
 	_window.load( function() {
+		var footerSidebar,
+			isCustomizeSelectiveRefresh = ( 'undefined' !== typeof wp && wp.customize && wp.customize.selectiveRefresh );
+
 		// Arrange footer widgets vertically.
 		if ( $.isFunction( $.fn.masonry ) ) {
-			$( '#footer-sidebar' ).masonry( {
+			footerSidebar = $( '#footer-sidebar' );
+			footerSidebar.masonry( {
 				itemSelector: '.widget',
 				columnWidth: function( containerWidth ) {
 					return containerWidth / 4;
@@ -157,6 +162,41 @@
 				isResizable: true,
 				isRTL: $( 'body' ).is( '.rtl' )
 			} );
+
+			if ( isCustomizeSelectiveRefresh ) {
+
+				// Retain previous masonry-brick initial position.
+				wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
+					var copyPosition = (
+						placement.partial.extended( wp.customize.widgetsPreview.WidgetPartial ) &&
+						placement.removedNodes instanceof jQuery &&
+						placement.removedNodes.is( '.masonry-brick' ) &&
+						placement.container instanceof jQuery
+					);
+					if ( copyPosition ) {
+						placement.container.css( {
+							position: placement.removedNodes.css( 'position' ),
+							top: placement.removedNodes.css( 'top' ),
+							left: placement.removedNodes.css( 'left' )
+						} );
+					}
+				} );
+
+				// Re-arrange footer widgets after selective refresh event.
+				wp.customize.selectiveRefresh.bind( 'sidebar-updated', function( sidebarPartial ) {
+					if ( 'sidebar-3' === sidebarPartial.sidebarId ) {
+						footerSidebar.masonry( 'reloadItems' );
+						footerSidebar.masonry( 'layout' );
+					}
+				} );
+			}
+		}
+
+		// Initialize audio and video players in Twenty_Fourteen_Ephemera_Widget widget when selectively refreshed in Customizer.
+		if ( isCustomizeSelectiveRefresh && wp.mediaelement ) {
+			wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
+				wp.mediaelement.initialize();
+			} );
 		}
 
 		// Initialize Featured Content slider.