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

Fixes https://github.com/angular-ui/ui-select/issues/859 #1152

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
};

Expand Down