Make WordPress Core

Ticket #25698: 25698.3.diff

File 25698.3.diff, 9.3 KB (added by atimmer, 11 years ago)
  • src/wp-admin/js/nav-menu.js

     
    384384                        api.refreshKeyboardAccessibility();
    385385                        api.refreshAdvancedAccessibility();
    386386
     387                        // Refresh the accessibility when the user comes close to the item in any way
     388                        menu.on( 'mouseenter.refreshAccessibility focus.refreshAccessibility touchstart.refreshAccessibility' , '.menu-item' , function(){
     389                                api.refreshAdvancedAccessibilityOfItem( $( this ).find( '.item-edit' ) );
     390                        } );
     391
     392                        // We have to update on click aswell because we might hover first, change the item and then click.
     393                        menu.on( 'click', '.item-edit', function( e ) {
     394                                api.refreshAdvancedAccessibilityOfItem( $( this ) );
     395                        } );
     396
    387397                        // Events
    388398                        menu.on( 'click', '.menus-move-up', function ( e ) {
    389399                                api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'up' );
     
    407417                        });
    408418                },
    409419
    410                 refreshAdvancedAccessibility : function() {
     420                /**
     421                 * refreshAdvancedAccessibilityOfItem( [itemToRefresh] )
     422                 *
     423                 * Refreshes advanced accessibility buttons for one menu item.
     424                 * Shows or hides buttons based on the location of the menu item.
     425                 *
     426                 * @param  {object} itemToRefresh The menu item that might need it's advanced accessibility buttons refreshed
     427                 */
     428                refreshAdvancedAccessibilityOfItem : function( itemToRefresh ) {
    411429
    412                         // Hide all links by default
    413                         $( '.menu-item-settings .field-move a' ).css( 'display', 'none' );
     430                        // Only refresh accessibility when necessary
     431                        if ( true !== $( itemToRefresh ).data( 'needs_accessibility_refresh' ) ) {
     432                                return;
     433                        }
    414434
    415                         $( '.item-edit' ).each( function() {
    416                                 var thisLink, thisLinkText, primaryItems, itemPosition, title,
    417                                         parentItem, parentItemId, parentItemName, subItems,
    418                                         $this = $(this),
    419                                         menuItem = $this.closest( 'li.menu-item' ).first(),
    420                                         depth = menuItem.menuItemDepth(),
    421                                         isPrimaryMenuItem = ( 0 === depth ),
    422                                         itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(),
    423                                         position = parseInt( menuItem.index(), 10 ),
    424                                         prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ),
    425                                         prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(),
    426                                         prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(),
    427                                         totalMenuItems = $('#menu-to-edit li').length,
    428                                         hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length;
     435                        var thisLink, thisLinkText, primaryItems, itemPosition, title,
     436                                parentItem, parentItemId, parentItemName, subItems,
     437                                $this = $( itemToRefresh ),
     438                                menuItem = $this.closest( 'li.menu-item' ).first(),
     439                                depth = menuItem.menuItemDepth(),
     440                                isPrimaryMenuItem = ( 0 === depth ),
     441                                itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(),
     442                                position = parseInt( menuItem.index(), 10 ),
     443                                prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ),
     444                                prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(),
     445                                prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(),
     446                                totalMenuItems = $('#menu-to-edit li').length,
     447                                hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length;
    429448
    430                                 // Where can they move this menu item?
    431                                 if ( 0 !== position ) {
    432                                         thisLink = menuItem.find( '.menus-move-up' );
    433                                         thisLink.prop( 'title', menus.moveUp ).css( 'display', 'inline' );
    434                                 }
     449                        // Where can they move this menu item?
     450                        if ( 0 !== position ) {
     451                                thisLink = menuItem.find( '.menus-move-up' );
     452                                thisLink.prop( 'title', menus.moveUp ).css( 'display', 'inline' );
     453                        }
    435454
    436                                 if ( 0 !== position && isPrimaryMenuItem ) {
    437                                         thisLink = menuItem.find( '.menus-move-top' );
    438                                         thisLink.prop( 'title', menus.moveToTop ).css( 'display', 'inline' );
    439                                 }
     455                        if ( 0 !== position && isPrimaryMenuItem ) {
     456                                thisLink = menuItem.find( '.menus-move-top' );
     457                                thisLink.prop( 'title', menus.moveToTop ).css( 'display', 'inline' );
     458                        }
    440459
    441                                 if ( position + 1 !== totalMenuItems && 0 !== position ) {
    442                                         thisLink = menuItem.find( '.menus-move-down' );
    443                                         thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
    444                                 }
     460                        if ( position + 1 !== totalMenuItems && 0 !== position ) {
     461                                thisLink = menuItem.find( '.menus-move-down' );
     462                                thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
     463                        }
    445464
    446                                 if ( 0 === position && 0 !== hasSameDepthSibling ) {
    447                                         thisLink = menuItem.find( '.menus-move-down' );
    448                                         thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
    449                                 }
     465                        if ( 0 === position && 0 !== hasSameDepthSibling ) {
     466                                thisLink = menuItem.find( '.menus-move-down' );
     467                                thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
     468                        }
    450469
    451                                 if ( ! isPrimaryMenuItem ) {
    452                                         thisLink = menuItem.find( '.menus-move-left' ),
    453                                         thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft );
    454                                         thisLink.prop( 'title', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).html( thisLinkText ).css( 'display', 'inline' );
    455                                 }
     470                        if ( ! isPrimaryMenuItem ) {
     471                                thisLink = menuItem.find( '.menus-move-left' ),
     472                                thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft );
     473                                thisLink.prop( 'title', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).html( thisLinkText ).css( 'display', 'inline' );
     474                        }
    456475
    457                                 if ( 0 !== position ) {
    458                                         if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) {
    459                                                 thisLink = menuItem.find( '.menus-move-right' ),
    460                                                 thisLinkText = menus.under.replace( '%s', prevItemNameRight );
    461                                                 thisLink.prop( 'title', menus.moveUnder.replace( '%s', prevItemNameRight ) ).html( thisLinkText ).css( 'display', 'inline' );
    462                                         }
     476                        if ( 0 !== position ) {
     477                                if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) {
     478                                        thisLink = menuItem.find( '.menus-move-right' ),
     479                                        thisLinkText = menus.under.replace( '%s', prevItemNameRight );
     480                                        thisLink.prop( 'title', menus.moveUnder.replace( '%s', prevItemNameRight ) ).html( thisLinkText ).css( 'display', 'inline' );
    463481                                }
     482                        }
    464483
    465                                 if ( isPrimaryMenuItem ) {
    466                                         primaryItems = $( '.menu-item-depth-0' ),
    467                                         itemPosition = primaryItems.index( menuItem ) + 1,
    468                                         totalMenuItems = primaryItems.length,
     484                        if ( isPrimaryMenuItem ) {
     485                                primaryItems = $( '.menu-item-depth-0' ),
     486                                itemPosition = primaryItems.index( menuItem ) + 1,
     487                                totalMenuItems = primaryItems.length,
    469488
    470                                         // String together help text for primary menu items
    471                                         title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems );
    472                                 } else {
    473                                         parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(),
    474                                         parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(),
    475                                         parentItemName = parentItem.find( '.menu-item-title' ).text(),
    476                                         subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ),
    477                                         itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1;
     489                                // String together help text for primary menu items
     490                                title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems );
     491                        } else {
     492                                parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(),
     493                                parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(),
     494                                parentItemName = parentItem.find( '.menu-item-title' ).text(),
     495                                subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ),
     496                                itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1;
    478497
    479                                         // String together help text for sub menu items
    480                                         title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName );
    481                                 }
     498                                // String together help text for sub menu items
     499                                title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName );
     500                        }
    482501
    483                                 $this.prop('title', title).html( title );
    484                         });
     502                        $this.prop( 'title', title ).html( title );
     503
     504                        // Mark this item's accessibility as refreshed
     505                        $this.data( 'needs_accessibility_refresh', false );
    485506                },
    486507
     508                /**
     509                 * refreshAdvancedAccessibility
     510                 *
     511                 * Hides all advanced accessibility buttons and marks them for refreshing.
     512                 */
     513                refreshAdvancedAccessibility : function() {
     514
     515                        // Hide all links by default
     516                        $( '.menu-item-settings .field-move a' ).hide();
     517
     518                        // Mark all menu items as unprocessed
     519                        $( '.item-edit' ).data( 'needs_accessibility_refresh', true );
     520
     521                        // All open items have to be refreshed or they will show no links
     522                        $( '.menu-item-edit-active .item-edit' ).each( function() {
     523                                api.refreshAdvancedAccessibilityOfItem( this );
     524                        } );
     525                },
     526
    487527                refreshKeyboardAccessibility : function() {
    488528                        $( '.item-edit' ).off( 'focus' ).on( 'focus', function(){
    489529                                $(this).off( 'keydown' ).on( 'keydown', function(e){