| | |
| | |
| | |
| |
|
| | |
| |
|
| | |
| | wp.customize.navMenusPreview = wp.customize.MenusCustomizerPreview = ( function( $, _, wp, api ) { |
| | 'use strict'; |
| |
|
| | var self = { |
| | data: { |
| | navMenuInstanceArgs: {} |
| | } |
| | }; |
| | if ( 'undefined' !== typeof _wpCustomizePreviewNavMenusExports ) { |
| | _.extend( self.data, _wpCustomizePreviewNavMenusExports ); |
| | } |
| |
|
| | |
| | |
| | |
| | self.init = function() { |
| | var self = this, synced = false; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | api.preview.bind( 'sync', function() { |
| | synced = true; |
| | } ); |
| |
|
| | if ( api.selectiveRefresh ) { |
| | |
| | api.each( function( setting ) { |
| | self.bindSettingListener( setting ); |
| | } ); |
| | api.bind( 'add', function( setting ) { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if ( setting.get() && ! setting.get()._invalid ) { |
| | self.bindSettingListener( setting, { fire: synced } ); |
| | } |
| | } ); |
| | api.bind( 'remove', function( setting ) { |
| | self.unbindSettingListener( setting ); |
| | } ); |
| |
|
| | |
| | |
| | |
| | |
| | api.selectiveRefresh.bind( 'render-partials-response', function( response ) { |
| | if ( response.nav_menu_instance_args ) { |
| | _.extend( self.data.navMenuInstanceArgs, response.nav_menu_instance_args ); |
| | } |
| | } ); |
| | } |
| |
|
| | api.preview.bind( 'active', function() { |
| | self.highlightControls(); |
| | } ); |
| | }; |
| |
|
| | if ( api.selectiveRefresh ) { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.NavMenuInstancePartial = api.selectiveRefresh.Partial.extend({ |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | initialize: function( id, options ) { |
| | var partial = this, matches, argsHmac; |
| | matches = id.match( /^nav_menu_instance\[([0-9a-f]{32})]$/ ); |
| | if ( ! matches ) { |
| | throw new Error( 'Illegal id for nav_menu_instance partial. The key corresponds with the args HMAC.' ); |
| | } |
| | argsHmac = matches[1]; |
| |
|
| | options = options || {}; |
| | options.params = _.extend( |
| | { |
| | selector: '[data-customize-partial-id="' + id + '"]', |
| | navMenuArgs: options.constructingContainerContext || {}, |
| | containerInclusive: true |
| | }, |
| | options.params || {} |
| | ); |
| | api.selectiveRefresh.Partial.prototype.initialize.call( partial, id, options ); |
| |
|
| | if ( ! _.isObject( partial.params.navMenuArgs ) ) { |
| | throw new Error( 'Missing navMenuArgs' ); |
| | } |
| | if ( partial.params.navMenuArgs.args_hmac !== argsHmac ) { |
| | throw new Error( 'args_hmac mismatch with id' ); |
| | } |
| | }, |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | isRelatedSetting: function( setting, newValue, oldValue ) { |
| | var partial = this, navMenuLocationSetting, navMenuId, isNavMenuItemSetting, _newValue, _oldValue, urlParser; |
| | if ( _.isString( setting ) ) { |
| | setting = api( setting ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | isNavMenuItemSetting = /^nav_menu_item\[/.test( setting.id ); |
| | if ( isNavMenuItemSetting && _.isObject( newValue ) && _.isObject( oldValue ) ) { |
| | _newValue = _.clone( newValue ); |
| | _oldValue = _.clone( oldValue ); |
| | delete _newValue.type_label; |
| | delete _oldValue.type_label; |
| |
|
| | |
| | if ( 'https' === api.preview.scheme.get() ) { |
| | urlParser = document.createElement( 'a' ); |
| | urlParser.href = _newValue.url; |
| | urlParser.protocol = 'https:'; |
| | _newValue.url = urlParser.href; |
| | urlParser.href = _oldValue.url; |
| | urlParser.protocol = 'https:'; |
| | _oldValue.url = urlParser.href; |
| | } |
| |
|
| | |
| | if ( newValue.title ) { |
| | delete _oldValue.original_title; |
| | delete _newValue.original_title; |
| | } |
| |
|
| | if ( _.isEqual( _oldValue, _newValue ) ) { |
| | return false; |
| | } |
| | } |
| |
|
| | if ( partial.params.navMenuArgs.theme_location ) { |
| | if ( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' === setting.id ) { |
| | return true; |
| | } |
| | navMenuLocationSetting = api( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' ); |
| | } |
| |
|
| | navMenuId = partial.params.navMenuArgs.menu; |
| | if ( ! navMenuId && navMenuLocationSetting ) { |
| | navMenuId = navMenuLocationSetting(); |
| | } |
| |
|
| | if ( ! navMenuId ) { |
| | return false; |
| | } |
| | return ( |
| | ( 'nav_menu[' + navMenuId + ']' === setting.id ) || |
| | ( isNavMenuItemSetting && ( |
| | ( newValue && newValue.nav_menu_term_id === navMenuId ) || |
| | ( oldValue && oldValue.nav_menu_term_id === navMenuId ) |
| | ) ) |
| | ); |
| | }, |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | refresh: function() { |
| | var partial = this, menuId, deferred = $.Deferred(); |
| |
|
| | |
| | if ( _.isNumber( partial.params.navMenuArgs.menu ) ) { |
| | menuId = partial.params.navMenuArgs.menu; |
| | } else if ( partial.params.navMenuArgs.theme_location && api.has( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' ) ) { |
| | menuId = api( 'nav_menu_locations[' + partial.params.navMenuArgs.theme_location + ']' ).get(); |
| | } |
| | if ( ! menuId ) { |
| | partial.fallback(); |
| | deferred.reject(); |
| | return deferred.promise(); |
| | } |
| |
|
| | return api.selectiveRefresh.Partial.prototype.refresh.call( partial ); |
| | }, |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | renderContent: function( placement ) { |
| | var partial = this, previousContainer = placement.container; |
| |
|
| | |
| | if ( '' === placement.addedContent ) { |
| | placement.partial.fallback(); |
| | } |
| |
|
| | if ( api.selectiveRefresh.Partial.prototype.renderContent.call( partial, placement ) ) { |
| |
|
| | |
| | $( document ).trigger( 'customize-preview-menu-refreshed', [ { |
| | instanceNumber: null, |
| | wpNavArgs: placement.context, |
| | wpNavMenuArgs: placement.context, |
| | oldContainer: previousContainer, |
| | newContainer: placement.container |
| | } ] ); |
| | } |
| | } |
| | }); |
| |
|
| | api.selectiveRefresh.partialConstructor.nav_menu_instance = self.NavMenuInstancePartial; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | self.handleUnplacedNavMenuInstances = function( navMenuInstanceArgs ) { |
| | var unplacedNavMenuInstances; |
| | unplacedNavMenuInstances = _.filter( _.values( self.data.navMenuInstanceArgs ), function( args ) { |
| | return ! api.selectiveRefresh.partial.has( 'nav_menu_instance[' + args.args_hmac + ']' ); |
| | } ); |
| | if ( _.findWhere( unplacedNavMenuInstances, navMenuInstanceArgs ) ) { |
| | api.selectiveRefresh.requestFullRefresh(); |
| | return true; |
| | } |
| | return false; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.bindSettingListener = function( setting, options ) { |
| | var matches; |
| | options = options || {}; |
| |
|
| | matches = setting.id.match( /^nav_menu\[(-?\d+)]$/ ); |
| | if ( matches ) { |
| | setting._navMenuId = parseInt( matches[1], 10 ); |
| | setting.bind( this.onChangeNavMenuSetting ); |
| | if ( options.fire ) { |
| | this.onChangeNavMenuSetting.call( setting, setting(), false ); |
| | } |
| | return true; |
| | } |
| |
|
| | matches = setting.id.match( /^nav_menu_item\[(-?\d+)]$/ ); |
| | if ( matches ) { |
| | setting._navMenuItemId = parseInt( matches[1], 10 ); |
| | setting.bind( this.onChangeNavMenuItemSetting ); |
| | if ( options.fire ) { |
| | this.onChangeNavMenuItemSetting.call( setting, setting(), false ); |
| | } |
| | return true; |
| | } |
| |
|
| | matches = setting.id.match( /^nav_menu_locations\[(.+?)]/ ); |
| | if ( matches ) { |
| | setting._navMenuThemeLocation = matches[1]; |
| | setting.bind( this.onChangeNavMenuLocationsSetting ); |
| | if ( options.fire ) { |
| | this.onChangeNavMenuLocationsSetting.call( setting, setting(), false ); |
| | } |
| | return true; |
| | } |
| |
|
| | return false; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.unbindSettingListener = function( setting ) { |
| | setting.unbind( this.onChangeNavMenuSetting ); |
| | setting.unbind( this.onChangeNavMenuItemSetting ); |
| | setting.unbind( this.onChangeNavMenuLocationsSetting ); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.onChangeNavMenuSetting = function() { |
| | var setting = this; |
| |
|
| | self.handleUnplacedNavMenuInstances( { |
| | menu: setting._navMenuId |
| | } ); |
| |
|
| | |
| | api.each( function( otherSetting ) { |
| | if ( ! otherSetting._navMenuThemeLocation ) { |
| | return; |
| | } |
| | if ( setting._navMenuId === otherSetting() ) { |
| | self.handleUnplacedNavMenuInstances( { |
| | theme_location: otherSetting._navMenuThemeLocation |
| | } ); |
| | } |
| | } ); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.onChangeNavMenuItemSetting = function( newItem, oldItem ) { |
| | var item = newItem || oldItem, navMenuSetting; |
| | navMenuSetting = api( 'nav_menu[' + String( item.nav_menu_term_id ) + ']' ); |
| | if ( navMenuSetting ) { |
| | self.onChangeNavMenuSetting.call( navMenuSetting ); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.onChangeNavMenuLocationsSetting = function() { |
| | var setting = this, hasNavMenuInstance; |
| | self.handleUnplacedNavMenuInstances( { |
| | theme_location: setting._navMenuThemeLocation |
| | } ); |
| |
|
| | |
| | hasNavMenuInstance = !! _.findWhere( _.values( self.data.navMenuInstanceArgs ), { |
| | theme_location: setting._navMenuThemeLocation |
| | } ); |
| | if ( ! hasNavMenuInstance ) { |
| | api.selectiveRefresh.requestFullRefresh(); |
| | } |
| | }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | self.highlightControls = function() { |
| | var selector = '.menu-item'; |
| |
|
| | |
| | if ( ! api.settings.channel ) { |
| | return; |
| | } |
| |
|
| | |
| | $( document ).on( 'click', selector, function( e ) { |
| | var navMenuItemParts; |
| | if ( ! e.shiftKey ) { |
| | return; |
| | } |
| |
|
| | navMenuItemParts = $( this ).attr( 'class' ).match( /(?:^|\s)menu-item-(-?\d+)(?:\s|$)/ ); |
| | if ( navMenuItemParts ) { |
| | e.preventDefault(); |
| | e.stopPropagation(); |
| | api.preview.send( 'focus-nav-menu-item-control', parseInt( navMenuItemParts[1], 10 ) ); |
| | } |
| | }); |
| | }; |
| |
|
| | api.bind( 'preview-ready', function() { |
| | self.init(); |
| | } ); |
| |
|
| | return self; |
| |
|
| | }( jQuery, _, wp, wp.customize ) ); |
| |
|