diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 486c245a2..3af7f52a3 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', - function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig) { + ['$scope', '$element', '$timeout', '$filter', '$animate', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$q', + function($scope, $element, $timeout, $filter, $animate, RepeatParser, uiSelectMinErr, uiSelectConfig, $q) { var ctrl = this; @@ -73,26 +73,45 @@ uis.controller('uiSelectCtrl', // When the user clicks on ui-select, displays the dropdown list ctrl.activate = function(initSearchValue, avoidReset) { - if (!ctrl.disabled && !ctrl.open) { - if(!avoidReset) _resetSearchInput(); + if(ctrl.disabled || ctrl.open){ + return; + } + if(!avoidReset) _resetSearchInput(); - $scope.$broadcast('uis:activate'); + $scope.$broadcast('uis:activate'); - ctrl.open = true; + ctrl.open = true; - ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex; + ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex; - // ensure that the index is set to zero for tagging variants - // that where first option is auto-selected - if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) { - ctrl.activeIndex = 0; - } + // ensure that the index is set to zero for tagging variants + // that where first option is auto-selected + if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) { + ctrl.activeIndex = 0; + } + + //sets search value and focuses the input + var setSearchAndInputFocus = function(){ + ctrl.search = initSearchValue || ctrl.search; + ctrl.searchInput[0].focus(); + }; + + // If ngAnimate is being used in the given app, we need to ensure that $animate is done showing the element before + // moving on + if($animate.enabled(ctrl.searchInput[0])){ + var deferedAnimPromise = $q.defer(); + // Wait until $animate does it's thing + $animate.on('removeClass', ctrl.searchInput[0], function() { + deferedAnimPromise.resolve(); + }); - // Give it time to appear before focus - $timeout(function() { - ctrl.search = initSearchValue || ctrl.search; - ctrl.searchInput[0].focus(); + deferedAnimPromise.promise.then(function(){ + setSearchAndInputFocus(); + $animate.off('removeClass', ctrl.searchInput[0]); }); + } else { + //If ngAnimate is not being used in the given app, we can just set the focus wrapped in a $timeout + $timeout(setSearchAndInputFocus); } };