Skip to content

Commit

Permalink
Add keynav support to dropdown (angular-ui#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleggett committed May 10, 2015
1 parent e9b9eb0 commit b5e970f
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])

.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', '$position', '$document', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate, $position, $document) {
var self = this,
scope = $scope.$new(), // create a child scope so we are not polluting original one
openClass = dropdownConfig.openClass,
getIsOpen,
setIsOpen = angular.noop,
toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop,
appendToBody = false;
scope = $scope.$new(), // create a child scope so we are not polluting original one
openClass = dropdownConfig.openClass,
getIsOpen,
setIsOpen = angular.noop,
toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop,
appendToBody = false,
selectedOption = null;

this.init = function( element ) {
self.$element = element;
Expand Down Expand Up @@ -135,6 +136,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
dropdownService.open( scope );
} else {
dropdownService.close( scope );
self.selectedOption = 0;
}

setIsOpen($scope, isOpen);
Expand Down Expand Up @@ -174,6 +176,42 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
};
})

.directive('keyboardNav', function() {
return {
restrict: 'A',
require: '?^dropdown',
link: function (scope, element, attrs, dropdownCtrl) {

element.bind('keydown', function(e) {

e.preventDefault();
e.stopPropagation();

var elems = angular.element(element).find('a');
switch (e.keyCode) {
case (40): // Down
{

if ( !angular.isNumber(dropdownCtrl.selectedOption) ) {
dropdownCtrl.selectedOption = 0;
} else {
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === elems.length -1 ? dropdownCtrl.selectedOption : dropdownCtrl.selectedOption+1);
}

}
break;
case (38): // Up
{
dropdownCtrl.selectedOption = (dropdownCtrl.selectedOption === 0 ? 0 : dropdownCtrl.selectedOption-1);
}
break;
}
elems[dropdownCtrl.selectedOption].focus();
});
}
};
})

.directive('dropdownToggle', function() {
return {
require: '?^dropdown',
Expand Down

0 comments on commit b5e970f

Please sign in to comment.