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

Commit e6ba734

Browse files
committed
refactor(uiSelectCtrl): update _animateDropdown() to be async and return promise
The promise is resolved when the animation is switched off (unsubscribed) This extracts the additional responsibility to call focus/scroll the active element at the end of the animation
1 parent a5eb57c commit e6ba734

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/uiSelectController.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* Contains ui-select "intelligence".
33
*
44
* The goal is to limit dependency on the DOM whenever possible and
5-
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
5+
* put as much logic in the controller (instead of the link functions) as possible so it can be
6+
* easily tested.
67
*/
78
uis.controller('uiSelectCtrl',
8-
['$scope', '$element', '$timeout', '$filter', '$$uisDebounce', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window',
9-
function($scope, $element, $timeout, $filter, $$uisDebounce, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window) {
9+
['$scope', '$element', '$timeout', '$filter', '$$uisDebounce', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window', '$q',
10+
function($scope, $element, $timeout, $filter, $$uisDebounce, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window, $q) {
1011

1112
var ctrl = this;
1213

@@ -156,7 +157,9 @@ uis.controller('uiSelectCtrl',
156157
var searchInput = $element.querySelectorAll('.ui-select-search');
157158

158159
if (_canAnimate(container)) {
159-
_animateDropdown(searchInput, initSearchValue, container);
160+
// Only focus input after the animation has finished
161+
_animateDropdown(searchInput, container)
162+
.then(_focusWhenReady.bind(null, initSearchValue));
160163
} else {
161164
_focusWhenReady(initSearchValue);
162165
}
@@ -167,26 +170,28 @@ uis.controller('uiSelectCtrl',
167170
return ctrl.$animate && ctrl.$animate.on && ctrl.$animate.enabled(element[0]);
168171
}
169172

170-
function _animateDropdown(searchInput, initSearchValue, container) {
171-
var animateHandler = function (elem, phase) {
172-
if (phase === 'start' && ctrl.items.length === 0) {
173-
// Only focus input after the animation has finished
174-
ctrl.$animate.off('removeClass', searchInput[0], animateHandler);
175-
_focusWhenReady(initSearchValue);
173+
function _animateDropdown(searchInput, container) {
174+
175+
return $q(function (resolve, reject) {
176+
177+
var animateHandler = function (elem, phase) {
178+
if (phase === 'start' && ctrl.items.length === 0) {
179+
ctrl.$animate.off('removeClass', searchInput[0], animateHandler);
180+
resolve();
181+
}
182+
else if (phase === 'close') {
183+
ctrl.$animate.off('enter', container[0], animateHandler);
184+
resolve();
185+
}
186+
};
187+
188+
if (ctrl.items.length > 0) {
189+
ctrl.$animate.on('enter', container[0], animateHandler);
176190
}
177-
else if (phase === 'close') {
178-
// Only focus input after the animation has finished
179-
ctrl.$animate.off('enter', container[0], animateHandler);
180-
_focusWhenReady(initSearchValue);
191+
else {
192+
ctrl.$animate.on('removeClass', searchInput[0], animateHandler);
181193
}
182-
};
183-
184-
if (ctrl.items.length > 0) {
185-
ctrl.$animate.on('enter', container[0], animateHandler);
186-
}
187-
else {
188-
ctrl.$animate.on('removeClass', searchInput[0], animateHandler);
189-
}
194+
});
190195
}
191196

192197
function _focusWhenReady(initSearchValue) {
@@ -326,7 +331,8 @@ uis.controller('uiSelectCtrl',
326331
/**
327332
* Typeahead mode: lets the user refresh the collection using his own function.
328333
*
329-
* See Expose $select.search for external / remote filtering https://github.com/angular-ui/ui-select/pull/31
334+
* See Expose $select.search for external / remote filtering
335+
* https://github.com/angular-ui/ui-select/pull/31
330336
*/
331337
ctrl.refresh = function(refreshAttr) {
332338
if (refreshAttr !== undefined) {

0 commit comments

Comments
 (0)