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

Commit 4191236

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 226218c commit 4191236

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

@@ -151,7 +152,9 @@ uis.controller('uiSelectCtrl',
151152
var searchInput = $element.querySelectorAll('.ui-select-search');
152153

153154
if (_canAnimate(container)) {
154-
_animateDropdown(searchInput, initSearchValue, container);
155+
// Only focus input after the animation has finished
156+
_animateDropdown(searchInput, container)
157+
.then(_focusWhenReady.bind(null, initSearchValue));
155158
} else {
156159
_focusWhenReady(initSearchValue);
157160
}
@@ -162,26 +165,28 @@ uis.controller('uiSelectCtrl',
162165
return ctrl.$animate && ctrl.$animate.on && ctrl.$animate.enabled(element[0]);
163166
}
164167

165-
function _animateDropdown(searchInput, initSearchValue, container) {
166-
var animateHandler = function (elem, phase) {
167-
if (phase === 'start' && ctrl.items.length === 0) {
168-
// Only focus input after the animation has finished
169-
ctrl.$animate.off('removeClass', searchInput[0], animateHandler);
170-
_focusWhenReady(initSearchValue);
168+
function _animateDropdown(searchInput, container) {
169+
170+
return $q(function (resolve, reject) {
171+
172+
var animateHandler = function (elem, phase) {
173+
if (phase === 'start' && ctrl.items.length === 0) {
174+
ctrl.$animate.off('removeClass', searchInput[0], animateHandler);
175+
resolve();
176+
}
177+
else if (phase === 'close') {
178+
ctrl.$animate.off('enter', container[0], animateHandler);
179+
resolve();
180+
}
181+
};
182+
183+
if (ctrl.items.length > 0) {
184+
ctrl.$animate.on('enter', container[0], animateHandler);
171185
}
172-
else if (phase === 'close') {
173-
// Only focus input after the animation has finished
174-
ctrl.$animate.off('enter', container[0], animateHandler);
175-
_focusWhenReady(initSearchValue);
186+
else {
187+
ctrl.$animate.on('removeClass', searchInput[0], animateHandler);
176188
}
177-
};
178-
179-
if (ctrl.items.length > 0) {
180-
ctrl.$animate.on('enter', container[0], animateHandler);
181-
}
182-
else {
183-
ctrl.$animate.on('removeClass', searchInput[0], animateHandler);
184-
}
189+
});
185190
}
186191

187192
function _focusWhenReady(initSearchValue) {
@@ -318,7 +323,8 @@ uis.controller('uiSelectCtrl',
318323
/**
319324
* Typeahead mode: lets the user refresh the collection using his own function.
320325
*
321-
* See Expose $select.search for external / remote filtering https://github.com/angular-ui/ui-select/pull/31
326+
* See Expose $select.search for external / remote filtering
327+
* https://github.com/angular-ui/ui-select/pull/31
322328
*/
323329
ctrl.refresh = function(refreshAttr) {
324330
if (refreshAttr !== undefined) {

0 commit comments

Comments
 (0)