|
5 | 5 | * put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
|
6 | 6 | */
|
7 | 7 | uis.controller('uiSelectCtrl',
|
8 |
| - ['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', |
9 |
| - function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse) { |
| 8 | + ['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', |
| 9 | + function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector) { |
10 | 10 |
|
11 | 11 | var ctrl = this;
|
12 | 12 |
|
@@ -41,11 +41,21 @@ uis.controller('uiSelectCtrl',
|
41 | 41 | ctrl.clickTriggeredSelect = false;
|
42 | 42 | ctrl.$filter = $filter;
|
43 | 43 |
|
| 44 | + // Use $injector to check for $animate and store a reference to it |
| 45 | + ctrl.$animate = (function () { |
| 46 | + try { |
| 47 | + return $injector.get('$animate'); |
| 48 | + } catch (err) { |
| 49 | + // $animate does not exist |
| 50 | + return null; |
| 51 | + } |
| 52 | + })(); |
| 53 | + |
44 | 54 | ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
|
45 | 55 | if (ctrl.searchInput.length !== 1) {
|
46 | 56 | throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length);
|
47 | 57 | }
|
48 |
| - |
| 58 | + |
49 | 59 | ctrl.isEmpty = function() {
|
50 | 60 | return angular.isUndefined(ctrl.selected) || ctrl.selected === null || ctrl.selected === '';
|
51 | 61 | };
|
@@ -90,14 +100,29 @@ uis.controller('uiSelectCtrl',
|
90 | 100 | ctrl.activeIndex = 0;
|
91 | 101 | }
|
92 | 102 |
|
93 |
| - // Give it time to appear before focus |
94 |
| - $timeout(function() { |
95 |
| - ctrl.search = initSearchValue || ctrl.search; |
96 |
| - ctrl.searchInput[0].focus(); |
97 |
| - if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { |
98 |
| - _ensureHighlightVisible(); |
99 |
| - } |
100 |
| - }); |
| 103 | + var container = $element.querySelectorAll('.ui-select-choices-content'); |
| 104 | + if (ctrl.$animate && ctrl.$animate.enabled(container[0])) { |
| 105 | + ctrl.$animate.on('enter', container[0], function (elem, phase) { |
| 106 | + if (phase === 'close') { |
| 107 | + // Only focus input after the animation has finished |
| 108 | + $timeout(function () { |
| 109 | + ctrl.focusSearchInput(initSearchValue); |
| 110 | + }); |
| 111 | + } |
| 112 | + }); |
| 113 | + } else { |
| 114 | + $timeout(function () { |
| 115 | + ctrl.focusSearchInput(initSearchValue); |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + ctrl.focusSearchInput = function (initSearchValue) { |
| 122 | + ctrl.search = initSearchValue || ctrl.search; |
| 123 | + ctrl.searchInput[0].focus(); |
| 124 | + if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { |
| 125 | + _ensureHighlightVisible(); |
101 | 126 | }
|
102 | 127 | };
|
103 | 128 |
|
@@ -149,7 +174,7 @@ uis.controller('uiSelectCtrl',
|
149 | 174 | //If collection is an Object, convert it to Array
|
150 | 175 |
|
151 | 176 | var originalSource = ctrl.parserResult.source;
|
152 |
| - |
| 177 | + |
153 | 178 | //When an object is used as source, we better create an array and use it as 'source'
|
154 | 179 | var createArrayFromObject = function(){
|
155 | 180 | var origSrc = originalSource($scope);
|
@@ -195,7 +220,7 @@ uis.controller('uiSelectCtrl',
|
195 | 220 | ctrl.items = [];
|
196 | 221 | } else {
|
197 | 222 | if (!angular.isArray(items)) {
|
198 |
| - throw uiSelectMinErr('items', "Expected an array but got '{0}'.", items); |
| 223 | + throw uiSelectMinErr('items', "Expected an array but got '{0}'.", items); |
199 | 224 | } else {
|
200 | 225 | //Remove already selected items (ex: while searching)
|
201 | 226 | //TODO Should add a test
|
|
0 commit comments