Make WordPress Core

Ticket #25698: 25698.4.diff

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

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