WordPress.org

Making WordPress.org

Changeset 6514


Ignore:
Timestamp:
02/01/2018 09:22:58 PM (4 months ago)
Author:
coffee2code
Message:

developer.wordpress.org: Port over accessibility updates and code formatting tweaks to js/navigation.js from wporg-support theme.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/navigation.js

    r6512 r6514  
    11/**
    2  * navigation.js
     2 * File navigation.js.
    33 *
    4  * Handles toggling the navigation menu for small screens.
     4 * Handles toggling the navigation menu for small screens and enables TAB key
     5 * navigation support for dropdown menus.
    56 */
    67( function() {
    7     var container, button, menu;
     8    var container, button, menu, links, subMenus, i, len;
    89
    910    container = document.getElementById( 'site-navigation' );
    10     if ( ! container )
     11    if ( ! container ) {
    1112        return;
     13    }
    1214
    1315    button = container.getElementsByTagName( 'button' )[0];
    14     if ( 'undefined' === typeof button )
     16    if ( 'undefined' === typeof button ) {
    1517        return;
     18    }
    1619
    1720    menu = container.getElementsByTagName( 'ul' )[0];
     
    2326    }
    2427
    25     if ( -1 === menu.className.indexOf( 'nav-menu' ) )
     28    menu.setAttribute( 'aria-expanded', 'false' );
     29    if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
    2630        menu.className += ' nav-menu';
     31    }
    2732
    2833    button.onclick = function() {
    29         if ( -1 !== container.className.indexOf( 'toggled' ) )
     34        if ( -1 !== container.className.indexOf( 'toggled' ) ) {
    3035            container.className = container.className.replace( ' toggled', '' );
    31         else
     36            button.setAttribute( 'aria-expanded', 'false' );
     37            menu.setAttribute( 'aria-expanded', 'false' );
     38        } else {
    3239            container.className += ' toggled';
     40            button.setAttribute( 'aria-expanded', 'true' );
     41            menu.setAttribute( 'aria-expanded', 'true' );
     42        }
    3343    };
     44
     45    // Get all the link elements within the menu.
     46    links    = menu.getElementsByTagName( 'a' );
     47    subMenus = menu.getElementsByTagName( 'ul' );
     48
     49    // Set menu items with submenus to aria-haspopup="true".
     50    for ( i = 0, len = subMenus.length; i < len; i++ ) {
     51        subMenus[i].parentNode.setAttribute( 'aria-haspopup', 'true' );
     52    }
     53
     54    // Each time a menu link is focused or blurred, toggle focus.
     55    for ( i = 0, len = links.length; i < len; i++ ) {
     56        links[i].addEventListener( 'focus', toggleFocus, true );
     57        links[i].addEventListener( 'blur', toggleFocus, true );
     58    }
     59
     60    /**
     61     * Sets or removes .focus class on an element.
     62     */
     63    function toggleFocus() {
     64        var self = this;
     65
     66        // Move up through the ancestors of the current link until we hit .nav-menu.
     67        while ( -1 === self.className.indexOf( 'nav-menu' ) ) {
     68
     69            // On li elements toggle the class .focus.
     70            if ( 'li' === self.tagName.toLowerCase() ) {
     71                if ( -1 !== self.className.indexOf( 'focus' ) ) {
     72                    self.className = self.className.replace( ' focus', '' );
     73                } else {
     74                    self.className += ' focus';
     75                }
     76            }
     77
     78            self = self.parentElement;
     79        }
     80    }
    3481} )();
Note: See TracChangeset for help on using the changeset viewer.