[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> customize-nav-menus.js (source)

   1  /**
   2   * @output wp-admin/js/customize-nav-menus.js
   3   */
   4  
   5  /* global _wpCustomizeNavMenusSettings, wpNavMenu, console */
   6  ( function( api, wp, $ ) {
   7      'use strict';
   8  
   9      /**
  10       * Set up wpNavMenu for drag and drop.
  11       */
  12      wpNavMenu.originalInit = wpNavMenu.init;
  13      wpNavMenu.options.menuItemDepthPerLevel = 20;
  14      wpNavMenu.options.sortableItems         = '> .customize-control-nav_menu_item';
  15      wpNavMenu.options.targetTolerance       = 10;
  16      wpNavMenu.init = function() {
  17          this.jQueryExtensions();
  18      };
  19  
  20      /**
  21       * @namespace wp.customize.Menus
  22       */
  23      api.Menus = api.Menus || {};
  24  
  25      // Link settings.
  26      api.Menus.data = {
  27          itemTypes: [],
  28          l10n: {},
  29          settingTransport: 'refresh',
  30          phpIntMax: 0,
  31          defaultSettingValues: {
  32              nav_menu: {},
  33              nav_menu_item: {}
  34          },
  35          locationSlugMappedToName: {}
  36      };
  37      if ( 'undefined' !== typeof _wpCustomizeNavMenusSettings ) {
  38          $.extend( api.Menus.data, _wpCustomizeNavMenusSettings );
  39      }
  40  
  41      /**
  42       * Newly-created Nav Menus and Nav Menu Items have negative integer IDs which
  43       * serve as placeholders until Save & Publish happens.
  44       *
  45       * @alias wp.customize.Menus.generatePlaceholderAutoIncrementId
  46       *
  47       * @return {number}
  48       */
  49      api.Menus.generatePlaceholderAutoIncrementId = function() {
  50          return -Math.ceil( api.Menus.data.phpIntMax * Math.random() );
  51      };
  52  
  53      /**
  54       * wp.customize.Menus.AvailableItemModel
  55       *
  56       * A single available menu item model. See PHP's WP_Customize_Nav_Menu_Item_Setting class.
  57       *
  58       * @class    wp.customize.Menus.AvailableItemModel
  59       * @augments Backbone.Model
  60       */
  61      api.Menus.AvailableItemModel = Backbone.Model.extend( $.extend(
  62          {
  63              id: null // This is only used by Backbone.
  64          },
  65          api.Menus.data.defaultSettingValues.nav_menu_item
  66      ) );
  67  
  68      /**
  69       * wp.customize.Menus.AvailableItemCollection
  70       *
  71       * Collection for available menu item models.
  72       *
  73       * @class    wp.customize.Menus.AvailableItemCollection
  74       * @augments Backbone.Collection
  75       */
  76      api.Menus.AvailableItemCollection = Backbone.Collection.extend(/** @lends wp.customize.Menus.AvailableItemCollection.prototype */{
  77          model: api.Menus.AvailableItemModel,
  78  
  79          sort_key: 'order',
  80  
  81          comparator: function( item ) {
  82              return -item.get( this.sort_key );
  83          },
  84  
  85          sortByField: function( fieldName ) {
  86              this.sort_key = fieldName;
  87              this.sort();
  88          }
  89      });
  90      api.Menus.availableMenuItems = new api.Menus.AvailableItemCollection( api.Menus.data.availableMenuItems );
  91  
  92      /**
  93       * Insert a new `auto-draft` post.
  94       *
  95       * @since 4.7.0
  96       * @alias wp.customize.Menus.insertAutoDraftPost
  97       *
  98       * @param {object} params - Parameters for the draft post to create.
  99       * @param {string} params.post_type - Post type to add.
 100       * @param {string} params.post_title - Post title to use.
 101       * @return {jQuery.promise} Promise resolved with the added post.
 102       */
 103      api.Menus.insertAutoDraftPost = function insertAutoDraftPost( params ) {
 104          var request, deferred = $.Deferred();
 105  
 106          request = wp.ajax.post( 'customize-nav-menus-insert-auto-draft', {
 107              'customize-menus-nonce': api.settings.nonce['customize-menus'],
 108              'wp_customize': 'on',
 109              'customize_changeset_uuid': api.settings.changeset.uuid,
 110              'params': params
 111          } );
 112  
 113          request.done( function( response ) {
 114              if ( response.post_id ) {
 115                  api( 'nav_menus_created_posts' ).set(
 116                      api( 'nav_menus_created_posts' ).get().concat( [ response.post_id ] )
 117                  );
 118  
 119                  if ( 'page' === params.post_type ) {
 120  
 121                      // Activate static front page controls as this could be the first page created.
 122                      if ( api.section.has( 'static_front_page' ) ) {
 123                          api.section( 'static_front_page' ).activate();
 124                      }
 125  
 126                      // Add new page to dropdown-pages controls.
 127                      api.control.each( function( control ) {
 128                          var select;
 129                          if ( 'dropdown-pages' === control.params.type ) {
 130                              select = control.container.find( 'select[name^="_customize-dropdown-pages-"]' );
 131                              select.append( new Option( params.post_title, response.post_id ) );
 132                          }
 133                      } );
 134                  }
 135                  deferred.resolve( response );
 136              }
 137          } );
 138  
 139          request.fail( function( response ) {
 140              var error = response || '';
 141  
 142              if ( 'undefined' !== typeof response.message ) {
 143                  error = response.message;
 144              }
 145  
 146              console.error( error );
 147              deferred.rejectWith( error );
 148          } );
 149  
 150          return deferred.promise();
 151      };
 152  
 153      api.Menus.AvailableMenuItemsPanelView = wp.Backbone.View.extend(/** @lends wp.customize.Menus.AvailableMenuItemsPanelView.prototype */{
 154  
 155          el: '#available-menu-items',
 156  
 157          events: {
 158              'input #menu-items-search': 'debounceSearch',
 159              'focus .menu-item-tpl': 'focus',
 160              'click .menu-item-tpl': '_submit',
 161              'click #custom-menu-item-submit': '_submitLink',
 162              'keypress #custom-menu-item-name': '_submitLink',
 163              'click .new-content-item .add-content': '_submitNew',
 164              'keypress .create-item-input': '_submitNew',
 165              'keydown': 'keyboardAccessible'
 166          },
 167  
 168          // Cache current selected menu item.
 169          selected: null,
 170  
 171          // Cache menu control that opened the panel.
 172          currentMenuControl: null,
 173          debounceSearch: null,
 174          $search: null,
 175          $clearResults: null,
 176          searchTerm: '',
 177          rendered: false,
 178          pages: {},
 179          sectionContent: '',
 180          loading: false,
 181          addingNew: false,
 182  
 183          /**
 184           * wp.customize.Menus.AvailableMenuItemsPanelView
 185           *
 186           * View class for the available menu items panel.
 187           *
 188           * @constructs wp.customize.Menus.AvailableMenuItemsPanelView
 189           * @augments   wp.Backbone.View
 190           */
 191          initialize: function() {
 192              var self = this;
 193  
 194              if ( ! api.panel.has( 'nav_menus' ) ) {
 195                  return;
 196              }
 197  
 198              this.$search = $( '#menu-items-search' );
 199              this.$clearResults = this.$el.find( '.clear-results' );
 200              this.sectionContent = this.$el.find( '.available-menu-items-list' );
 201  
 202              this.debounceSearch = _.debounce( self.search, 500 );
 203  
 204              _.bindAll( this, 'close' );
 205  
 206              // If the available menu items panel is open and the customize controls are
 207              // interacted with (other than an item being deleted), then close the
 208              // available menu items panel. Also close on back button click.
 209              $( '#customize-controls, .customize-section-back' ).on( 'click keydown', function( e ) {
 210                  var isDeleteBtn = $( e.target ).is( '.item-delete, .item-delete *' ),
 211                      isAddNewBtn = $( e.target ).is( '.add-new-menu-item, .add-new-menu-item *' );
 212                  if ( $( 'body' ).hasClass( 'adding-menu-items' ) && ! isDeleteBtn && ! isAddNewBtn ) {
 213                      self.close();
 214                  }
 215              } );
 216  
 217              // Clear the search results and trigger a `keyup` event to fire a new search.
 218              this.$clearResults.on( 'click', function() {
 219                  self.$search.val( '' ).focus().trigger( 'keyup' );
 220              } );
 221  
 222              this.$el.on( 'input', '#custom-menu-item-name.invalid, #custom-menu-item-url.invalid', function() {
 223                  $( this ).removeClass( 'invalid' );
 224              });
 225  
 226              // Load available items if it looks like we'll need them.
 227              api.panel( 'nav_menus' ).container.bind( 'expanded', function() {
 228                  if ( ! self.rendered ) {
 229                      self.initList();
 230                      self.rendered = true;
 231                  }
 232              });
 233  
 234              // Load more items.
 235              this.sectionContent.scroll( function() {
 236                  var totalHeight = self.$el.find( '.accordion-section.open .available-menu-items-list' ).prop( 'scrollHeight' ),
 237                      visibleHeight = self.$el.find( '.accordion-section.open' ).height();
 238  
 239                  if ( ! self.loading && $( this ).scrollTop() > 3 / 4 * totalHeight - visibleHeight ) {
 240                      var type = $( this ).data( 'type' ),
 241                          object = $( this ).data( 'object' );
 242  
 243                      if ( 'search' === type ) {
 244                          if ( self.searchTerm ) {
 245                              self.doSearch( self.pages.search );
 246                          }
 247                      } else {
 248                          self.loadItems( [
 249                              { type: type, object: object }
 250                          ] );
 251                      }
 252                  }
 253              });
 254  
 255              // Close the panel if the URL in the preview changes
 256              api.previewer.bind( 'url', this.close );
 257  
 258              self.delegateEvents();
 259          },
 260  
 261          // Search input change handler.
 262          search: function( event ) {
 263              var $searchSection = $( '#available-menu-items-search' ),
 264                  $otherSections = $( '#available-menu-items .accordion-section' ).not( $searchSection );
 265  
 266              if ( ! event ) {
 267                  return;
 268              }
 269  
 270              if ( this.searchTerm === event.target.value ) {
 271                  return;
 272              }
 273  
 274              if ( '' !== event.target.value && ! $searchSection.hasClass( 'open' ) ) {
 275                  $otherSections.fadeOut( 100 );
 276                  $searchSection.find( '.accordion-section-content' ).slideDown( 'fast' );
 277                  $searchSection.addClass( 'open' );
 278                  this.$clearResults.addClass( 'is-visible' );
 279              } else if ( '' === event.target.value ) {
 280                  $searchSection.removeClass( 'open' );
 281                  $otherSections.show();
 282                  this.$clearResults.removeClass( 'is-visible' );
 283              }
 284  
 285              this.searchTerm = event.target.value;
 286              this.pages.search = 1;
 287              this.doSearch( 1 );
 288          },
 289  
 290          // Get search results.
 291          doSearch: function( page ) {
 292              var self = this, params,
 293                  $section = $( '#available-menu-items-search' ),
 294                  $content = $section.find( '.accordion-section-content' ),
 295                  itemTemplate = wp.template( 'available-menu-item' );
 296  
 297              if ( self.currentRequest ) {
 298                  self.currentRequest.abort();
 299              }
 300  
 301              if ( page < 0 ) {
 302                  return;
 303              } else if ( page > 1 ) {
 304                  $section.addClass( 'loading-more' );
 305                  $content.attr( 'aria-busy', 'true' );
 306                  wp.a11y.speak( api.Menus.data.l10n.itemsLoadingMore );
 307              } else if ( '' === self.searchTerm ) {
 308                  $content.html( '' );
 309                  wp.a11y.speak( '' );
 310                  return;
 311              }
 312  
 313              $section.addClass( 'loading' );
 314              self.loading = true;
 315  
 316              params = api.previewer.query( { excludeCustomizedSaved: true } );
 317              _.extend( params, {
 318                  'customize-menus-nonce': api.settings.nonce['customize-menus'],
 319                  'wp_customize': 'on',
 320                  'search': self.searchTerm,
 321                  'page': page
 322              } );
 323  
 324              self.currentRequest = wp.ajax.post( 'search-available-menu-items-customizer', params );
 325  
 326              self.currentRequest.done(function( data ) {
 327                  var items;
 328                  if ( 1 === page ) {
 329                      // Clear previous results as it's a new search.
 330                      $content.empty();
 331                  }
 332                  $section.removeClass( 'loading loading-more' );
 333                  $content.attr( 'aria-busy', 'false' );
 334                  $section.addClass( 'open' );
 335                  self.loading = false;
 336                  items = new api.Menus.AvailableItemCollection( data.items );
 337                  self.collection.add( items.models );
 338                  items.each( function( menuItem ) {
 339                      $content.append( itemTemplate( menuItem.attributes ) );
 340                  } );
 341                  if ( 20 > items.length ) {
 342                      self.pages.search = -1; // Up to 20 posts and 20 terms in results, if <20, no more results for either.
 343                  } else {
 344                      self.pages.search = self.pages.search + 1;
 345                  }
 346                  if ( items && page > 1 ) {
 347                      wp.a11y.speak( api.Menus.data.l10n.itemsFoundMore.replace( '%d', items.length ) );
 348                  } else if ( items && page === 1 ) {
 349                      wp.a11y.speak( api.Menus.data.l10n.itemsFound.replace( '%d', items.length ) );
 350                  }
 351              });
 352  
 353              self.currentRequest.fail(function( data ) {
 354                  // data.message may be undefined, for example when typing slow and the request is aborted.
 355                  if ( data.message ) {
 356                      $content.empty().append( $( '<li class="nothing-found"></li>' ).text( data.message ) );
 357                      wp.a11y.speak( data.message );
 358                  }
 359                  self.pages.search = -1;
 360              });
 361  
 362              self.currentRequest.always(function() {
 363                  $section.removeClass( 'loading loading-more' );
 364                  $content.attr( 'aria-busy', 'false' );
 365                  self.loading = false;
 366                  self.currentRequest = null;
 367              });
 368          },
 369  
 370          // Render the individual items.
 371          initList: function() {
 372              var self = this;
 373  
 374              // Render the template for each item by type.
 375              _.each( api.Menus.data.itemTypes, function( itemType ) {
 376                  self.pages[ itemType.type + ':' + itemType.object ] = 0;
 377              } );
 378              self.loadItems( api.Menus.data.itemTypes );
 379          },
 380  
 381          /**
 382           * Load available nav menu items.
 383           *
 384           * @since 4.3.0
 385           * @since 4.7.0 Changed function signature to take list of item types instead of single type/object.
 386           * @access private
 387           *
 388           * @param {Array.<object>} itemTypes List of objects containing type and key.
 389           * @param {string} deprecated Formerly the object parameter.
 390           * @return {void}
 391           */
 392          loadItems: function( itemTypes, deprecated ) {
 393              var self = this, _itemTypes, requestItemTypes = [], params, request, itemTemplate, availableMenuItemContainers = {};
 394              itemTemplate = wp.template( 'available-menu-item' );
 395  
 396              if ( _.isString( itemTypes ) && _.isString( deprecated ) ) {
 397                  _itemTypes = [ { type: itemTypes, object: deprecated } ];
 398              } else {
 399                  _itemTypes = itemTypes;
 400              }
 401  
 402              _.each( _itemTypes, function( itemType ) {
 403                  var container, name = itemType.type + ':' + itemType.object;
 404                  if ( -1 === self.pages[ name ] ) {
 405                      return; // Skip types for which there are no more results.
 406                  }
 407                  container = $( '#available-menu-items-' + itemType.type + '-' + itemType.object );
 408                  container.find( '.accordion-section-title' ).addClass( 'loading' );
 409                  availableMenuItemContainers[ name ] = container;
 410  
 411                  requestItemTypes.push( {
 412                      object: itemType.object,
 413                      type: itemType.type,
 414                      page: self.pages[ name ]
 415                  } );
 416              } );
 417  
 418              if ( 0 === requestItemTypes.length ) {
 419                  return;
 420              }
 421  
 422              self.loading = true;
 423  
 424              params = api.previewer.query( { excludeCustomizedSaved: true } );
 425              _.extend( params, {
 426                  'customize-menus-nonce': api.settings.nonce['customize-menus'],
 427                  'wp_customize': 'on',
 428                  'item_types': requestItemTypes
 429              } );
 430  
 431              request = wp.ajax.post( 'load-available-menu-items-customizer', params );
 432  
 433              request.done(function( data ) {
 434                  var typeInner;
 435                  _.each( data.items, function( typeItems, name ) {
 436                      if ( 0 === typeItems.length ) {
 437                          if ( 0 === self.pages[ name ] ) {
 438                              availableMenuItemContainers[ name ].find( '.accordion-section-title' )
 439                                  .addClass( 'cannot-expand' )
 440                                  .removeClass( 'loading' )
 441                                  .find( '.accordion-section-title > button' )
 442                                  .prop( 'tabIndex', -1 );
 443                          }
 444                          self.pages[ name ] = -1;
 445                          return;
 446                      } else if ( ( 'post_type:page' === name ) && ( ! availableMenuItemContainers[ name ].hasClass( 'open' ) ) ) {
 447                          availableMenuItemContainers[ name ].find( '.accordion-section-title > button' ).click();
 448                      }
 449                      typeItems = new api.Menus.AvailableItemCollection( typeItems ); // @todo Why is this collection created and then thrown away?
 450                      self.collection.add( typeItems.models );
 451                      typeInner = availableMenuItemContainers[ name ].find( '.available-menu-items-list' );
 452                      typeItems.each( function( menuItem ) {
 453                          typeInner.append( itemTemplate( menuItem.attributes ) );
 454                      } );
 455                      self.pages[ name ] += 1;
 456                  });
 457              });
 458              request.fail(function( data ) {
 459                  if ( typeof console !== 'undefined' && console.error ) {
 460                      console.error( data );
 461                  }
 462              });
 463              request.always(function() {
 464                  _.each( availableMenuItemContainers, function( container ) {
 465                      container.find( '.accordion-section-title' ).removeClass( 'loading' );
 466                  } );
 467                  self.loading = false;
 468              });
 469          },
 470  
 471          // Adjust the height of each section of items to fit the screen.
 472          itemSectionHeight: function() {
 473              var sections, lists, totalHeight, accordionHeight, diff;
 474              totalHeight = window.innerHeight;
 475              sections = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .accordion-section-content' );
 476              lists = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .available-menu-items-list:not(":only-child")' );
 477              accordionHeight =  46 * ( 1 + sections.length ) + 14; // Magic numbers.
 478              diff = totalHeight - accordionHeight;
 479              if ( 120 < diff && 290 > diff ) {
 480                  sections.css( 'max-height', diff );
 481                  lists.css( 'max-height', ( diff - 60 ) );
 482              }
 483          },
 484  
 485          // Highlights a menu item.
 486          select: function( menuitemTpl ) {
 487              this.selected = $( menuitemTpl );
 488              this.selected.siblings( '.menu-item-tpl' ).removeClass( 'selected' );
 489              this.selected.addClass( 'selected' );
 490          },
 491  
 492          // Highlights a menu item on focus.
 493          focus: function( event ) {
 494              this.select( $( event.currentTarget ) );
 495          },
 496  
 497          // Submit handler for keypress and click on menu item.
 498          _submit: function( event ) {
 499              // Only proceed with keypress if it is Enter or Spacebar
 500              if ( 'keypress' === event.type && ( 13 !== event.which && 32 !== event.which ) ) {
 501                  return;
 502              }
 503  
 504              this.submit( $( event.currentTarget ) );
 505          },
 506  
 507          // Adds a selected menu item to the menu.
 508          submit: function( menuitemTpl ) {
 509              var menuitemId, menu_item;
 510  
 511              if ( ! menuitemTpl ) {
 512                  menuitemTpl = this.selected;
 513              }
 514  
 515              if ( ! menuitemTpl || ! this.currentMenuControl ) {
 516                  return;
 517              }
 518  
 519              this.select( menuitemTpl );
 520  
 521              menuitemId = $( this.selected ).data( 'menu-item-id' );
 522              menu_item = this.collection.findWhere( { id: menuitemId } );
 523              if ( ! menu_item ) {
 524                  return;
 525              }
 526  
 527              this.currentMenuControl.addItemToMenu( menu_item.attributes );
 528  
 529              $( menuitemTpl ).find( '.menu-item-handle' ).addClass( 'item-added' );
 530          },
 531  
 532          // Submit handler for keypress and click on custom menu item.
 533          _submitLink: function( event ) {
 534              // Only proceed with keypress if it is Enter.
 535              if ( 'keypress' === event.type && 13 !== event.which ) {
 536                  return;
 537              }
 538  
 539              this.submitLink();
 540          },
 541  
 542          // Adds the custom menu item to the menu.
 543          submitLink: function() {
 544              var menuItem,
 545                  itemName = $( '#custom-menu-item-name' ),
 546                  itemUrl = $( '#custom-menu-item-url' ),
 547                  url = itemUrl.val().trim(),
 548                  urlRegex;
 549  
 550              if ( ! this.currentMenuControl ) {
 551                  return;
 552              }
 553  
 554              /*
 555               * Allow URLs including:
 556               * - http://example.com/
 557               * - //example.com
 558               * - /directory/
 559               * - ?query-param
 560               * - #target
 561               * - mailto:foo@example.com
 562               *
 563               * Any further validation will be handled on the server when the setting is attempted to be saved,
 564               * so this pattern does not need to be complete.
 565               */
 566              urlRegex = /^((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#)/;
 567  
 568              if ( '' === itemName.val() ) {
 569                  itemName.addClass( 'invalid' );
 570                  return;
 571              } else if ( ! urlRegex.test( url ) ) {
 572                  itemUrl.addClass( 'invalid' );
 573                  return;
 574              }
 575  
 576              menuItem = {
 577                  'title': itemName.val(),
 578                  'url': url,
 579                  'type': 'custom',
 580                  'type_label': api.Menus.data.l10n.custom_label,
 581                  'object': 'custom'
 582              };
 583  
 584              this.currentMenuControl.addItemToMenu( menuItem );
 585  
 586              // Reset the custom link form.
 587              itemUrl.val( '' ).attr( 'placeholder', 'https://' );
 588              itemName.val( '' );
 589          },
 590  
 591          /**
 592           * Submit handler for keypress (enter) on field and click on button.
 593           *
 594           * @since 4.7.0
 595           * @private
 596           *
 597           * @param {jQuery.Event} event Event.
 598           * @return {void}
 599           */
 600          _submitNew: function( event ) {
 601              var container;
 602  
 603              // Only proceed with keypress if it is Enter.
 604              if ( 'keypress' === event.type && 13 !== event.which ) {
 605                  return;
 606              }
 607  
 608              if ( this.addingNew ) {
 609                  return;
 610              }
 611  
 612              container = $( event.target ).closest( '.accordion-section' );
 613  
 614              this.submitNew( container );
 615          },
 616  
 617          /**
 618           * Creates a new object and adds an associated menu item to the menu.
 619           *
 620           * @since 4.7.0
 621           * @private
 622           *
 623           * @param {jQuery} container
 624           * @return {void}
 625           */
 626          submitNew: function( container ) {
 627              var panel = this,
 628                  itemName = container.find( '.create-item-input' ),
 629                  title = itemName.val(),
 630                  dataContainer = container.find( '.available-menu-items-list' ),
 631                  itemType = dataContainer.data( 'type' ),
 632                  itemObject = dataContainer.data( 'object' ),
 633                  itemTypeLabel = dataContainer.data( 'type_label' ),
 634                  promise;
 635  
 636              if ( ! this.currentMenuControl ) {
 637                  return;
 638              }
 639  
 640              // Only posts are supported currently.
 641              if ( 'post_type' !== itemType ) {
 642                  return;
 643              }
 644  
 645              if ( '' === $.trim( itemName.val() ) ) {
 646                  itemName.addClass( 'invalid' );
 647                  itemName.focus();
 648                  return;
 649              } else {
 650                  itemName.removeClass( 'invalid' );
 651                  container.find( '.accordion-section-title' ).addClass( 'loading' );
 652              }
 653  
 654              panel.addingNew = true;
 655              itemName.attr( 'disabled', 'disabled' );
 656              promise = api.Menus.insertAutoDraftPost( {
 657                  post_title: title,
 658                  post_type: itemObject
 659              } );
 660              promise.done( function( data ) {
 661                  var availableItem, $content, itemElement;
 662                  availableItem = new api.Menus.AvailableItemModel( {
 663                      'id': 'post-' + data.post_id, // Used for available menu item Backbone models.
 664                      'title': itemName.val(),
 665                      'type': itemType,
 666                      'type_label': itemTypeLabel,
 667                      'object': itemObject,
 668                      'object_id': data.post_id,
 669                      'url': data.url
 670                  } );
 671  
 672                  // Add new item to menu.
 673                  panel.currentMenuControl.addItemToMenu( availableItem.attributes );
 674  
 675                  // Add the new item to the list of available items.
 676                  api.Menus.availableMenuItemsPanel.collection.add( availableItem );
 677                  $content = container.find( '.available-menu-items-list' );
 678                  itemElement = $( wp.template( 'available-menu-item' )( availableItem.attributes ) );
 679                  itemElement.find( '.menu-item-handle:first' ).addClass( 'item-added' );
 680                  $content.prepend( itemElement );
 681                  $content.scrollTop();
 682  
 683                  // Reset the create content form.
 684                  itemName.val( '' ).removeAttr( 'disabled' );
 685                  panel.addingNew = false;
 686                  container.find( '.accordion-section-title' ).removeClass( 'loading' );
 687              } );
 688          },
 689  
 690          // Opens the panel.
 691          open: function( menuControl ) {
 692              var panel = this, close;
 693  
 694              this.currentMenuControl = menuControl;
 695  
 696              this.itemSectionHeight();
 697  
 698              if ( api.section.has( 'publish_settings' ) ) {
 699                  api.section( 'publish_settings' ).collapse();
 700              }
 701  
 702              $( 'body' ).addClass( 'adding-menu-items' );
 703  
 704              close = function() {
 705                  panel.close();
 706                  $( this ).off( 'click', close );
 707              };
 708              $( '#customize-preview' ).on( 'click', close );
 709  
 710              // Collapse all controls.
 711              _( this.currentMenuControl.getMenuItemControls() ).each( function( control ) {
 712                  control.collapseForm();
 713              } );
 714  
 715              this.$el.find( '.selected' ).removeClass( 'selected' );
 716  
 717              this.$search.focus();
 718          },
 719  
 720          // Closes the panel
 721          close: function( options ) {
 722              options = options || {};
 723  
 724              if ( options.returnFocus && this.currentMenuControl ) {
 725                  this.currentMenuControl.container.find( '.add-new-menu-item' ).focus();
 726              }
 727  
 728              this.currentMenuControl = null;
 729              this.selected = null;
 730  
 731              $( 'body' ).removeClass( 'adding-menu-items' );
 732              $( '#available-menu-items .menu-item-handle.item-added' ).removeClass( 'item-added' );
 733  
 734              this.$search.val( '' ).trigger( 'keyup' );
 735          },
 736  
 737          // Add a few keyboard enhancements to the panel.
 738          keyboardAccessible: function( event ) {
 739              var isEnter = ( 13 === event.which ),
 740                  isEsc = ( 27 === event.which ),
 741                  isBackTab = ( 9 === event.which && event.shiftKey ),
 742                  isSearchFocused = $( event.target ).is( this.$search );
 743  
 744              // If enter pressed but nothing entered, don't do anything
 745              if ( isEnter && ! this.$search.val() ) {
 746                  return;
 747              }
 748  
 749              if ( isSearchFocused && isBackTab ) {
 750                  this.currentMenuControl.container.find( '.add-new-menu-item' ).focus();
 751                  event.preventDefault(); // Avoid additional back-tab.
 752              } else if ( isEsc ) {
 753                  this.close( { returnFocus: true } );
 754              }
 755          }
 756      });
 757  
 758      /**
 759       * wp.customize.Menus.MenusPanel
 760       *
 761       * Customizer panel for menus. This is used only for screen options management.
 762       * Note that 'menus' must match the WP_Customize_Menu_Panel::$type.
 763       *
 764       * @class    wp.customize.Menus.MenusPanel
 765       * @augments wp.customize.Panel
 766       */
 767      api.Menus.MenusPanel = api.Panel.extend(/** @lends wp.customize.Menus.MenusPanel.prototype */{
 768  
 769          attachEvents: function() {
 770              api.Panel.prototype.attachEvents.call( this );
 771  
 772              var panel = this,
 773                  panelMeta = panel.container.find( '.panel-meta' ),
 774                  help = panelMeta.find( '.customize-help-toggle' ),
 775                  content = panelMeta.find( '.customize-panel-description' ),
 776                  options = $( '#screen-options-wrap' ),
 777                  button = panelMeta.find( '.customize-screen-options-toggle' );
 778              button.on( 'click keydown', function( event ) {
 779                  if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
 780                      return;
 781                  }
 782                  event.preventDefault();
 783  
 784                  // Hide description
 785                  if ( content.not( ':hidden' ) ) {
 786                      content.slideUp( 'fast' );
 787                      help.attr( 'aria-expanded', 'false' );
 788                  }
 789  
 790                  if ( 'true' === button.attr( 'aria-expanded' ) ) {
 791                      button.attr( 'aria-expanded', 'false' );
 792                      panelMeta.removeClass( 'open' );
 793                      panelMeta.removeClass( 'active-menu-screen-options' );
 794                      options.slideUp( 'fast' );
 795                  } else {
 796                      button.attr( 'aria-expanded', 'true' );
 797                      panelMeta.addClass( 'open' );
 798                      panelMeta.addClass( 'active-menu-screen-options' );
 799                      options.slideDown( 'fast' );
 800                  }
 801  
 802                  return false;
 803              } );
 804  
 805              // Help toggle
 806              help.on( 'click keydown', function( event ) {
 807                  if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
 808                      return;
 809                  }
 810                  event.preventDefault();
 811  
 812                  if ( 'true' === button.attr( 'aria-expanded' ) ) {
 813                      button.attr( 'aria-expanded', 'false' );
 814                      help.attr( 'aria-expanded', 'true' );
 815                      panelMeta.addClass( 'open' );
 816                      panelMeta.removeClass( 'active-menu-screen-options' );
 817                      options.slideUp( 'fast' );
 818                      content.slideDown( 'fast' );
 819                  }
 820              } );
 821          },
 822  
 823          /**
 824           * Update field visibility when clicking on the field toggles.
 825           */
 826          ready: function() {
 827              var panel = this;
 828              panel.container.find( '.hide-column-tog' ).click( function() {
 829                  panel.saveManageColumnsState();
 830              });
 831  
 832              // Inject additional heading into the menu locations section's head container.
 833              api.section( 'menu_locations', function( section ) {
 834                  section.headContainer.prepend(
 835                      wp.template( 'nav-menu-locations-header' )( api.Menus.data )
 836                  );
 837              } );
 838          },
 839  
 840          /**
 841           * Save hidden column states.
 842           *
 843           * @since 4.3.0
 844           * @private
 845           *
 846           * @return {void}
 847           */
 848          saveManageColumnsState: _.debounce( function() {
 849              var panel = this;
 850              if ( panel._updateHiddenColumnsRequest ) {
 851                  panel._updateHiddenColumnsRequest.abort();
 852              }
 853  
 854              panel._updateHiddenColumnsRequest = wp.ajax.post( 'hidden-columns', {
 855                  hidden: panel.hidden(),
 856                  screenoptionnonce: $( '#screenoptionnonce' ).val(),
 857                  page: 'nav-menus'
 858              } );
 859              panel._updateHiddenColumnsRequest.always( function() {
 860                  panel._updateHiddenColumnsRequest = null;
 861              } );
 862          }, 2000 ),
 863  
 864          /**
 865           * @deprecated Since 4.7.0 now that the nav_menu sections are responsible for toggling the classes on their own containers.
 866           */
 867          checked: function() {},
 868  
 869          /**
 870           * @deprecated Since 4.7.0 now that the nav_menu sections are responsible for toggling the classes on their own containers.
 871           */
 872          unchecked: function() {},
 873  
 874          /**
 875           * Get hidden fields.
 876           *
 877           * @since 4.3.0
 878           * @private
 879           *
 880           * @return {Array} Fields (columns) that are hidden.
 881           */
 882          hidden: function() {
 883              return $( '.hide-column-tog' ).not( ':checked' ).map( function() {
 884                  var id = this.id;
 885                  return id.substring( 0, id.length - 5 );
 886              }).get().join( ',' );
 887          }
 888      } );
 889  
 890      /**
 891       * wp.customize.Menus.MenuSection
 892       *
 893       * Customizer section for menus. This is used only for lazy-loading child controls.
 894       * Note that 'nav_menu' must match the WP_Customize_Menu_Section::$type.
 895       *
 896       * @class    wp.customize.Menus.MenuSection
 897       * @augments wp.customize.Section
 898       */
 899      api.Menus.MenuSection = api.Section.extend(/** @lends wp.customize.Menus.MenuSection.prototype */{
 900  
 901          /**
 902           * Initialize.
 903           *
 904           * @since 4.3.0
 905           *
 906           * @param {String} id
 907           * @param {Object} options
 908           */
 909          initialize: function( id, options ) {
 910              var section = this;
 911              api.Section.prototype.initialize.call( section, id, options );
 912              section.deferred.initSortables = $.Deferred();
 913          },
 914  
 915          /**
 916           * Ready.
 917           */
 918          ready: function() {
 919              var section = this, fieldActiveToggles, handleFieldActiveToggle;
 920  
 921              if ( 'undefined' === typeof section.params.menu_id ) {
 922                  throw new Error( 'params.menu_id was not defined' );
 923              }
 924  
 925              /*
 926               * Since newly created sections won't be registered in PHP, we need to prevent the
 927               * preview's sending of the activeSections to result in this control
 928               * being deactivated when the preview refreshes. So we can hook onto
 929               * the setting that has the same ID and its presence can dictate
 930               * whether the section is active.
 931               */
 932              section.active.validate = function() {
 933                  if ( ! api.has( section.id ) ) {
 934                      return false;
 935                  }
 936                  return !! api( section.id ).get();
 937              };
 938  
 939              section.populateControls();
 940  
 941              section.navMenuLocationSettings = {};
 942              section.assignedLocations = new api.Value( [] );
 943  
 944              api.each(function( setting, id ) {
 945                  var matches = id.match( /^nav_menu_locations\[(.+?)]/ );
 946                  if ( matches ) {
 947                      section.navMenuLocationSettings[ matches[1] ] = setting;
 948                      setting.bind( function() {
 949                          section.refreshAssignedLocations();
 950                      });
 951                  }
 952              });
 953  
 954              section.assignedLocations.bind(function( to ) {
 955                  section.updateAssignedLocationsInSectionTitle( to );
 956              });
 957  
 958              section.refreshAssignedLocations();
 959  
 960              api.bind( 'pane-contents-reflowed', function() {
 961                  // Skip menus that have been removed.
 962                  if ( ! section.contentContainer.parent().length ) {
 963                      return;
 964                  }
 965                  section.container.find( '.menu-item .menu-item-reorder-nav button' ).attr({ 'tabindex': '0', 'aria-hidden': 'false' });
 966                  section.container.find( '.menu-item.move-up-disabled .menus-move-up' ).attr({ 'tabindex': '-1', 'aria-hidden': 'true' });
 967                  section.container.find( '.menu-item.move-down-disabled .menus-move-down' ).attr({ 'tabindex': '-1', 'aria-hidden': 'true' });
 968                  section.container.find( '.menu-item.move-left-disabled .menus-move-left' ).attr({ 'tabindex': '-1', 'aria-hidden': 'true' });
 969                  section.container.find( '.menu-item.move-right-disabled .menus-move-right' ).attr({ 'tabindex': '-1', 'aria-hidden': 'true' });
 970              } );
 971  
 972              /**
 973               * Update the active field class for the content container for a given checkbox toggle.
 974               *
 975               * @this {jQuery}
 976               * @return {void}
 977               */
 978              handleFieldActiveToggle = function() {
 979                  var className = 'field-' + $( this ).val() + '-active';
 980                  section.contentContainer.toggleClass( className, $( this ).prop( 'checked' ) );
 981              };
 982              fieldActiveToggles = api.panel( 'nav_menus' ).contentContainer.find( '.metabox-prefs:first' ).find( '.hide-column-tog' );
 983              fieldActiveToggles.each( handleFieldActiveToggle );
 984              fieldActiveToggles.on( 'click', handleFieldActiveToggle );
 985          },
 986  
 987          populateControls: function() {
 988              var section = this,
 989                  menuNameControlId,
 990                  menuLocationsControlId,
 991                  menuAutoAddControlId,
 992                  menuDeleteControlId,
 993                  menuControl,
 994                  menuNameControl,
 995                  menuLocationsControl,
 996                  menuAutoAddControl,
 997                  menuDeleteControl;
 998  
 999              // Add the control for managing the menu name.
1000              menuNameControlId = section.id + '[name]';
1001              menuNameControl = api.control( menuNameControlId );
1002              if ( ! menuNameControl ) {
1003                  menuNameControl = new api.controlConstructor.nav_menu_name( menuNameControlId, {
1004                      type: 'nav_menu_name',
1005                      label: api.Menus.data.l10n.menuNameLabel,
1006                      section: section.id,
1007                      priority: 0,
1008                      settings: {
1009                          'default': section.id
1010                      }
1011                  } );
1012                  api.control.add( menuNameControl );
1013                  menuNameControl.active.set( true );
1014              }
1015  
1016              // Add the menu control.
1017              menuControl = api.control( section.id );
1018              if ( ! menuControl ) {
1019                  menuControl = new api.controlConstructor.nav_menu( section.id, {
1020                      type: 'nav_menu',
1021                      section: section.id,
1022                      priority: 998,
1023                      settings: {
1024                          'default': section.id
1025                      },
1026                      menu_id: section.params.menu_id
1027                  } );
1028                  api.control.add( menuControl );
1029                  menuControl.active.set( true );
1030              }
1031  
1032              // Add the menu locations control.
1033              menuLocationsControlId = section.id + '[locations]';
1034              menuLocationsControl = api.control( menuLocationsControlId );
1035              if ( ! menuLocationsControl ) {
1036                  menuLocationsControl = new api.controlConstructor.nav_menu_locations( menuLocationsControlId, {
1037                      section: section.id,
1038                      priority: 999,
1039                      settings: {
1040                          'default': section.id
1041                      },
1042                      menu_id: section.params.menu_id
1043                  } );
1044                  api.control.add( menuLocationsControl.id, menuLocationsControl );
1045                  menuControl.active.set( true );
1046              }
1047  
1048              // Add the control for managing the menu auto_add.
1049              menuAutoAddControlId = section.id + '[auto_add]';
1050              menuAutoAddControl = api.control( menuAutoAddControlId );
1051              if ( ! menuAutoAddControl ) {
1052                  menuAutoAddControl = new api.controlConstructor.nav_menu_auto_add( menuAutoAddControlId, {
1053                      type: 'nav_menu_auto_add',
1054                      label: '',
1055                      section: section.id,
1056                      priority: 1000,
1057                      settings: {
1058                          'default': section.id
1059                      }
1060                  } );
1061                  api.control.add( menuAutoAddControl );
1062                  menuAutoAddControl.active.set( true );
1063              }
1064  
1065              // Add the control for deleting the menu
1066              menuDeleteControlId = section.id + '[delete]';
1067              menuDeleteControl = api.control( menuDeleteControlId );
1068              if ( ! menuDeleteControl ) {
1069                  menuDeleteControl = new api.Control( menuDeleteControlId, {
1070                      section: section.id,
1071                      priority: 1001,
1072                      templateId: 'nav-menu-delete-button'
1073                  } );
1074                  api.control.add( menuDeleteControl.id, menuDeleteControl );
1075                  menuDeleteControl.active.set( true );
1076                  menuDeleteControl.deferred.embedded.done( function () {
1077                      menuDeleteControl.container.find( 'button' ).on( 'click', function() {
1078                          var menuId = section.params.menu_id;
1079                          var menuControl = api.Menus.getMenuControl( menuId );
1080                          menuControl.setting.set( false );
1081                      });
1082                  } );
1083              }
1084          },
1085  
1086          /**
1087           *
1088           */
1089          refreshAssignedLocations: function() {
1090              var section = this,
1091                  menuTermId = section.params.menu_id,
1092                  currentAssignedLocations = [];
1093              _.each( section.navMenuLocationSettings, function( setting, themeLocation ) {
1094                  if ( setting() === menuTermId ) {
1095                      currentAssignedLocations.push( themeLocation );
1096                  }
1097              });
1098              section.assignedLocations.set( currentAssignedLocations );
1099          },
1100  
1101          /**
1102           * @param {Array} themeLocationSlugs Theme location slugs.
1103           */
1104          updateAssignedLocationsInSectionTitle: function( themeLocationSlugs ) {
1105              var section = this,
1106                  $title;
1107  
1108              $title = section.container.find( '.accordion-section-title:first' );
1109              $title.find( '.menu-in-location' ).remove();
1110              _.each( themeLocationSlugs, function( themeLocationSlug ) {
1111                  var $label, locationName;
1112                  $label = $( '<span class="menu-in-location"></span>' );
1113                  locationName = api.Menus.data.locationSlugMappedToName[ themeLocationSlug ];
1114                  $label.text( api.Menus.data.l10n.menuLocation.replace( '%s', locationName ) );
1115                  $title.append( $label );
1116              });
1117  
1118              section.container.toggleClass( 'assigned-to-menu-location', 0 !== themeLocationSlugs.length );
1119  
1120          },
1121  
1122          onChangeExpanded: function( expanded, args ) {
1123              var section = this, completeCallback;
1124  
1125              if ( expanded ) {
1126                  wpNavMenu.menuList = section.contentContainer;
1127                  wpNavMenu.targetList = wpNavMenu.menuList;
1128  
1129                  // Add attributes needed by wpNavMenu
1130                  $( '#menu-to-edit' ).removeAttr( 'id' );
1131                  wpNavMenu.menuList.attr( 'id', 'menu-to-edit' ).addClass( 'menu' );
1132  
1133                  _.each( api.section( section.id ).controls(), function( control ) {
1134                      if ( 'nav_menu_item' === control.params.type ) {
1135                          control.actuallyEmbed();
1136                      }
1137                  } );
1138  
1139                  // Make sure Sortables is initialized after the section has been expanded to prevent `offset` issues.
1140                  if ( args.completeCallback ) {
1141                      completeCallback = args.completeCallback;
1142                  }
1143                  args.completeCallback = function() {
1144                      if ( 'resolved' !== section.deferred.initSortables.state() ) {
1145                          wpNavMenu.initSortables(); // Depends on menu-to-edit ID being set above.
1146                          section.deferred.initSortables.resolve( wpNavMenu.menuList ); // Now MenuControl can extend the sortable.
1147  
1148                          // @todo Note that wp.customize.reflowPaneContents() is debounced, so this immediate change will show a slight flicker while priorities get updated.
1149                          api.control( 'nav_menu[' + String( section.params.menu_id ) + ']' ).reflowMenuItems();
1150                      }
1151                      if ( _.isFunction( completeCallback ) ) {
1152                          completeCallback();
1153                      }
1154                  };
1155              }
1156              api.Section.prototype.onChangeExpanded.call( section, expanded, args );
1157          },
1158  
1159          /**
1160           * Highlight how a user may create new menu items.
1161           *
1162           * This method reminds the user to create new menu items and how.
1163           * It's exposed this way because this class knows best which UI needs
1164           * highlighted but those expanding this section know more about why and
1165           * when the affordance should be highlighted.
1166           *
1167           * @since 4.9.0
1168           *
1169           * @return {void}
1170           */
1171          highlightNewItemButton: function() {
1172              api.utils.highlightButton( this.contentContainer.find( '.add-new-menu-item' ), { delay: 2000 } );
1173          }
1174      });
1175  
1176      /**
1177       * Create a nav menu setting and section.
1178       *
1179       * @since 4.9.0
1180       *
1181       * @param {string} [name=''] Nav menu name.
1182       * @return {wp.customize.Menus.MenuSection} Added nav menu.
1183       */
1184      api.Menus.createNavMenu = function createNavMenu( name ) {
1185          var customizeId, placeholderId, setting;
1186          placeholderId = api.Menus.generatePlaceholderAutoIncrementId();
1187  
1188          customizeId = 'nav_menu[' + String( placeholderId ) + ']';
1189  
1190          // Register the menu control setting.
1191          setting = api.create( customizeId, customizeId, {}, {
1192              type: 'nav_menu',
1193              transport: api.Menus.data.settingTransport,
1194              previewer: api.previewer
1195          } );
1196          setting.set( $.extend(
1197              {},
1198              api.Menus.data.defaultSettingValues.nav_menu,
1199              {
1200                  name: name || ''
1201              }
1202          ) );
1203  
1204          /*
1205           * Add the menu section (and its controls).
1206           * Note that this will automatically create the required controls
1207           * inside via the Section's ready method.
1208           */
1209          return api.section.add( new api.Menus.MenuSection( customizeId, {
1210              panel: 'nav_menus',
1211              title: displayNavMenuName( name ),
1212              customizeAction: api.Menus.data.l10n.customizingMenus,
1213              priority: 10,
1214              menu_id: placeholderId
1215          } ) );
1216      };
1217  
1218      /**
1219       * wp.customize.Menus.NewMenuSection
1220       *
1221       * Customizer section for new menus.
1222       *
1223       * @class    wp.customize.Menus.NewMenuSection
1224       * @augments wp.customize.Section
1225       */
1226      api.Menus.NewMenuSection = api.Section.extend(/** @lends wp.customize.Menus.NewMenuSection.prototype */{
1227  
1228          /**
1229           * Add behaviors for the accordion section.
1230           *
1231           * @since 4.3.0
1232           */
1233          attachEvents: function() {
1234              var section = this,
1235                  container = section.container,
1236                  contentContainer = section.contentContainer,
1237                  navMenuSettingPattern = /^nav_menu\[/;
1238  
1239              section.headContainer.find( '.accordion-section-title' ).replaceWith(
1240                  wp.template( 'nav-menu-create-menu-section-title' )
1241              );
1242  
1243              /*
1244               * We have to manually handle section expanded because we do not
1245               * apply the `accordion-section-title` class to this button-driven section.
1246               */
1247              container.on( 'click', '.customize-add-menu-button', function() {
1248                  section.expand();
1249              });
1250  
1251              contentContainer.on( 'keydown', '.menu-name-field', function( event ) {
1252                  if ( 13 === event.which ) { // Enter.
1253                      section.submit();
1254                  }
1255              } );
1256              contentContainer.on( 'click', '#customize-new-menu-submit', function( event ) {
1257                  section.submit();
1258                  event.stopPropagation();
1259                  event.preventDefault();
1260              } );
1261  
1262              /**
1263               * Get number of non-deleted nav menus.
1264               *
1265               * @since 4.9.0
1266               * @return {number} Count.
1267               */
1268  			function getNavMenuCount() {
1269                  var count = 0;
1270                  api.each( function( setting ) {
1271                      if ( navMenuSettingPattern.test( setting.id ) && false !== setting.get() ) {
1272                          count += 1;
1273                      }
1274                  } );
1275                  return count;
1276              }
1277  
1278              /**
1279               * Update visibility of notice to prompt users to create menus.
1280               *
1281               * @since 4.9.0
1282               * @return {void}
1283               */
1284  			function updateNoticeVisibility() {
1285                  container.find( '.add-new-menu-notice' ).prop( 'hidden', getNavMenuCount() > 0 );
1286              }
1287  
1288              /**
1289               * Handle setting addition.
1290               *
1291               * @since 4.9.0
1292               * @param {wp.customize.Setting} setting - Added setting.
1293               * @return {void}
1294               */
1295  			function addChangeEventListener( setting ) {
1296                  if ( navMenuSettingPattern.test( setting.id ) ) {
1297                      setting.bind( updateNoticeVisibility );
1298                      updateNoticeVisibility();
1299                  }
1300              }
1301  
1302              /**
1303               * Handle setting removal.
1304               *
1305               * @since 4.9.0
1306               * @param {wp.customize.Setting} setting - Removed setting.
1307               * @return {void}
1308               */
1309  			function removeChangeEventListener( setting ) {
1310                  if ( navMenuSettingPattern.test( setting.id ) ) {
1311                      setting.unbind( updateNoticeVisibility );
1312                      updateNoticeVisibility();
1313                  }
1314              }
1315  
1316              api.each( addChangeEventListener );
1317              api.bind( 'add', addChangeEventListener );
1318              api.bind( 'removed', removeChangeEventListener );
1319              updateNoticeVisibility();
1320  
1321              api.Section.prototype.attachEvents.apply( section, arguments );
1322          },
1323  
1324          /**
1325           * Set up the control.
1326           *
1327           * @since 4.9.0
1328           */
1329          ready: function() {
1330              this.populateControls();
1331          },
1332  
1333          /**
1334           * Create the controls for this section.
1335           *
1336           * @since 4.9.0
1337           */
1338          populateControls: function() {
1339              var section = this,
1340                  menuNameControlId,
1341                  menuLocationsControlId,
1342                  newMenuSubmitControlId,
1343                  menuNameControl,
1344                  menuLocationsControl,
1345                  newMenuSubmitControl;
1346  
1347              menuNameControlId = section.id + '[name]';
1348              menuNameControl = api.control( menuNameControlId );
1349              if ( ! menuNameControl ) {
1350                  menuNameControl = new api.controlConstructor.nav_menu_name( menuNameControlId, {
1351                      label: api.Menus.data.l10n.menuNameLabel,
1352                      description: api.Menus.data.l10n.newMenuNameDescription,
1353                      section: section.id,
1354                      priority: 0
1355                  } );
1356                  api.control.add( menuNameControl.id, menuNameControl );
1357                  menuNameControl.active.set( true );
1358              }
1359  
1360              menuLocationsControlId = section.id + '[locations]';
1361              menuLocationsControl = api.control( menuLocationsControlId );
1362              if ( ! menuLocationsControl ) {
1363                  menuLocationsControl = new api.controlConstructor.nav_menu_locations( menuLocationsControlId, {
1364                      section: section.id,
1365                      priority: 1,
1366                      menu_id: '',
1367                      isCreating: true
1368                  } );
1369                  api.control.add( menuLocationsControlId, menuLocationsControl );
1370                  menuLocationsControl.active.set( true );
1371              }
1372  
1373              newMenuSubmitControlId = section.id + '[submit]';
1374              newMenuSubmitControl = api.control( newMenuSubmitControlId );
1375              if ( !newMenuSubmitControl ) {
1376                  newMenuSubmitControl = new api.Control( newMenuSubmitControlId, {
1377                      section: section.id,
1378                      priority: 1,
1379                      templateId: 'nav-menu-submit-new-button'
1380                  } );
1381                  api.control.add( newMenuSubmitControlId, newMenuSubmitControl );
1382                  newMenuSubmitControl.active.set( true );
1383              }
1384          },
1385  
1386          /**
1387           * Create the new menu with name and location supplied by the user.
1388           *
1389           * @since 4.9.0
1390           */
1391          submit: function() {
1392              var section = this,
1393                  contentContainer = section.contentContainer,
1394                  nameInput = contentContainer.find( '.menu-name-field' ).first(),
1395                  name = nameInput.val(),
1396                  menuSection;
1397  
1398              if ( ! name ) {
1399                  nameInput.addClass( 'invalid' );
1400                  nameInput.focus();
1401                  return;
1402              }
1403  
1404              menuSection = api.Menus.createNavMenu( name );
1405  
1406              // Clear name field.
1407              nameInput.val( '' );
1408              nameInput.removeClass( 'invalid' );
1409  
1410              contentContainer.find( '.assigned-menu-location input[type=checkbox]' ).each( function() {
1411                  var checkbox = $( this ),
1412                  navMenuLocationSetting;
1413  
1414                  if ( checkbox.prop( 'checked' ) ) {
1415                      navMenuLocationSetting = api( 'nav_menu_locations[' + checkbox.data( 'location-id' ) + ']' );
1416                      navMenuLocationSetting.set( menuSection.params.menu_id );
1417  
1418                      // Reset state for next new menu
1419                      checkbox.prop( 'checked', false );
1420                  }
1421              } );
1422  
1423              wp.a11y.speak( api.Menus.data.l10n.menuAdded );
1424  
1425              // Focus on the new menu section.
1426              menuSection.focus( {
1427                  completeCallback: function() {
1428                      menuSection.highlightNewItemButton();
1429                  }
1430              } );
1431          },
1432  
1433          /**
1434           * Select a default location.
1435           *
1436           * This method selects a single location by default so we can support
1437           * creating a menu for a specific menu location.
1438           *
1439           * @since 4.9.0
1440           *
1441           * @param {string|null} locationId - The ID of the location to select. `null` clears all selections.
1442           * @return {void}
1443           */
1444          selectDefaultLocation: function( locationId ) {
1445              var locationControl = api.control( this.id + '[locations]' ),
1446                  locationSelections = {};
1447  
1448              if ( locationId !== null ) {
1449                  locationSelections[ locationId ] = true;
1450              }
1451  
1452              locationControl.setSelections( locationSelections );
1453          }
1454      });
1455  
1456      /**
1457       * wp.customize.Menus.MenuLocationControl
1458       *
1459       * Customizer control for menu locations (rendered as a <select>).
1460       * Note that 'nav_menu_location' must match the WP_Customize_Nav_Menu_Location_Control::$type.
1461       *
1462       * @class    wp.customize.Menus.MenuLocationControl
1463       * @augments wp.customize.Control
1464       */
1465      api.Menus.MenuLocationControl = api.Control.extend(/** @lends wp.customize.Menus.MenuLocationControl.prototype */{
1466          initialize: function( id, options ) {
1467              var control = this,
1468                  matches = id.match( /^nav_menu_locations\[(.+?)]/ );
1469              control.themeLocation = matches[1];
1470              api.Control.prototype.initialize.call( control, id, options );
1471          },
1472  
1473          ready: function() {
1474              var control = this, navMenuIdRegex = /^nav_menu\[(-?\d+)]/;
1475  
1476              // @todo It would be better if this was added directly on the setting itself, as opposed to the control.
1477              control.setting.validate = function( value ) {
1478                  if ( '' === value ) {
1479                      return 0;
1480                  } else {
1481                      return parseInt( value, 10 );
1482                  }
1483              };
1484  
1485              // Create and Edit menu buttons.
1486              control.container.find( '.create-menu' ).on( 'click', function() {
1487                  var addMenuSection = api.section( 'add_menu' );
1488                  addMenuSection.selectDefaultLocation( this.dataset.locationId );
1489                  addMenuSection.focus();
1490              } );
1491              control.container.find( '.edit-menu' ).on( 'click', function() {
1492                  var menuId = control.setting();
1493                  api.section( 'nav_menu[' + menuId + ']' ).focus();
1494              });
1495              control.setting.bind( 'change', function() {
1496                  var menuIsSelected = 0 !== control.setting();
1497                  control.container.find( '.create-menu' ).toggleClass( 'hidden', menuIsSelected );
1498                  control.container.find( '.edit-menu' ).toggleClass( 'hidden', ! menuIsSelected );
1499              });
1500  
1501              // Add/remove menus from the available options when they are added and removed.
1502              api.bind( 'add', function( setting ) {
1503                  var option, menuId, matches = setting.id.match( navMenuIdRegex );
1504                  if ( ! matches || false === setting() ) {
1505                      return;
1506                  }
1507                  menuId = matches[1];
1508                  option = new Option( displayNavMenuName( setting().name ), menuId );
1509                  control.container.find( 'select' ).append( option );
1510              });
1511              api.bind( 'remove', function( setting ) {
1512                  var menuId, matches = setting.id.match( navMenuIdRegex );
1513                  if ( ! matches ) {
1514                      return;
1515                  }
1516                  menuId = parseInt( matches[1], 10 );
1517                  if ( control.setting() === menuId ) {
1518                      control.setting.set( '' );
1519                  }
1520                  control.container.find( 'option[value=' + menuId + ']' ).remove();
1521              });
1522              api.bind( 'change', function( setting ) {
1523                  var menuId, matches = setting.id.match( navMenuIdRegex );
1524                  if ( ! matches ) {
1525                      return;
1526                  }
1527                  menuId = parseInt( matches[1], 10 );
1528                  if ( false === setting() ) {
1529                      if ( control.setting() === menuId ) {
1530                          control.setting.set( '' );
1531                      }
1532                      control.container.find( 'option[value=' + menuId + ']' ).remove();
1533                  } else {
1534                      control.container.find( 'option[value=' + menuId + ']' ).text( displayNavMenuName( setting().name ) );
1535                  }
1536              });
1537          }
1538      });
1539  
1540      api.Menus.MenuItemControl = api.Control.extend(/** @lends wp.customize.Menus.MenuItemControl.prototype */{
1541  
1542          /**
1543           * wp.customize.Menus.MenuItemControl
1544           *
1545           * Customizer control for menu items.
1546           * Note that 'menu_item' must match the WP_Customize_Menu_Item_Control::$type.
1547           *
1548           * @constructs wp.customize.Menus.MenuItemControl
1549           * @augments   wp.customize.Control
1550           *
1551           * @inheritDoc
1552           */
1553          initialize: function( id, options ) {
1554              var control = this;
1555              control.expanded = new api.Value( false );
1556              control.expandedArgumentsQueue = [];
1557              control.expanded.bind( function( expanded ) {
1558                  var args = control.expandedArgumentsQueue.shift();
1559                  args = $.extend( {}, control.defaultExpandedArguments, args );
1560                  control.onChangeExpanded( expanded, args );
1561              });
1562              api.Control.prototype.initialize.call( control, id, options );
1563              control.active.validate = function() {
1564                  var value, section = api.section( control.section() );
1565                  if ( section ) {
1566                      value = section.active();
1567                  } else {
1568                      value = false;
1569                  }
1570                  return value;
1571              };
1572          },
1573  
1574          /**
1575           * Override the embed() method to do nothing,
1576           * so that the control isn't embedded on load,
1577           * unless the containing section is already expanded.
1578           *
1579           * @since 4.3.0
1580           */
1581          embed: function() {
1582              var control = this,
1583                  sectionId = control.section(),
1584                  section;
1585              if ( ! sectionId ) {
1586                  return;
1587              }
1588              section = api.section( sectionId );
1589              if ( ( section && section.expanded() ) || api.settings.autofocus.control === control.id ) {
1590                  control.actuallyEmbed();
1591              }
1592          },
1593  
1594          /**
1595           * This function is called in Section.onChangeExpanded() so the control
1596           * will only get embedded when the Section is first expanded.
1597           *
1598           * @since 4.3.0
1599           */
1600          actuallyEmbed: function() {
1601              var control = this;
1602              if ( 'resolved' === control.deferred.embedded.state() ) {
1603                  return;
1604              }
1605              control.renderContent();
1606              control.deferred.embedded.resolve(); // This triggers control.ready().
1607          },
1608  
1609          /**
1610           * Set up the control.
1611           */
1612          ready: function() {
1613              if ( 'undefined' === typeof this.params.menu_item_id ) {
1614                  throw new Error( 'params.menu_item_id was not defined' );
1615              }
1616  
1617              this._setupControlToggle();
1618              this._setupReorderUI();
1619              this._setupUpdateUI();
1620              this._setupRemoveUI();
1621              this._setupLinksUI();
1622              this._setupTitleUI();
1623          },
1624  
1625          /**
1626           * Show/hide the settings when clicking on the menu item handle.
1627           */
1628          _setupControlToggle: function() {
1629              var control = this;
1630  
1631              this.container.find( '.menu-item-handle' ).on( 'click', function( e ) {
1632                  e.preventDefault();
1633                  e.stopPropagation();
1634                  var menuControl = control.getMenuControl(),
1635                      isDeleteBtn = $( e.target ).is( '.item-delete, .item-delete *' ),
1636                      isAddNewBtn = $( e.target ).is( '.add-new-menu-item, .add-new-menu-item *' );
1637  
1638                  if ( $( 'body' ).hasClass( 'adding-menu-items' ) && ! isDeleteBtn && ! isAddNewBtn ) {
1639                      api.Menus.availableMenuItemsPanel.close();
1640                  }
1641  
1642                  if ( menuControl.isReordering || menuControl.isSorting ) {
1643                      return;
1644                  }
1645                  control.toggleForm();
1646              } );
1647          },
1648  
1649          /**
1650           * Set up the menu-item-reorder-nav
1651           */
1652          _setupReorderUI: function() {
1653              var control = this, template, $reorderNav;
1654  
1655              template = wp.template( 'menu-item-reorder-nav' );
1656  
1657              // Add the menu item reordering elements to the menu item control.
1658              control.container.find( '.item-controls' ).after( template );
1659  
1660              // Handle clicks for up/down/left-right on the reorder nav.
1661              $reorderNav = control.container.find( '.menu-item-reorder-nav' );
1662              $reorderNav.find( '.menus-move-up, .menus-move-down, .menus-move-left, .menus-move-right' ).on( 'click', function() {
1663                  var moveBtn = $( this );
1664                  moveBtn.focus();
1665  
1666                  var isMoveUp = moveBtn.is( '.menus-move-up' ),
1667                      isMoveDown = moveBtn.is( '.menus-move-down' ),
1668                      isMoveLeft = moveBtn.is( '.menus-move-left' ),
1669                      isMoveRight = moveBtn.is( '.menus-move-right' );
1670  
1671                  if ( isMoveUp ) {
1672                      control.moveUp();
1673                  } else if ( isMoveDown ) {
1674                      control.moveDown();
1675                  } else if ( isMoveLeft ) {
1676                      control.moveLeft();
1677                  } else if ( isMoveRight ) {
1678                      control.moveRight();
1679                  }
1680  
1681                  moveBtn.focus(); // Re-focus after the container was moved.
1682              } );
1683          },
1684  
1685          /**
1686           * Set up event handlers for menu item updating.
1687           */
1688          _setupUpdateUI: function() {
1689              var control = this,
1690                  settingValue = control.setting(),
1691                  updateNotifications;
1692  
1693              control.elements = {};
1694              control.elements.url = new api.Element( control.container.find( '.edit-menu-item-url' ) );
1695              control.elements.title = new api.Element( control.container.find( '.edit-menu-item-title' ) );
1696              control.elements.attr_title = new api.Element( control.container.find( '.edit-menu-item-attr-title' ) );
1697              control.elements.target = new api.Element( control.container.find( '.edit-menu-item-target' ) );
1698              control.elements.classes = new api.Element( control.container.find( '.edit-menu-item-classes' ) );
1699              control.elements.xfn = new api.Element( control.container.find( '.edit-menu-item-xfn' ) );
1700              control.elements.description = new api.Element( control.container.find( '.edit-menu-item-description' ) );
1701              // @todo allow other elements, added by plugins, to be automatically picked up here; allow additional values to be added to setting array.
1702  
1703              _.each( control.elements, function( element, property ) {
1704                  element.bind(function( value ) {
1705                      if ( element.element.is( 'input[type=checkbox]' ) ) {
1706                          value = ( value ) ? element.element.val() : '';
1707                      }
1708  
1709                      var settingValue = control.setting();
1710                      if ( settingValue && settingValue[ property ] !== value ) {
1711                          settingValue = _.clone( settingValue );
1712                          settingValue[ property ] = value;
1713                          control.setting.set( settingValue );
1714                      }
1715                  });
1716                  if ( settingValue ) {
1717                      if ( ( property === 'classes' || property === 'xfn' ) && _.isArray( settingValue[ property ] ) ) {
1718                          element.set( settingValue[ property ].join( ' ' ) );
1719                      } else {
1720                          element.set( settingValue[ property ] );
1721                      }
1722                  }
1723              });
1724  
1725              control.setting.bind(function( to, from ) {
1726                  var itemId = control.params.menu_item_id,
1727                      followingSiblingItemControls = [],
1728                      childrenItemControls = [],
1729                      menuControl;
1730  
1731                  if ( false === to ) {
1732                      menuControl = api.control( 'nav_menu[' + String( from.nav_menu_term_id ) + ']' );
1733                      control.container.remove();
1734  
1735                      _.each( menuControl.getMenuItemControls(), function( otherControl ) {
1736                          if ( from.menu_item_parent === otherControl.setting().menu_item_parent && otherControl.setting().position > from.position ) {
1737                              followingSiblingItemControls.push( otherControl );
1738                          } else if ( otherControl.setting().menu_item_parent === itemId ) {
1739                              childrenItemControls.push( otherControl );
1740                          }
1741                      });
1742  
1743                      // Shift all following siblings by the number of children this item has.
1744                      _.each( followingSiblingItemControls, function( followingSiblingItemControl ) {
1745                          var value = _.clone( followingSiblingItemControl.setting() );
1746                          value.position += childrenItemControls.length;
1747                          followingSiblingItemControl.setting.set( value );
1748                      });
1749  
1750                      // Now move the children up to be the new subsequent siblings.
1751                      _.each( childrenItemControls, function( childrenItemControl, i ) {
1752                          var value = _.clone( childrenItemControl.setting() );
1753                          value.position = from.position + i;
1754                          value.menu_item_parent = from.menu_item_parent;
1755                          childrenItemControl.setting.set( value );
1756                      });
1757  
1758                      menuControl.debouncedReflowMenuItems();
1759                  } else {
1760                      // Update the elements' values to match the new setting properties.
1761                      _.each( to, function( value, key ) {
1762                          if ( control.elements[ key] ) {
1763                              control.elements[ key ].set( to[ key ] );
1764                          }
1765                      } );
1766                      control.container.find( '.menu-item-data-parent-id' ).val( to.menu_item_parent );
1767  
1768                      // Handle UI updates when the position or depth (parent) change.
1769                      if ( to.position !== from.position || to.menu_item_parent !== from.menu_item_parent ) {
1770                          control.getMenuControl().debouncedReflowMenuItems();
1771                      }
1772                  }
1773              });
1774  
1775              // Style the URL field as invalid when there is an invalid_url notification.
1776              updateNotifications = function() {
1777                  control.elements.url.element.toggleClass( 'invalid', control.setting.notifications.has( 'invalid_url' ) );
1778              };
1779              control.setting.notifications.bind( 'add', updateNotifications );
1780              control.setting.notifications.bind( 'removed', updateNotifications );
1781          },
1782  
1783          /**
1784           * Set up event handlers for menu item deletion.
1785           */
1786          _setupRemoveUI: function() {
1787              var control = this, $removeBtn;
1788  
1789              // Configure delete button.
1790              $removeBtn = control.container.find( '.item-delete' );
1791  
1792              $removeBtn.on( 'click', function() {
1793                  // Find an adjacent element to add focus to when this menu item goes away
1794                  var addingItems = true, $adjacentFocusTarget, $next, $prev;
1795  
1796                  if ( ! $( 'body' ).hasClass( 'adding-menu-items' ) ) {
1797                      addingItems = false;
1798                  }
1799  
1800                  $next = control.container.nextAll( '.customize-control-nav_menu_item:visible' ).first();
1801                  $prev = control.container.prevAll( '.customize-control-nav_menu_item:visible' ).first();
1802  
1803                  if ( $next.length ) {
1804                      $adjacentFocusTarget = $next.find( false === addingItems ? '.item-edit' : '.item-delete' ).first();
1805                  } else if ( $prev.length ) {
1806                      $adjacentFocusTarget = $prev.find( false === addingItems ? '.item-edit' : '.item-delete' ).first();
1807                  } else {
1808                      $adjacentFocusTarget = control.container.nextAll( '.customize-control-nav_menu' ).find( '.add-new-menu-item' ).first();
1809                  }
1810  
1811                  control.container.slideUp( function() {
1812                      control.setting.set( false );
1813                      wp.a11y.speak( api.Menus.data.l10n.itemDeleted );
1814                      $adjacentFocusTarget.focus(); // keyboard accessibility
1815                  } );
1816  
1817                  control.setting.set( false );
1818              } );
1819          },
1820  
1821          _setupLinksUI: function() {
1822              var $origBtn;
1823  
1824              // Configure original link.
1825              $origBtn = this.container.find( 'a.original-link' );
1826  
1827              $origBtn.on( 'click', function( e ) {
1828                  e.preventDefault();
1829                  api.previewer.previewUrl( e.target.toString() );
1830              } );
1831          },
1832  
1833          /**
1834           * Update item handle title when changed.
1835           */
1836          _setupTitleUI: function() {
1837              var control = this, titleEl;
1838  
1839              // Ensure that whitespace is trimmed on blur so placeholder can be shown.
1840              control.container.find( '.edit-menu-item-title' ).on( 'blur', function() {
1841                  $( this ).val( $.trim( $( this ).val() ) );
1842              } );
1843  
1844              titleEl = control.container.find( '.menu-item-title' );
1845              control.setting.bind( function( item ) {
1846                  var trimmedTitle, titleText;
1847                  if ( ! item ) {
1848                      return;
1849                  }
1850                  trimmedTitle = $.trim( item.title );
1851  
1852                  titleText = trimmedTitle || item.original_title || api.Menus.data.l10n.untitled;
1853  
1854                  if ( item._invalid ) {
1855                      titleText = api.Menus.data.l10n.invalidTitleTpl.replace( '%s', titleText );
1856                  }
1857  
1858                  // Don't update to an empty title.
1859                  if ( trimmedTitle || item.original_title ) {
1860                      titleEl
1861                          .text( titleText )
1862                          .removeClass( 'no-title' );
1863                  } else {
1864                      titleEl
1865                          .text( titleText )
1866                          .addClass( 'no-title' );
1867                  }
1868              } );
1869          },
1870  
1871          /**
1872           *
1873           * @return {number}
1874           */
1875          getDepth: function() {
1876              var control = this, setting = control.setting(), depth = 0;
1877              if ( ! setting ) {
1878                  return 0;
1879              }
1880              while ( setting && setting.menu_item_parent ) {
1881                  depth += 1;
1882                  control = api.control( 'nav_menu_item[' + setting.menu_item_parent + ']' );
1883                  if ( ! control ) {
1884                      break;
1885                  }
1886                  setting = control.setting();
1887              }
1888              return depth;
1889          },
1890  
1891          /**
1892           * Amend the control's params with the data necessary for the JS template just in time.
1893           */
1894          renderContent: function() {
1895              var control = this,
1896                  settingValue = control.setting(),
1897                  containerClasses;
1898  
1899              control.params.title = settingValue.title || '';
1900              control.params.depth = control.getDepth();
1901              control.container.data( 'item-depth', control.params.depth );
1902              containerClasses = [
1903                  'menu-item',
1904                  'menu-item-depth-' + String( control.params.depth ),
1905                  'menu-item-' + settingValue.object,
1906                  'menu-item-edit-inactive'
1907              ];
1908  
1909              if ( settingValue._invalid ) {
1910                  containerClasses.push( 'menu-item-invalid' );
1911                  control.params.title = api.Menus.data.l10n.invalidTitleTpl.replace( '%s', control.params.title );
1912              } else if ( 'draft' === settingValue.status ) {
1913                  containerClasses.push( 'pending' );
1914                  control.params.title = api.Menus.data.pendingTitleTpl.replace( '%s', control.params.title );
1915              }
1916  
1917              control.params.el_classes = containerClasses.join( ' ' );
1918              control.params.item_type_label = settingValue.type_label;
1919              control.params.item_type = settingValue.type;
1920              control.params.url = settingValue.url;
1921              control.params.target = settingValue.target;
1922              control.params.attr_title = settingValue.attr_title;
1923              control.params.classes = _.isArray( settingValue.classes ) ? settingValue.classes.join( ' ' ) : settingValue.classes;
1924              control.params.xfn = settingValue.xfn;
1925              control.params.description = settingValue.description;
1926              control.params.parent = settingValue.menu_item_parent;
1927              control.params.original_title = settingValue.original_title || '';
1928  
1929              control.container.addClass( control.params.el_classes );
1930  
1931              api.Control.prototype.renderContent.call( control );
1932          },
1933  
1934          /***********************************************************************
1935           * Begin public API methods
1936           **********************************************************************/
1937  
1938          /**
1939           * @return {wp.customize.controlConstructor.nav_menu|null}
1940           */
1941          getMenuControl: function() {
1942              var control = this, settingValue = control.setting();
1943              if ( settingValue && settingValue.nav_menu_term_id ) {
1944                  return api.control( 'nav_menu[' + settingValue.nav_menu_term_id + ']' );
1945              } else {
1946                  return null;
1947              }
1948          },
1949  
1950          /**
1951           * Expand the accordion section containing a control
1952           */
1953          expandControlSection: function() {
1954              var $section = this.container.closest( '.accordion-section' );
1955              if ( ! $section.hasClass( 'open' ) ) {
1956                  $section.find( '.accordion-section-title:first' ).trigger( 'click' );
1957              }
1958          },
1959  
1960          /**
1961           * @since 4.6.0
1962           *
1963           * @param {Boolean} expanded
1964           * @param {Object} [params]
1965           * @return {Boolean} False if state already applied.
1966           */
1967          _toggleExpanded: api.Section.prototype._toggleExpanded,
1968  
1969          /**
1970           * @since 4.6.0
1971           *
1972           * @param {Object} [params]
1973           * @return {Boolean} False if already expanded.
1974           */
1975          expand: api.Section.prototype.expand,
1976  
1977          /**
1978           * Expand the menu item form control.
1979           *
1980           * @since 4.5.0 Added params.completeCallback.
1981           *
1982           * @param {Object}   [params] - Optional params.
1983           * @param {Function} [params.completeCallback] - Function to call when the form toggle has finished animating.
1984           */
1985          expandForm: function( params ) {
1986              this.expand( params );
1987          },
1988  
1989          /**
1990           * @since 4.6.0
1991           *
1992           * @param {Object} [params]
1993           * @return {Boolean} False if already collapsed.
1994           */
1995          collapse: api.Section.prototype.collapse,
1996  
1997          /**
1998           * Collapse the menu item form control.
1999           *
2000           * @since 4.5.0 Added params.completeCallback.
2001           *
2002           * @param {Object}   [params] - Optional params.
2003           * @param {Function} [params.completeCallback] - Function to call when the form toggle has finished animating.
2004           */
2005          collapseForm: function( params ) {
2006              this.collapse( params );
2007          },
2008  
2009          /**
2010           * Expand or collapse the menu item control.
2011           *
2012           * @deprecated this is poor naming, and it is better to directly set control.expanded( showOrHide )
2013           * @since 4.5.0 Added params.completeCallback.
2014           *
2015           * @param {boolean}  [showOrHide] - If not supplied, will be inverse of current visibility
2016           * @param {Object}   [params] - Optional params.
2017           * @param {Function} [params.completeCallback] - Function to call when the form toggle has finished animating.
2018           */
2019          toggleForm: function( showOrHide, params ) {
2020              if ( typeof showOrHide === 'undefined' ) {
2021                  showOrHide = ! this.expanded();
2022              }
2023              if ( showOrHide ) {
2024                  this.expand( params );
2025              } else {
2026                  this.collapse( params );
2027              }
2028          },
2029  
2030          /**
2031           * Expand or collapse the menu item control.
2032           *
2033           * @since 4.6.0
2034           * @param {boolean}  [showOrHide] - If not supplied, will be inverse of current visibility
2035           * @param {Object}   [params] - Optional params.
2036           * @param {Function} [params.completeCallback] - Function to call when the form toggle has finished animating.
2037           */
2038          onChangeExpanded: function( showOrHide, params ) {
2039              var self = this, $menuitem, $inside, complete;
2040  
2041              $menuitem = this.container;
2042              $inside = $menuitem.find( '.menu-item-settings:first' );
2043              if ( 'undefined' === typeof showOrHide ) {
2044                  showOrHide = ! $inside.is( ':visible' );
2045              }
2046  
2047              // Already expanded or collapsed.
2048              if ( $inside.is( ':visible' ) === showOrHide ) {
2049                  if ( params && params.completeCallback ) {
2050                      params.completeCallback();
2051                  }
2052                  return;
2053              }
2054  
2055              if ( showOrHide ) {
2056                  // Close all other menu item controls before expanding this one.
2057                  api.control.each( function( otherControl ) {
2058                      if ( self.params.type === otherControl.params.type && self !== otherControl ) {
2059                          otherControl.collapseForm();
2060                      }
2061                  } );
2062  
2063                  complete = function() {
2064                      $menuitem
2065                          .removeClass( 'menu-item-edit-inactive' )
2066                          .addClass( 'menu-item-edit-active' );
2067                      self.container.trigger( 'expanded' );
2068  
2069                      if ( params && params.completeCallback ) {
2070                          params.completeCallback();
2071                      }
2072                  };
2073  
2074                  $menuitem.find( '.item-edit' ).attr( 'aria-expanded', 'true' );
2075                  $inside.slideDown( 'fast', complete );
2076  
2077                  self.container.trigger( 'expand' );
2078              } else {
2079                  complete = function() {
2080                      $menuitem
2081                          .addClass( 'menu-item-edit-inactive' )
2082                          .removeClass( 'menu-item-edit-active' );
2083                      self.container.trigger( 'collapsed' );
2084  
2085                      if ( params && params.completeCallback ) {
2086                          params.completeCallback();
2087                      }
2088                  };
2089  
2090                  self.container.trigger( 'collapse' );
2091  
2092                  $menuitem.find( '.item-edit' ).attr( 'aria-expanded', 'false' );
2093                  $inside.slideUp( 'fast', complete );
2094              }
2095          },
2096  
2097          /**
2098           * Expand the containing menu section, expand the form, and focus on
2099           * the first input in the control.
2100           *
2101           * @since 4.5.0 Added params.completeCallback.
2102           *
2103           * @param {Object}   [params] - Params object.
2104           * @param {Function} [params.completeCallback] - Optional callback function when focus has completed.
2105           */
2106          focus: function( params ) {
2107              params = params || {};
2108              var control = this, originalCompleteCallback = params.completeCallback, focusControl;
2109  
2110              focusControl = function() {
2111                  control.expandControlSection();
2112  
2113                  params.completeCallback = function() {
2114                      var focusable;
2115  
2116                      // Note that we can't use :focusable due to a jQuery UI issue. See: https://github.com/jquery/jquery-ui/pull/1583
2117                      focusable = control.container.find( '.menu-item-settings' ).find( 'input, select, textarea, button, object, a[href], [tabindex]' ).filter( ':visible' );
2118                      focusable.first().focus();
2119  
2120                      if ( originalCompleteCallback ) {
2121                          originalCompleteCallback();
2122                      }
2123                  };
2124  
2125                  control.expandForm( params );
2126              };
2127  
2128              if ( api.section.has( control.section() ) ) {
2129                  api.section( control.section() ).expand( {
2130                      completeCallback: focusControl
2131                  } );
2132              } else {
2133                  focusControl();
2134              }
2135          },
2136  
2137          /**
2138           * Move menu item up one in the menu.
2139           */
2140          moveUp: function() {
2141              this._changePosition( -1 );
2142              wp.a11y.speak( api.Menus.data.l10n.movedUp );
2143          },
2144  
2145          /**
2146           * Move menu item up one in the menu.
2147           */
2148          moveDown: function() {
2149              this._changePosition( 1 );
2150              wp.a11y.speak( api.Menus.data.l10n.movedDown );
2151          },
2152          /**
2153           * Move menu item and all children up one level of depth.
2154           */
2155          moveLeft: function() {
2156              this._changeDepth( -1 );
2157              wp.a11y.speak( api.Menus.data.l10n.movedLeft );
2158          },
2159  
2160          /**
2161           * Move menu item and children one level deeper, as a submenu of the previous item.
2162           */
2163          moveRight: function() {
2164              this._changeDepth( 1 );
2165              wp.a11y.speak( api.Menus.data.l10n.movedRight );
2166          },
2167  
2168          /**
2169           * Note that this will trigger a UI update, causing child items to
2170           * move as well and cardinal order class names to be updated.
2171           *
2172           * @private
2173           *
2174           * @param {Number} offset 1|-1
2175           */
2176          _changePosition: function( offset ) {
2177              var control = this,
2178                  adjacentSetting,
2179                  settingValue = _.clone( control.setting() ),
2180                  siblingSettings = [],
2181                  realPosition;
2182  
2183              if ( 1 !== offset && -1 !== offset ) {
2184                  throw new Error( 'Offset changes by 1 are only supported.' );
2185              }
2186  
2187              // Skip moving deleted items.
2188              if ( ! control.setting() ) {
2189                  return;
2190              }
2191  
2192              // Locate the other items under the same parent (siblings).
2193              _( control.getMenuControl().getMenuItemControls() ).each(function( otherControl ) {
2194                  if ( otherControl.setting().menu_item_parent === settingValue.menu_item_parent ) {
2195                      siblingSettings.push( otherControl.setting );
2196                  }
2197              });
2198              siblingSettings.sort(function( a, b ) {
2199                  return a().position - b().position;
2200              });
2201  
2202              realPosition = _.indexOf( siblingSettings, control.setting );
2203              if ( -1 === realPosition ) {
2204                  throw new Error( 'Expected setting to be among siblings.' );
2205              }
2206  
2207              // Skip doing anything if the item is already at the edge in the desired direction.
2208              if ( ( realPosition === 0 && offset < 0 ) || ( realPosition === siblingSettings.length - 1 && offset > 0 ) ) {
2209                  // @todo Should we allow a menu item to be moved up to break it out of a parent? Adopt with previous or following parent?
2210                  return;
2211              }
2212  
2213              // Update any adjacent menu item setting to take on this item's position.
2214              adjacentSetting = siblingSettings[ realPosition + offset ];
2215              if ( adjacentSetting ) {
2216                  adjacentSetting.set( $.extend(
2217                      _.clone( adjacentSetting() ),
2218                      {
2219                          position: settingValue.position
2220                      }
2221                  ) );
2222              }
2223  
2224              settingValue.position += offset;
2225              control.setting.set( settingValue );
2226          },
2227  
2228          /**
2229           * Note that this will trigger a UI update, causing child items to
2230           * move as well and cardinal order class names to be updated.
2231           *
2232           * @private
2233           *
2234           * @param {Number} offset 1|-1
2235           */
2236          _changeDepth: function( offset ) {
2237              if ( 1 !== offset && -1 !== offset ) {
2238                  throw new Error( 'Offset changes by 1 are only supported.' );
2239              }
2240              var control = this,
2241                  settingValue = _.clone( control.setting() ),
2242                  siblingControls = [],
2243                  realPosition,
2244                  siblingControl,
2245                  parentControl;
2246  
2247              // Locate the other items under the same parent (siblings).
2248              _( control.getMenuControl().getMenuItemControls() ).each(function( otherControl ) {
2249                  if ( otherControl.setting().menu_item_parent === settingValue.menu_item_parent ) {
2250                      siblingControls.push( otherControl );
2251                  }
2252              });
2253              siblingControls.sort(function( a, b ) {
2254                  return a.setting().position - b.setting().position;
2255              });
2256  
2257              realPosition = _.indexOf( siblingControls, control );
2258              if ( -1 === realPosition ) {
2259                  throw new Error( 'Expected control to be among siblings.' );
2260              }
2261  
2262              if ( -1 === offset ) {
2263                  // Skip moving left an item that is already at the top level.
2264                  if ( ! settingValue.menu_item_parent ) {
2265                      return;
2266                  }
2267  
2268                  parentControl = api.control( 'nav_menu_item[' + settingValue.menu_item_parent + ']' );
2269  
2270                  // Make this control the parent of all the following siblings.
2271                  _( siblingControls ).chain().slice( realPosition ).each(function( siblingControl, i ) {
2272                      siblingControl.setting.set(
2273                          $.extend(
2274                              {},
2275                              siblingControl.setting(),
2276                              {
2277                                  menu_item_parent: control.params.menu_item_id,
2278                                  position: i
2279                              }
2280                          )
2281                      );
2282                  });
2283  
2284                  // Increase the positions of the parent item's subsequent children to make room for this one.
2285                  _( control.getMenuControl().getMenuItemControls() ).each(function( otherControl ) {
2286                      var otherControlSettingValue, isControlToBeShifted;
2287                      isControlToBeShifted = (
2288                          otherControl.setting().menu_item_parent === parentControl.setting().menu_item_parent &&
2289                          otherControl.setting().position > parentControl.setting().position
2290                      );
2291                      if ( isControlToBeShifted ) {
2292                          otherControlSettingValue = _.clone( otherControl.setting() );
2293                          otherControl.setting.set(
2294                              $.extend(
2295                                  otherControlSettingValue,
2296                                  { position: otherControlSettingValue.position + 1 }
2297                              )
2298                          );
2299                      }
2300                  });
2301  
2302                  // Make this control the following sibling of its parent item.
2303                  settingValue.position = parentControl.setting().position + 1;
2304                  settingValue.menu_item_parent = parentControl.setting().menu_item_parent;
2305                  control.setting.set( settingValue );
2306  
2307              } else if ( 1 === offset ) {
2308                  // Skip moving right an item that doesn't have a previous sibling.
2309                  if ( realPosition === 0 ) {
2310                      return;
2311                  }
2312  
2313                  // Make the control the last child of the previous sibling.
2314                  siblingControl = siblingControls[ realPosition - 1 ];
2315                  settingValue.menu_item_parent = siblingControl.params.menu_item_id;
2316                  settingValue.position = 0;
2317                  _( control.getMenuControl().getMenuItemControls() ).each(function( otherControl ) {
2318                      if ( otherControl.setting().menu_item_parent === settingValue.menu_item_parent ) {
2319                          settingValue.position = Math.max( settingValue.position, otherControl.setting().position );
2320                      }
2321                  });
2322                  settingValue.position += 1;
2323                  control.setting.set( settingValue );
2324              }
2325          }
2326      } );
2327  
2328      /**
2329       * wp.customize.Menus.MenuNameControl
2330       *
2331       * Customizer control for a nav menu's name.
2332       *
2333       * @class    wp.customize.Menus.MenuNameControl
2334       * @augments wp.customize.Control
2335       */
2336      api.Menus.MenuNameControl = api.Control.extend(/** @lends wp.customize.Menus.MenuNameControl.prototype */{
2337  
2338          ready: function() {
2339              var control = this;
2340  
2341              if ( control.setting ) {
2342                  var settingValue = control.setting();
2343  
2344                  control.nameElement = new api.Element( control.container.find( '.menu-name-field' ) );
2345  
2346                  control.nameElement.bind(function( value ) {
2347                      var settingValue = control.setting();
2348                      if ( settingValue && settingValue.name !== value ) {
2349                          settingValue = _.clone( settingValue );
2350                          settingValue.name = value;
2351                          control.setting.set( settingValue );
2352                      }
2353                  });
2354                  if ( settingValue ) {
2355                      control.nameElement.set( settingValue.name );
2356                  }
2357  
2358                  control.setting.bind(function( object ) {
2359                      if ( object ) {
2360                          control.nameElement.set( object.name );
2361                      }
2362                  });
2363              }
2364          }
2365      });
2366  
2367      /**
2368       * wp.customize.Menus.MenuLocationsControl
2369       *
2370       * Customizer control for a nav menu's locations.
2371       *
2372       * @since 4.9.0
2373       * @class    wp.customize.Menus.MenuLocationsControl
2374       * @augments wp.customize.Control
2375       */
2376      api.Menus.MenuLocationsControl = api.Control.extend(/** @lends wp.customize.Menus.MenuLocationsControl.prototype */{
2377  
2378          /**
2379           * Set up the control.
2380           *
2381           * @since 4.9.0
2382           */
2383          ready: function () {
2384              var control = this;
2385  
2386              control.container.find( '.assigned-menu-location' ).each(function() {
2387                  var container = $( this ),
2388                      checkbox = container.find( 'input[type=checkbox]' ),
2389                      element = new api.Element( checkbox ),
2390                      navMenuLocationSetting = api( 'nav_menu_locations[' + checkbox.data( 'location-id' ) + ']' ),
2391                      isNewMenu = control.params.menu_id === '',
2392                      updateCheckbox = isNewMenu ? _.noop : function( checked ) {
2393                          element.set( checked );
2394                      },
2395                      updateSetting = isNewMenu ? _.noop : function( checked ) {
2396                          navMenuLocationSetting.set( checked ? control.params.menu_id : 0 );
2397                      },
2398                      updateSelectedMenuLabel = function( selectedMenuId ) {
2399                          var menuSetting = api( 'nav_menu[' + String( selectedMenuId ) + ']' );
2400                          if ( ! selectedMenuId || ! menuSetting || ! menuSetting() ) {
2401                              container.find( '.theme-location-set' ).hide();
2402                          } else {
2403                              container.find( '.theme-location-set' ).show().find( 'span' ).text( displayNavMenuName( menuSetting().name ) );
2404                          }
2405                      };
2406  
2407                  updateCheckbox( navMenuLocationSetting.get() === control.params.menu_id );
2408  
2409                  checkbox.on( 'change', function() {
2410                      // Note: We can't use element.bind( function( checked ){ ... } ) here because it will trigger a change as well.
2411                      updateSetting( this.checked );
2412                  } );
2413  
2414                  navMenuLocationSetting.bind( function( selectedMenuId ) {
2415                      updateCheckbox( selectedMenuId === control.params.menu_id );
2416                      updateSelectedMenuLabel( selectedMenuId );
2417                  } );
2418                  updateSelectedMenuLabel( navMenuLocationSetting.get() );
2419              });
2420          },
2421  
2422          /**
2423           * Set the selected locations.
2424           *
2425           * This method sets the selected locations and allows us to do things like
2426           * set the default location for a new menu.
2427           *
2428           * @since 4.9.0
2429           *
2430           * @param {Object.<string,boolean>} selections - A map of location selections.
2431           * @return {void}
2432           */
2433          setSelections: function( selections ) {
2434              this.container.find( '.menu-location' ).each( function( i, checkboxNode ) {
2435                  var locationId = checkboxNode.dataset.locationId;
2436                  checkboxNode.checked = locationId in selections ? selections[ locationId ] : false;
2437              } );
2438          }
2439      });
2440  
2441      /**
2442       * wp.customize.Menus.MenuAutoAddControl
2443       *
2444       * Customizer control for a nav menu's auto add.
2445       *
2446       * @class    wp.customize.Menus.MenuAutoAddControl
2447       * @augments wp.customize.Control
2448       */
2449      api.Menus.MenuAutoAddControl = api.Control.extend(/** @lends wp.customize.Menus.MenuAutoAddControl.prototype */{
2450  
2451          ready: function() {
2452              var control = this,
2453                  settingValue = control.setting();
2454  
2455              /*
2456               * Since the control is not registered in PHP, we need to prevent the
2457               * preview's sending of the activeControls to result in this control
2458               * being deactivated.
2459               */
2460              control.active.validate = function() {
2461                  var value, section = api.section( control.section() );
2462                  if ( section ) {
2463                      value = section.active();
2464                  } else {
2465                      value = false;
2466                  }
2467                  return value;
2468              };
2469  
2470              control.autoAddElement = new api.Element( control.container.find( 'input[type=checkbox].auto_add' ) );
2471  
2472              control.autoAddElement.bind(function( value ) {
2473                  var settingValue = control.setting();
2474                  if ( settingValue && settingValue.name !== value ) {
2475                      settingValue = _.clone( settingValue );
2476                      settingValue.auto_add = value;
2477                      control.setting.set( settingValue );
2478                  }
2479              });
2480              if ( settingValue ) {
2481                  control.autoAddElement.set( settingValue.auto_add );
2482              }
2483  
2484              control.setting.bind(function( object ) {
2485                  if ( object ) {
2486                      control.autoAddElement.set( object.auto_add );
2487                  }
2488              });
2489          }
2490  
2491      });
2492  
2493      /**
2494       * wp.customize.Menus.MenuControl
2495       *
2496       * Customizer control for menus.
2497       * Note that 'nav_menu' must match the WP_Menu_Customize_Control::$type
2498       *
2499       * @class    wp.customize.Menus.MenuControl
2500       * @augments wp.customize.Control
2501       */
2502      api.Menus.MenuControl = api.Control.extend(/** @lends wp.customize.Menus.MenuControl.prototype */{
2503          /**
2504           * Set up the control.
2505           */
2506          ready: function() {
2507              var control = this,
2508                  section = api.section( control.section() ),
2509                  menuId = control.params.menu_id,
2510                  menu = control.setting(),
2511                  name,
2512                  widgetTemplate,
2513                  select;
2514  
2515              if ( 'undefined' === typeof this.params.menu_id ) {
2516                  throw new Error( 'params.menu_id was not defined' );
2517              }
2518  
2519              /*
2520               * Since the control is not registered in PHP, we need to prevent the
2521               * preview's sending of the activeControls to result in this control
2522               * being deactivated.
2523               */
2524              control.active.validate = function() {
2525                  var value;
2526                  if ( section ) {
2527                      value = section.active();
2528                  } else {
2529                      value = false;
2530                  }
2531                  return value;
2532              };
2533  
2534              control.$controlSection = section.headContainer;
2535              control.$sectionContent = control.container.closest( '.accordion-section-content' );
2536  
2537              this._setupModel();
2538  
2539              api.section( control.section(), function( section ) {
2540                  section.deferred.initSortables.done(function( menuList ) {
2541                      control._setupSortable( menuList );
2542                  });
2543              } );
2544  
2545              this._setupAddition();
2546              this._setupTitle();
2547  
2548              // Add menu to Navigation Menu widgets.
2549              if ( menu ) {
2550                  name = displayNavMenuName( menu.name );
2551  
2552                  // Add the menu to the existing controls.
2553                  api.control.each( function( widgetControl ) {
2554                      if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
2555                          return;
2556                      }
2557                      widgetControl.container.find( '.nav-menu-widget-form-controls:first' ).show();
2558                      widgetControl.container.find( '.nav-menu-widget-no-menus-message:first' ).hide();
2559  
2560                      select = widgetControl.container.find( 'select' );
2561                      if ( 0 === select.find( 'option[value=' + String( menuId ) + ']' ).length ) {
2562                          select.append( new Option( name, menuId ) );
2563                      }
2564                  } );
2565  
2566                  // Add the menu to the widget template.
2567                  widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
2568                  widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).show();
2569                  widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).hide();
2570                  select = widgetTemplate.find( '.widget-inside select:first' );
2571                  if ( 0 === select.find( 'option[value=' + String( menuId ) + ']' ).length ) {
2572                      select.append( new Option( name, menuId ) );
2573                  }
2574              }
2575  
2576              /*
2577               * Wait for menu items to be added.
2578               * Ideally, we'd bind to an event indicating construction is complete,
2579               * but deferring appears to be the best option today.
2580               */
2581              _.defer( function () {
2582                  control.updateInvitationVisibility();
2583              } );
2584          },
2585  
2586          /**
2587           * Update ordering of menu item controls when the setting is updated.
2588           */
2589          _setupModel: function() {
2590              var control = this,
2591                  menuId = control.params.menu_id;
2592  
2593              control.setting.bind( function( to ) {
2594                  var name;
2595                  if ( false === to ) {
2596                      control._handleDeletion();
2597                  } else {
2598                      // Update names in the Navigation Menu widgets.
2599                      name = displayNavMenuName( to.name );
2600                      api.control.each( function( widgetControl ) {
2601                          if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
2602                              return;
2603                          }
2604                          var select = widgetControl.container.find( 'select' );
2605                          select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
2606                      });
2607                  }
2608              } );
2609          },
2610  
2611          /**
2612           * Allow items in each menu to be re-ordered, and for the order to be previewed.
2613           *
2614           * Notice that the UI aspects here are handled by wpNavMenu.initSortables()
2615           * which is called in MenuSection.onChangeExpanded()
2616           *
2617           * @param {object} menuList - The element that has sortable().
2618           */
2619          _setupSortable: function( menuList ) {
2620              var control = this;
2621  
2622              if ( ! menuList.is( control.$sectionContent ) ) {
2623                  throw new Error( 'Unexpected menuList.' );
2624              }
2625  
2626              menuList.on( 'sortstart', function() {
2627                  control.isSorting = true;
2628              });
2629  
2630              menuList.on( 'sortstop', function() {
2631                  setTimeout( function() { // Next tick.
2632                      var menuItemContainerIds = control.$sectionContent.sortable( 'toArray' ),
2633                          menuItemControls = [],
2634                          position = 0,
2635                          priority = 10;
2636  
2637                      control.isSorting = false;
2638  
2639                      // Reset horizontal scroll position when done dragging.
2640                      control.$sectionContent.scrollLeft( 0 );
2641  
2642                      _.each( menuItemContainerIds, function( menuItemContainerId ) {
2643                          var menuItemId, menuItemControl, matches;
2644                          matches = menuItemContainerId.match( /^customize-control-nav_menu_item-(-?\d+)$/, '' );
2645                          if ( ! matches ) {
2646                              return;
2647                          }
2648                          menuItemId = parseInt( matches[1], 10 );
2649                          menuItemControl = api.control( 'nav_menu_item[' + String( menuItemId ) + ']' );
2650                          if ( menuItemControl ) {
2651                              menuItemControls.push( menuItemControl );
2652                          }
2653                      } );
2654  
2655                      _.each( menuItemControls, function( menuItemControl ) {
2656                          if ( false === menuItemControl.setting() ) {
2657                              // Skip deleted items.
2658                              return;
2659                          }
2660                          var setting = _.clone( menuItemControl.setting() );
2661                          position += 1;
2662                          priority += 1;
2663                          setting.position = position;
2664                          menuItemControl.priority( priority );
2665  
2666                          // Note that wpNavMenu will be setting this .menu-item-data-parent-id input's value.
2667                          setting.menu_item_parent = parseInt( menuItemControl.container.find( '.menu-item-data-parent-id' ).val(), 10 );
2668                          if ( ! setting.menu_item_parent ) {
2669                              setting.menu_item_parent = 0;
2670                          }
2671  
2672                          menuItemControl.setting.set( setting );
2673                      });
2674                  });
2675  
2676              });
2677              control.isReordering = false;
2678  
2679              /**
2680               * Keyboard-accessible reordering.
2681               */
2682              this.container.find( '.reorder-toggle' ).on( 'click', function() {
2683                  control.toggleReordering( ! control.isReordering );
2684              } );
2685          },
2686  
2687          /**
2688           * Set up UI for adding a new menu item.
2689           */
2690          _setupAddition: function() {
2691              var self = this;
2692  
2693              this.container.find( '.add-new-menu-item' ).on( 'click', function( event ) {
2694                  if ( self.$sectionContent.hasClass( 'reordering' ) ) {
2695                      return;
2696                  }
2697  
2698                  if ( ! $( 'body' ).hasClass( 'adding-menu-items' ) ) {
2699                      $( this ).attr( 'aria-expanded', 'true' );
2700                      api.Menus.availableMenuItemsPanel.open( self );
2701                  } else {
2702                      $( this ).attr( 'aria-expanded', 'false' );
2703                      api.Menus.availableMenuItemsPanel.close();
2704                      event.stopPropagation();
2705                  }
2706              } );
2707          },
2708  
2709          _handleDeletion: function() {
2710              var control = this,
2711                  section,
2712                  menuId = control.params.menu_id,
2713                  removeSection,
2714                  widgetTemplate,
2715                  navMenuCount = 0;
2716              section = api.section( control.section() );
2717              removeSection = function() {
2718                  section.container.remove();
2719                  api.section.remove( section.id );
2720              };
2721  
2722              if ( section && section.expanded() ) {
2723                  section.collapse({
2724                      completeCallback: function() {
2725                          removeSection();
2726                          wp.a11y.speak( api.Menus.data.l10n.menuDeleted );
2727                          api.panel( 'nav_menus' ).focus();
2728                      }
2729                  });
2730              } else {
2731                  removeSection();
2732              }
2733  
2734              api.each(function( setting ) {
2735                  if ( /^nav_menu\[/.test( setting.id ) && false !== setting() ) {
2736                      navMenuCount += 1;
2737                  }
2738              });
2739  
2740              // Remove the menu from any Navigation Menu widgets.
2741              api.control.each(function( widgetControl ) {
2742                  if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
2743                      return;
2744                  }
2745                  var select = widgetControl.container.find( 'select' );
2746                  if ( select.val() === String( menuId ) ) {
2747                      select.prop( 'selectedIndex', 0 ).trigger( 'change' );
2748                  }
2749  
2750                  widgetControl.container.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
2751                  widgetControl.container.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
2752                  widgetControl.container.find( 'option[value=' + String( menuId ) + ']' ).remove();
2753              });
2754  
2755              // Remove the menu to the nav menu widget template.
2756              widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
2757              widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
2758              widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
2759              widgetTemplate.find( 'option[value=' + String( menuId ) + ']' ).remove();
2760          },
2761  
2762          /**
2763           * Update Section Title as menu name is changed.
2764           */
2765          _setupTitle: function() {
2766              var control = this;
2767  
2768              control.setting.bind( function( menu ) {
2769                  if ( ! menu ) {
2770                      return;
2771                  }
2772  
2773                  var section = api.section( control.section() ),
2774                      menuId = control.params.menu_id,
2775                      controlTitle = section.headContainer.find( '.accordion-section-title' ),
2776                      sectionTitle = section.contentContainer.find( '.customize-section-title h3' ),
2777                      location = section.headContainer.find( '.menu-in-location' ),
2778                      action = sectionTitle.find( '.customize-action' ),
2779                      name = displayNavMenuName( menu.name );
2780  
2781                  // Update the control title
2782                  controlTitle.text( name );
2783                  if ( location.length ) {
2784                      location.appendTo( controlTitle );
2785                  }
2786  
2787                  // Update the section title
2788                  sectionTitle.text( name );
2789                  if ( action.length ) {
2790                      action.prependTo( sectionTitle );
2791                  }
2792  
2793                  // Update the nav menu name in location selects.
2794                  api.control.each( function( control ) {
2795                      if ( /^nav_menu_locations\[/.test( control.id ) ) {
2796                          control.container.find( 'option[value=' + menuId + ']' ).text( name );
2797                      }
2798                  } );
2799  
2800                  // Update the nav menu name in all location checkboxes.
2801                  section.contentContainer.find( '.customize-control-checkbox input' ).each( function() {
2802                      if ( $( this ).prop( 'checked' ) ) {
2803                          $( '.current-menu-location-name-' + $( this ).data( 'location-id' ) ).text( name );
2804                      }
2805                  } );
2806              } );
2807          },
2808  
2809          /***********************************************************************
2810           * Begin public API methods
2811           **********************************************************************/
2812  
2813          /**
2814           * Enable/disable the reordering UI
2815           *
2816           * @param {Boolean} showOrHide to enable/disable reordering
2817           */
2818          toggleReordering: function( showOrHide ) {
2819              var addNewItemBtn = this.container.find( '.add-new-menu-item' ),
2820                  reorderBtn = this.container.find( '.reorder-toggle' ),
2821                  itemsTitle = this.$sectionContent.find( '.item-title' );
2822  
2823              showOrHide = Boolean( showOrHide );
2824  
2825              if ( showOrHide === this.$sectionContent.hasClass( 'reordering' ) ) {
2826                  return;
2827              }
2828  
2829              this.isReordering = showOrHide;
2830              this.$sectionContent.toggleClass( 'reordering', showOrHide );
2831              this.$sectionContent.sortable( this.isReordering ? 'disable' : 'enable' );
2832              if ( this.isReordering ) {
2833                  addNewItemBtn.attr({ 'tabindex': '-1', 'aria-hidden': 'true' });
2834                  reorderBtn.attr( 'aria-label', api.Menus.data.l10n.reorderLabelOff );
2835                  wp.a11y.speak( api.Menus.data.l10n.reorderModeOn );
2836                  itemsTitle.attr( 'aria-hidden', 'false' );
2837              } else {
2838                  addNewItemBtn.removeAttr( 'tabindex aria-hidden' );
2839                  reorderBtn.attr( 'aria-label', api.Menus.data.l10n.reorderLabelOn );
2840                  wp.a11y.speak( api.Menus.data.l10n.reorderModeOff );
2841                  itemsTitle.attr( 'aria-hidden', 'true' );
2842              }
2843  
2844              if ( showOrHide ) {
2845                  _( this.getMenuItemControls() ).each( function( formControl ) {
2846                      formControl.collapseForm();
2847                  } );
2848              }
2849          },
2850  
2851          /**
2852           * @return {wp.customize.controlConstructor.nav_menu_item[]}
2853           */
2854          getMenuItemControls: function() {
2855              var menuControl = this,
2856                  menuItemControls = [],
2857                  menuTermId = menuControl.params.menu_id;
2858  
2859              api.control.each(function( control ) {
2860                  if ( 'nav_menu_item' === control.params.type && control.setting() && menuTermId === control.setting().nav_menu_term_id ) {
2861                      menuItemControls.push( control );
2862                  }
2863              });
2864  
2865              return menuItemControls;
2866          },
2867  
2868          /**
2869           * Make sure that each menu item control has the proper depth.
2870           */
2871          reflowMenuItems: function() {
2872              var menuControl = this,
2873                  menuItemControls = menuControl.getMenuItemControls(),
2874                  reflowRecursively;
2875  
2876              reflowRecursively = function( context ) {
2877                  var currentMenuItemControls = [],
2878                      thisParent = context.currentParent;
2879                  _.each( context.menuItemControls, function( menuItemControl ) {
2880                      if ( thisParent === menuItemControl.setting().menu_item_parent ) {
2881                          currentMenuItemControls.push( menuItemControl );
2882                          // @todo We could remove this item from menuItemControls now, for efficiency.
2883                      }
2884                  });
2885                  currentMenuItemControls.sort( function( a, b ) {
2886                      return a.setting().position - b.setting().position;
2887                  });
2888  
2889                  _.each( currentMenuItemControls, function( menuItemControl ) {
2890                      // Update position.
2891                      context.currentAbsolutePosition += 1;
2892                      menuItemControl.priority.set( context.currentAbsolutePosition ); // This will change the sort order.
2893  
2894                      // Update depth.
2895                      if ( ! menuItemControl.container.hasClass( 'menu-item-depth-' + String( context.currentDepth ) ) ) {
2896                          _.each( menuItemControl.container.prop( 'className' ).match( /menu-item-depth-\d+/g ), function( className ) {
2897                              menuItemControl.container.removeClass( className );
2898                          });
2899                          menuItemControl.container.addClass( 'menu-item-depth-' + String( context.currentDepth ) );
2900                      }
2901                      menuItemControl.container.data( 'item-depth', context.currentDepth );
2902  
2903                      // Process any children items.
2904                      context.currentDepth += 1;
2905                      context.currentParent = menuItemControl.params.menu_item_id;
2906                      reflowRecursively( context );
2907                      context.currentDepth -= 1;
2908                      context.currentParent = thisParent;
2909                  });
2910  
2911                  // Update class names for reordering controls.
2912                  if ( currentMenuItemControls.length ) {
2913                      _( currentMenuItemControls ).each(function( menuItemControl ) {
2914                          menuItemControl.container.removeClass( 'move-up-disabled move-down-disabled move-left-disabled move-right-disabled' );
2915                          if ( 0 === context.currentDepth ) {
2916                              menuItemControl.container.addClass( 'move-left-disabled' );
2917                          } else if ( 10 === context.currentDepth ) {
2918                              menuItemControl.container.addClass( 'move-right-disabled' );
2919                          }
2920                      });
2921  
2922                      currentMenuItemControls[0].container
2923                          .addClass( 'move-up-disabled' )
2924                          .addClass( 'move-right-disabled' )
2925                          .toggleClass( 'move-down-disabled', 1 === currentMenuItemControls.length );
2926                      currentMenuItemControls[ currentMenuItemControls.length - 1 ].container
2927                          .addClass( 'move-down-disabled' )
2928                          .toggleClass( 'move-up-disabled', 1 === currentMenuItemControls.length );
2929                  }
2930              };
2931  
2932              reflowRecursively( {
2933                  menuItemControls: menuItemControls,
2934                  currentParent: 0,
2935                  currentDepth: 0,
2936                  currentAbsolutePosition: 0
2937              } );
2938  
2939              menuControl.updateInvitationVisibility( menuItemControls );
2940              menuControl.container.find( '.reorder-toggle' ).toggle( menuItemControls.length > 1 );
2941          },
2942  
2943          /**
2944           * Note that this function gets debounced so that when a lot of setting
2945           * changes are made at once, for instance when moving a menu item that
2946           * has child items, this function will only be called once all of the
2947           * settings have been updated.
2948           */
2949          debouncedReflowMenuItems: _.debounce( function() {
2950              this.reflowMenuItems.apply( this, arguments );
2951          }, 0 ),
2952  
2953          /**
2954           * Add a new item to this menu.
2955           *
2956           * @param {object} item - Value for the nav_menu_item setting to be created.
2957           * @return {wp.customize.Menus.controlConstructor.nav_menu_item} The newly-created nav_menu_item control instance.
2958           */
2959          addItemToMenu: function( item ) {
2960              var menuControl = this, customizeId, settingArgs, setting, menuItemControl, placeholderId, position = 0, priority = 10;
2961  
2962              _.each( menuControl.getMenuItemControls(), function( control ) {
2963                  if ( false === control.setting() ) {
2964                      return;
2965                  }
2966                  priority = Math.max( priority, control.priority() );
2967                  if ( 0 === control.setting().menu_item_parent ) {
2968                      position = Math.max( position, control.setting().position );
2969                  }
2970              });
2971              position += 1;
2972              priority += 1;
2973  
2974              item = $.extend(
2975                  {},
2976                  api.Menus.data.defaultSettingValues.nav_menu_item,
2977                  item,
2978                  {
2979                      nav_menu_term_id: menuControl.params.menu_id,
2980                      original_title: item.title,
2981                      position: position
2982                  }
2983              );
2984              delete item.id; // only used by Backbone
2985  
2986              placeholderId = api.Menus.generatePlaceholderAutoIncrementId();
2987              customizeId = 'nav_menu_item[' + String( placeholderId ) + ']';
2988              settingArgs = {
2989                  type: 'nav_menu_item',
2990                  transport: api.Menus.data.settingTransport,
2991                  previewer: api.previewer
2992              };
2993              setting = api.create( customizeId, customizeId, {}, settingArgs );
2994              setting.set( item ); // Change from initial empty object to actual item to mark as dirty.
2995  
2996              // Add the menu item control.
2997              menuItemControl = new api.controlConstructor.nav_menu_item( customizeId, {
2998                  type: 'nav_menu_item',
2999                  section: menuControl.id,
3000                  priority: priority,
3001                  settings: {
3002                      'default': customizeId
3003                  },
3004                  menu_item_id: placeholderId
3005              } );
3006  
3007              api.control.add( menuItemControl );
3008              setting.preview();
3009              menuControl.debouncedReflowMenuItems();
3010  
3011              wp.a11y.speak( api.Menus.data.l10n.itemAdded );
3012  
3013              return menuItemControl;
3014          },
3015  
3016          /**
3017           * Show an invitation to add new menu items when there are no menu items.
3018           *
3019           * @since 4.9.0
3020           *
3021           * @param {wp.customize.controlConstructor.nav_menu_item[]} optionalMenuItemControls
3022           */
3023          updateInvitationVisibility: function ( optionalMenuItemControls ) {
3024              var menuItemControls = optionalMenuItemControls || this.getMenuItemControls();
3025  
3026              this.container.find( '.new-menu-item-invitation' ).toggle( menuItemControls.length === 0 );
3027          }
3028      } );
3029  
3030      /**
3031       * Extends wp.customize.controlConstructor with control constructor for
3032       * menu_location, menu_item, nav_menu, and new_menu.
3033       */
3034      $.extend( api.controlConstructor, {
3035          nav_menu_location: api.Menus.MenuLocationControl,
3036          nav_menu_item: api.Menus.MenuItemControl,
3037          nav_menu: api.Menus.MenuControl,
3038          nav_menu_name: api.Menus.MenuNameControl,
3039          nav_menu_locations: api.Menus.MenuLocationsControl,
3040          nav_menu_auto_add: api.Menus.MenuAutoAddControl
3041      });
3042  
3043      /**
3044       * Extends wp.customize.panelConstructor with section constructor for menus.
3045       */
3046      $.extend( api.panelConstructor, {
3047          nav_menus: api.Menus.MenusPanel
3048      });
3049  
3050      /**
3051       * Extends wp.customize.sectionConstructor with section constructor for menu.
3052       */
3053      $.extend( api.sectionConstructor, {
3054          nav_menu: api.Menus.MenuSection,
3055          new_menu: api.Menus.NewMenuSection
3056      });
3057  
3058      /**
3059       * Init Customizer for menus.
3060       */
3061      api.bind( 'ready', function() {
3062  
3063          // Set up the menu items panel.
3064          api.Menus.availableMenuItemsPanel = new api.Menus.AvailableMenuItemsPanelView({
3065              collection: api.Menus.availableMenuItems
3066          });
3067  
3068          api.bind( 'saved', function( data ) {
3069              if ( data.nav_menu_updates || data.nav_menu_item_updates ) {
3070                  api.Menus.applySavedData( data );
3071              }
3072          } );
3073  
3074          /*
3075           * Reset the list of posts created in the customizer once published.
3076           * The setting is updated quietly (bypassing events being triggered)
3077           * so that the customized state doesn't become immediately dirty.
3078           */
3079          api.state( 'changesetStatus' ).bind( function( status ) {
3080              if ( 'publish' === status ) {
3081                  api( 'nav_menus_created_posts' )._value = [];
3082              }
3083          } );
3084  
3085          // Open and focus menu control.
3086          api.previewer.bind( 'focus-nav-menu-item-control', api.Menus.focusMenuItemControl );
3087      } );
3088  
3089      /**
3090       * When customize_save comes back with a success, make sure any inserted
3091       * nav menus and items are properly re-added with their newly-assigned IDs.
3092       *
3093       * @alias wp.customize.Menus.applySavedData
3094       *
3095       * @param {object} data
3096       * @param {array} data.nav_menu_updates
3097       * @param {array} data.nav_menu_item_updates
3098       */
3099      api.Menus.applySavedData = function( data ) {
3100  
3101          var insertedMenuIdMapping = {}, insertedMenuItemIdMapping = {};
3102  
3103          _( data.nav_menu_updates ).each(function( update ) {
3104              var oldCustomizeId, newCustomizeId, customizeId, oldSetting, newSetting, setting, settingValue, oldSection, newSection, wasSaved, widgetTemplate, navMenuCount, shouldExpandNewSection;
3105              if ( 'inserted' === update.status ) {
3106                  if ( ! update.previous_term_id ) {
3107                      throw new Error( 'Expected previous_term_id' );
3108                  }
3109                  if ( ! update.term_id ) {
3110                      throw new Error( 'Expected term_id' );
3111                  }
3112                  oldCustomizeId = 'nav_menu[' + String( update.previous_term_id ) + ']';
3113                  if ( ! api.has( oldCustomizeId ) ) {
3114                      throw new Error( 'Expected setting to exist: ' + oldCustomizeId );
3115                  }
3116                  oldSetting = api( oldCustomizeId );
3117                  if ( ! api.section.has( oldCustomizeId ) ) {
3118                      throw new Error( 'Expected control to exist: ' + oldCustomizeId );
3119                  }
3120                  oldSection = api.section( oldCustomizeId );
3121  
3122                  settingValue = oldSetting.get();
3123                  if ( ! settingValue ) {
3124                      throw new Error( 'Did not expect setting to be empty (deleted).' );
3125                  }
3126                  settingValue = $.extend( _.clone( settingValue ), update.saved_value );
3127  
3128                  insertedMenuIdMapping[ update.previous_term_id ] = update.term_id;
3129                  newCustomizeId = 'nav_menu[' + String( update.term_id ) + ']';
3130                  newSetting = api.create( newCustomizeId, newCustomizeId, settingValue, {
3131                      type: 'nav_menu',
3132                      transport: api.Menus.data.settingTransport,
3133                      previewer: api.previewer
3134                  } );
3135  
3136                  shouldExpandNewSection = oldSection.expanded();
3137                  if ( shouldExpandNewSection ) {
3138                      oldSection.collapse();
3139                  }
3140  
3141                  // Add the menu section.
3142                  newSection = new api.Menus.MenuSection( newCustomizeId, {
3143                      panel: 'nav_menus',
3144                      title: settingValue.name,
3145                      customizeAction: api.Menus.data.l10n.customizingMenus,
3146                      type: 'nav_menu',
3147                      priority: oldSection.priority.get(),
3148                      menu_id: update.term_id
3149                  } );
3150  
3151                  // Add new control for the new menu.
3152                  api.section.add( newSection );
3153  
3154                  // Update the values for nav menus in Navigation Menu controls.
3155                  api.control.each( function( setting ) {
3156                      if ( ! setting.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== setting.params.widget_id_base ) {
3157                          return;
3158                      }
3159                      var select, oldMenuOption, newMenuOption;
3160                      select = setting.container.find( 'select' );
3161                      oldMenuOption = select.find( 'option[value=' + String( update.previous_term_id ) + ']' );
3162                      newMenuOption = select.find( 'option[value=' + String( update.term_id ) + ']' );
3163                      newMenuOption.prop( 'selected', oldMenuOption.prop( 'selected' ) );
3164                      oldMenuOption.remove();
3165                  } );
3166  
3167                  // Delete the old placeholder nav_menu.
3168                  oldSetting.callbacks.disable(); // Prevent setting triggering Customizer dirty state when set.
3169                  oldSetting.set( false );
3170                  oldSetting.preview();
3171                  newSetting.preview();
3172                  oldSetting._dirty = false;
3173  
3174                  // Remove nav_menu section.
3175                  oldSection.container.remove();
3176                  api.section.remove( oldCustomizeId );
3177  
3178                  // Update the nav_menu widget to reflect removed placeholder menu.
3179                  navMenuCount = 0;
3180                  api.each(function( setting ) {
3181                      if ( /^nav_menu\[/.test( setting.id ) && false !== setting() ) {
3182                          navMenuCount += 1;
3183                      }
3184                  });
3185                  widgetTemplate = $( '#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )' );
3186                  widgetTemplate.find( '.nav-menu-widget-form-controls:first' ).toggle( 0 !== navMenuCount );
3187                  widgetTemplate.find( '.nav-menu-widget-no-menus-message:first' ).toggle( 0 === navMenuCount );
3188                  widgetTemplate.find( 'option[value=' + String( update.previous_term_id ) + ']' ).remove();
3189  
3190                  // Update the nav_menu_locations[...] controls to remove the placeholder menus from the dropdown options.
3191                  wp.customize.control.each(function( control ){
3192                      if ( /^nav_menu_locations\[/.test( control.id ) ) {
3193                          control.container.find( 'option[value=' + String( update.previous_term_id ) + ']' ).remove();
3194                      }
3195                  });
3196  
3197                  // Update nav_menu_locations to reference the new ID.
3198                  api.each( function( setting ) {
3199                      var wasSaved = api.state( 'saved' ).get();
3200                      if ( /^nav_menu_locations\[/.test( setting.id ) && setting.get() === update.previous_term_id ) {
3201                          setting.set( update.term_id );
3202                          setting._dirty = false; // Not dirty because this is has also just been done on server in WP_Customize_Nav_Menu_Setting::update().
3203                          api.state( 'saved' ).set( wasSaved );
3204                          setting.preview();
3205                      }
3206                  } );
3207  
3208                  if ( shouldExpandNewSection ) {
3209                      newSection.expand();
3210                  }
3211              } else if ( 'updated' === update.status ) {
3212                  customizeId = 'nav_menu[' + String( update.term_id ) + ']';
3213                  if ( ! api.has( customizeId ) ) {
3214                      throw new Error( 'Expected setting to exist: ' + customizeId );
3215                  }
3216  
3217                  // Make sure the setting gets updated with its sanitized server value (specifically the conflict-resolved name).
3218                  setting = api( customizeId );
3219                  if ( ! _.isEqual( update.saved_value, setting.get() ) ) {
3220                      wasSaved = api.state( 'saved' ).get();
3221                      setting.set( update.saved_value );
3222                      setting._dirty = false;
3223                      api.state( 'saved' ).set( wasSaved );
3224                  }
3225              }
3226          } );
3227  
3228          // Build up mapping of nav_menu_item placeholder IDs to inserted IDs.
3229          _( data.nav_menu_item_updates ).each(function( update ) {
3230              if ( update.previous_post_id ) {
3231                  insertedMenuItemIdMapping[ update.previous_post_id ] = update.post_id;
3232              }
3233          });
3234  
3235          _( data.nav_menu_item_updates ).each(function( update ) {
3236              var oldCustomizeId, newCustomizeId, oldSetting, newSetting, settingValue, oldControl, newControl;
3237              if ( 'inserted' === update.status ) {
3238                  if ( ! update.previous_post_id ) {
3239                      throw new Error( 'Expected previous_post_id' );
3240                  }
3241                  if ( ! update.post_id ) {
3242                      throw new Error( 'Expected post_id' );
3243                  }
3244                  oldCustomizeId = 'nav_menu_item[' + String( update.previous_post_id ) + ']';
3245                  if ( ! api.has( oldCustomizeId ) ) {
3246                      throw new Error( 'Expected setting to exist: ' + oldCustomizeId );
3247                  }
3248                  oldSetting = api( oldCustomizeId );
3249                  if ( ! api.control.has( oldCustomizeId ) ) {
3250                      throw new Error( 'Expected control to exist: ' + oldCustomizeId );
3251                  }
3252                  oldControl = api.control( oldCustomizeId );
3253  
3254                  settingValue = oldSetting.get();
3255                  if ( ! settingValue ) {
3256                      throw new Error( 'Did not expect setting to be empty (deleted).' );
3257                  }
3258                  settingValue = _.clone( settingValue );
3259  
3260                  // If the parent menu item was also inserted, update the menu_item_parent to the new ID.
3261                  if ( settingValue.menu_item_parent < 0 ) {
3262                      if ( ! insertedMenuItemIdMapping[ settingValue.menu_item_parent ] ) {
3263                          throw new Error( 'inserted ID for menu_item_parent not available' );
3264                      }
3265                      settingValue.menu_item_parent = insertedMenuItemIdMapping[ settingValue.menu_item_parent ];
3266                  }
3267  
3268                  // If the menu was also inserted, then make sure it uses the new menu ID for nav_menu_term_id.
3269                  if ( insertedMenuIdMapping[ settingValue.nav_menu_term_id ] ) {
3270                      settingValue.nav_menu_term_id = insertedMenuIdMapping[ settingValue.nav_menu_term_id ];
3271                  }
3272  
3273                  newCustomizeId = 'nav_menu_item[' + String( update.post_id ) + ']';
3274                  newSetting = api.create( newCustomizeId, newCustomizeId, settingValue, {
3275                      type: 'nav_menu_item',
3276                      transport: api.Menus.data.settingTransport,
3277                      previewer: api.previewer
3278                  } );
3279  
3280                  // Add the menu control.
3281                  newControl = new api.controlConstructor.nav_menu_item( newCustomizeId, {
3282                      type: 'nav_menu_item',
3283                      menu_id: update.post_id,
3284                      section: 'nav_menu[' + String( settingValue.nav_menu_term_id ) + ']',
3285                      priority: oldControl.priority.get(),
3286                      settings: {
3287                          'default': newCustomizeId
3288                      },
3289                      menu_item_id: update.post_id
3290                  } );
3291  
3292                  // Remove old control.
3293                  oldControl.container.remove();
3294                  api.control.remove( oldCustomizeId );
3295  
3296                  // Add new control to take its place.
3297                  api.control.add( newControl );
3298  
3299                  // Delete the placeholder and preview the new setting.
3300                  oldSetting.callbacks.disable(); // Prevent setting triggering Customizer dirty state when set.
3301                  oldSetting.set( false );
3302                  oldSetting.preview();
3303                  newSetting.preview();
3304                  oldSetting._dirty = false;
3305  
3306                  newControl.container.toggleClass( 'menu-item-edit-inactive', oldControl.container.hasClass( 'menu-item-edit-inactive' ) );
3307              }
3308          });
3309  
3310          /*
3311           * Update the settings for any nav_menu widgets that had selected a placeholder ID.
3312           */
3313          _.each( data.widget_nav_menu_updates, function( widgetSettingValue, widgetSettingId ) {
3314              var setting = api( widgetSettingId );
3315              if ( setting ) {
3316                  setting._value = widgetSettingValue;
3317                  setting.preview(); // Send to the preview now so that menu refresh will use the inserted menu.
3318              }
3319          });
3320      };
3321  
3322      /**
3323       * Focus a menu item control.
3324       *
3325       * @alias wp.customize.Menus.focusMenuItemControl
3326       *
3327       * @param {string} menuItemId
3328       */
3329      api.Menus.focusMenuItemControl = function( menuItemId ) {
3330          var control = api.Menus.getMenuItemControl( menuItemId );
3331          if ( control ) {
3332              control.focus();
3333          }
3334      };
3335  
3336      /**
3337       * Get the control for a given menu.
3338       *
3339       * @alias wp.customize.Menus.getMenuControl
3340       *
3341       * @param menuId
3342       * @return {wp.customize.controlConstructor.menus[]}
3343       */
3344      api.Menus.getMenuControl = function( menuId ) {
3345          return api.control( 'nav_menu[' + menuId + ']' );
3346      };
3347  
3348      /**
3349       * Given a menu item ID, get the control associated with it.
3350       *
3351       * @alias wp.customize.Menus.getMenuItemControl
3352       *
3353       * @param {string} menuItemId
3354       * @return {object|null}
3355       */
3356      api.Menus.getMenuItemControl = function( menuItemId ) {
3357          return api.control( menuItemIdToSettingId( menuItemId ) );
3358      };
3359  
3360      /**
3361       * @alias wp.customize.Menus~menuItemIdToSettingId
3362       *
3363       * @param {String} menuItemId
3364       */
3365  	function menuItemIdToSettingId( menuItemId ) {
3366          return 'nav_menu_item[' + menuItemId + ']';
3367      }
3368  
3369      /**
3370       * Apply sanitize_text_field()-like logic to the supplied name, returning a
3371       * "unnammed" fallback string if the name is then empty.
3372       *
3373       * @alias wp.customize.Menus~displayNavMenuName
3374       *
3375       * @param {string} name
3376       * @return {string}
3377       */
3378  	function displayNavMenuName( name ) {
3379          name = name || '';
3380          name = wp.sanitize.stripTagsAndEncodeText( name ); // Remove any potential tags from name.
3381          name = $.trim( name );
3382          return name || api.Menus.data.l10n.unnamed;
3383      }
3384  
3385  })( wp.customize, wp, jQuery );


Generated: Mon Dec 16 01:00:04 2019 Cross-referenced by PHPXref 0.7.1