|
1 | | -(function (angular) { |
2 | | - |
| 1 | +(function(angular) { |
3 | 2 | 'use strict'; |
4 | 3 |
|
5 | 4 | var directiveModule = angular.module('angularjs-dropdown-multiselect', []); |
@@ -67,25 +66,23 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co |
67 | 66 | template += '</ul>'; |
68 | 67 | template += '</div>'; |
69 | 68 |
|
70 | | - return template; |
| 69 | + return template; |
71 | 70 | }, |
72 | 71 | link: function($scope, $element, $attrs) { |
73 | 72 | var $dropdownTrigger = $element.children()[0]; |
74 | 73 |
|
75 | 74 | $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 } |
79 | 78 | if ($scope.settings.keyboardControls) { |
80 | 79 | if ($scope.open) { |
81 | 80 | if ($scope.settings.selectionLimit === 1 && $scope.settings.enableSearch) { |
82 | 81 | setTimeout(function() { |
83 | 82 | angular.element($element)[0].querySelector('.searchField').focus(); |
84 | 83 | }, 0); |
85 | 84 | } else { |
86 | | - setTimeout(function() { |
87 | | - angular.element($element)[0].querySelector('.option').focus(); |
88 | | - }, 0); |
| 85 | + focusFirstOption(); |
89 | 86 | } |
90 | 87 | } |
91 | 88 | } |
@@ -168,7 +165,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co |
168 | 165 | } |
169 | 166 | }); |
170 | 167 |
|
171 | | - $scope.close = function(){ |
| 168 | + $scope.close = function() { |
172 | 169 | $scope.open = false; |
173 | 170 | $scope.externalEvents.onClose(); |
174 | 171 | } |
@@ -263,8 +260,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co |
263 | 260 | if ($scope.settings.smartButtonMaxItems > 0) { |
264 | 261 |
|
265 | 262 | var paddingWidth = 12 * 2, |
266 | | - borderWidth = 1 * 2, |
267 | | - dropdownIconWidth = 8; |
| 263 | + borderWidth = 1 * 2, |
| 264 | + dropdownIconWidth = 8; |
268 | 265 | var ellipsisWidth = textWidth("..."); |
269 | 266 | var widthLimit = $element[0].offsetWidth - paddingWidth - borderWidth - dropdownIconWidth; |
270 | 267 |
|
@@ -472,9 +469,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co |
472 | 469 | } |
473 | 470 | if (event.keyCode === 9 || event.keyCode === 40) { //tab |
474 | 471 | event.preventDefault(); |
475 | | - setTimeout(function() { |
476 | | - angular.element($element)[0].querySelector('.option').focus(); |
477 | | - }, 0); |
| 472 | + focusFirstOption(); |
478 | 473 | } else if (event.keyCode === 38) { |
479 | 474 | event.preventDefault(); |
480 | 475 | if (!!parent.previousElementSibling) { |
@@ -542,10 +537,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co |
542 | 537 | }, 0 |
543 | 538 | ); |
544 | 539 | } else { |
545 | | - angular.element($element)[0].querySelector('.option').focus(); |
| 540 | + focusFirstOption(); |
546 | 541 | } |
547 | 542 | } |
548 | 543 | }; |
| 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 | + } |
549 | 553 | } |
550 | 554 | }; |
551 | 555 | }]); |
@@ -600,5 +604,4 @@ function findIndex(collection, properties) { |
600 | 604 |
|
601 | 605 | return index; |
602 | 606 | } |
603 | | - |
604 | 607 | })(angular); |
0 commit comments