Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b1c7154

Browse files
committedJul 17, 2020
fix(nav-bar): throws exception when indexing null tabs variable
- `MdNavBarController._getTabs()` returns an empty array instead of `null` to match existing return type - rename `MdNavItemController._focused` to `MdNavItemController.isFocused` - make it non-private since it is accessed by `MdNavBarController.onFocus()` - fix/improve types - remove unused variables - add JSDoc Relates to #11964
1 parent cb06f35 commit b1c7154

File tree

1 file changed

+69
-37
lines changed

1 file changed

+69
-37
lines changed
 

‎src/components/navBar/navBar.js

+69-37
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* @ngdoc module
33
* @name material.components.navBar
44
*/
5-
6-
75
angular.module('material.components.navBar', ['material.core'])
86
.controller('MdNavBarController', MdNavBarController)
97
.directive('mdNavBar', MdNavBar)
@@ -138,38 +136,46 @@ function MdNavBar($mdAria, $mdTheming, $window, $mdUtil) {
138136
* (https://www.w3.org/TR/wai-aria-1.0/complete#tablist) and
139137
* tabs (https://www.w3.org/TR/wai-aria-1.0/complete#tab).
140138
*
141-
* @param {!angular.JQLite} $element
142-
* @param {!angular.Scope} $scope
143-
* @param {!angular.Timeout} $timeout
139+
* @param {!JQLite} $element
140+
* @param {!IScope} $scope
141+
* @param {!ITimeoutService} $timeout
144142
* @param {!Object} $mdConstant
145143
* @constructor
146144
* @final
147145
* @ngInject
148146
*/
149147
function MdNavBarController($element, $scope, $timeout, $mdConstant) {
150148
// Injected variables
151-
/** @private @const {!angular.Timeout} */
149+
/**
150+
* @private @const
151+
* @type {!ITimeoutService}
152+
*/
152153
this._$timeout = $timeout;
153154

154-
/** @private @const {!angular.Scope} */
155+
/**
156+
* @private @const
157+
* @type {!IScope}
158+
*/
155159
this._$scope = $scope;
156160

157-
/** @private @const {!Object} */
161+
/**
162+
* @private @const
163+
* @type {!Object}
164+
*/
158165
this._$mdConstant = $mdConstant;
159166

160167
// Data-bound variables.
161-
/** @type {string} */
168+
/** @type {?string} */
162169
this.mdSelectedNavItem;
163170

164-
/** @type {string} */
171+
/** @type {?string} */
165172
this.navBarAriaLabel;
166173

167174
// State variables.
168-
169-
/** @type {?angular.JQLite} */
175+
/** @type {?HTMLElement} */
170176
this._navBarEl = $element[0];
171177

172-
/** @type {?angular.JQLite} */
178+
/** @type {?JQLite} */
173179
this._inkbar;
174180

175181
var self = this;
@@ -220,14 +226,12 @@ MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
220226
// this._getTabs can return null if nav-bar has not yet been initialized
221227
if (!tabs) return;
222228

223-
var oldIndex = -1;
224229
var newIndex = -1;
225230
var newTab = this._getTabByName(newValue);
226231
var oldTab = this._getTabByName(oldValue);
227232

228233
if (oldTab) {
229234
oldTab.setSelected(false);
230-
oldIndex = tabs.indexOf(oldTab);
231235
}
232236

233237
if (newTab) {
@@ -236,7 +240,7 @@ MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
236240
}
237241

238242
this._$timeout(function() {
239-
self._updateInkBarStyles(newTab, newIndex, oldIndex);
243+
self._updateInkBarStyles(newTab, newIndex);
240244
// Don't change focus when there is no newTab, the new and old tabs are the same, or when
241245
// called from MdNavBarController._initTabs() which would have no oldTab defined.
242246
if (newTab && oldTab && !sameTab) {
@@ -247,6 +251,8 @@ MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
247251

248252
/**
249253
* Repositions the ink bar to the selected tab.
254+
* @param {MdNavItemController} tab the nav item that should have ink bar styles applied
255+
* @param {number=} newIndex the index of the newly selected nav item
250256
* @private
251257
*/
252258
MdNavBarController.prototype._updateInkBarStyles = function(tab, newIndex) {
@@ -265,11 +271,11 @@ MdNavBarController.prototype._updateInkBarStyles = function(tab, newIndex) {
265271
};
266272

267273
/**
268-
* Updates inkbar to match current tab.
274+
* Updates ink bar to match current tab.
269275
*/
270276
MdNavBarController.prototype.updateSelectedTabInkBar = function() {
271277
this._updateInkBarStyles(this._getSelectedTab());
272-
}
278+
};
273279

274280
/**
275281
* Returns an array of the current tabs.
@@ -282,7 +288,7 @@ MdNavBarController.prototype._getTabs = function() {
282288
.map(function(el) {
283289
return angular.element(el).controller('mdNavItem');
284290
});
285-
return controllers.indexOf(undefined) ? controllers : null;
291+
return controllers.indexOf(undefined) ? controllers : [];
286292
};
287293

288294
/**
@@ -326,11 +332,11 @@ MdNavBarController.prototype.getFocusedTab = function() {
326332
* @private
327333
*/
328334
MdNavBarController.prototype._findTab = function(fn, startIndex) {
329-
var tabs = this._getTabs();
330-
if (startIndex === undefined || startIndex === null) {
335+
var tabs = this._getTabs(), i;
336+
if (startIndex == null) {
331337
startIndex = 0;
332338
}
333-
for (var i = startIndex; i < tabs.length; i++) {
339+
for (i = startIndex; i < tabs.length; i++) {
334340
if (fn(tabs[i])) {
335341
return tabs[i];
336342
}
@@ -363,7 +369,7 @@ MdNavBarController.prototype._findTabReverse = function(fn, startIndex) {
363369
*/
364370
MdNavBarController.prototype.onFocus = function() {
365371
var tab = this._getSelectedTab();
366-
if (tab && !tab._focused) {
372+
if (tab && !tab.isFocused) {
367373
tab.setFocused(true);
368374
}
369375
};
@@ -670,40 +676,66 @@ function MdNavItem($mdAria, $$rAF, $mdUtil, $window) {
670676

671677
/**
672678
* Controller for the nav-item component.
673-
* @param {!angular.JQLite} $element
679+
* @param {!JQLite} $element
674680
* @constructor
675681
* @final
676682
* @ngInject
677683
*/
678684
function MdNavItemController($element) {
679685

680-
/** @private @const {!angular.JQLite} */
686+
/**
687+
* @private @const
688+
* @type {!JQLite}
689+
*/
681690
this._$element = $element;
682691

683692
// Data-bound variables
684693

685-
/** @const {?Function} */
694+
/**
695+
* @const
696+
* @type {?Function}
697+
*/
686698
this.mdNavClick;
687699

688-
/** @const {?string} */
700+
/**
701+
* @const
702+
* @type {?string}
703+
*/
689704
this.mdNavHref;
690705

691-
/** @const {?string} */
706+
/**
707+
* @const
708+
* @type {?string}
709+
*/
692710
this.mdNavSref;
693-
/** @const {?Object} */
711+
/**
712+
* @const
713+
* @type {?Object}
714+
*/
694715
this.srefOpts;
695-
/** @const {?string} */
716+
/**
717+
* @const
718+
* @type {?string}
719+
*/
696720
this.name;
697721

698-
/** @type {string} */
722+
/**
723+
* @const
724+
* @type {string}
725+
*/
699726
this.navItemAriaLabel;
700727

701728
// State variables
702-
/** @private {boolean} */
729+
/**
730+
* @private
731+
* @type {boolean}
732+
*/
703733
this._selected = false;
704734

705-
/** @private {boolean} */
706-
this._focused = false;
735+
/**
736+
* @type {boolean}
737+
*/
738+
this.isFocused = false;
707739
}
708740

709741
/**
@@ -715,7 +747,7 @@ MdNavItemController.prototype.getNgClassMap = function() {
715747
'md-active': this._selected,
716748
'md-primary': this._selected,
717749
'md-unselected': !this._selected,
718-
'md-focused': this._focused,
750+
'md-focused': this.isFocused,
719751
};
720752
};
721753

@@ -764,7 +796,7 @@ MdNavItemController.prototype.isSelected = function() {
764796
* @param {boolean} isFocused
765797
*/
766798
MdNavItemController.prototype.setFocused = function(isFocused) {
767-
this._focused = isFocused;
799+
this.isFocused = isFocused;
768800

769801
if (isFocused) {
770802
this.getButtonEl().focus();
@@ -775,7 +807,7 @@ MdNavItemController.prototype.setFocused = function(isFocused) {
775807
* @return {boolean} true if the tab has focus, false if not.
776808
*/
777809
MdNavItemController.prototype.hasFocus = function() {
778-
return this._focused;
810+
return this.isFocused;
779811
};
780812

781813
/**

0 commit comments

Comments
 (0)
Failed to load comments.