Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
/*global jQuery, JSON, _wpCustomizePreviewNavMenusExports, _ */
|
|
|
|
|
|
|
|
wp.customize.menusPreview = ( function( $, api ) {
|
|
|
|
'use strict';
|
|
|
|
var self;
|
|
|
|
|
|
|
|
self = {
|
|
|
|
renderQueryVar: null,
|
|
|
|
renderNonceValue: null,
|
|
|
|
renderNoncePostKey: null,
|
|
|
|
previewCustomizeNonce: null,
|
|
|
|
requestUri: '/',
|
|
|
|
theme: {
|
|
|
|
active: false,
|
|
|
|
stylesheet: ''
|
|
|
|
},
|
|
|
|
navMenuInstanceArgs: {},
|
|
|
|
refreshDebounceDelay: 200
|
|
|
|
};
|
|
|
|
|
|
|
|
api.bind( 'preview-ready', function() {
|
2015-07-08 16:42:24 -04:00
|
|
|
api.preview.bind( 'active', function() {
|
|
|
|
self.init();
|
|
|
|
} );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap functionality.
|
|
|
|
*/
|
|
|
|
self.init = function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if ( 'undefined' !== typeof _wpCustomizePreviewNavMenusExports ) {
|
|
|
|
$.extend( self, _wpCustomizePreviewNavMenusExports );
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:42:24 -04:00
|
|
|
api.each( function( setting, id ) {
|
|
|
|
setting.id = id;
|
|
|
|
self.bindListener( setting );
|
|
|
|
} );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
|
2015-07-08 16:42:24 -04:00
|
|
|
api.preview.bind( 'setting', function( args ) {
|
|
|
|
var id, value, setting;
|
|
|
|
args = args.slice();
|
|
|
|
id = args.shift();
|
|
|
|
value = args.shift();
|
|
|
|
if ( ! api.has( id ) ) {
|
|
|
|
// Currently customize-preview.js is not creating settings for dynamically-created settings in the pane; so we have to do it
|
|
|
|
setting = api.create( id, value ); // @todo This should be in core
|
|
|
|
setting.id = id;
|
|
|
|
if ( self.bindListener( setting ) ) {
|
|
|
|
setting.callbacks.fireWith( setting, [ setting(), null ] );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
}
|
2015-07-08 16:42:24 -04:00
|
|
|
}
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {wp.customize.Value} setting
|
|
|
|
* @returns {boolean} Whether the setting was bound.
|
|
|
|
*/
|
|
|
|
self.bindListener = function( setting ) {
|
|
|
|
var matches, themeLocation;
|
|
|
|
|
|
|
|
matches = setting.id.match( /^nav_menu\[(-?\d+)]$/ );
|
|
|
|
if ( matches ) {
|
|
|
|
setting.navMenuId = parseInt( matches[1], 10 );
|
|
|
|
setting.bind( self.onChangeNavMenuSetting );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = setting.id.match( /^nav_menu_item\[(-?\d+)]$/ );
|
|
|
|
if ( matches ) {
|
|
|
|
setting.navMenuItemId = parseInt( matches[1], 10 );
|
|
|
|
setting.bind( self.onChangeNavMenuItemSetting );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = setting.id.match( /^nav_menu_locations\[(.+?)]/ );
|
|
|
|
if ( matches ) {
|
|
|
|
themeLocation = matches[1];
|
|
|
|
setting.bind( function() {
|
|
|
|
self.refreshMenuLocation( themeLocation );
|
|
|
|
} );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changing of a nav_menu setting.
|
|
|
|
*
|
|
|
|
* @this {wp.customize.Setting}
|
|
|
|
*/
|
|
|
|
self.onChangeNavMenuSetting = function() {
|
|
|
|
var setting = this;
|
|
|
|
if ( ! setting.navMenuId ) {
|
|
|
|
throw new Error( 'Expected navMenuId property to be set.' );
|
|
|
|
}
|
|
|
|
self.refreshMenu( setting.navMenuId );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changing of a nav_menu_item setting.
|
|
|
|
*
|
|
|
|
* @this {wp.customize.Setting}
|
|
|
|
* @param {object} to
|
|
|
|
* @param {object} from
|
|
|
|
*/
|
|
|
|
self.onChangeNavMenuItemSetting = function( to, from ) {
|
|
|
|
if ( from && from.nav_menu_term_id && ( ! to || from.nav_menu_term_id !== to.nav_menu_term_id ) ) {
|
|
|
|
self.refreshMenu( from.nav_menu_term_id );
|
|
|
|
}
|
|
|
|
if ( to && to.nav_menu_term_id ) {
|
|
|
|
self.refreshMenu( to.nav_menu_term_id );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a given menu rendered in the preview.
|
|
|
|
*
|
|
|
|
* @param {int} menuId
|
|
|
|
*/
|
|
|
|
self.refreshMenu = function( menuId ) {
|
|
|
|
var self = this, assignedLocations = [];
|
|
|
|
|
|
|
|
api.each(function( setting, id ) {
|
|
|
|
var matches = id.match( /^nav_menu_locations\[(.+?)]/ );
|
|
|
|
if ( matches && menuId === setting() ) {
|
|
|
|
assignedLocations.push( matches[1] );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each( self.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) {
|
|
|
|
if ( menuId === navMenuArgs.menu || -1 !== _.indexOf( assignedLocations, navMenuArgs.theme_location ) ) {
|
|
|
|
self.refreshMenuInstanceDebounced( instanceNumber );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
self.refreshMenuLocation = function( location ) {
|
|
|
|
var foundInstance = false;
|
|
|
|
_.each( self.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) {
|
|
|
|
if ( location === navMenuArgs.theme_location ) {
|
|
|
|
self.refreshMenuInstanceDebounced( instanceNumber );
|
|
|
|
foundInstance = true;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
if ( ! foundInstance ) {
|
|
|
|
api.preview.send( 'refresh' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a specific instance of a given menu on the page.
|
|
|
|
*
|
|
|
|
* @param {int} instanceNumber
|
|
|
|
*/
|
|
|
|
self.refreshMenuInstance = function( instanceNumber ) {
|
2015-07-08 17:30:24 -04:00
|
|
|
var self = this, data, menuId, customized, container, request, wpNavArgs, instance, containerInstanceClassName;
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
|
|
|
|
if ( ! self.navMenuInstanceArgs[ instanceNumber ] ) {
|
|
|
|
throw new Error( 'unknown_instance_number' );
|
|
|
|
}
|
|
|
|
instance = self.navMenuInstanceArgs[ instanceNumber ];
|
|
|
|
|
2015-07-08 17:30:24 -04:00
|
|
|
containerInstanceClassName = 'partial-refreshable-nav-menu-' + String( instanceNumber );
|
|
|
|
container = $( '.' + containerInstanceClassName );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
|
2015-07-08 17:30:24 -04:00
|
|
|
if ( _.isNumber( instance.menu ) ) {
|
|
|
|
menuId = instance.menu;
|
|
|
|
} else if ( instance.theme_location && api.has( 'nav_menu_locations[' + instance.theme_location + ']' ) ) {
|
|
|
|
menuId = api( 'nav_menu_locations[' + instance.theme_location + ']' ).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! menuId || ! instance.can_partial_refresh || 0 === container.length ) {
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
api.preview.send( 'refresh' );
|
|
|
|
return;
|
|
|
|
}
|
2015-07-08 17:30:24 -04:00
|
|
|
menuId = parseInt( menuId, 10 );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
|
|
|
|
data = {
|
|
|
|
nonce: self.previewCustomizeNonce, // for Customize Preview
|
|
|
|
wp_customize: 'on'
|
|
|
|
};
|
|
|
|
if ( ! self.theme.active ) {
|
|
|
|
data.theme = self.theme.stylesheet;
|
|
|
|
}
|
|
|
|
data[ self.renderQueryVar ] = '1';
|
|
|
|
customized = {};
|
|
|
|
api.each( function( setting, id ) {
|
2015-07-08 17:30:24 -04:00
|
|
|
// @todo Core should propagate the dirty state into the Preview as well so we can use that here.
|
|
|
|
if ( id === 'nav_menu[' + String( menuId ) + ']' || ( /^nav_menu_item\[/.test( id ) && setting() && menuId === setting().nav_menu_term_id ) ) {
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
customized[ id ] = setting.get();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
data.customized = JSON.stringify( customized );
|
|
|
|
data[ self.renderNoncePostKey ] = self.renderNonceValue;
|
|
|
|
|
|
|
|
wpNavArgs = $.extend( {}, instance );
|
|
|
|
data.wp_nav_menu_args_hash = wpNavArgs.args_hash;
|
|
|
|
delete wpNavArgs.args_hash;
|
|
|
|
data.wp_nav_menu_args = JSON.stringify( wpNavArgs );
|
|
|
|
|
|
|
|
container.addClass( 'customize-partial-refreshing' );
|
|
|
|
|
|
|
|
request = wp.ajax.send( null, {
|
|
|
|
data: data,
|
|
|
|
url: self.requestUri
|
|
|
|
} );
|
|
|
|
request.done( function( data ) {
|
2015-07-08 17:30:24 -04:00
|
|
|
// If the menu is now not visible, refresh since the page layout may have changed.
|
|
|
|
if ( false === data ) {
|
|
|
|
api.preview.send( 'refresh' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventParam, previousContainer = container;
|
|
|
|
container = $( data );
|
|
|
|
container.addClass( containerInstanceClassName );
|
|
|
|
container.addClass( 'partial-refreshable-nav-menu customize-partial-refreshing' );
|
|
|
|
previousContainer.replaceWith( container );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
eventParam = {
|
|
|
|
instanceNumber: instanceNumber,
|
2015-07-08 17:30:24 -04:00
|
|
|
wpNavArgs: wpNavArgs,
|
|
|
|
oldContainer: previousContainer,
|
|
|
|
newContainer: container
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
};
|
|
|
|
container.removeClass( 'customize-partial-refreshing' );
|
2015-07-08 17:30:24 -04:00
|
|
|
$( document ).trigger( 'customize-preview-menu-refreshed', [ eventParam ] );
|
Add menu management to the Customizer.
This brings in the Menu Customizer plugin: https://wordpress.org/plugins/menu-customizer/.
props celloexpressions, westonruter, valendesigns, voldemortensen, ocean90, adamsilverstein, kucrut, jorbin, designsimply, afercia, davidakennedy, obenland.
see #32576.
Built from https://develop.svn.wordpress.org/trunk@32806
git-svn-id: http://core.svn.wordpress.org/trunk@32777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-06-16 18:08:26 -04:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
self.currentRefreshMenuInstanceDebouncedCalls = {};
|
|
|
|
|
|
|
|
self.refreshMenuInstanceDebounced = function( instanceNumber ) {
|
|
|
|
if ( self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] ) {
|
|
|
|
clearTimeout( self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] );
|
|
|
|
}
|
|
|
|
self.currentRefreshMenuInstanceDebouncedCalls[ instanceNumber ] = setTimeout(
|
|
|
|
function() {
|
|
|
|
self.refreshMenuInstance( instanceNumber );
|
|
|
|
},
|
|
|
|
self.refreshDebounceDelay
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
}( jQuery, wp.customize ) );
|