diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 7661da94b..eb536a1ec 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -5,8 +5,8 @@ * put as much logic in the controller (instead of the link functions) as possible so it can be easily tested. */ uis.controller('uiSelectCtrl', - ['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', - function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse) { + ['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', + function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector) { var ctrl = this; @@ -41,11 +41,21 @@ uis.controller('uiSelectCtrl', ctrl.clickTriggeredSelect = false; ctrl.$filter = $filter; + // Use $injector to check for $animate and store a reference to it + ctrl.$animate = (function () { + try { + return $injector.get('$animate'); + } catch (err) { + // $animate does not exist + return null; + } + })(); + ctrl.searchInput = $element.querySelectorAll('input.ui-select-search'); if (ctrl.searchInput.length !== 1) { throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length); } - + ctrl.isEmpty = function() { return angular.isUndefined(ctrl.selected) || ctrl.selected === null || ctrl.selected === ''; }; @@ -90,14 +100,29 @@ uis.controller('uiSelectCtrl', ctrl.activeIndex = 0; } - // Give it time to appear before focus - $timeout(function() { - ctrl.search = initSearchValue || ctrl.search; - ctrl.searchInput[0].focus(); - if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { - _ensureHighlightVisible(); - } - }); + var container = $element.querySelectorAll('.ui-select-choices-content'); + if (ctrl.$animate && ctrl.$animate.enabled(container[0])) { + ctrl.$animate.on('enter', container[0], function (elem, phase) { + if (phase === 'close') { + // Only focus input after the animation has finished + $timeout(function () { + ctrl.focusSearchInput(initSearchValue); + }); + } + }); + } else { + $timeout(function () { + ctrl.focusSearchInput(initSearchValue); + }); + } + } + }; + + ctrl.focusSearchInput = function (initSearchValue) { + ctrl.search = initSearchValue || ctrl.search; + ctrl.searchInput[0].focus(); + if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { + _ensureHighlightVisible(); } }; @@ -149,7 +174,7 @@ uis.controller('uiSelectCtrl', //If collection is an Object, convert it to Array var originalSource = ctrl.parserResult.source; - + //When an object is used as source, we better create an array and use it as 'source' var createArrayFromObject = function(){ var origSrc = originalSource($scope); @@ -195,7 +220,7 @@ uis.controller('uiSelectCtrl', ctrl.items = []; } else { if (!angular.isArray(items)) { - throw uiSelectMinErr('items', "Expected an array but got '{0}'.", items); + throw uiSelectMinErr('items', "Expected an array but got '{0}'.", items); } else { //Remove already selected items (ex: while searching) //TODO Should add a test