[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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, 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.attachBulkSelectButtonListeners(); 49 this.attachMenuCheckBoxListeners(); 50 this.attachMenuItemDeleteButton(); 51 this.attachPendingMenuItemsListForDeletion(); 52 53 this.attachQuickSearchListeners(); 54 this.attachThemeLocationsListeners(); 55 this.attachMenuSaveSubmitListeners(); 56 57 this.attachTabsPanelListeners(); 58 59 this.attachUnsavedChangesListener(); 60 61 if ( api.menuList.length ) 62 this.initSortables(); 63 64 if ( menus.oneThemeLocationNoMenus ) 65 $( '#posttype-page' ).addSelectedToMenu( api.addMenuItemToBottom ); 66 67 this.initManageLocations(); 68 69 this.initAccessibility(); 70 71 this.initToggles(); 72 73 this.initPreviewing(); 74 }, 75 76 jQueryExtensions : function() { 77 // jQuery extensions. 78 $.fn.extend({ 79 menuItemDepth : function() { 80 var margin = api.isRTL ? this.eq(0).css('margin-right') : this.eq(0).css('margin-left'); 81 return api.pxToDepth( margin && -1 != margin.indexOf('px') ? margin.slice(0, -2) : 0 ); 82 }, 83 updateDepthClass : function(current, prev) { 84 return this.each(function(){ 85 var t = $(this); 86 prev = prev || t.menuItemDepth(); 87 $(this).removeClass('menu-item-depth-'+ prev ) 88 .addClass('menu-item-depth-'+ current ); 89 }); 90 }, 91 shiftDepthClass : function(change) { 92 return this.each(function(){ 93 var t = $(this), 94 depth = t.menuItemDepth(), 95 newDepth = depth + change; 96 97 t.removeClass( 'menu-item-depth-'+ depth ) 98 .addClass( 'menu-item-depth-'+ ( newDepth ) ); 99 100 if ( 0 === newDepth ) { 101 t.find( '.is-submenu' ).hide(); 102 } 103 }); 104 }, 105 childMenuItems : function() { 106 var result = $(); 107 this.each(function(){ 108 var t = $(this), depth = t.menuItemDepth(), next = t.next( '.menu-item' ); 109 while( next.length && next.menuItemDepth() > depth ) { 110 result = result.add( next ); 111 next = next.next( '.menu-item' ); 112 } 113 }); 114 return result; 115 }, 116 shiftHorizontally : function( dir ) { 117 return this.each(function(){ 118 var t = $(this), 119 depth = t.menuItemDepth(), 120 newDepth = depth + dir; 121 122 // Change .menu-item-depth-n class. 123 t.moveHorizontally( newDepth, depth ); 124 }); 125 }, 126 moveHorizontally : function( newDepth, depth ) { 127 return this.each(function(){ 128 var t = $(this), 129 children = t.childMenuItems(), 130 diff = newDepth - depth, 131 subItemText = t.find('.is-submenu'); 132 133 // Change .menu-item-depth-n class. 134 t.updateDepthClass( newDepth, depth ).updateParentMenuItemDBId(); 135 136 // If it has children, move those too. 137 if ( children ) { 138 children.each(function() { 139 var t = $(this), 140 thisDepth = t.menuItemDepth(), 141 newDepth = thisDepth + diff; 142 t.updateDepthClass(newDepth, thisDepth).updateParentMenuItemDBId(); 143 }); 144 } 145 146 // Show "Sub item" helper text. 147 if (0 === newDepth) 148 subItemText.hide(); 149 else 150 subItemText.show(); 151 }); 152 }, 153 updateParentMenuItemDBId : function() { 154 return this.each(function(){ 155 var item = $(this), 156 input = item.find( '.menu-item-data-parent-id' ), 157 depth = parseInt( item.menuItemDepth(), 10 ), 158 parentDepth = depth - 1, 159 parent = item.prevAll( '.menu-item-depth-' + parentDepth ).first(); 160 161 if ( 0 === depth ) { // Item is on the top level, has no parent. 162 input.val(0); 163 } else { // Find the parent item, and retrieve its object id. 164 input.val( parent.find( '.menu-item-data-db-id' ).val() ); 165 } 166 }); 167 }, 168 hideAdvancedMenuItemFields : function() { 169 return this.each(function(){ 170 var that = $(this); 171 $('.hide-column-tog').not(':checked').each(function(){ 172 that.find('.field-' + $(this).val() ).addClass('hidden-field'); 173 }); 174 }); 175 }, 176 /** 177 * Adds selected menu items to the menu. 178 * 179 * @ignore 180 * 181 * @param jQuery metabox The metabox jQuery object. 182 */ 183 addSelectedToMenu : function(processMethod) { 184 if ( 0 === $('#menu-to-edit').length ) { 185 return false; 186 } 187 188 return this.each(function() { 189 var t = $(this), menuItems = {}, 190 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' ), 191 re = /menu-item\[([^\]]*)/; 192 193 processMethod = processMethod || api.addMenuItemToBottom; 194 195 // If no items are checked, bail. 196 if ( !checkboxes.length ) 197 return false; 198 199 // Show the Ajax spinner. 200 t.find( '.button-controls .spinner' ).addClass( 'is-active' ); 201 202 // Retrieve menu item data. 203 $(checkboxes).each(function(){ 204 var t = $(this), 205 listItemDBIDMatch = re.exec( t.attr('name') ), 206 listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10); 207 208 if ( this.className && -1 != this.className.indexOf('add-to-top') ) 209 processMethod = api.addMenuItemToTop; 210 menuItems[listItemDBID] = t.closest('li').getItemData( 'add-menu-item', listItemDBID ); 211 }); 212 213 // Add the items. 214 api.addItemToMenu(menuItems, processMethod, function(){ 215 // Deselect the items and hide the Ajax spinner. 216 checkboxes.prop( 'checked', false ); 217 t.find( '.button-controls .select-all' ).prop( 'checked', false ); 218 t.find( '.button-controls .spinner' ).removeClass( 'is-active' ); 219 t.updateParentDropdown(); 220 t.updateOrderDropdown(); 221 }); 222 }); 223 }, 224 getItemData : function( itemType, id ) { 225 itemType = itemType || 'menu-item'; 226 227 var itemData = {}, i, 228 fields = [ 229 'menu-item-db-id', 230 'menu-item-object-id', 231 'menu-item-object', 232 'menu-item-parent-id', 233 'menu-item-position', 234 'menu-item-type', 235 'menu-item-title', 236 'menu-item-url', 237 'menu-item-description', 238 'menu-item-attr-title', 239 'menu-item-target', 240 'menu-item-classes', 241 'menu-item-xfn' 242 ]; 243 244 if( !id && itemType == 'menu-item' ) { 245 id = this.find('.menu-item-data-db-id').val(); 246 } 247 248 if( !id ) return itemData; 249 250 this.find('input').each(function() { 251 var field; 252 i = fields.length; 253 while ( i-- ) { 254 if( itemType == 'menu-item' ) 255 field = fields[i] + '[' + id + ']'; 256 else if( itemType == 'add-menu-item' ) 257 field = 'menu-item[' + id + '][' + fields[i] + ']'; 258 259 if ( 260 this.name && 261 field == this.name 262 ) { 263 itemData[fields[i]] = this.value; 264 } 265 } 266 }); 267 268 return itemData; 269 }, 270 setItemData : function( itemData, itemType, id ) { // Can take a type, such as 'menu-item', or an id. 271 itemType = itemType || 'menu-item'; 272 273 if( !id && itemType == 'menu-item' ) { 274 id = $('.menu-item-data-db-id', this).val(); 275 } 276 277 if( !id ) return this; 278 279 this.find('input').each(function() { 280 var t = $(this), field; 281 $.each( itemData, function( attr, val ) { 282 if( itemType == 'menu-item' ) 283 field = attr + '[' + id + ']'; 284 else if( itemType == 'add-menu-item' ) 285 field = 'menu-item[' + id + '][' + attr + ']'; 286 287 if ( field == t.attr('name') ) { 288 t.val( val ); 289 } 290 }); 291 }); 292 return this; 293 }, 294 updateParentDropdown : function() { 295 return this.each(function(){ 296 var menuItems = $( '#menu-to-edit li' ), 297 parentDropdowns = $( '.edit-menu-item-parent' ); 298 299 $.each( parentDropdowns, function() { 300 var parentDropdown = $( this ), 301 $html = '', 302 $selected = '', 303 currentItemID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-db-id' ).val(), 304 currentparentID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-parent-id' ).val(), 305 currentItem = parentDropdown.closest( 'li.menu-item' ), 306 currentMenuItemChild = currentItem.childMenuItems(), 307 excludeMenuItem = [ currentItemID ]; 308 309 if ( currentMenuItemChild.length > 0 ) { 310 $.each( currentMenuItemChild, function(){ 311 var childItem = $(this), 312 childID = childItem.find( '.menu-item-data-db-id' ).val(); 313 314 excludeMenuItem.push( childID ); 315 }); 316 } 317 318 if ( currentparentID == 0 ) { 319 $selected = 'selected'; 320 } 321 322 $html += '<option ' + $selected + ' value="0">' + wp.i18n._x( 'No Parent', 'menu item without a parent in navigation menu' ) + '</option>'; 323 324 $.each( menuItems, function() { 325 var menuItem = $(this), 326 $selected = '', 327 menuID = menuItem.find( '.menu-item-data-db-id' ).val(), 328 menuTitle = menuItem.find( '.edit-menu-item-title' ).val(); 329 330 if ( ! excludeMenuItem.includes( menuID ) ) { 331 if ( currentparentID == menuID ) { 332 $selected = 'selected'; 333 } 334 $html += '<option ' + $selected + ' value="' + menuID + '">' + menuTitle + '</option>'; 335 } 336 }); 337 338 parentDropdown.html( $html ); 339 }); 340 341 }); 342 }, 343 updateOrderDropdown : function() { 344 return this.each( function() { 345 var itemPosition, 346 orderDropdowns = $( '.edit-menu-item-order' ); 347 348 $.each( orderDropdowns, function() { 349 var orderDropdown = $( this ), 350 menuItem = orderDropdown.closest( 'li.menu-item' ).first(), 351 depth = menuItem.menuItemDepth(), 352 isPrimaryMenuItem = ( 0 === depth ), 353 $html = '', 354 $selected = ''; 355 356 if ( isPrimaryMenuItem ) { 357 var primaryItems = $( '.menu-item-depth-0' ), 358 totalMenuItems = primaryItems.length; 359 360 itemPosition = primaryItems.index( menuItem ) + 1; 361 362 for ( let i = 1; i < totalMenuItems + 1; i++ ) { 363 $selected = ''; 364 if ( i == itemPosition ) { 365 $selected = 'selected'; 366 } 367 var itemString = wp.i18n.sprintf( 368 /* translators: 1: The current menu item number, 2: The total number of menu items. */ 369 wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ), 370 i, 371 totalMenuItems 372 ); 373 $html += '<option ' + $selected + ' value="' + i + '">' + itemString + '</option>'; 374 } 375 376 } else { 377 var parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(), 378 parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), 379 subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), 380 totalSubMenuItems = subItems.length; 381 382 itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1; 383 384 for ( let i = 1; i < totalSubMenuItems + 1; i++ ) { 385 $selected = ''; 386 if ( i == itemPosition ) { 387 $selected = 'selected'; 388 } 389 var submenuString = wp.i18n.sprintf( 390 /* translators: 1: The current submenu item number, 2: The total number of submenu items. */ 391 wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ), 392 i, 393 totalSubMenuItems 394 ); 395 $html += '<option ' + $selected + ' value="' + i + '">' + submenuString + '</option>'; 396 } 397 398 } 399 400 orderDropdown.html( $html ); 401 }); 402 403 }); 404 } 405 }); 406 }, 407 408 countMenuItems : function( depth ) { 409 return $( '.menu-item-depth-' + depth ).length; 410 }, 411 412 moveMenuItem : function( $this, dir ) { 413 var items, newItemPosition, newDepth, 414 menuItems = $( '#menu-to-edit li' ), 415 menuItemsCount = menuItems.length, 416 thisItem = $this.parents( 'li.menu-item' ), 417 thisItemChildren = thisItem.childMenuItems(), 418 thisItemData = thisItem.getItemData(), 419 thisItemDepth = parseInt( thisItem.menuItemDepth(), 10 ), 420 thisItemPosition = parseInt( thisItem.index(), 10 ), 421 nextItem = thisItem.next(), 422 nextItemChildren = nextItem.childMenuItems(), 423 nextItemDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1, 424 prevItem = thisItem.prev(), 425 prevItemDepth = parseInt( prevItem.menuItemDepth(), 10 ), 426 prevItemId = prevItem.getItemData()['menu-item-db-id'], 427 a11ySpeech = menus[ 'moved' + dir.charAt(0).toUpperCase() + dir.slice(1) ]; 428 429 switch ( dir ) { 430 case 'up': 431 newItemPosition = thisItemPosition - 1; 432 433 // Already at top. 434 if ( 0 === thisItemPosition ) 435 break; 436 437 // If a sub item is moved to top, shift it to 0 depth. 438 if ( 0 === newItemPosition && 0 !== thisItemDepth ) 439 thisItem.moveHorizontally( 0, thisItemDepth ); 440 441 // If prev item is sub item, shift to match depth. 442 if ( 0 !== prevItemDepth ) 443 thisItem.moveHorizontally( prevItemDepth, thisItemDepth ); 444 445 // Does this item have sub items? 446 if ( thisItemChildren ) { 447 items = thisItem.add( thisItemChildren ); 448 // Move the entire block. 449 items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); 450 } else { 451 thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); 452 } 453 break; 454 case 'down': 455 // Does this item have sub items? 456 if ( thisItemChildren ) { 457 items = thisItem.add( thisItemChildren ), 458 nextItem = menuItems.eq( items.length + thisItemPosition ), 459 nextItemChildren = 0 !== nextItem.childMenuItems().length; 460 461 if ( nextItemChildren ) { 462 newDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1; 463 thisItem.moveHorizontally( newDepth, thisItemDepth ); 464 } 465 466 // Have we reached the bottom? 467 if ( menuItemsCount === thisItemPosition + items.length ) 468 break; 469 470 items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId(); 471 } else { 472 // If next item has sub items, shift depth. 473 if ( 0 !== nextItemChildren.length ) 474 thisItem.moveHorizontally( nextItemDepth, thisItemDepth ); 475 476 // Have we reached the bottom? 477 if ( menuItemsCount === thisItemPosition + 1 ) 478 break; 479 thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId(); 480 } 481 break; 482 case 'top': 483 // Already at top. 484 if ( 0 === thisItemPosition ) 485 break; 486 // Does this item have sub items? 487 if ( thisItemChildren ) { 488 items = thisItem.add( thisItemChildren ); 489 // Move the entire block. 490 items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); 491 } else { 492 thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); 493 } 494 break; 495 case 'left': 496 // As far left as possible. 497 if ( 0 === thisItemDepth ) 498 break; 499 thisItem.shiftHorizontally( -1 ); 500 break; 501 case 'right': 502 // Can't be sub item at top. 503 if ( 0 === thisItemPosition ) 504 break; 505 // Already sub item of prevItem. 506 if ( thisItemData['menu-item-parent-id'] === prevItemId ) 507 break; 508 thisItem.shiftHorizontally( 1 ); 509 break; 510 } 511 $this.trigger( 'focus' ); 512 api.registerChange(); 513 api.refreshKeyboardAccessibility(); 514 api.refreshAdvancedAccessibility(); 515 thisItem.updateParentDropdown(); 516 thisItem.updateOrderDropdown(); 517 518 if ( a11ySpeech ) { 519 wp.a11y.speak( a11ySpeech ); 520 } 521 }, 522 523 initAccessibility : function() { 524 var menu = $( '#menu-to-edit' ); 525 526 api.refreshKeyboardAccessibility(); 527 api.refreshAdvancedAccessibility(); 528 529 // Refresh the accessibility when the user comes close to the item in any way. 530 menu.on( 'mouseenter.refreshAccessibility focus.refreshAccessibility touchstart.refreshAccessibility' , '.menu-item' , function(){ 531 api.refreshAdvancedAccessibilityOfItem( $( this ).find( 'a.item-edit' ) ); 532 } ); 533 534 // We have to update on click as well because we might hover first, change the item, and then click. 535 menu.on( 'click', 'a.item-edit', function() { 536 api.refreshAdvancedAccessibilityOfItem( $( this ) ); 537 } ); 538 539 // Links for moving items. 540 menu.on( 'click', '.menus-move', function () { 541 var $this = $( this ), 542 dir = $this.data( 'dir' ); 543 544 if ( 'undefined' !== typeof dir ) { 545 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), dir ); 546 } 547 }); 548 549 // Set menu parents data for all menu items. 550 menu.updateParentDropdown(); 551 552 // Set menu order data for all menu items. 553 menu.updateOrderDropdown(); 554 555 // Update menu item parent when value is changed. 556 menu.on( 'change', '.edit-menu-item-parent', function() { 557 api.changeMenuParent( $( this ) ); 558 }); 559 560 // Update menu item order when value is changed. 561 menu.on( 'change', '.edit-menu-item-order', function() { 562 api.changeMenuOrder( $( this ) ); 563 }); 564 }, 565 566 /** 567 * changeMenuParent( [parentDropdown] ) 568 * 569 * @since 6.7.0 570 * 571 * @param {object} parentDropdown select field 572 */ 573 changeMenuParent : function( parentDropdown ) { 574 var menuItemNewPosition, 575 menuItems = $( '#menu-to-edit li' ), 576 $this = $( parentDropdown ), 577 newParentID = $this.val(), 578 menuItem = $this.closest( 'li.menu-item' ).first(), 579 menuItemOldDepth = menuItem.menuItemDepth(), 580 menuItemChildren = menuItem.childMenuItems(), 581 menuItemNoChildren = parseInt( menuItem.childMenuItems().length, 10 ), 582 parentItem = $( '#menu-item-' + newParentID ), 583 parentItemDepth = parentItem.menuItemDepth(), 584 menuItemNewDepth = parseInt( parentItemDepth ) + 1; 585 586 if ( newParentID == 0 ) { 587 menuItemNewDepth = 0; 588 } 589 590 menuItem.find( '.menu-item-data-parent-id' ).val( newParentID ); 591 menuItem.moveHorizontally( menuItemNewDepth, menuItemOldDepth ); 592 593 if ( menuItemNoChildren > 0 ) { 594 menuItem = menuItem.add( menuItemChildren ); 595 } 596 menuItem.detach(); 597 598 menuItems = $( '#menu-to-edit li' ); 599 600 var parentItemPosition = parseInt( parentItem.index(), 10 ), 601 parentItemNoChild = parseInt( parentItem.childMenuItems().length, 10 ); 602 603 if ( parentItemNoChild > 0 ){ 604 menuItemNewPosition = parentItemPosition + parentItemNoChild; 605 } else { 606 menuItemNewPosition = parentItemPosition; 607 } 608 609 if ( newParentID == 0 ) { 610 menuItemNewPosition = menuItems.length - 1; 611 } 612 613 menuItem.insertAfter( menuItems.eq( menuItemNewPosition ) ).updateParentMenuItemDBId().updateParentDropdown().updateOrderDropdown(); 614 615 api.registerChange(); 616 api.refreshKeyboardAccessibility(); 617 api.refreshAdvancedAccessibility(); 618 $this.trigger( 'focus' ); 619 wp.a11y.speak( menus.parentUpdated, 'polite' ); 620 }, 621 622 /** 623 * changeMenuOrder( [OrderDropdown] ) 624 * 625 * @since 6.7.0 626 * 627 * @param {object} orderDropdown select field 628 */ 629 changeMenuOrder : function( orderDropdown ) { 630 var menuItems = $( '#menu-to-edit li' ), 631 $this = $( orderDropdown ), 632 newOrderID = parseInt( $this.val(), 10), 633 menuItem = $this.closest( 'li.menu-item' ).first(), 634 menuItemChildren = menuItem.childMenuItems(), 635 menuItemNoChildren = menuItemChildren.length, 636 menuItemCurrentPosition = parseInt( menuItem.index(), 10 ), 637 parentItemID = menuItem.find( '.menu-item-data-parent-id' ).val(), 638 subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemID + '"]' ), 639 currentItemAtPosition = $(subItems[newOrderID - 1]).closest( 'li.menu-item' ); 640 641 if ( menuItemNoChildren > 0 ) { 642 menuItem = menuItem.add( menuItemChildren ); 643 } 644 645 var currentItemNoChildren = currentItemAtPosition.childMenuItems().length, 646 currentItemPosition = parseInt( currentItemAtPosition.index(), 10 ); 647 648 menuItems = $( '#menu-to-edit li' ); 649 650 var menuItemNewPosition = currentItemPosition; 651 652 if(menuItemCurrentPosition > menuItemNewPosition){ 653 menuItemNewPosition = currentItemPosition; 654 menuItem.detach().insertBefore( menuItems.eq( menuItemNewPosition ) ).updateOrderDropdown(); 655 } else { 656 menuItemNewPosition = menuItemNewPosition + currentItemNoChildren; 657 menuItem.detach().insertAfter( menuItems.eq( menuItemNewPosition ) ).updateOrderDropdown(); 658 } 659 660 api.registerChange(); 661 api.refreshKeyboardAccessibility(); 662 api.refreshAdvancedAccessibility(); 663 $this.trigger( 'focus' ); 664 wp.a11y.speak( menus.orderUpdated, 'polite' ); 665 }, 666 667 /** 668 * refreshAdvancedAccessibilityOfItem( [itemToRefresh] ) 669 * 670 * Refreshes advanced accessibility buttons for one menu item. 671 * Shows or hides buttons based on the location of the menu item. 672 * 673 * @param {Object} itemToRefresh The menu item that might need its advanced accessibility buttons refreshed 674 */ 675 refreshAdvancedAccessibilityOfItem : function( itemToRefresh ) { 676 677 // Only refresh accessibility when necessary. 678 if ( true !== $( itemToRefresh ).data( 'needs_accessibility_refresh' ) ) { 679 return; 680 } 681 682 var thisLink, thisLinkText, primaryItems, itemPosition, title, 683 parentItem, parentItemId, parentItemName, subItems, totalSubItems, 684 $this = $( itemToRefresh ), 685 menuItem = $this.closest( 'li.menu-item' ).first(), 686 depth = menuItem.menuItemDepth(), 687 isPrimaryMenuItem = ( 0 === depth ), 688 itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(), 689 menuItemType = $this.closest( '.menu-item-handle' ).find( '.item-controls' ).find( '.item-type' ).text(), 690 position = parseInt( menuItem.index(), 10 ), 691 prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ), 692 prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(), 693 prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(), 694 totalMenuItems = $('#menu-to-edit li').length, 695 hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length; 696 697 menuItem.find( '.field-move' ).toggle( totalMenuItems > 1 ); 698 699 // Where can they move this menu item? 700 if ( 0 !== position ) { 701 thisLink = menuItem.find( '.menus-move-up' ); 702 thisLink.attr( 'aria-label', menus.moveUp ).css( 'display', 'inline' ); 703 } 704 705 if ( 0 !== position && isPrimaryMenuItem ) { 706 thisLink = menuItem.find( '.menus-move-top' ); 707 thisLink.attr( 'aria-label', menus.moveToTop ).css( 'display', 'inline' ); 708 } 709 710 if ( position + 1 !== totalMenuItems && 0 !== position ) { 711 thisLink = menuItem.find( '.menus-move-down' ); 712 thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' ); 713 } 714 715 if ( 0 === position && 0 !== hasSameDepthSibling ) { 716 thisLink = menuItem.find( '.menus-move-down' ); 717 thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' ); 718 } 719 720 if ( ! isPrimaryMenuItem ) { 721 thisLink = menuItem.find( '.menus-move-left' ), 722 thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft ); 723 thisLink.attr( 'aria-label', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).text( thisLinkText ).css( 'display', 'inline' ); 724 } 725 726 if ( 0 !== position ) { 727 if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) { 728 thisLink = menuItem.find( '.menus-move-right' ), 729 thisLinkText = menus.under.replace( '%s', prevItemNameRight ); 730 thisLink.attr( 'aria-label', menus.moveUnder.replace( '%s', prevItemNameRight ) ).text( thisLinkText ).css( 'display', 'inline' ); 731 } 732 } 733 734 if ( isPrimaryMenuItem ) { 735 primaryItems = $( '.menu-item-depth-0' ), 736 itemPosition = primaryItems.index( menuItem ) + 1, 737 totalMenuItems = primaryItems.length, 738 // String together help text for primary menu items. 739 title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$s', menuItemType ).replace( '%3$d', itemPosition ).replace( '%4$d', totalMenuItems ); 740 } else { 741 parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(), 742 parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), 743 parentItemName = parentItem.find( '.menu-item-title' ).text(), 744 subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), 745 totalSubItems = subItems.length, 746 itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1; 747 748 // String together help text for sub menu items. 749 if ( depth < 2 ) { 750 title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$s', menuItemType ).replace( '%3$d', itemPosition ).replace( '%4$d', totalSubItems ).replace( '%5$s', parentItemName ); 751 } else { 752 title = menus.subMenuMoreDepthFocus.replace( '%1$s', itemName ).replace( '%2$s', menuItemType ).replace( '%3$d', itemPosition ).replace( '%4$d', totalSubItems ).replace( '%5$s', parentItemName ).replace( '%6$d', depth ); 753 } 754 } 755 756 $this.attr( 'aria-label', title ); 757 758 // Mark this item's accessibility as refreshed. 759 $this.data( 'needs_accessibility_refresh', false ); 760 }, 761 762 /** 763 * refreshAdvancedAccessibility 764 * 765 * Hides all advanced accessibility buttons and marks them for refreshing. 766 */ 767 refreshAdvancedAccessibility : function() { 768 769 // Hide all the move buttons by default. 770 $( '.menu-item-settings .field-move .menus-move' ).hide(); 771 772 // Mark all menu items as unprocessed. 773 $( 'a.item-edit' ).data( 'needs_accessibility_refresh', true ); 774 775 // All open items have to be refreshed or they will show no links. 776 $( '.menu-item-edit-active a.item-edit' ).each( function() { 777 api.refreshAdvancedAccessibilityOfItem( this ); 778 } ); 779 }, 780 781 refreshKeyboardAccessibility : function() { 782 $( 'a.item-edit' ).off( 'focus' ).on( 'focus', function(){ 783 $(this).off( 'keydown' ).on( 'keydown', function(e){ 784 785 var arrows, 786 $this = $( this ), 787 thisItem = $this.parents( 'li.menu-item' ), 788 thisItemData = thisItem.getItemData(); 789 790 // Bail if it's not an arrow key. 791 if ( 37 != e.which && 38 != e.which && 39 != e.which && 40 != e.which ) 792 return; 793 794 // Avoid multiple keydown events. 795 $this.off('keydown'); 796 797 // Bail if there is only one menu item. 798 if ( 1 === $('#menu-to-edit li').length ) 799 return; 800 801 // If RTL, swap left/right arrows. 802 arrows = { '38': 'up', '40': 'down', '37': 'left', '39': 'right' }; 803 if ( $('body').hasClass('rtl') ) 804 arrows = { '38' : 'up', '40' : 'down', '39' : 'left', '37' : 'right' }; 805 806 switch ( arrows[e.which] ) { 807 case 'up': 808 api.moveMenuItem( $this, 'up' ); 809 break; 810 case 'down': 811 api.moveMenuItem( $this, 'down' ); 812 break; 813 case 'left': 814 api.moveMenuItem( $this, 'left' ); 815 break; 816 case 'right': 817 api.moveMenuItem( $this, 'right' ); 818 break; 819 } 820 // Put focus back on same menu item. 821 $( '#edit-' + thisItemData['menu-item-db-id'] ).trigger( 'focus' ); 822 return false; 823 }); 824 }); 825 }, 826 827 initPreviewing : function() { 828 // Update the item handle title when the navigation label is changed. 829 $( '#menu-to-edit' ).on( 'change input', '.edit-menu-item-title', function(e) { 830 var input = $( e.currentTarget ), title, titleEl; 831 title = input.val(); 832 titleEl = input.closest( '.menu-item' ).find( '.menu-item-title' ); 833 // Don't update to empty title. 834 if ( title ) { 835 titleEl.text( title ).removeClass( 'no-title' ); 836 } else { 837 titleEl.text( wp.i18n._x( '(no label)', 'missing menu item navigation label' ) ).addClass( 'no-title' ); 838 } 839 } ); 840 }, 841 842 initToggles : function() { 843 // Init postboxes. 844 postboxes.add_postbox_toggles('nav-menus'); 845 846 // Adjust columns functions for menus UI. 847 columns.useCheckboxesForHidden(); 848 columns.checked = function(field) { 849 $('.field-' + field).removeClass('hidden-field'); 850 }; 851 columns.unchecked = function(field) { 852 $('.field-' + field).addClass('hidden-field'); 853 }; 854 // Hide fields. 855 api.menuList.hideAdvancedMenuItemFields(); 856 857 $('.hide-postbox-tog').on( 'click', function () { 858 var hidden = $( '.accordion-container li.accordion-section' ).filter(':hidden').map(function() { return this.id; }).get().join(','); 859 $.post(ajaxurl, { 860 action: 'closed-postboxes', 861 hidden: hidden, 862 closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(), 863 page: 'nav-menus' 864 }); 865 }); 866 }, 867 868 initSortables : function() { 869 var currentDepth = 0, originalDepth, minDepth, maxDepth, 870 prev, next, prevBottom, nextThreshold, helperHeight, transport, 871 menuEdge = api.menuList.offset().left, 872 body = $('body'), maxChildDepth, 873 menuMaxDepth = initialMenuMaxDepth(); 874 875 if( 0 !== $( '#menu-to-edit li' ).length ) 876 $( '.drag-instructions' ).show(); 877 878 // Use the right edge if RTL. 879 menuEdge += api.isRTL ? api.menuList.width() : 0; 880 881 api.menuList.sortable({ 882 handle: '.menu-item-handle', 883 placeholder: 'sortable-placeholder', 884 items: api.options.sortableItems, 885 start: function(e, ui) { 886 var height, width, parent, children, tempHolder; 887 888 // Handle placement for RTL orientation. 889 if ( api.isRTL ) 890 ui.item[0].style.right = 'auto'; 891 892 transport = ui.item.children('.menu-item-transport'); 893 894 // Set depths. currentDepth must be set before children are located. 895 originalDepth = ui.item.menuItemDepth(); 896 updateCurrentDepth(ui, originalDepth); 897 898 // Attach child elements to parent. 899 // Skip the placeholder. 900 parent = ( ui.item.next()[0] == ui.placeholder[0] ) ? ui.item.next() : ui.item; 901 children = parent.childMenuItems(); 902 transport.append( children ); 903 904 // Update the height of the placeholder to match the moving item. 905 height = transport.outerHeight(); 906 // If there are children, account for distance between top of children and parent. 907 height += ( height > 0 ) ? (ui.placeholder.css('margin-top').slice(0, -2) * 1) : 0; 908 height += ui.helper.outerHeight(); 909 helperHeight = height; 910 height -= 2; // Subtract 2 for borders. 911 ui.placeholder.height(height); 912 913 // Update the width of the placeholder to match the moving item. 914 maxChildDepth = originalDepth; 915 children.each(function(){ 916 var depth = $(this).menuItemDepth(); 917 maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth; 918 }); 919 width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width. 920 width += api.depthToPx(maxChildDepth - originalDepth); // Account for children. 921 width -= 2; // Subtract 2 for borders. 922 ui.placeholder.width(width); 923 924 // Update the list of menu items. 925 tempHolder = ui.placeholder.next( '.menu-item' ); 926 tempHolder.css( 'margin-top', helperHeight + 'px' ); // Set the margin to absorb the placeholder. 927 ui.placeholder.detach(); // Detach or jQuery UI will think the placeholder is a menu item. 928 $(this).sortable( 'refresh' ); // The children aren't sortable. We should let jQuery UI know. 929 ui.item.after( ui.placeholder ); // Reattach the placeholder. 930 tempHolder.css('margin-top', 0); // Reset the margin. 931 932 // Now that the element is complete, we can update... 933 updateSharedVars(ui); 934 }, 935 stop: function(e, ui) { 936 var children, subMenuTitle, 937 depthChange = currentDepth - originalDepth; 938 939 // Return child elements to the list. 940 children = transport.children().insertAfter(ui.item); 941 942 // Add "sub menu" description. 943 subMenuTitle = ui.item.find( '.item-title .is-submenu' ); 944 if ( 0 < currentDepth ) 945 subMenuTitle.show(); 946 else 947 subMenuTitle.hide(); 948 949 // Update depth classes. 950 if ( 0 !== depthChange ) { 951 ui.item.updateDepthClass( currentDepth ); 952 children.shiftDepthClass( depthChange ); 953 updateMenuMaxDepth( depthChange ); 954 } 955 // Register a change. 956 api.registerChange(); 957 // Update the item data. 958 ui.item.updateParentMenuItemDBId(); 959 960 // Address sortable's incorrectly-calculated top in Opera. 961 ui.item[0].style.top = 0; 962 963 // Handle drop placement for rtl orientation. 964 if ( api.isRTL ) { 965 ui.item[0].style.left = 'auto'; 966 ui.item[0].style.right = 0; 967 } 968 969 api.refreshKeyboardAccessibility(); 970 api.refreshAdvancedAccessibility(); 971 ui.item.updateParentDropdown(); 972 ui.item.updateOrderDropdown(); 973 api.refreshAdvancedAccessibilityOfItem( ui.item.find( 'a.item-edit' ) ); 974 }, 975 change: function(e, ui) { 976 // Make sure the placeholder is inside the menu. 977 // Otherwise fix it, or we're in trouble. 978 if( ! ui.placeholder.parent().hasClass('menu') ) 979 (prev.length) ? prev.after( ui.placeholder ) : api.menuList.prepend( ui.placeholder ); 980 981 updateSharedVars(ui); 982 }, 983 sort: function(e, ui) { 984 var offset = ui.helper.offset(), 985 edge = api.isRTL ? offset.left + ui.helper.width() : offset.left, 986 depth = api.negateIfRTL * api.pxToDepth( edge - menuEdge ); 987 988 /* 989 * Check and correct if depth is not within range. 990 * Also, if the dragged element is dragged upwards over an item, 991 * shift the placeholder to a child position. 992 */ 993 if ( depth > maxDepth || offset.top < ( prevBottom - api.options.targetTolerance ) ) { 994 depth = maxDepth; 995 } else if ( depth < minDepth ) { 996 depth = minDepth; 997 } 998 999 if( depth != currentDepth ) 1000 updateCurrentDepth(ui, depth); 1001 1002 // If we overlap the next element, manually shift downwards. 1003 if( nextThreshold && offset.top + helperHeight > nextThreshold ) { 1004 next.after( ui.placeholder ); 1005 updateSharedVars( ui ); 1006 $( this ).sortable( 'refreshPositions' ); 1007 } 1008 } 1009 }); 1010 1011 function updateSharedVars(ui) { 1012 var depth; 1013 1014 prev = ui.placeholder.prev( '.menu-item' ); 1015 next = ui.placeholder.next( '.menu-item' ); 1016 1017 // Make sure we don't select the moving item. 1018 if( prev[0] == ui.item[0] ) prev = prev.prev( '.menu-item' ); 1019 if( next[0] == ui.item[0] ) next = next.next( '.menu-item' ); 1020 1021 prevBottom = (prev.length) ? prev.offset().top + prev.height() : 0; 1022 nextThreshold = (next.length) ? next.offset().top + next.height() / 3 : 0; 1023 minDepth = (next.length) ? next.menuItemDepth() : 0; 1024 1025 if( prev.length ) 1026 maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth; 1027 else 1028 maxDepth = 0; 1029 } 1030 1031 function updateCurrentDepth(ui, depth) { 1032 ui.placeholder.updateDepthClass( depth, currentDepth ); 1033 currentDepth = depth; 1034 } 1035 1036 function initialMenuMaxDepth() { 1037 if( ! body[0].className ) return 0; 1038 var match = body[0].className.match(/menu-max-depth-(\d+)/); 1039 return match && match[1] ? parseInt( match[1], 10 ) : 0; 1040 } 1041 1042 function updateMenuMaxDepth( depthChange ) { 1043 var depth, newDepth = menuMaxDepth; 1044 if ( depthChange === 0 ) { 1045 return; 1046 } else if ( depthChange > 0 ) { 1047 depth = maxChildDepth + depthChange; 1048 if( depth > menuMaxDepth ) 1049 newDepth = depth; 1050 } else if ( depthChange < 0 && maxChildDepth == menuMaxDepth ) { 1051 while( ! $('.menu-item-depth-' + newDepth, api.menuList).length && newDepth > 0 ) 1052 newDepth--; 1053 } 1054 // Update the depth class. 1055 body.removeClass( 'menu-max-depth-' + menuMaxDepth ).addClass( 'menu-max-depth-' + newDepth ); 1056 menuMaxDepth = newDepth; 1057 } 1058 }, 1059 1060 initManageLocations : function () { 1061 $('#menu-locations-wrap form').on( 'submit', function(){ 1062 window.onbeforeunload = null; 1063 }); 1064 $('.menu-location-menus select').on('change', function () { 1065 var editLink = $(this).closest('tr').find('.locations-edit-menu-link'); 1066 if ($(this).find('option:selected').data('orig')) 1067 editLink.show(); 1068 else 1069 editLink.hide(); 1070 }); 1071 }, 1072 1073 attachMenuEditListeners : function() { 1074 var that = this; 1075 $('#update-nav-menu').on('click', function(e) { 1076 if ( e.target && e.target.className ) { 1077 if ( -1 != e.target.className.indexOf('item-edit') ) { 1078 return that.eventOnClickEditLink(e.target); 1079 } else if ( -1 != e.target.className.indexOf('menu-save') ) { 1080 return that.eventOnClickMenuSave(e.target); 1081 } else if ( -1 != e.target.className.indexOf('menu-delete') ) { 1082 return that.eventOnClickMenuDelete(e.target); 1083 } else if ( -1 != e.target.className.indexOf('item-delete') ) { 1084 return that.eventOnClickMenuItemDelete(e.target); 1085 } else if ( -1 != e.target.className.indexOf('item-cancel') ) { 1086 return that.eventOnClickCancelLink(e.target); 1087 } 1088 } 1089 }); 1090 1091 $( '#menu-name' ).on( 'input', _.debounce( function () { 1092 var menuName = $( document.getElementById( 'menu-name' ) ), 1093 menuNameVal = menuName.val(); 1094 1095 if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) { 1096 // Add warning for invalid menu name. 1097 menuName.parent().addClass( 'form-invalid' ); 1098 } else { 1099 // Remove warning for valid menu name. 1100 menuName.parent().removeClass( 'form-invalid' ); 1101 } 1102 }, 500 ) ); 1103 1104 $('#add-custom-links input[type="text"]').on( 'keypress', function(e){ 1105 $('#customlinkdiv').removeClass('form-invalid'); 1106 1107 if ( e.keyCode === 13 ) { 1108 e.preventDefault(); 1109 $( '#submit-customlinkdiv' ).trigger( 'click' ); 1110 } 1111 }); 1112 }, 1113 1114 /** 1115 * Handle toggling bulk selection checkboxes for menu items. 1116 * 1117 * @since 5.8.0 1118 */ 1119 attachBulkSelectButtonListeners : function() { 1120 var that = this; 1121 1122 $( '.bulk-select-switcher' ).on( 'change', function() { 1123 if ( this.checked ) { 1124 $( '.bulk-select-switcher' ).prop( 'checked', true ); 1125 that.enableBulkSelection(); 1126 } else { 1127 $( '.bulk-select-switcher' ).prop( 'checked', false ); 1128 that.disableBulkSelection(); 1129 } 1130 }); 1131 }, 1132 1133 /** 1134 * Enable bulk selection checkboxes for menu items. 1135 * 1136 * @since 5.8.0 1137 */ 1138 enableBulkSelection : function() { 1139 var checkbox = $( '#menu-to-edit .menu-item-checkbox' ); 1140 1141 $( '#menu-to-edit' ).addClass( 'bulk-selection' ); 1142 $( '#nav-menu-bulk-actions-top' ).addClass( 'bulk-selection' ); 1143 $( '#nav-menu-bulk-actions-bottom' ).addClass( 'bulk-selection' ); 1144 1145 $.each( checkbox, function() { 1146 $(this).prop( 'disabled', false ); 1147 }); 1148 }, 1149 1150 /** 1151 * Disable bulk selection checkboxes for menu items. 1152 * 1153 * @since 5.8.0 1154 */ 1155 disableBulkSelection : function() { 1156 var checkbox = $( '#menu-to-edit .menu-item-checkbox' ); 1157 1158 $( '#menu-to-edit' ).removeClass( 'bulk-selection' ); 1159 $( '#nav-menu-bulk-actions-top' ).removeClass( 'bulk-selection' ); 1160 $( '#nav-menu-bulk-actions-bottom' ).removeClass( 'bulk-selection' ); 1161 1162 if ( $( '.menu-items-delete' ).is( '[aria-describedby="pending-menu-items-to-delete"]' ) ) { 1163 $( '.menu-items-delete' ).removeAttr( 'aria-describedby' ); 1164 } 1165 1166 $.each( checkbox, function() { 1167 $(this).prop( 'disabled', true ).prop( 'checked', false ); 1168 }); 1169 1170 $( '.menu-items-delete' ).addClass( 'disabled' ); 1171 $( '#pending-menu-items-to-delete ul' ).empty(); 1172 }, 1173 1174 /** 1175 * Listen for state changes on bulk action checkboxes. 1176 * 1177 * @since 5.8.0 1178 */ 1179 attachMenuCheckBoxListeners : function() { 1180 var that = this; 1181 1182 $( '#menu-to-edit' ).on( 'change', '.menu-item-checkbox', function() { 1183 that.setRemoveSelectedButtonStatus(); 1184 }); 1185 }, 1186 1187 /** 1188 * Create delete button to remove menu items from collection. 1189 * 1190 * @since 5.8.0 1191 */ 1192 attachMenuItemDeleteButton : function() { 1193 var that = this; 1194 1195 $( document ).on( 'click', '.menu-items-delete', function( e ) { 1196 var itemsPendingDeletion, itemsPendingDeletionList, deletionSpeech; 1197 1198 e.preventDefault(); 1199 1200 if ( ! $(this).hasClass( 'disabled' ) ) { 1201 $.each( $( '.menu-item-checkbox:checked' ), function( index, element ) { 1202 $( element ).parents( 'li' ).find( 'a.item-delete' ).trigger( 'click' ); 1203 }); 1204 1205 $( '.menu-items-delete' ).addClass( 'disabled' ); 1206 $( '.bulk-select-switcher' ).prop( 'checked', false ); 1207 1208 itemsPendingDeletion = ''; 1209 itemsPendingDeletionList = $( '#pending-menu-items-to-delete ul li' ); 1210 1211 $.each( itemsPendingDeletionList, function( index, element ) { 1212 var itemName = $( element ).find( '.pending-menu-item-name' ).text(); 1213 var itemSpeech = menus.menuItemDeletion.replace( '%s', itemName ); 1214 1215 itemsPendingDeletion += itemSpeech; 1216 if ( ( index + 1 ) < itemsPendingDeletionList.length ) { 1217 itemsPendingDeletion += ', '; 1218 } 1219 }); 1220 1221 deletionSpeech = menus.itemsDeleted.replace( '%s', itemsPendingDeletion ); 1222 wp.a11y.speak( deletionSpeech, 'polite' ); 1223 that.disableBulkSelection(); 1224 menus.updateParentDropdown(); 1225 menus.updateOrderDropdown(); 1226 } 1227 }); 1228 }, 1229 1230 /** 1231 * List menu items awaiting deletion. 1232 * 1233 * @since 5.8.0 1234 */ 1235 attachPendingMenuItemsListForDeletion : function() { 1236 $( '#post-body-content' ).on( 'change', '.menu-item-checkbox', function() { 1237 var menuItemName, menuItemType, menuItemID, listedMenuItem; 1238 1239 if ( ! $( '.menu-items-delete' ).is( '[aria-describedby="pending-menu-items-to-delete"]' ) ) { 1240 $( '.menu-items-delete' ).attr( 'aria-describedby', 'pending-menu-items-to-delete' ); 1241 } 1242 1243 menuItemName = $(this).next().text(); 1244 menuItemType = $(this).parent().next( '.item-controls' ).find( '.item-type' ).text(); 1245 menuItemID = $(this).attr( 'data-menu-item-id' ); 1246 1247 listedMenuItem = $( '#pending-menu-items-to-delete ul' ).find( '[data-menu-item-id=' + menuItemID + ']' ); 1248 if ( listedMenuItem.length > 0 ) { 1249 listedMenuItem.remove(); 1250 } 1251 1252 if ( this.checked === true ) { 1253 $( '#pending-menu-items-to-delete ul' ).append( 1254 '<li data-menu-item-id="' + menuItemID + '">' + 1255 '<span class="pending-menu-item-name">' + menuItemName + '</span> ' + 1256 '<span class="pending-menu-item-type">(' + menuItemType + ')</span>' + 1257 '<span class="separator"></span>' + 1258 '</li>' 1259 ); 1260 } 1261 1262 $( '#pending-menu-items-to-delete li .separator' ).html( ', ' ); 1263 $( '#pending-menu-items-to-delete li .separator' ).last().html( '.' ); 1264 }); 1265 }, 1266 1267 /** 1268 * Set status of bulk delete checkbox. 1269 * 1270 * @since 5.8.0 1271 */ 1272 setBulkDeleteCheckboxStatus : function() { 1273 var that = this; 1274 var checkbox = $( '#menu-to-edit .menu-item-checkbox' ); 1275 1276 $.each( checkbox, function() { 1277 if ( $(this).prop( 'disabled' ) ) { 1278 $(this).prop( 'disabled', false ); 1279 } else { 1280 $(this).prop( 'disabled', true ); 1281 } 1282 1283 if ( $(this).is( ':checked' ) ) { 1284 $(this).prop( 'checked', false ); 1285 } 1286 }); 1287 1288 that.setRemoveSelectedButtonStatus(); 1289 }, 1290 1291 /** 1292 * Set status of menu items removal button. 1293 * 1294 * @since 5.8.0 1295 */ 1296 setRemoveSelectedButtonStatus : function() { 1297 var button = $( '.menu-items-delete' ); 1298 1299 if ( $( '.menu-item-checkbox:checked' ).length > 0 ) { 1300 button.removeClass( 'disabled' ); 1301 } else { 1302 button.addClass( 'disabled' ); 1303 } 1304 }, 1305 1306 attachMenuSaveSubmitListeners : function() { 1307 /* 1308 * When a navigation menu is saved, store a JSON representation of all form data 1309 * in a single input to avoid PHP `max_input_vars` limitations. See #14134. 1310 */ 1311 $( '#update-nav-menu' ).on( 'submit', function() { 1312 var navMenuData = $( '#update-nav-menu' ).serializeArray(); 1313 $( '[name="nav-menu-data"]' ).val( JSON.stringify( navMenuData ) ); 1314 }); 1315 }, 1316 1317 attachThemeLocationsListeners : function() { 1318 var loc = $('#nav-menu-theme-locations'), params = {}; 1319 params.action = 'menu-locations-save'; 1320 params['menu-settings-column-nonce'] = $('#menu-settings-column-nonce').val(); 1321 loc.find('input[type="submit"]').on( 'click', function() { 1322 loc.find('select').each(function() { 1323 params[this.name] = $(this).val(); 1324 }); 1325 loc.find( '.spinner' ).addClass( 'is-active' ); 1326 $.post( ajaxurl, params, function() { 1327 loc.find( '.spinner' ).removeClass( 'is-active' ); 1328 }); 1329 return false; 1330 }); 1331 }, 1332 1333 attachQuickSearchListeners : function() { 1334 var searchTimer; 1335 1336 // Prevent form submission. 1337 $( '#nav-menu-meta' ).on( 'submit', function( event ) { 1338 event.preventDefault(); 1339 }); 1340 1341 $( '#nav-menu-meta' ).on( 'input', '.quick-search', function() { 1342 var $this = $( this ); 1343 1344 $this.attr( 'autocomplete', 'off' ); 1345 1346 if ( searchTimer ) { 1347 clearTimeout( searchTimer ); 1348 } 1349 1350 searchTimer = setTimeout( function() { 1351 api.updateQuickSearchResults( $this ); 1352 }, 500 ); 1353 }).on( 'blur', '.quick-search', function() { 1354 api.lastSearch = ''; 1355 }); 1356 }, 1357 1358 updateQuickSearchResults : function(input) { 1359 var panel, params, 1360 minSearchLength = 2, 1361 q = input.val(); 1362 1363 /* 1364 * Minimum characters for a search. Also avoid a new Ajax search when 1365 * the pressed key (e.g. arrows) doesn't change the searched term. 1366 */ 1367 if ( q.length < minSearchLength || api.lastSearch == q ) { 1368 return; 1369 } 1370 1371 api.lastSearch = q; 1372 1373 panel = input.parents('.tabs-panel'); 1374 params = { 1375 'action': 'menu-quick-search', 1376 'response-format': 'markup', 1377 'menu': $('#menu').val(), 1378 'menu-settings-column-nonce': $('#menu-settings-column-nonce').val(), 1379 'q': q, 1380 'type': input.attr('name') 1381 }; 1382 1383 $( '.spinner', panel ).addClass( 'is-active' ); 1384 1385 $.post( ajaxurl, params, function(menuMarkup) { 1386 api.processQuickSearchQueryResponse(menuMarkup, params, panel); 1387 }); 1388 }, 1389 1390 addCustomLink : function( processMethod ) { 1391 var url = $('#custom-menu-item-url').val().toString(), 1392 label = $('#custom-menu-item-name').val(); 1393 1394 if ( '' !== url ) { 1395 url = url.trim(); 1396 } 1397 1398 processMethod = processMethod || api.addMenuItemToBottom; 1399 1400 if ( '' === url || 'https://' == url || 'http://' == url ) { 1401 $('#customlinkdiv').addClass('form-invalid'); 1402 return false; 1403 } 1404 1405 // Show the Ajax spinner. 1406 $( '.customlinkdiv .spinner' ).addClass( 'is-active' ); 1407 this.addLinkToMenu( url, label, processMethod, function() { 1408 // Remove the Ajax spinner. 1409 $( '.customlinkdiv .spinner' ).removeClass( 'is-active' ); 1410 // Set custom link form back to defaults. 1411 $('#custom-menu-item-name').val('').trigger( 'blur' ); 1412 $( '#custom-menu-item-url' ).val( '' ).attr( 'placeholder', 'https://' ); 1413 }); 1414 }, 1415 1416 addLinkToMenu : function(url, label, processMethod, callback) { 1417 processMethod = processMethod || api.addMenuItemToBottom; 1418 callback = callback || function(){}; 1419 1420 api.addItemToMenu({ 1421 '-1': { 1422 'menu-item-type': 'custom', 1423 'menu-item-url': url, 1424 'menu-item-title': label 1425 } 1426 }, processMethod, callback); 1427 }, 1428 1429 addItemToMenu : function(menuItem, processMethod, callback) { 1430 var menu = $('#menu').val(), 1431 nonce = $('#menu-settings-column-nonce').val(), 1432 params; 1433 1434 processMethod = processMethod || function(){}; 1435 callback = callback || function(){}; 1436 1437 params = { 1438 'action': 'add-menu-item', 1439 'menu': menu, 1440 'menu-settings-column-nonce': nonce, 1441 'menu-item': menuItem 1442 }; 1443 1444 $.post( ajaxurl, params, function(menuMarkup) { 1445 var ins = $('#menu-instructions'); 1446 1447 menuMarkup = menuMarkup || ''; 1448 menuMarkup = menuMarkup.toString().trim(); // Trim leading whitespaces. 1449 processMethod(menuMarkup, params); 1450 1451 // Make it stand out a bit more visually, by adding a fadeIn. 1452 $( 'li.pending' ).hide().fadeIn('slow'); 1453 $( '.drag-instructions' ).show(); 1454 if( ! ins.hasClass( 'menu-instructions-inactive' ) && ins.siblings().length ) 1455 ins.addClass( 'menu-instructions-inactive' ); 1456 1457 callback(); 1458 }); 1459 }, 1460 1461 /** 1462 * Process the add menu item request response into menu list item. Appends to menu. 1463 * 1464 * @param {string} menuMarkup The text server response of menu item markup. 1465 * 1466 * @fires document#menu-item-added Passes menuMarkup as a jQuery object. 1467 */ 1468 addMenuItemToBottom : function( menuMarkup ) { 1469 var $menuMarkup = $( menuMarkup ); 1470 $menuMarkup.hideAdvancedMenuItemFields().appendTo( api.targetList ); 1471 api.refreshKeyboardAccessibility(); 1472 api.refreshAdvancedAccessibility(); 1473 wp.a11y.speak( menus.itemAdded ); 1474 $( document ).trigger( 'menu-item-added', [ $menuMarkup ] ); 1475 }, 1476 1477 /** 1478 * Process the add menu item request response into menu list item. Prepends to menu. 1479 * 1480 * @param {string} menuMarkup The text server response of menu item markup. 1481 * 1482 * @fires document#menu-item-added Passes menuMarkup as a jQuery object. 1483 */ 1484 addMenuItemToTop : function( menuMarkup ) { 1485 var $menuMarkup = $( menuMarkup ); 1486 $menuMarkup.hideAdvancedMenuItemFields().prependTo( api.targetList ); 1487 api.refreshKeyboardAccessibility(); 1488 api.refreshAdvancedAccessibility(); 1489 wp.a11y.speak( menus.itemAdded ); 1490 $( document ).trigger( 'menu-item-added', [ $menuMarkup ] ); 1491 }, 1492 1493 attachUnsavedChangesListener : function() { 1494 $('#menu-management input, #menu-management select, #menu-management, #menu-management textarea, .menu-location-menus select').on( 'change', function(){ 1495 api.registerChange(); 1496 }); 1497 1498 if ( 0 !== $('#menu-to-edit').length || 0 !== $('.menu-location-menus select').length ) { 1499 window.onbeforeunload = function(){ 1500 if ( api.menusChanged ) 1501 return wp.i18n.__( 'The changes you made will be lost if you navigate away from this page.' ); 1502 }; 1503 } else { 1504 // Make the post boxes read-only, as they can't be used yet. 1505 $( '#menu-settings-column' ).find( 'input,select' ).end().find( 'a' ).attr( 'href', '#' ).off( 'click' ); 1506 } 1507 }, 1508 1509 registerChange : function() { 1510 api.menusChanged = true; 1511 }, 1512 1513 attachTabsPanelListeners : function() { 1514 $('#menu-settings-column').on('click', function(e) { 1515 var selectAreaMatch, selectAll, panelId, wrapper, items, 1516 target = $(e.target); 1517 1518 if ( target.hasClass('nav-tab-link') ) { 1519 1520 panelId = target.data( 'type' ); 1521 1522 wrapper = target.parents('.accordion-section-content').first(); 1523 1524 // Upon changing tabs, we want to uncheck all checkboxes. 1525 $( 'input', wrapper ).prop( 'checked', false ); 1526 1527 $('.tabs-panel-active', wrapper).removeClass('tabs-panel-active').addClass('tabs-panel-inactive'); 1528 $('#' + panelId, wrapper).removeClass('tabs-panel-inactive').addClass('tabs-panel-active'); 1529 1530 $('.tabs', wrapper).removeClass('tabs'); 1531 target.parent().addClass('tabs'); 1532 1533 // Select the search bar. 1534 $('.quick-search', wrapper).trigger( 'focus' ); 1535 1536 // Hide controls in the search tab if no items found. 1537 if ( ! wrapper.find( '.tabs-panel-active .menu-item-title' ).length ) { 1538 wrapper.addClass( 'has-no-menu-item' ); 1539 } else { 1540 wrapper.removeClass( 'has-no-menu-item' ); 1541 } 1542 1543 e.preventDefault(); 1544 } else if ( target.hasClass( 'select-all' ) ) { 1545 selectAreaMatch = target.closest( '.button-controls' ).data( 'items-type' ); 1546 if ( selectAreaMatch ) { 1547 items = $( '#' + selectAreaMatch + ' .tabs-panel-active .menu-item-title input' ); 1548 1549 if ( items.length === items.filter( ':checked' ).length && ! target.is( ':checked' ) ) { 1550 items.prop( 'checked', false ); 1551 } else if ( target.is( ':checked' ) ) { 1552 items.prop( 'checked', true ); 1553 } 1554 } 1555 } else if ( target.hasClass( 'menu-item-checkbox' ) ) { 1556 selectAreaMatch = target.closest( '.tabs-panel-active' ).parent().attr( 'id' ); 1557 if ( selectAreaMatch ) { 1558 items = $( '#' + selectAreaMatch + ' .tabs-panel-active .menu-item-title input' ); 1559 selectAll = $( '.button-controls[data-items-type="' + selectAreaMatch + '"] .select-all' ); 1560 1561 if ( items.length === items.filter( ':checked' ).length && ! selectAll.is( ':checked' ) ) { 1562 selectAll.prop( 'checked', true ); 1563 } else if ( selectAll.is( ':checked' ) ) { 1564 selectAll.prop( 'checked', false ); 1565 } 1566 } 1567 } else if ( target.hasClass('submit-add-to-menu') ) { 1568 api.registerChange(); 1569 1570 if ( e.target.id && 'submit-customlinkdiv' == e.target.id ) 1571 api.addCustomLink( api.addMenuItemToBottom ); 1572 else if ( e.target.id && -1 != e.target.id.indexOf('submit-') ) 1573 $('#' + e.target.id.replace(/submit-/, '')).addSelectedToMenu( api.addMenuItemToBottom ); 1574 return false; 1575 } 1576 }); 1577 1578 /* 1579 * Delegate the `click` event and attach it just to the pagination 1580 * links thus excluding the current page `<span>`. See ticket #35577. 1581 */ 1582 $( '#nav-menu-meta' ).on( 'click', 'a.page-numbers', function() { 1583 var $container = $( this ).closest( '.inside' ); 1584 1585 $.post( ajaxurl, this.href.replace( /.*\?/, '' ).replace( /action=([^&]*)/, '' ) + '&action=menu-get-metabox', 1586 function( resp ) { 1587 var metaBoxData = JSON.parse( resp ), 1588 toReplace; 1589 1590 if ( -1 === resp.indexOf( 'replace-id' ) ) { 1591 return; 1592 } 1593 1594 // Get the post type menu meta box to update. 1595 toReplace = document.getElementById( metaBoxData['replace-id'] ); 1596 1597 if ( ! metaBoxData.markup || ! toReplace ) { 1598 return; 1599 } 1600 1601 // Update the post type menu meta box with new content from the response. 1602 $container.html( metaBoxData.markup ); 1603 } 1604 ); 1605 1606 return false; 1607 }); 1608 }, 1609 1610 eventOnClickEditLink : function(clickedEl) { 1611 var settings, item, 1612 matchedSection = /#(.*)$/.exec(clickedEl.href); 1613 1614 if ( matchedSection && matchedSection[1] ) { 1615 settings = $('#'+matchedSection[1]); 1616 item = settings.parent(); 1617 if( 0 !== item.length ) { 1618 if( item.hasClass('menu-item-edit-inactive') ) { 1619 if( ! settings.data('menu-item-data') ) { 1620 settings.data( 'menu-item-data', settings.getItemData() ); 1621 } 1622 settings.slideDown('fast'); 1623 item.removeClass('menu-item-edit-inactive') 1624 .addClass('menu-item-edit-active'); 1625 } else { 1626 settings.slideUp('fast'); 1627 item.removeClass('menu-item-edit-active') 1628 .addClass('menu-item-edit-inactive'); 1629 } 1630 return false; 1631 } 1632 } 1633 }, 1634 1635 eventOnClickCancelLink : function(clickedEl) { 1636 var settings = $( clickedEl ).closest( '.menu-item-settings' ), 1637 thisMenuItem = $( clickedEl ).closest( '.menu-item' ); 1638 1639 thisMenuItem.removeClass( 'menu-item-edit-active' ).addClass( 'menu-item-edit-inactive' ); 1640 settings.setItemData( settings.data( 'menu-item-data' ) ).hide(); 1641 // Restore the title of the currently active/expanded menu item. 1642 thisMenuItem.find( '.menu-item-title' ).text( settings.data( 'menu-item-data' )['menu-item-title'] ); 1643 1644 return false; 1645 }, 1646 1647 eventOnClickMenuSave : function() { 1648 var locs = '', 1649 menuName = $('#menu-name'), 1650 menuNameVal = menuName.val(); 1651 1652 // Cancel and warn if invalid menu name. 1653 if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) { 1654 menuName.parent().addClass( 'form-invalid' ); 1655 return false; 1656 } 1657 // Copy menu theme locations. 1658 $('#nav-menu-theme-locations select').each(function() { 1659 locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />'; 1660 }); 1661 $('#update-nav-menu').append( locs ); 1662 // Update menu item position data. 1663 api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } ); 1664 window.onbeforeunload = null; 1665 1666 return true; 1667 }, 1668 1669 eventOnClickMenuDelete : function() { 1670 // Delete warning AYS. 1671 if ( window.confirm( wp.i18n.__( 'You are about to permanently delete this menu.\n\'Cancel\' to stop, \'OK\' to delete.' ) ) ) { 1672 window.onbeforeunload = null; 1673 return true; 1674 } 1675 return false; 1676 }, 1677 1678 eventOnClickMenuItemDelete : function(clickedEl) { 1679 var itemID = parseInt(clickedEl.id.replace('delete-', ''), 10); 1680 1681 api.removeMenuItem( $('#menu-item-' + itemID) ); 1682 api.registerChange(); 1683 return false; 1684 }, 1685 1686 /** 1687 * Process the quick search response into a search result 1688 * 1689 * @param string resp The server response to the query. 1690 * @param object req The request arguments. 1691 * @param jQuery panel The tabs panel we're searching in. 1692 */ 1693 processQuickSearchQueryResponse : function(resp, req, panel) { 1694 var matched, newID, 1695 takenIDs = {}, 1696 form = document.getElementById('nav-menu-meta'), 1697 pattern = /menu-item[(\[^]\]*/, 1698 $items = $('<div>').html(resp).find('li'), 1699 wrapper = panel.closest( '.accordion-section-content' ), 1700 selectAll = wrapper.find( '.button-controls .select-all' ), 1701 $item; 1702 1703 if( ! $items.length ) { 1704 $('.categorychecklist', panel).html( '<li><p>' + wp.i18n.__( 'No results found.' ) + '</p></li>' ); 1705 $( '.spinner', panel ).removeClass( 'is-active' ); 1706 wrapper.addClass( 'has-no-menu-item' ); 1707 return; 1708 } 1709 1710 $items.each(function(){ 1711 $item = $(this); 1712 1713 // Make a unique DB ID number. 1714 matched = pattern.exec($item.html()); 1715 1716 if ( matched && matched[1] ) { 1717 newID = matched[1]; 1718 while( form.elements['menu-item[' + newID + '][menu-item-type]'] || takenIDs[ newID ] ) { 1719 newID--; 1720 } 1721 1722 takenIDs[newID] = true; 1723 if ( newID != matched[1] ) { 1724 $item.html( $item.html().replace(new RegExp( 1725 'menu-item\\[' + matched[1] + '\\]', 'g'), 1726 'menu-item[' + newID + ']' 1727 ) ); 1728 } 1729 } 1730 }); 1731 1732 $('.categorychecklist', panel).html( $items ); 1733 $( '.spinner', panel ).removeClass( 'is-active' ); 1734 wrapper.removeClass( 'has-no-menu-item' ); 1735 1736 if ( selectAll.is( ':checked' ) ) { 1737 selectAll.prop( 'checked', false ); 1738 } 1739 }, 1740 1741 /** 1742 * Remove a menu item. 1743 * 1744 * @param {Object} el The element to be removed as a jQuery object. 1745 * 1746 * @fires document#menu-removing-item Passes the element to be removed. 1747 */ 1748 removeMenuItem : function(el) { 1749 var children = el.childMenuItems(); 1750 1751 $( document ).trigger( 'menu-removing-item', [ el ] ); 1752 el.addClass('deleting').animate({ 1753 opacity : 0, 1754 height: 0 1755 }, 350, function() { 1756 var ins = $('#menu-instructions'); 1757 el.remove(); 1758 children.shiftDepthClass( -1 ).updateParentMenuItemDBId(); 1759 if ( 0 === $( '#menu-to-edit li' ).length ) { 1760 $( '.drag-instructions' ).hide(); 1761 ins.removeClass( 'menu-instructions-inactive' ); 1762 } 1763 api.refreshAdvancedAccessibility(); 1764 wp.a11y.speak( menus.itemRemoved ); 1765 menus.updateParentDropdown(); 1766 menus.updateOrderDropdown(); 1767 }); 1768 }, 1769 1770 depthToPx : function(depth) { 1771 return depth * api.options.menuItemDepthPerLevel; 1772 }, 1773 1774 pxToDepth : function(px) { 1775 return Math.floor(px / api.options.menuItemDepthPerLevel); 1776 } 1777 1778 }; 1779 1780 $( function() { 1781 1782 wpNavMenu.init(); 1783 1784 // Prevent focused element from being hidden by the sticky footer. 1785 $( '.menu-edit a, .menu-edit button, .menu-edit input, .menu-edit textarea, .menu-edit select' ).on('focus', function() { 1786 if ( window.innerWidth >= 783 ) { 1787 var navMenuHeight = $( '#nav-menu-footer' ).height() + 20; 1788 var bottomOffset = $(this).offset().top - ( $(window).scrollTop() + $(window).height() - $(this).height() ); 1789 1790 if ( bottomOffset > 0 ) { 1791 bottomOffset = 0; 1792 } 1793 bottomOffset = bottomOffset * -1; 1794 1795 if( bottomOffset < navMenuHeight ) { 1796 var scrollTop = $(document).scrollTop(); 1797 $(document).scrollTop( scrollTop + ( navMenuHeight - bottomOffset ) ); 1798 } 1799 } 1800 }); 1801 }); 1802 1803 // Show bulk action. 1804 $( document ).on( 'menu-item-added', function() { 1805 if ( ! $( '.bulk-actions' ).is( ':visible' ) ) { 1806 $( '.bulk-actions' ).show(); 1807 } 1808 } ); 1809 1810 // Hide bulk action. 1811 $( document ).on( 'menu-removing-item', function( e, el ) { 1812 var menuElement = $( el ).parents( '#menu-to-edit' ); 1813 if ( menuElement.find( 'li' ).length === 1 && $( '.bulk-actions' ).is( ':visible' ) ) { 1814 $( '.bulk-actions' ).hide(); 1815 } 1816 } ); 1817 1818 })(jQuery);
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |