From 4e8fc7b69c573110b7a0993adf444a22ae5b112a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 21 Jul 2015 16:14:24 +0000 Subject: [PATCH] Customizer: Nav Menus JS cleanup, first round * In `init`, settings should be fresh and set to default values before extending * When looping via `_.each`, pass `this` as 3rd arg to bind context * Settings should be encapsulated in the module, instead of being exposed as static class props * `currentRefreshMenuInstanceDebouncedCalls` should be encapsulated and renamed, instead of being the longest symbol in the entire codebase See #32911. Built from https://develop.svn.wordpress.org/trunk@33345 git-svn-id: http://core.svn.wordpress.org/trunk@33317 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/customize-preview-nav-menus.js | 72 +++++++++---------- .../js/customize-preview-nav-menus.min.js | 2 +- wp-includes/version.php | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/wp-includes/js/customize-preview-nav-menus.js b/wp-includes/js/customize-preview-nav-menus.js index 94c8be6c18..8f0b7df34a 100644 --- a/wp-includes/js/customize-preview-nav-menus.js +++ b/wp-includes/js/customize-preview-nav-menus.js @@ -2,21 +2,22 @@ wp.customize.menusPreview = ( function( $, api ) { 'use strict'; - var self; - - self = { - renderQueryVar: null, - renderNonceValue: null, - renderNoncePostKey: null, - previewCustomizeNonce: null, - requestUri: '/', - theme: { - active: false, - stylesheet: '' + var currentRefreshDebounced = {}, + refreshDebounceDelay = 200, + settings = {}, + defaultSettings = { + renderQueryVar: null, + renderNonceValue: null, + renderNoncePostKey: null, + previewCustomizeNonce: null, + requestUri: '/', + theme: { + active: false, + stylesheet: '' + }, + navMenuInstanceArgs: {} }, - navMenuInstanceArgs: {}, - refreshDebounceDelay: 200 - }; + self = {}; api.bind( 'preview-ready', function() { api.preview.bind( 'active', function() { @@ -30,8 +31,9 @@ wp.customize.menusPreview = ( function( $, api ) { self.init = function() { var self = this, initializedSettings = {}; + settings = _.extend( {}, defaultSettings ); if ( 'undefined' !== typeof _wpCustomizePreviewNavMenusExports ) { - $.extend( self, _wpCustomizePreviewNavMenusExports ); + _.extend( settings, _wpCustomizePreviewNavMenusExports ); } api.each( function( setting, id ) { @@ -134,7 +136,7 @@ wp.customize.menusPreview = ( function( $, api ) { * @param {int} menuId */ self.refreshMenu = function( menuId ) { - var self = this, assignedLocations = []; + var assignedLocations = []; api.each(function( setting, id ) { var matches = id.match( /^nav_menu_locations\[(.+?)]/ ); @@ -143,11 +145,11 @@ wp.customize.menusPreview = ( function( $, api ) { } }); - _.each( self.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) { + _.each( settings.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) { if ( menuId === navMenuArgs.menu || -1 !== _.indexOf( assignedLocations, navMenuArgs.theme_location ) ) { - self.refreshMenuInstanceDebounced( instanceNumber ); + this.refreshMenuInstanceDebounced( instanceNumber ); } - } ); + }, this ); }; /** @@ -157,12 +159,12 @@ wp.customize.menusPreview = ( function( $, api ) { */ self.refreshMenuLocation = function( location ) { var foundInstance = false; - _.each( self.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) { + _.each( settings.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) { if ( location === navMenuArgs.theme_location ) { - self.refreshMenuInstanceDebounced( instanceNumber ); + this.refreshMenuInstanceDebounced( instanceNumber ); foundInstance = true; } - } ); + }, this ); if ( ! foundInstance ) { api.preview.send( 'refresh' ); } @@ -176,10 +178,10 @@ wp.customize.menusPreview = ( function( $, api ) { self.refreshMenuInstance = function( instanceNumber ) { var self = this, data, menuId, customized, container, request, wpNavArgs, instance, containerInstanceClassName; - if ( ! self.navMenuInstanceArgs[ instanceNumber ] ) { + if ( ! settings.navMenuInstanceArgs[ instanceNumber ] ) { throw new Error( 'unknown_instance_number' ); } - instance = self.navMenuInstanceArgs[ instanceNumber ]; + instance = settings.navMenuInstanceArgs[ instanceNumber ]; containerInstanceClassName = 'partial-refreshable-nav-menu-' + String( instanceNumber ); container = $( '.' + containerInstanceClassName ); @@ -197,13 +199,13 @@ wp.customize.menusPreview = ( function( $, api ) { menuId = parseInt( menuId, 10 ); data = { - nonce: self.previewCustomizeNonce, // for Customize Preview + nonce: settings.previewCustomizeNonce, // for Customize Preview wp_customize: 'on' }; - if ( ! self.theme.active ) { - data.theme = self.theme.stylesheet; + if ( ! settings.theme.active ) { + data.theme = settings.theme.stylesheet; } - data[ self.renderQueryVar ] = '1'; + data[ settings.renderQueryVar ] = '1'; // Gather settings to send in partial refresh request. customized = {}; @@ -225,7 +227,7 @@ wp.customize.menusPreview = ( function( $, api ) { } } ); data.customized = JSON.stringify( customized ); - data[ self.renderNoncePostKey ] = self.renderNonceValue; + data[ settings.renderNoncePostKey ] = settings.renderNonceValue; wpNavArgs = $.extend( {}, instance ); data.wp_nav_menu_args_hash = wpNavArgs.args_hash; @@ -236,7 +238,7 @@ wp.customize.menusPreview = ( function( $, api ) { request = wp.ajax.send( null, { data: data, - url: self.requestUri + url: settings.requestUri } ); request.done( function( data ) { // If the menu is now not visible, refresh since the page layout may have changed. @@ -261,17 +263,15 @@ wp.customize.menusPreview = ( function( $, api ) { } ); }; - self.currentRefreshMenuInstanceDebouncedCalls = {}; - self.refreshMenuInstanceDebounced = function( instanceNumber ) { - if ( self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] ) { - clearTimeout( self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] ); + if ( currentRefreshDebounced[ instanceNumber ] ) { + clearTimeout( currentRefreshDebounced[ instanceNumber ] ); } - self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] = setTimeout( + currentRefreshDebounced[ instanceNumber ] = setTimeout( function() { self.refreshMenuInstance( instanceNumber ); }, - self.refreshDebounceDelay + refreshDebounceDelay ); }; diff --git a/wp-includes/js/customize-preview-nav-menus.min.js b/wp-includes/js/customize-preview-nav-menus.min.js index 1c4a601f62..a896bb9bd9 100644 --- a/wp-includes/js/customize-preview-nav-menus.min.js +++ b/wp-includes/js/customize-preview-nav-menus.min.js @@ -1 +1 @@ -wp.customize.menusPreview=function(a,b){"use strict";var c;return c={renderQueryVar:null,renderNonceValue:null,renderNoncePostKey:null,previewCustomizeNonce:null,requestUri:"/",theme:{active:!1,stylesheet:""},navMenuInstanceArgs:{},refreshDebounceDelay:200},b.bind("preview-ready",function(){b.preview.bind("active",function(){c.init()})}),c.init=function(){var c=this,d={};"undefined"!=typeof _wpCustomizePreviewNavMenusExports&&a.extend(c,_wpCustomizePreviewNavMenusExports),b.each(function(a,b){a.id=b,d[a.id]=!0,c.bindListener(a)}),b.preview.bind("setting",function(a){var e,f,g;a=a.slice(),e=a.shift(),f=a.shift(),g=b(e),g||(g=b.create(e,f)),g.id||(g.id=e),d[g.id]||(d[g.id]=!0,c.bindListener(g)&&g.callbacks.fireWith(g,[g(),null]))})},c.bindListener=function(a){var b,d;return(b=a.id.match(/^nav_menu\[(-?\d+)]$/))?(a.navMenuId=parseInt(b[1],10),a.bind(c.onChangeNavMenuSetting),!0):(b=a.id.match(/^nav_menu_item\[(-?\d+)]$/))?(a.navMenuItemId=parseInt(b[1],10),a.bind(c.onChangeNavMenuItemSetting),!0):(b=a.id.match(/^nav_menu_locations\[(.+?)]/),b?(d=b[1],a.bind(function(){c.refreshMenuLocation(d)}),!0):!1)},c.onChangeNavMenuSetting=function(){var a=this;if(!a.navMenuId)throw new Error("Expected navMenuId property to be set.");c.refreshMenu(a.navMenuId)},c.onChangeNavMenuItemSetting=function(a,b){!b||!b.nav_menu_term_id||a&&b.nav_menu_term_id===a.nav_menu_term_id||c.refreshMenu(b.nav_menu_term_id),a&&a.nav_menu_term_id&&c.refreshMenu(a.nav_menu_term_id)},c.refreshMenu=function(a){var c=this,d=[];b.each(function(b,c){var e=c.match(/^nav_menu_locations\[(.+?)]/);e&&a===b()&&d.push(e[1])}),_.each(c.navMenuInstanceArgs,function(b,e){(a===b.menu||-1!==_.indexOf(d,b.theme_location))&&c.refreshMenuInstanceDebounced(e)})},c.refreshMenuLocation=function(a){var d=!1;_.each(c.navMenuInstanceArgs,function(b,e){a===b.theme_location&&(c.refreshMenuInstanceDebounced(e),d=!0)}),d||b.preview.send("refresh")},c.refreshMenuInstance=function(c){var d,e,f,g,h,i,j,k,l=this;if(!l.navMenuInstanceArgs[c])throw new Error("unknown_instance_number");return j=l.navMenuInstanceArgs[c],k="partial-refreshable-nav-menu-"+String(c),g=a("."+k),_.isNumber(j.menu)?e=j.menu:j.theme_location&&b.has("nav_menu_locations["+j.theme_location+"]")&&(e=b("nav_menu_locations["+j.theme_location+"]").get()),e&&j.can_partial_refresh&&0!==g.length?(e=parseInt(e,10),d={nonce:l.previewCustomizeNonce,wp_customize:"on"},l.theme.active||(d.theme=l.theme.stylesheet),d[l.renderQueryVar]="1",f={},b.each(function(a,b){var c=a.get(),d=!1;d=d||/^nav_menu_locations\[/.test(b),d=d||b==="nav_menu["+String(e)+"]",d=d||/^nav_menu_item\[/.test(b)&&(!1===c||e===c.nav_menu_term_id),d&&(f[b]=c)}),d.customized=JSON.stringify(f),d[l.renderNoncePostKey]=l.renderNonceValue,i=a.extend({},j),d.wp_nav_menu_args_hash=i.args_hash,delete i.args_hash,d.wp_nav_menu_args=JSON.stringify(i),g.addClass("customize-partial-refreshing"),h=wp.ajax.send(null,{data:d,url:l.requestUri}),void h.done(function(d){if(!1===d)return void b.preview.send("refresh");var e,f=g;g=a(d),g.addClass(k),g.addClass("partial-refreshable-nav-menu customize-partial-refreshing"),f.replaceWith(g),e={instanceNumber:c,wpNavArgs:i,oldContainer:f,newContainer:g},g.removeClass("customize-partial-refreshing"),a(document).trigger("customize-preview-menu-refreshed",[e])})):void b.preview.send("refresh")},c.currentRefreshMenuInstanceDebouncedCalls={},c.refreshMenuInstanceDebounced=function(a){c.currentRefreshMenuInstanceDebouncedCalls[a]&&clearTimeout(c.currentRefreshMenuInstanceDebouncedCalls[a]),c.currentRefreshMenuInstanceDebouncedCalls[a]=setTimeout(function(){c.refreshMenuInstance(a)},c.refreshDebounceDelay)},c}(jQuery,wp.customize); \ No newline at end of file +wp.customize.menusPreview=function(a,b){"use strict";var c={},d=200,e={},f={renderQueryVar:null,renderNonceValue:null,renderNoncePostKey:null,previewCustomizeNonce:null,requestUri:"/",theme:{active:!1,stylesheet:""},navMenuInstanceArgs:{}},g={};return b.bind("preview-ready",function(){b.preview.bind("active",function(){g.init()})}),g.init=function(){var a=this,c={};e=_.extend({},f),"undefined"!=typeof _wpCustomizePreviewNavMenusExports&&_.extend(e,_wpCustomizePreviewNavMenusExports),b.each(function(b,d){b.id=d,c[b.id]=!0,a.bindListener(b)}),b.preview.bind("setting",function(d){var e,f,g;d=d.slice(),e=d.shift(),f=d.shift(),g=b(e),g||(g=b.create(e,f)),g.id||(g.id=e),c[g.id]||(c[g.id]=!0,a.bindListener(g)&&g.callbacks.fireWith(g,[g(),null]))})},g.bindListener=function(a){var b,c;return(b=a.id.match(/^nav_menu\[(-?\d+)]$/))?(a.navMenuId=parseInt(b[1],10),a.bind(g.onChangeNavMenuSetting),!0):(b=a.id.match(/^nav_menu_item\[(-?\d+)]$/))?(a.navMenuItemId=parseInt(b[1],10),a.bind(g.onChangeNavMenuItemSetting),!0):(b=a.id.match(/^nav_menu_locations\[(.+?)]/),b?(c=b[1],a.bind(function(){g.refreshMenuLocation(c)}),!0):!1)},g.onChangeNavMenuSetting=function(){var a=this;if(!a.navMenuId)throw new Error("Expected navMenuId property to be set.");g.refreshMenu(a.navMenuId)},g.onChangeNavMenuItemSetting=function(a,b){!b||!b.nav_menu_term_id||a&&b.nav_menu_term_id===a.nav_menu_term_id||g.refreshMenu(b.nav_menu_term_id),a&&a.nav_menu_term_id&&g.refreshMenu(a.nav_menu_term_id)},g.refreshMenu=function(a){var c=[];b.each(function(b,d){var e=d.match(/^nav_menu_locations\[(.+?)]/);e&&a===b()&&c.push(e[1])}),_.each(e.navMenuInstanceArgs,function(b,d){(a===b.menu||-1!==_.indexOf(c,b.theme_location))&&this.refreshMenuInstanceDebounced(d)},this)},g.refreshMenuLocation=function(a){var c=!1;_.each(e.navMenuInstanceArgs,function(b,d){a===b.theme_location&&(this.refreshMenuInstanceDebounced(d),c=!0)},this),c||b.preview.send("refresh")},g.refreshMenuInstance=function(c){var d,f,g,h,i,j,k,l;if(!e.navMenuInstanceArgs[c])throw new Error("unknown_instance_number");return k=e.navMenuInstanceArgs[c],l="partial-refreshable-nav-menu-"+String(c),h=a("."+l),_.isNumber(k.menu)?f=k.menu:k.theme_location&&b.has("nav_menu_locations["+k.theme_location+"]")&&(f=b("nav_menu_locations["+k.theme_location+"]").get()),f&&k.can_partial_refresh&&0!==h.length?(f=parseInt(f,10),d={nonce:e.previewCustomizeNonce,wp_customize:"on"},e.theme.active||(d.theme=e.theme.stylesheet),d[e.renderQueryVar]="1",g={},b.each(function(a,b){var c=a.get(),d=!1;d=d||/^nav_menu_locations\[/.test(b),d=d||b==="nav_menu["+String(f)+"]",d=d||/^nav_menu_item\[/.test(b)&&(!1===c||f===c.nav_menu_term_id),d&&(g[b]=c)}),d.customized=JSON.stringify(g),d[e.renderNoncePostKey]=e.renderNonceValue,j=a.extend({},k),d.wp_nav_menu_args_hash=j.args_hash,delete j.args_hash,d.wp_nav_menu_args=JSON.stringify(j),h.addClass("customize-partial-refreshing"),i=wp.ajax.send(null,{data:d,url:e.requestUri}),void i.done(function(d){if(!1===d)return void b.preview.send("refresh");var e,f=h;h=a(d),h.addClass(l),h.addClass("partial-refreshable-nav-menu customize-partial-refreshing"),f.replaceWith(h),e={instanceNumber:c,wpNavArgs:j,oldContainer:f,newContainer:h},h.removeClass("customize-partial-refreshing"),a(document).trigger("customize-preview-menu-refreshed",[e])})):void b.preview.send("refresh")},g.refreshMenuInstanceDebounced=function(a){c[a]&&clearTimeout(c[a]),c[a]=setTimeout(function(){g.refreshMenuInstance(a)},d)},g}(jQuery,wp.customize); \ No newline at end of file diff --git a/wp-includes/version.php b/wp-includes/version.php index 2e3367ac43..1aad86216a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta3-33344'; +$wp_version = '4.3-beta3-33345'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.