Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions src/components/navBar/navBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ angular.module('material.components.navBar', ['material.core'])
*</hljs>
* <hljs lang="js">
* (function() {
* use strict;
* 'use strict';
*
* $rootScope.$on('$routeChangeSuccess', function(event, current) {
* $scope.currentLink = getCurrentLinkFromRoute(current);
Expand Down Expand Up @@ -122,7 +122,6 @@ function MdNavBar($mdAria, $mdTheming) {
'<ul class="_md-nav-bar-list" ng-transclude role="listbox"' +
'tabindex="0"' +
'ng-focus="ctrl.onFocus()"' +
'ng-blur="ctrl.onBlur()"' +
'ng-keydown="ctrl.onKeydown($event)"' +
'aria-label="{{ctrl.navBarAriaLabel}}">' +
'</ul>' +
Expand Down Expand Up @@ -332,16 +331,6 @@ MdNavBarController.prototype.onFocus = function() {
}
};

/**
* Clear tab focus when focus leaves the nav bar.
*/
MdNavBarController.prototype.onBlur = function() {
var tab = this.getFocusedTab();
if (tab) {
tab.setFocused(false);
}
};

/**
* Move focus from oldTab to newTab.
* @param {!NavItemController} oldTab
Expand Down Expand Up @@ -425,13 +414,14 @@ function MdNavItem($$rAF) {
} else if (hasNavSref) {
navigationAttribute = 'ui-sref="{{ctrl.mdNavSref}}"';
}

navigationOptions = hasSrefOpts ? 'ui-sref-opts="{{ctrl.srefOpts}}" ' : '';

if (navigationAttribute) {
buttonTemplate = '' +
'<md-button class="_md-nav-button md-accent" ' +
'ng-class="ctrl.getNgClassMap()" ' +
'ng-blur="ctrl.setFocused(false)" ' +
'tabindex="-1" ' +
navigationOptions +
navigationAttribute + '>' +
Expand All @@ -454,20 +444,19 @@ function MdNavItem($$rAF) {
'name': '@',
},
link: function(scope, element, attrs, controllers) {
var mdNavItem = controllers[0];
var mdNavBar = controllers[1];

// When accessing the element's contents synchronously, they
// may not be defined yet because of transclusion. There is a higher
// chance that it will be accessible if we wait one frame.
$$rAF(function() {
var mdNavItem = controllers[0];
var mdNavBar = controllers[1];
var navButton = angular.element(element[0].querySelector('._md-nav-button'));

if (!mdNavItem.name) {
mdNavItem.name = angular.element(element[0]
.querySelector('._md-nav-button-text')).text().trim();
}

var navButton = angular.element(element[0]
.querySelector('._md-nav-button'));
navButton.on('click', function() {
mdNavBar.mdSelectedNavItem = mdNavItem.name;
scope.$apply();
Expand Down Expand Up @@ -562,6 +551,10 @@ MdNavItemController.prototype.isSelected = function() {
*/
MdNavItemController.prototype.setFocused = function(isFocused) {
this._focused = isFocused;

if (isFocused) {
this.getButtonEl().focus();
}
};

/**
Expand Down
7 changes: 5 additions & 2 deletions src/components/navBar/navBar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,17 @@ describe('mdNavBar', function() {
$scope.$apply();

expect(getTab('tab2')).toHaveClass('md-focused');
expect(document.activeElement).toBe(getTab('tab2')[0]);
});

it('removes tab focus when the navbar blurs', function() {
it('removes tab focus when the tab blurs', function() {
$scope.selectedTabRoute = 'tab2';
createTabs();

tabContainer.triggerHandler('focus');
expect(getTab('tab2')).toHaveClass('md-focused');

tabContainer.triggerHandler('blur');
getTab('tab2').triggerHandler('blur');
expect(getTab('tab2')).not.toHaveClass('md-focused');
});

Expand All @@ -273,6 +274,7 @@ describe('mdNavBar', function() {
$scope.$apply();

expect(getTab('tab1')).toHaveClass('md-focused');
expect(document.activeElement).toBe(getTab('tab1')[0]);
expect(getTab('tab2')).not.toHaveClass('md-focused');
expect(getTab('tab3')).not.toHaveClass('md-focused');
});
Expand All @@ -295,6 +297,7 @@ describe('mdNavBar', function() {
expect(getTab('tab1')).not.toHaveClass('md-focused');
expect(getTab('tab2')).not.toHaveClass('md-focused');
expect(getTab('tab3')).toHaveClass('md-focused');
expect(document.activeElement).toBe(getTab('tab3')[0]);
});

it('enter selects a tab', function() {
Expand Down