[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> nav-menu.js (source)

   1  /**
   2   * WordPress Administration Navigation Menu
   3   * Interface JS functions
   4   *
   5   * @version 2.0.0
   6   *
   7   * @package WordPress
   8   * @subpackage Administration
   9   * @output wp-admin/js/nav-menu.js
  10   */
  11  
  12  /* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl, wpNavMenu */
  13  
  14  (function($) {
  15  
  16      var api;
  17  
  18      /**
  19       * Contains all the functions to handle WordPress navigation menus administration.
  20       *
  21       * @namespace wpNavMenu
  22       */
  23      api = window.wpNavMenu = {
  24  
  25          options : {
  26              menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead.
  27              globalMaxDepth:  11,
  28              sortableItems:   '> *',
  29              targetTolerance: 0
  30          },
  31  
  32          menuList : undefined,    // Set in init.
  33          targetList : undefined, // Set in init.
  34          menusChanged : false,
  35          isRTL: !! ( 'undefined' != typeof isRtl && isRtl ),
  36          negateIfRTL: ( 'undefined' != typeof isRtl && isRtl ) ? -1 : 1,
  37          lastSearch: '',
  38  
  39          // Functions that run on init.
  40          init : function() {
  41              api.menuList = $('#menu-to-edit');
  42              api.targetList = api.menuList;
  43  
  44              this.jQueryExtensions();
  45  
  46              this.attachMenuEditListeners();
  47  
  48              this.attachQuickSearchListeners();
  49              this.attachThemeLocationsListeners();
  50              this.attachMenuSaveSubmitListeners();
  51  
  52              this.attachTabsPanelListeners();
  53  
  54              this.attachUnsavedChangesListener();
  55  
  56              if ( api.menuList.length )
  57                  this.initSortables();
  58  
  59              if ( menus.oneThemeLocationNoMenus )
  60                  $( '#posttype-page' ).addSelectedToMenu( api.addMenuItemToBottom );
  61  
  62              this.initManageLocations();
  63  
  64              this.initAccessibility();
  65  
  66              this.initToggles();
  67  
  68              this.initPreviewing();
  69          },
  70  
  71          jQueryExtensions : function() {
  72              // jQuery extensions
  73              $.fn.extend({
  74                  menuItemDepth : function() {
  75                      var margin = api.isRTL ? this.eq(0).css('margin-right') : this.eq(0).css('margin-left');
  76                      return api.pxToDepth( margin && -1 != margin.indexOf('px') ? margin.slice(0, -2) : 0 );
  77                  },
  78                  updateDepthClass : function(current, prev) {
  79                      return this.each(function(){
  80                          var t = $(this);
  81                          prev = prev || t.menuItemDepth();
  82                          $(this).removeClass('menu-item-depth-'+ prev )
  83                              .addClass('menu-item-depth-'+ current );
  84                      });
  85                  },
  86                  shiftDepthClass : function(change) {
  87                      return this.each(function(){
  88                          var t = $(this),
  89                              depth = t.menuItemDepth(),
  90                              newDepth = depth + change;
  91  
  92                          t.removeClass( 'menu-item-depth-'+ depth )
  93                              .addClass( 'menu-item-depth-'+ ( newDepth ) );
  94  
  95                          if ( 0 === newDepth ) {
  96                              t.find( '.is-submenu' ).hide();
  97                          }
  98                      });
  99                  },
 100                  childMenuItems : function() {
 101                      var result = $();
 102                      this.each(function(){
 103                          var t = $(this), depth = t.menuItemDepth(), next = t.next( '.menu-item' );
 104                          while( next.length && next.menuItemDepth() > depth ) {
 105                              result = result.add( next );
 106                              next = next.next( '.menu-item' );
 107                          }
 108                      });
 109                      return result;
 110                  },
 111                  shiftHorizontally : function( dir ) {
 112                      return this.each(function(){
 113                          var t = $(this),
 114                              depth = t.menuItemDepth(),
 115                              newDepth = depth + dir;
 116  
 117                          // Change .menu-item-depth-n class
 118                          t.moveHorizontally( newDepth, depth );
 119                      });
 120                  },
 121                  moveHorizontally : function( newDepth, depth ) {
 122                      return this.each(function(){
 123                          var t = $(this),
 124                              children = t.childMenuItems(),
 125                              diff = newDepth - depth,
 126                              subItemText = t.find('.is-submenu');
 127  
 128                          // Change .menu-item-depth-n class
 129                          t.updateDepthClass( newDepth, depth ).updateParentMenuItemDBId();
 130  
 131                          // If it has children, move those too
 132                          if ( children ) {
 133                              children.each(function() {
 134                                  var t = $(this),
 135                                      thisDepth = t.menuItemDepth(),
 136                                      newDepth = thisDepth + diff;
 137                                  t.updateDepthClass(newDepth, thisDepth).updateParentMenuItemDBId();
 138                              });
 139                          }
 140  
 141                          // Show "Sub item" helper text
 142                          if (0 === newDepth)
 143                              subItemText.hide();
 144                          else
 145                              subItemText.show();
 146                      });
 147                  },
 148                  updateParentMenuItemDBId : function() {
 149                      return this.each(function(){
 150                          var item = $(this),
 151                              input = item.find( '.menu-item-data-parent-id' ),
 152                              depth = parseInt( item.menuItemDepth(), 10 ),
 153                              parentDepth = depth - 1,
 154                              parent = item.prevAll( '.menu-item-depth-' + parentDepth ).first();
 155  
 156                          if ( 0 === depth ) { // Item is on the top level, has no parent
 157                              input.val(0);
 158                          } else { // Find the parent item, and retrieve its object id.
 159                              input.val( parent.find( '.menu-item-data-db-id' ).val() );
 160                          }
 161                      });
 162                  },
 163                  hideAdvancedMenuItemFields : function() {
 164                      return this.each(function(){
 165                          var that = $(this);
 166                          $('.hide-column-tog').not(':checked').each(function(){
 167                              that.find('.field-' + $(this).val() ).addClass('hidden-field');
 168                          });
 169                      });
 170                  },
 171                  /**
 172                   * Adds selected menu items to the menu.
 173                   *
 174                   * @ignore
 175                   *
 176                   * @param jQuery metabox The metabox jQuery object.
 177                   */
 178                  addSelectedToMenu : function(processMethod) {
 179                      if ( 0 === $('#menu-to-edit').length ) {
 180                          return false;
 181                      }
 182  
 183                      return this.each(function() {
 184                          var t = $(this), menuItems = {},
 185                              checkboxes = ( menus.oneThemeLocationNoMenus && 0 === t.find( '.tabs-panel-active .categorychecklist li input:checked' ).length ) ? t.find( '#page-all li input[type="checkbox"]' ) : t.find( '.tabs-panel-active .categorychecklist li input:checked' ),
 186                              re = /menu-item\[([^\]]*)/;
 187  
 188                          processMethod = processMethod || api.addMenuItemToBottom;
 189  
 190                          // If no items are checked, bail.
 191                          if ( !checkboxes.length )
 192                              return false;
 193  
 194                          // Show the ajax spinner
 195                          t.find( '.button-controls .spinner' ).addClass( 'is-active' );
 196  
 197                          // Retrieve menu item data
 198                          $(checkboxes).each(function(){
 199                              var t = $(this),
 200                                  listItemDBIDMatch = re.exec( t.attr('name') ),
 201                                  listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
 202  
 203                              if ( this.className && -1 != this.className.indexOf('add-to-top') )
 204                                  processMethod = api.addMenuItemToTop;
 205                              menuItems[listItemDBID] = t.closest('li').getItemData( 'add-menu-item', listItemDBID );
 206                          });
 207  
 208                          // Add the items
 209                          api.addItemToMenu(menuItems, processMethod, function(){
 210                              // Deselect the items and hide the ajax spinner
 211                              checkboxes.removeAttr('checked');
 212                              t.find( '.button-controls .spinner' ).removeClass( 'is-active' );
 213                          });
 214                      });
 215                  },
 216                  getItemData : function( itemType, id ) {
 217                      itemType = itemType || 'menu-item';
 218  
 219                      var itemData = {}, i,
 220                      fields = [
 221                          'menu-item-db-id',
 222                          'menu-item-object-id',
 223                          'menu-item-object',
 224                          'menu-item-parent-id',
 225                          'menu-item-position',
 226                          'menu-item-type',
 227                          'menu-item-title',
 228                          'menu-item-url',
 229                          'menu-item-description',
 230                          'menu-item-attr-title',
 231                          'menu-item-target',
 232                          'menu-item-classes',
 233                          'menu-item-xfn'
 234                      ];
 235  
 236                      if( !id && itemType == 'menu-item' ) {
 237                          id = this.find('.menu-item-data-db-id').val();
 238                      }
 239  
 240                      if( !id ) return itemData;
 241  
 242                      this.find('input').each(function() {
 243                          var field;
 244                          i = fields.length;
 245                          while ( i-- ) {
 246                              if( itemType == 'menu-item' )
 247                                  field = fields[i] + '[' + id + ']';
 248                              else if( itemType == 'add-menu-item' )
 249                                  field = 'menu-item[' + id + '][' + fields[i] + ']';
 250  
 251                              if (
 252                                  this.name &&
 253                                  field == this.name
 254                              ) {
 255                                  itemData[fields[i]] = this.value;
 256                              }
 257                          }
 258                      });
 259  
 260                      return itemData;
 261                  },
 262                  setItemData : function( itemData, itemType, id ) { // Can take a type, such as 'menu-item', or an id.
 263                      itemType = itemType || 'menu-item';
 264  
 265                      if( !id && itemType == 'menu-item' ) {
 266                          id = $('.menu-item-data-db-id', this).val();
 267                      }
 268  
 269                      if( !id ) return this;
 270  
 271                      this.find('input').each(function() {
 272                          var t = $(this), field;
 273                          $.each( itemData, function( attr, val ) {
 274                              if( itemType == 'menu-item' )
 275                                  field = attr + '[' + id + ']';
 276                              else if( itemType == 'add-menu-item' )
 277                                  field = 'menu-item[' + id + '][' + attr + ']';
 278  
 279                              if ( field == t.attr('name') ) {
 280                                  t.val( val );
 281                              }
 282                          });
 283                      });
 284                      return this;
 285                  }
 286              });
 287          },
 288  
 289          countMenuItems : function( depth ) {
 290              return $( '.menu-item-depth-' + depth ).length;
 291          },
 292  
 293          moveMenuItem : function( $this, dir ) {
 294  
 295              var items, newItemPosition, newDepth,
 296                  menuItems = $( '#menu-to-edit li' ),
 297                  menuItemsCount = menuItems.length,
 298                  thisItem = $this.parents( 'li.menu-item' ),
 299                  thisItemChildren = thisItem.childMenuItems(),
 300                  thisItemData = thisItem.getItemData(),
 301                  thisItemDepth = parseInt( thisItem.menuItemDepth(), 10 ),
 302                  thisItemPosition = parseInt( thisItem.index(), 10 ),
 303                  nextItem = thisItem.next(),
 304                  nextItemChildren = nextItem.childMenuItems(),
 305                  nextItemDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1,
 306                  prevItem = thisItem.prev(),
 307                  prevItemDepth = parseInt( prevItem.menuItemDepth(), 10 ),
 308                  prevItemId = prevItem.getItemData()['menu-item-db-id'];
 309  
 310              switch ( dir ) {
 311              case 'up':
 312                  newItemPosition = thisItemPosition - 1;
 313  
 314                  // Already at top
 315                  if ( 0 === thisItemPosition )
 316                      break;
 317  
 318                  // If a sub item is moved to top, shift it to 0 depth
 319                  if ( 0 === newItemPosition && 0 !== thisItemDepth )
 320                      thisItem.moveHorizontally( 0, thisItemDepth );
 321  
 322                  // If prev item is sub item, shift to match depth
 323                  if ( 0 !== prevItemDepth )
 324                      thisItem.moveHorizontally( prevItemDepth, thisItemDepth );
 325  
 326                  // Does this item have sub items?
 327                  if ( thisItemChildren ) {
 328                      items = thisItem.add( thisItemChildren );
 329                      // Move the entire block
 330                      items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
 331                  } else {
 332                      thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
 333                  }
 334                  break;
 335              case 'down':
 336                  // Does this item have sub items?
 337                  if ( thisItemChildren ) {
 338                      items = thisItem.add( thisItemChildren ),
 339                          nextItem = menuItems.eq( items.length + thisItemPosition ),
 340                          nextItemChildren = 0 !== nextItem.childMenuItems().length;
 341  
 342                      if ( nextItemChildren ) {
 343                          newDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1;
 344                          thisItem.moveHorizontally( newDepth, thisItemDepth );
 345                      }
 346  
 347                      // Have we reached the bottom?
 348                      if ( menuItemsCount === thisItemPosition + items.length )
 349                          break;
 350  
 351                      items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId();
 352                  } else {
 353                      // If next item has sub items, shift depth
 354                      if ( 0 !== nextItemChildren.length )
 355                          thisItem.moveHorizontally( nextItemDepth, thisItemDepth );
 356  
 357                      // Have we reached the bottom
 358                      if ( menuItemsCount === thisItemPosition + 1 )
 359                          break;
 360                      thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId();
 361                  }
 362                  break;
 363              case 'top':
 364                  // Already at top
 365                  if ( 0 === thisItemPosition )
 366                      break;
 367                  // Does this item have sub items?
 368                  if ( thisItemChildren ) {
 369                      items = thisItem.add( thisItemChildren );
 370                      // Move the entire block
 371                      items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
 372                  } else {
 373                      thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
 374                  }
 375                  break;
 376              case 'left':
 377                  // As far left as possible
 378                  if ( 0 === thisItemDepth )
 379                      break;
 380                  thisItem.shiftHorizontally( -1 );
 381                  break;
 382              case 'right':
 383                  // Can't be sub item at top
 384                  if ( 0 === thisItemPosition )
 385                      break;
 386                  // Already sub item of prevItem
 387                  if ( thisItemData['menu-item-parent-id'] === prevItemId )
 388                      break;
 389                  thisItem.shiftHorizontally( 1 );
 390                  break;
 391              }
 392              $this.focus();
 393              api.registerChange();
 394              api.refreshKeyboardAccessibility();
 395              api.refreshAdvancedAccessibility();
 396          },
 397  
 398          initAccessibility : function() {
 399              var menu = $( '#menu-to-edit' );
 400  
 401              api.refreshKeyboardAccessibility();
 402              api.refreshAdvancedAccessibility();
 403  
 404              // Refresh the accessibility when the user comes close to the item in any way
 405              menu.on( 'mouseenter.refreshAccessibility focus.refreshAccessibility touchstart.refreshAccessibility' , '.menu-item' , function(){
 406                  api.refreshAdvancedAccessibilityOfItem( $( this ).find( 'a.item-edit' ) );
 407              } );
 408  
 409              // We have to update on click as well because we might hover first, change the item, and then click.
 410              menu.on( 'click', 'a.item-edit', function() {
 411                  api.refreshAdvancedAccessibilityOfItem( $( this ) );
 412              } );
 413  
 414              // Links for moving items
 415              menu.on( 'click', '.menus-move', function () {
 416                  var $this = $( this ),
 417                      dir = $this.data( 'dir' );
 418  
 419                  if ( 'undefined' !== typeof dir ) {
 420                      api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), dir );
 421                  }
 422              });
 423          },
 424  
 425          /**
 426           * refreshAdvancedAccessibilityOfItem( [itemToRefresh] )
 427           *
 428           * Refreshes advanced accessibility buttons for one menu item.
 429           * Shows or hides buttons based on the location of the menu item.
 430           *
 431           * @param  {object} itemToRefresh The menu item that might need its advanced accessibility buttons refreshed
 432           */
 433          refreshAdvancedAccessibilityOfItem : function( itemToRefresh ) {
 434  
 435              // Only refresh accessibility when necessary
 436              if ( true !== $( itemToRefresh ).data( 'needs_accessibility_refresh' ) ) {
 437                  return;
 438              }
 439  
 440              var thisLink, thisLinkText, primaryItems, itemPosition, title,
 441                  parentItem, parentItemId, parentItemName, subItems,
 442                  $this = $( itemToRefresh ),
 443                  menuItem = $this.closest( 'li.menu-item' ).first(),
 444                  depth = menuItem.menuItemDepth(),
 445                  isPrimaryMenuItem = ( 0 === depth ),
 446                  itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(),
 447                  position = parseInt( menuItem.index(), 10 ),
 448                  prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ),
 449                  prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(),
 450                  prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(),
 451                  totalMenuItems = $('#menu-to-edit li').length,
 452                  hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length;
 453  
 454                  menuItem.find( '.field-move' ).toggle( totalMenuItems > 1 );
 455  
 456              // Where can they move this menu item?
 457              if ( 0 !== position ) {
 458                  thisLink = menuItem.find( '.menus-move-up' );
 459                  thisLink.attr( 'aria-label', menus.moveUp ).css( 'display', 'inline' );
 460              }
 461  
 462              if ( 0 !== position && isPrimaryMenuItem ) {
 463                  thisLink = menuItem.find( '.menus-move-top' );
 464                  thisLink.attr( 'aria-label', menus.moveToTop ).css( 'display', 'inline' );
 465              }
 466  
 467              if ( position + 1 !== totalMenuItems && 0 !== position ) {
 468                  thisLink = menuItem.find( '.menus-move-down' );
 469                  thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' );
 470              }
 471  
 472              if ( 0 === position && 0 !== hasSameDepthSibling ) {
 473                  thisLink = menuItem.find( '.menus-move-down' );
 474                  thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' );
 475              }
 476  
 477              if ( ! isPrimaryMenuItem ) {
 478                  thisLink = menuItem.find( '.menus-move-left' ),
 479                  thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft );
 480                  thisLink.attr( 'aria-label', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).text( thisLinkText ).css( 'display', 'inline' );
 481              }
 482  
 483              if ( 0 !== position ) {
 484                  if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) {
 485                      thisLink = menuItem.find( '.menus-move-right' ),
 486                      thisLinkText = menus.under.replace( '%s', prevItemNameRight );
 487                      thisLink.attr( 'aria-label', menus.moveUnder.replace( '%s', prevItemNameRight ) ).text( thisLinkText ).css( 'display', 'inline' );
 488                  }
 489              }
 490  
 491              if ( isPrimaryMenuItem ) {
 492                  primaryItems = $( '.menu-item-depth-0' ),
 493                  itemPosition = primaryItems.index( menuItem ) + 1,
 494                  totalMenuItems = primaryItems.length,
 495  
 496                  // String together help text for primary menu items
 497                  title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems );
 498              } else {
 499                  parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(),
 500                  parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(),
 501                  parentItemName = parentItem.find( '.menu-item-title' ).text(),
 502                  subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ),
 503                  itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1;
 504  
 505                  // String together help text for sub menu items
 506                  title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName );
 507              }
 508  
 509              $this.attr( 'aria-label', title );
 510  
 511              // Mark this item's accessibility as refreshed
 512              $this.data( 'needs_accessibility_refresh', false );
 513          },
 514  
 515          /**
 516           * refreshAdvancedAccessibility
 517           *
 518           * Hides all advanced accessibility buttons and marks them for refreshing.
 519           */
 520          refreshAdvancedAccessibility : function() {
 521  
 522              // Hide all the move buttons by default.
 523              $( '.menu-item-settings .field-move .menus-move' ).hide();
 524  
 525              // Mark all menu items as unprocessed
 526              $( 'a.item-edit' ).data( 'needs_accessibility_refresh', true );
 527  
 528              // All open items have to be refreshed or they will show no links
 529              $( '.menu-item-edit-active a.item-edit' ).each( function() {
 530                  api.refreshAdvancedAccessibilityOfItem( this );
 531              } );
 532          },
 533  
 534          refreshKeyboardAccessibility : function() {
 535              $( 'a.item-edit' ).off( 'focus' ).on( 'focus', function(){
 536                  $(this).off( 'keydown' ).on( 'keydown', function(e){
 537  
 538                      var arrows,
 539                          $this = $( this ),
 540                          thisItem = $this.parents( 'li.menu-item' ),
 541                          thisItemData = thisItem.getItemData();
 542  
 543                      // Bail if it's not an arrow key
 544                      if ( 37 != e.which && 38 != e.which && 39 != e.which && 40 != e.which )
 545                          return;
 546  
 547                      // Avoid multiple keydown events
 548                      $this.off('keydown');
 549  
 550                      // Bail if there is only one menu item
 551                      if ( 1 === $('#menu-to-edit li').length )
 552                          return;
 553  
 554                      // If RTL, swap left/right arrows
 555                      arrows = { '38': 'up', '40': 'down', '37': 'left', '39': 'right' };
 556                      if ( $('body').hasClass('rtl') )
 557                          arrows = { '38' : 'up', '40' : 'down', '39' : 'left', '37' : 'right' };
 558  
 559                      switch ( arrows[e.which] ) {
 560                      case 'up':
 561                          api.moveMenuItem( $this, 'up' );
 562                          break;
 563                      case 'down':
 564                          api.moveMenuItem( $this, 'down' );
 565                          break;
 566                      case 'left':
 567                          api.moveMenuItem( $this, 'left' );
 568                          break;
 569                      case 'right':
 570                          api.moveMenuItem( $this, 'right' );
 571                          break;
 572                      }
 573                      // Put focus back on same menu item
 574                      $( '#edit-' + thisItemData['menu-item-db-id'] ).focus();
 575                      return false;
 576                  });
 577              });
 578          },
 579  
 580          initPreviewing : function() {
 581              // Update the item handle title when the navigation label is changed.
 582              $( '#menu-to-edit' ).on( 'change input', '.edit-menu-item-title', function(e) {
 583                  var input = $( e.currentTarget ), title, titleEl;
 584                  title = input.val();
 585                  titleEl = input.closest( '.menu-item' ).find( '.menu-item-title' );
 586                  // Don't update to empty title.
 587                  if ( title ) {
 588                      titleEl.text( title ).removeClass( 'no-title' );
 589                  } else {
 590                      titleEl.text( navMenuL10n.untitled ).addClass( 'no-title' );
 591                  }
 592              } );
 593          },
 594  
 595          initToggles : function() {
 596              // init postboxes
 597              postboxes.add_postbox_toggles('nav-menus');
 598  
 599              // adjust columns functions for menus UI
 600              columns.useCheckboxesForHidden();
 601              columns.checked = function(field) {
 602                  $('.field-' + field).removeClass('hidden-field');
 603              };
 604              columns.unchecked = function(field) {
 605                  $('.field-' + field).addClass('hidden-field');
 606              };
 607              // hide fields
 608              api.menuList.hideAdvancedMenuItemFields();
 609  
 610              $('.hide-postbox-tog').click(function () {
 611                  var hidden = $( '.accordion-container li.accordion-section' ).filter(':hidden').map(function() { return this.id; }).get().join(',');
 612                  $.post(ajaxurl, {
 613                      action: 'closed-postboxes',
 614                      hidden: hidden,
 615                      closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(),
 616                      page: 'nav-menus'
 617                  });
 618              });
 619          },
 620  
 621          initSortables : function() {
 622              var currentDepth = 0, originalDepth, minDepth, maxDepth,
 623                  prev, next, prevBottom, nextThreshold, helperHeight, transport,
 624                  menuEdge = api.menuList.offset().left,
 625                  body = $('body'), maxChildDepth,
 626                  menuMaxDepth = initialMenuMaxDepth();
 627  
 628              if( 0 !== $( '#menu-to-edit li' ).length )
 629                  $( '.drag-instructions' ).show();
 630  
 631              // Use the right edge if RTL.
 632              menuEdge += api.isRTL ? api.menuList.width() : 0;
 633  
 634              api.menuList.sortable({
 635                  handle: '.menu-item-handle',
 636                  placeholder: 'sortable-placeholder',
 637                  items: api.options.sortableItems,
 638                  start: function(e, ui) {
 639                      var height, width, parent, children, tempHolder;
 640  
 641                      // handle placement for rtl orientation
 642                      if ( api.isRTL )
 643                          ui.item[0].style.right = 'auto';
 644  
 645                      transport = ui.item.children('.menu-item-transport');
 646  
 647                      // Set depths. currentDepth must be set before children are located.
 648                      originalDepth = ui.item.menuItemDepth();
 649                      updateCurrentDepth(ui, originalDepth);
 650  
 651                      // Attach child elements to parent
 652                      // Skip the placeholder
 653                      parent = ( ui.item.next()[0] == ui.placeholder[0] ) ? ui.item.next() : ui.item;
 654                      children = parent.childMenuItems();
 655                      transport.append( children );
 656  
 657                      // Update the height of the placeholder to match the moving item.
 658                      height = transport.outerHeight();
 659                      // If there are children, account for distance between top of children and parent
 660                      height += ( height > 0 ) ? (ui.placeholder.css('margin-top').slice(0, -2) * 1) : 0;
 661                      height += ui.helper.outerHeight();
 662                      helperHeight = height;
 663                      height -= 2; // Subtract 2 for borders
 664                      ui.placeholder.height(height);
 665  
 666                      // Update the width of the placeholder to match the moving item.
 667                      maxChildDepth = originalDepth;
 668                      children.each(function(){
 669                          var depth = $(this).menuItemDepth();
 670                          maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth;
 671                      });
 672                      width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width
 673                      width += api.depthToPx(maxChildDepth - originalDepth); // Account for children
 674                      width -= 2; // Subtract 2 for borders
 675                      ui.placeholder.width(width);
 676  
 677                      // Update the list of menu items.
 678                      tempHolder = ui.placeholder.next( '.menu-item' );
 679                      tempHolder.css( 'margin-top', helperHeight + 'px' ); // Set the margin to absorb the placeholder
 680                      ui.placeholder.detach(); // detach or jQuery UI will think the placeholder is a menu item
 681                      $(this).sortable( 'refresh' ); // The children aren't sortable. We should let jQ UI know.
 682                      ui.item.after( ui.placeholder ); // reattach the placeholder.
 683                      tempHolder.css('margin-top', 0); // reset the margin
 684  
 685                      // Now that the element is complete, we can update...
 686                      updateSharedVars(ui);
 687                  },
 688                  stop: function(e, ui) {
 689                      var children, subMenuTitle,
 690                          depthChange = currentDepth - originalDepth;
 691  
 692                      // Return child elements to the list
 693                      children = transport.children().insertAfter(ui.item);
 694  
 695                      // Add "sub menu" description
 696                      subMenuTitle = ui.item.find( '.item-title .is-submenu' );
 697                      if ( 0 < currentDepth )
 698                          subMenuTitle.show();
 699                      else
 700                          subMenuTitle.hide();
 701  
 702                      // Update depth classes
 703                      if ( 0 !== depthChange ) {
 704                          ui.item.updateDepthClass( currentDepth );
 705                          children.shiftDepthClass( depthChange );
 706                          updateMenuMaxDepth( depthChange );
 707                      }
 708                      // Register a change
 709                      api.registerChange();
 710                      // Update the item data.
 711                      ui.item.updateParentMenuItemDBId();
 712  
 713                      // address sortable's incorrectly-calculated top in opera
 714                      ui.item[0].style.top = 0;
 715  
 716                      // handle drop placement for rtl orientation
 717                      if ( api.isRTL ) {
 718                          ui.item[0].style.left = 'auto';
 719                          ui.item[0].style.right = 0;
 720                      }
 721  
 722                      api.refreshKeyboardAccessibility();
 723                      api.refreshAdvancedAccessibility();
 724                  },
 725                  change: function(e, ui) {
 726                      // Make sure the placeholder is inside the menu.
 727                      // Otherwise fix it, or we're in trouble.
 728                      if( ! ui.placeholder.parent().hasClass('menu') )
 729                          (prev.length) ? prev.after( ui.placeholder ) : api.menuList.prepend( ui.placeholder );
 730  
 731                      updateSharedVars(ui);
 732                  },
 733                  sort: function(e, ui) {
 734                      var offset = ui.helper.offset(),
 735                          edge = api.isRTL ? offset.left + ui.helper.width() : offset.left,
 736                          depth = api.negateIfRTL * api.pxToDepth( edge - menuEdge );
 737  
 738                      // Check and correct if depth is not within range.
 739                      // Also, if the dragged element is dragged upwards over
 740                      // an item, shift the placeholder to a child position.
 741                      if ( depth > maxDepth || offset.top < ( prevBottom - api.options.targetTolerance ) ) {
 742                          depth = maxDepth;
 743                      } else if ( depth < minDepth ) {
 744                          depth = minDepth;
 745                      }
 746  
 747                      if( depth != currentDepth )
 748                          updateCurrentDepth(ui, depth);
 749  
 750                      // If we overlap the next element, manually shift downwards
 751                      if( nextThreshold && offset.top + helperHeight > nextThreshold ) {
 752                          next.after( ui.placeholder );
 753                          updateSharedVars( ui );
 754                          $( this ).sortable( 'refreshPositions' );
 755                      }
 756                  }
 757              });
 758  
 759  			function updateSharedVars(ui) {
 760                  var depth;
 761  
 762                  prev = ui.placeholder.prev( '.menu-item' );
 763                  next = ui.placeholder.next( '.menu-item' );
 764  
 765                  // Make sure we don't select the moving item.
 766                  if( prev[0] == ui.item[0] ) prev = prev.prev( '.menu-item' );
 767                  if( next[0] == ui.item[0] ) next = next.next( '.menu-item' );
 768  
 769                  prevBottom = (prev.length) ? prev.offset().top + prev.height() : 0;
 770                  nextThreshold = (next.length) ? next.offset().top + next.height() / 3 : 0;
 771                  minDepth = (next.length) ? next.menuItemDepth() : 0;
 772  
 773                  if( prev.length )
 774                      maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth;
 775                  else
 776                      maxDepth = 0;
 777              }
 778  
 779  			function updateCurrentDepth(ui, depth) {
 780                  ui.placeholder.updateDepthClass( depth, currentDepth );
 781                  currentDepth = depth;
 782              }
 783  
 784  			function initialMenuMaxDepth() {
 785                  if( ! body[0].className ) return 0;
 786                  var match = body[0].className.match(/menu-max-depth-(\d+)/);
 787                  return match && match[1] ? parseInt( match[1], 10 ) : 0;
 788              }
 789  
 790  			function updateMenuMaxDepth( depthChange ) {
 791                  var depth, newDepth = menuMaxDepth;
 792                  if ( depthChange === 0 ) {
 793                      return;
 794                  } else if ( depthChange > 0 ) {
 795                      depth = maxChildDepth + depthChange;
 796                      if( depth > menuMaxDepth )
 797                          newDepth = depth;
 798                  } else if ( depthChange < 0 && maxChildDepth == menuMaxDepth ) {
 799                      while( ! $('.menu-item-depth-' + newDepth, api.menuList).length && newDepth > 0 )
 800                          newDepth--;
 801                  }
 802                  // Update the depth class.
 803                  body.removeClass( 'menu-max-depth-' + menuMaxDepth ).addClass( 'menu-max-depth-' + newDepth );
 804                  menuMaxDepth = newDepth;
 805              }
 806          },
 807  
 808          initManageLocations : function () {
 809              $('#menu-locations-wrap form').submit(function(){
 810                  window.onbeforeunload = null;
 811              });
 812              $('.menu-location-menus select').on('change', function () {
 813                  var editLink = $(this).closest('tr').find('.locations-edit-menu-link');
 814                  if ($(this).find('option:selected').data('orig'))
 815                      editLink.show();
 816                  else
 817                      editLink.hide();
 818              });
 819          },
 820  
 821          attachMenuEditListeners : function() {
 822              var that = this;
 823              $('#update-nav-menu').bind('click', function(e) {
 824                  if ( e.target && e.target.className ) {
 825                      if ( -1 != e.target.className.indexOf('item-edit') ) {
 826                          return that.eventOnClickEditLink(e.target);
 827                      } else if ( -1 != e.target.className.indexOf('menu-save') ) {
 828                          return that.eventOnClickMenuSave(e.target);
 829                      } else if ( -1 != e.target.className.indexOf('menu-delete') ) {
 830                          return that.eventOnClickMenuDelete(e.target);
 831                      } else if ( -1 != e.target.className.indexOf('item-delete') ) {
 832                          return that.eventOnClickMenuItemDelete(e.target);
 833                      } else if ( -1 != e.target.className.indexOf('item-cancel') ) {
 834                          return that.eventOnClickCancelLink(e.target);
 835                      }
 836                  }
 837              });
 838  
 839              $( '#menu-name' ).on( 'input', _.debounce( function () {
 840                  var menuName = $( document.getElementById( 'menu-name' ) ),
 841                      menuNameVal = menuName.val();
 842  
 843                  if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) {
 844                      // Add warning for invalid menu name.
 845                      menuName.parent().addClass( 'form-invalid' );
 846                  } else {
 847                      // Remove warning for valid menu name.
 848                      menuName.parent().removeClass( 'form-invalid' );
 849                  }
 850              }, 500 ) );
 851  
 852              $('#add-custom-links input[type="text"]').keypress(function(e){
 853                  $('#customlinkdiv').removeClass('form-invalid');
 854  
 855                  if ( e.keyCode === 13 ) {
 856                      e.preventDefault();
 857                      $( '#submit-customlinkdiv' ).click();
 858                  }
 859              });
 860          },
 861  
 862          attachMenuSaveSubmitListeners : function() {
 863              /*
 864               * When a navigation menu is saved, store a JSON representation of all form data
 865               * in a single input to avoid PHP `max_input_vars` limitations. See #14134.
 866               */
 867              $( '#update-nav-menu' ).submit( function() {
 868                  var navMenuData = $( '#update-nav-menu' ).serializeArray();
 869                  $( '[name="nav-menu-data"]' ).val( JSON.stringify( navMenuData ) );
 870              });
 871          },
 872  
 873          attachThemeLocationsListeners : function() {
 874              var loc = $('#nav-menu-theme-locations'), params = {};
 875              params.action = 'menu-locations-save';
 876              params['menu-settings-column-nonce'] = $('#menu-settings-column-nonce').val();
 877              loc.find('input[type="submit"]').click(function() {
 878                  loc.find('select').each(function() {
 879                      params[this.name] = $(this).val();
 880                  });
 881                  loc.find( '.spinner' ).addClass( 'is-active' );
 882                  $.post( ajaxurl, params, function() {
 883                      loc.find( '.spinner' ).removeClass( 'is-active' );
 884                  });
 885                  return false;
 886              });
 887          },
 888  
 889          attachQuickSearchListeners : function() {
 890              var searchTimer;
 891  
 892              // Prevent form submission.
 893              $( '#nav-menu-meta' ).on( 'submit', function( event ) {
 894                  event.preventDefault();
 895              });
 896  
 897              $( '#nav-menu-meta' ).on( 'input', '.quick-search', function() {
 898                  var $this = $( this );
 899  
 900                  $this.attr( 'autocomplete', 'off' );
 901  
 902                  if ( searchTimer ) {
 903                      clearTimeout( searchTimer );
 904                  }
 905  
 906                  searchTimer = setTimeout( function() {
 907                      api.updateQuickSearchResults( $this );
 908                   }, 500 );
 909              }).on( 'blur', '.quick-search', function() {
 910                  api.lastSearch = '';
 911              });
 912          },
 913  
 914          updateQuickSearchResults : function(input) {
 915              var panel, params,
 916                  minSearchLength = 2,
 917                  q = input.val();
 918  
 919              /*
 920               * Minimum characters for a search. Also avoid a new AJAX search when
 921               * the pressed key (e.g. arrows) doesn't change the searched term.
 922               */
 923              if ( q.length < minSearchLength || api.lastSearch == q ) {
 924                  return;
 925              }
 926  
 927              api.lastSearch = q;
 928  
 929              panel = input.parents('.tabs-panel');
 930              params = {
 931                  'action': 'menu-quick-search',
 932                  'response-format': 'markup',
 933                  'menu': $('#menu').val(),
 934                  'menu-settings-column-nonce': $('#menu-settings-column-nonce').val(),
 935                  'q': q,
 936                  'type': input.attr('name')
 937              };
 938  
 939              $( '.spinner', panel ).addClass( 'is-active' );
 940  
 941              $.post( ajaxurl, params, function(menuMarkup) {
 942                  api.processQuickSearchQueryResponse(menuMarkup, params, panel);
 943              });
 944          },
 945  
 946          addCustomLink : function( processMethod ) {
 947              var url = $('#custom-menu-item-url').val().trim(),
 948                  label = $('#custom-menu-item-name').val();
 949  
 950              processMethod = processMethod || api.addMenuItemToBottom;
 951  
 952              if ( '' === url || 'http://' == url ) {
 953                  $('#customlinkdiv').addClass('form-invalid');
 954                  return false;
 955              }
 956  
 957              // Show the ajax spinner
 958              $( '.customlinkdiv .spinner' ).addClass( 'is-active' );
 959              this.addLinkToMenu( url, label, processMethod, function() {
 960                  // Remove the ajax spinner
 961                  $( '.customlinkdiv .spinner' ).removeClass( 'is-active' );
 962                  // Set custom link form back to defaults
 963                  $('#custom-menu-item-name').val('').blur();
 964                  $('#custom-menu-item-url').val('http://');
 965              });
 966          },
 967  
 968          addLinkToMenu : function(url, label, processMethod, callback) {
 969              processMethod = processMethod || api.addMenuItemToBottom;
 970              callback = callback || function(){};
 971  
 972              api.addItemToMenu({
 973                  '-1': {
 974                      'menu-item-type': 'custom',
 975                      'menu-item-url': url,
 976                      'menu-item-title': label
 977                  }
 978              }, processMethod, callback);
 979          },
 980  
 981          addItemToMenu : function(menuItem, processMethod, callback) {
 982              var menu = $('#menu').val(),
 983                  nonce = $('#menu-settings-column-nonce').val(),
 984                  params;
 985  
 986              processMethod = processMethod || function(){};
 987              callback = callback || function(){};
 988  
 989              params = {
 990                  'action': 'add-menu-item',
 991                  'menu': menu,
 992                  'menu-settings-column-nonce': nonce,
 993                  'menu-item': menuItem
 994              };
 995  
 996              $.post( ajaxurl, params, function(menuMarkup) {
 997                  var ins = $('#menu-instructions');
 998  
 999                  menuMarkup = $.trim( menuMarkup ); // Trim leading whitespaces
1000                  processMethod(menuMarkup, params);
1001  
1002                  // Make it stand out a bit more visually, by adding a fadeIn
1003                  $( 'li.pending' ).hide().fadeIn('slow');
1004                  $( '.drag-instructions' ).show();
1005                  if( ! ins.hasClass( 'menu-instructions-inactive' ) && ins.siblings().length )
1006                      ins.addClass( 'menu-instructions-inactive' );
1007  
1008                  callback();
1009              });
1010          },
1011  
1012          /**
1013           * Process the add menu item request response into menu list item. Appends to menu.
1014           *
1015           * @param {string} menuMarkup The text server response of menu item markup.
1016           *
1017           * @fires document#menu-item-added Passes menuMarkup as a jQuery object.
1018           */
1019          addMenuItemToBottom : function( menuMarkup ) {
1020              var $menuMarkup = $( menuMarkup );
1021              $menuMarkup.hideAdvancedMenuItemFields().appendTo( api.targetList );
1022              api.refreshKeyboardAccessibility();
1023              api.refreshAdvancedAccessibility();
1024              $( document ).trigger( 'menu-item-added', [ $menuMarkup ] );
1025          },
1026  
1027          /**
1028           * Process the add menu item request response into menu list item. Prepends to menu.
1029           *
1030           * @param {string} menuMarkup The text server response of menu item markup.
1031           *
1032           * @fires document#menu-item-added Passes menuMarkup as a jQuery object.
1033           */
1034          addMenuItemToTop : function( menuMarkup ) {
1035              var $menuMarkup = $( menuMarkup );
1036              $menuMarkup.hideAdvancedMenuItemFields().prependTo( api.targetList );
1037              api.refreshKeyboardAccessibility();
1038              api.refreshAdvancedAccessibility();
1039              $( document ).trigger( 'menu-item-added', [ $menuMarkup ] );
1040          },
1041  
1042          attachUnsavedChangesListener : function() {
1043              $('#menu-management input, #menu-management select, #menu-management, #menu-management textarea, .menu-location-menus select').change(function(){
1044                  api.registerChange();
1045              });
1046  
1047              if ( 0 !== $('#menu-to-edit').length || 0 !== $('.menu-location-menus select').length ) {
1048                  window.onbeforeunload = function(){
1049                      if ( api.menusChanged )
1050                          return navMenuL10n.saveAlert;
1051                  };
1052              } else {
1053                  // Make the post boxes read-only, as they can't be used yet
1054                  $( '#menu-settings-column' ).find( 'input,select' ).end().find( 'a' ).attr( 'href', '#' ).unbind( 'click' );
1055              }
1056          },
1057  
1058          registerChange : function() {
1059              api.menusChanged = true;
1060          },
1061  
1062          attachTabsPanelListeners : function() {
1063              $('#menu-settings-column').bind('click', function(e) {
1064                  var selectAreaMatch, panelId, wrapper, items,
1065                      target = $(e.target);
1066  
1067                  if ( target.hasClass('nav-tab-link') ) {
1068  
1069                      panelId = target.data( 'type' );
1070  
1071                      wrapper = target.parents('.accordion-section-content').first();
1072  
1073                      // upon changing tabs, we want to uncheck all checkboxes
1074                      $('input', wrapper).removeAttr('checked');
1075  
1076                      $('.tabs-panel-active', wrapper).removeClass('tabs-panel-active').addClass('tabs-panel-inactive');
1077                      $('#' + panelId, wrapper).removeClass('tabs-panel-inactive').addClass('tabs-panel-active');
1078  
1079                      $('.tabs', wrapper).removeClass('tabs');
1080                      target.parent().addClass('tabs');
1081  
1082                      // select the search bar
1083                      $('.quick-search', wrapper).focus();
1084  
1085                      // Hide controls in the search tab if no items found.
1086                      if ( ! wrapper.find( '.tabs-panel-active .menu-item-title' ).length ) {
1087                          wrapper.addClass( 'has-no-menu-item' );
1088                      } else {
1089                          wrapper.removeClass( 'has-no-menu-item' );
1090                      }
1091  
1092                      e.preventDefault();
1093                  } else if ( target.hasClass('select-all') ) {
1094                      selectAreaMatch = /#(.*)$/.exec(e.target.href);
1095                      if ( selectAreaMatch && selectAreaMatch[1] ) {
1096                          items = $('#' + selectAreaMatch[1] + ' .tabs-panel-active .menu-item-title input');
1097                          if( items.length === items.filter(':checked').length )
1098                              items.removeAttr('checked');
1099                          else
1100                              items.prop('checked', true);
1101                          return false;
1102                      }
1103                  } else if ( target.hasClass('submit-add-to-menu') ) {
1104                      api.registerChange();
1105  
1106                      if ( e.target.id && 'submit-customlinkdiv' == e.target.id )
1107                          api.addCustomLink( api.addMenuItemToBottom );
1108                      else if ( e.target.id && -1 != e.target.id.indexOf('submit-') )
1109                          $('#' + e.target.id.replace(/submit-/, '')).addSelectedToMenu( api.addMenuItemToBottom );
1110                      return false;
1111                  }
1112              });
1113  
1114              /*
1115               * Delegate the `click` event and attach it just to the pagination
1116               * links thus excluding the current page `<span>`. See ticket #35577.
1117               */
1118              $( '#nav-menu-meta' ).on( 'click', 'a.page-numbers', function() {
1119                  var $container = $( this ).closest( '.inside' );
1120  
1121                  $.post( ajaxurl, this.href.replace( /.*\?/, '' ).replace( /action=([^&]*)/, '' ) + '&action=menu-get-metabox',
1122                      function( resp ) {
1123                          var metaBoxData = $.parseJSON( resp ),
1124                              toReplace;
1125  
1126                          if ( -1 === resp.indexOf( 'replace-id' ) ) {
1127                              return;
1128                          }
1129  
1130                          // Get the post type menu meta box to update.
1131                          toReplace = document.getElementById( metaBoxData['replace-id'] );
1132  
1133                          if ( ! metaBoxData.markup || ! toReplace ) {
1134                              return;
1135                          }
1136  
1137                          // Update the post type menu meta box with new content from the response.
1138                          $container.html( metaBoxData.markup );
1139                      }
1140                  );
1141  
1142                  return false;
1143              });
1144          },
1145  
1146          eventOnClickEditLink : function(clickedEl) {
1147              var settings, item,
1148              matchedSection = /#(.*)$/.exec(clickedEl.href);
1149              if ( matchedSection && matchedSection[1] ) {
1150                  settings = $('#'+matchedSection[1]);
1151                  item = settings.parent();
1152                  if( 0 !== item.length ) {
1153                      if( item.hasClass('menu-item-edit-inactive') ) {
1154                          if( ! settings.data('menu-item-data') ) {
1155                              settings.data( 'menu-item-data', settings.getItemData() );
1156                          }
1157                          settings.slideDown('fast');
1158                          item.removeClass('menu-item-edit-inactive')
1159                              .addClass('menu-item-edit-active');
1160                      } else {
1161                          settings.slideUp('fast');
1162                          item.removeClass('menu-item-edit-active')
1163                              .addClass('menu-item-edit-inactive');
1164                      }
1165                      return false;
1166                  }
1167              }
1168          },
1169  
1170          eventOnClickCancelLink : function(clickedEl) {
1171              var settings = $( clickedEl ).closest( '.menu-item-settings' ),
1172                  thisMenuItem = $( clickedEl ).closest( '.menu-item' );
1173              thisMenuItem.removeClass('menu-item-edit-active').addClass('menu-item-edit-inactive');
1174              settings.setItemData( settings.data('menu-item-data') ).hide();
1175              return false;
1176          },
1177  
1178          eventOnClickMenuSave : function() {
1179              var locs = '',
1180              menuName = $('#menu-name'),
1181              menuNameVal = menuName.val();
1182              // Cancel and warn if invalid menu name
1183              if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) {
1184                  menuName.parent().addClass( 'form-invalid' );
1185                  return false;
1186              }
1187              // Copy menu theme locations
1188              $('#nav-menu-theme-locations select').each(function() {
1189                  locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />';
1190              });
1191              $('#update-nav-menu').append( locs );
1192              // Update menu item position data
1193              api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
1194              window.onbeforeunload = null;
1195  
1196              return true;
1197          },
1198  
1199          eventOnClickMenuDelete : function() {
1200              // Delete warning AYS
1201              if ( window.confirm( navMenuL10n.warnDeleteMenu ) ) {
1202                  window.onbeforeunload = null;
1203                  return true;
1204              }
1205              return false;
1206          },
1207  
1208          eventOnClickMenuItemDelete : function(clickedEl) {
1209              var itemID = parseInt(clickedEl.id.replace('delete-', ''), 10);
1210              api.removeMenuItem( $('#menu-item-' + itemID) );
1211              api.registerChange();
1212              return false;
1213          },
1214  
1215          /**
1216           * Process the quick search response into a search result
1217           *
1218           * @param string resp The server response to the query.
1219           * @param object req The request arguments.
1220           * @param jQuery panel The tabs panel we're searching in.
1221           */
1222          processQuickSearchQueryResponse : function(resp, req, panel) {
1223              var matched, newID,
1224              takenIDs = {},
1225              form = document.getElementById('nav-menu-meta'),
1226              pattern = /menu-item[(\[^]\]*/,
1227              $items = $('<div>').html(resp).find('li'),
1228              wrapper = panel.closest( '.accordion-section-content' ),
1229              $item;
1230  
1231              if( ! $items.length ) {
1232                  $('.categorychecklist', panel).html( '<li><p>' + navMenuL10n.noResultsFound + '</p></li>' );
1233                  $( '.spinner', panel ).removeClass( 'is-active' );
1234                  wrapper.addClass( 'has-no-menu-item' );
1235                  return;
1236              }
1237  
1238              $items.each(function(){
1239                  $item = $(this);
1240  
1241                  // make a unique DB ID number
1242                  matched = pattern.exec($item.html());
1243  
1244                  if ( matched && matched[1] ) {
1245                      newID = matched[1];
1246                      while( form.elements['menu-item[' + newID + '][menu-item-type]'] || takenIDs[ newID ] ) {
1247                          newID--;
1248                      }
1249  
1250                      takenIDs[newID] = true;
1251                      if ( newID != matched[1] ) {
1252                          $item.html( $item.html().replace(new RegExp(
1253                              'menu-item\\[' + matched[1] + '\\]', 'g'),
1254                              'menu-item[' + newID + ']'
1255                          ) );
1256                      }
1257                  }
1258              });
1259  
1260              $('.categorychecklist', panel).html( $items );
1261              $( '.spinner', panel ).removeClass( 'is-active' );
1262              wrapper.removeClass( 'has-no-menu-item' );
1263          },
1264  
1265          /**
1266           * Remove a menu item.
1267           * @param  {object} el The element to be removed as a jQuery object.
1268           *
1269           * @fires document#menu-removing-item Passes the element to be removed.
1270           */
1271          removeMenuItem : function(el) {
1272              var children = el.childMenuItems();
1273  
1274              $( document ).trigger( 'menu-removing-item', [ el ] );
1275              el.addClass('deleting').animate({
1276                      opacity : 0,
1277                      height: 0
1278                  }, 350, function() {
1279                      var ins = $('#menu-instructions');
1280                      el.remove();
1281                      children.shiftDepthClass( -1 ).updateParentMenuItemDBId();
1282                      if ( 0 === $( '#menu-to-edit li' ).length ) {
1283                          $( '.drag-instructions' ).hide();
1284                          ins.removeClass( 'menu-instructions-inactive' );
1285                      }
1286                      api.refreshAdvancedAccessibility();
1287                  });
1288          },
1289  
1290          depthToPx : function(depth) {
1291              return depth * api.options.menuItemDepthPerLevel;
1292          },
1293  
1294          pxToDepth : function(px) {
1295              return Math.floor(px / api.options.menuItemDepthPerLevel);
1296          }
1297  
1298      };
1299  
1300      $(document).ready(function(){ wpNavMenu.init(); });
1301  
1302  })(jQuery);


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1