Skip to content
This repository was archived by the owner on Dec 16, 2019. It is now read-only.

Commit dba6385

Browse files
author
pkempenaers
committed
Fix: console errors when no options present and keyboard controls are enabled
1 parent 0ba8c67 commit dba6385

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/angularjs-dropdown-multiselect.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
(function (angular) {
2-
1+
(function(angular) {
32
'use strict';
43

54
var directiveModule = angular.module('angularjs-dropdown-multiselect', []);
@@ -67,25 +66,23 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
6766
template += '</ul>';
6867
template += '</div>';
6968

70-
return template;
69+
return template;
7170
},
7271
link: function($scope, $element, $attrs) {
7372
var $dropdownTrigger = $element.children()[0];
7473

7574
$scope.toggleDropdown = function() {
76-
if ($scope.open) {
77-
$scope.close()
78-
} else { $scope.open = true }
75+
if ($scope.open) {
76+
$scope.close()
77+
} else { $scope.open = true }
7978
if ($scope.settings.keyboardControls) {
8079
if ($scope.open) {
8180
if ($scope.settings.selectionLimit === 1 && $scope.settings.enableSearch) {
8281
setTimeout(function() {
8382
angular.element($element)[0].querySelector('.searchField').focus();
8483
}, 0);
8584
} else {
86-
setTimeout(function() {
87-
angular.element($element)[0].querySelector('.option').focus();
88-
}, 0);
85+
focusFirstOption();
8986
}
9087
}
9188
}
@@ -168,7 +165,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
168165
}
169166
});
170167

171-
$scope.close = function(){
168+
$scope.close = function() {
172169
$scope.open = false;
173170
$scope.externalEvents.onClose();
174171
}
@@ -263,8 +260,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
263260
if ($scope.settings.smartButtonMaxItems > 0) {
264261

265262
var paddingWidth = 12 * 2,
266-
borderWidth = 1 * 2,
267-
dropdownIconWidth = 8;
263+
borderWidth = 1 * 2,
264+
dropdownIconWidth = 8;
268265
var ellipsisWidth = textWidth("...");
269266
var widthLimit = $element[0].offsetWidth - paddingWidth - borderWidth - dropdownIconWidth;
270267

@@ -472,9 +469,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
472469
}
473470
if (event.keyCode === 9 || event.keyCode === 40) { //tab
474471
event.preventDefault();
475-
setTimeout(function() {
476-
angular.element($element)[0].querySelector('.option').focus();
477-
}, 0);
472+
focusFirstOption();
478473
} else if (event.keyCode === 38) {
479474
event.preventDefault();
480475
if (!!parent.previousElementSibling) {
@@ -542,10 +537,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
542537
}, 0
543538
);
544539
} else {
545-
angular.element($element)[0].querySelector('.option').focus();
540+
focusFirstOption();
546541
}
547542
}
548543
};
544+
545+
function focusFirstOption() {
546+
setTimeout(function() {
547+
var elementToFocus = angular.element($element)[0].querySelector('.option');
548+
if (angular.isDefined(elementToFocus) && elementToFocus != null) {
549+
elementToFocus.focus();
550+
}
551+
}, 0);
552+
}
549553
}
550554
};
551555
}]);
@@ -600,5 +604,4 @@ function findIndex(collection, properties) {
600604

601605
return index;
602606
}
603-
604607
})(angular);

0 commit comments

Comments
 (0)