From 4c396f6f9be20fc894ff4e64e43faf6d134f8528 Mon Sep 17 00:00:00 2001 From: Michael Fields Date: Sun, 16 Jun 2013 20:29:17 -0700 Subject: [PATCH] Rework menu navigation: these changes build off pull request #239 for issue #200 * Only set button and menu if container is not null. * Break up the "return early" conditional into multiple parts. * Shorten the name of the toggled class from `toggled-on` to `toggled`. * Restore the "Hide menu toggle button if menu is empty." documentation. * Remove the `nav-menu` class. The previous code would replace any custom custom classes applied via `wp_nav_menu()` or template file. * Replace the `nav-menu` selector is style.css with selectors that will target both a custom nav menu and a page menu. These changes could use critical feedback. Do you know of an edge case where they might not work as expected? --- js/navigation.js | 40 ++++++++++++++++++++-------------------- style.css | 4 +++- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/js/navigation.js b/js/navigation.js index 22f7444..584f3f6 100644 --- a/js/navigation.js +++ b/js/navigation.js @@ -5,28 +5,28 @@ */ ( function() { var container = document.getElementById( 'site-navigation' ), - button = container.getElementsByTagName( 'h1' )[0], - menu = container.getElementsByTagName( 'ul' )[0]; + button, + menu; - if ( undefined == button || undefined == menu ) - return false; + if ( ! container ) + return; + + button = container.getElementsByTagName( 'h1' )[0]; + if ( 'undefined' == typeof button ) + return; + + menu = container.getElementsByTagName( 'ul' )[0]; + + // Hide menu toggle button if menu is empty and return early. + if ( 'undefined' == typeof menu ) { + button.style.display = 'none'; + return; + } button.onclick = function() { - if ( -1 == menu.className.indexOf( 'nav-menu' ) ) - menu.className = 'nav-menu'; - - if ( -1 != button.className.indexOf( 'toggled-on' ) ) { - button.className = button.className.replace( ' toggled-on', '' ); - menu.className = menu.className.replace( ' toggled-on', '' ); - container.className = container.className.replace( 'main-small-navigation', 'navigation-main' ); - } else { - button.className += ' toggled-on'; - menu.className += ' toggled-on'; - container.className = container.className.replace( 'navigation-main', 'main-small-navigation' ); - } + if ( -1 != container.className.indexOf( 'toggled' ) ) + container.className = container.className.replace( ' toggled', '' ); + else + container.className += ' toggled'; }; - - // Hide menu toggle button if menu is empty. - if ( ! menu.childNodes.length ) - button.style.display = 'none'; } )(); \ No newline at end of file diff --git a/style.css b/style.css index b0e814c..d1299f7 100644 --- a/style.css +++ b/style.css @@ -447,7 +447,8 @@ a:active { @media screen and (max-width: 600px) { .menu-toggle, - .main-small-navigation ul.nav-menu.toggled-on { + .navigation-main.toggled ul.menu, + .navigation-main.toggled div.menu > ul { display: block; } @@ -456,6 +457,7 @@ a:active { } } + /* =Content ----------------------------------------------- */