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

Commit aa90dd8

Browse files
committed
fix(positioning): wait for animation to complete
When calculating the dropdown position the height of the dropdown needs to be known. When using ngAnimate the animation has not finished by the time the height calculation is needed. This fix extracts the positioning calculation out to a function that is called after animation is done (or immediately when animation is not in use). Fixes #1593.
1 parent 2133f82 commit aa90dd8

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

Diff for: src/uiSelectDirective.js

+49-37
Original file line numberDiff line numberDiff line change
@@ -323,57 +323,69 @@ uis.directive('uiSelect',
323323

324324
};
325325

326-
scope.calculateDropdownPos = function(){
326+
var calculateDropdownPosAfterAnimation = function() {
327+
// Delay positioning the dropdown until all choices have been added so its height is correct.
328+
$timeout(function() {
329+
if ($select.dropdownPosition === 'up') {
330+
//Go UP
331+
setDropdownPosUp();
332+
} else {
333+
//AUTO
334+
element.removeClass(directionUpClassName);
335+
336+
var offset = uisOffset(element);
337+
var offsetDropdown = uisOffset(dropdown);
338+
339+
//https://code.google.com/p/chromium/issues/detail?id=342307#c4
340+
var scrollTop = $document[0].documentElement.scrollTop || $document[0].body.scrollTop; //To make it cross browser (blink, webkit, IE, Firefox).
341+
342+
// Determine if the direction of the dropdown needs to be changed.
343+
if (offset.top + offset.height + offsetDropdown.height > scrollTop + $document[0].documentElement.clientHeight) {
344+
//Go UP
345+
setDropdownPosUp(offset, offsetDropdown);
346+
}else{
347+
//Go DOWN
348+
setDropdownPosDown(offset, offsetDropdown);
349+
}
350+
}
327351

352+
// Display the dropdown once it has been positioned.
353+
dropdown[0].style.opacity = 1;
354+
});
355+
};
356+
357+
scope.calculateDropdownPos = function() {
328358
if ($select.open) {
329359
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
360+
330361
if (dropdown.length === 0) {
331362
return;
332363
}
333364

334365
// Hide the dropdown so there is no flicker until $timeout is done executing.
335366
dropdown[0].style.opacity = 0;
336367

337-
// Delay positioning the dropdown until all choices have been added so its height is correct.
338-
$timeout(function(){
339-
340-
if ($select.dropdownPosition === 'up'){
341-
//Go UP
342-
setDropdownPosUp();
343-
344-
}else{ //AUTO
368+
if (!uisOffset(dropdown).height && $select.$animate && $select.$animate.on && $select.$animate.enabled(dropdown)) {
369+
var needsCalculated = true;
345370

346-
element.removeClass(directionUpClassName);
347-
348-
var offset = uisOffset(element);
349-
var offsetDropdown = uisOffset(dropdown);
350-
351-
//https://code.google.com/p/chromium/issues/detail?id=342307#c4
352-
var scrollTop = $document[0].documentElement.scrollTop || $document[0].body.scrollTop; //To make it cross browser (blink, webkit, IE, Firefox).
353-
354-
// Determine if the direction of the dropdown needs to be changed.
355-
if (offset.top + offset.height + offsetDropdown.height > scrollTop + $document[0].documentElement.clientHeight) {
356-
//Go UP
357-
setDropdownPosUp(offset, offsetDropdown);
358-
}else{
359-
//Go DOWN
360-
setDropdownPosDown(offset, offsetDropdown);
371+
$select.$animate.on('enter', dropdown, function (elem, phase) {
372+
if (phase === 'close' && needsCalculated) {
373+
calculateDropdownPosAfterAnimation();
374+
needsCalculated = false;
361375
}
362-
363-
}
364-
365-
// Display the dropdown once it has been positioned.
366-
dropdown[0].style.opacity = 1;
367-
});
376+
});
377+
} else {
378+
calculateDropdownPosAfterAnimation();
379+
}
368380
} else {
369-
if (dropdown === null || dropdown.length === 0) {
370-
return;
371-
}
381+
if (dropdown === null || dropdown.length === 0) {
382+
return;
383+
}
372384

373-
// Reset the position of the dropdown.
374-
dropdown[0].style.position = '';
375-
dropdown[0].style.top = '';
376-
element.removeClass(directionUpClassName);
385+
// Reset the position of the dropdown.
386+
dropdown[0].style.position = '';
387+
dropdown[0].style.top = '';
388+
element.removeClass(directionUpClassName);
377389
}
378390
};
379391
};

0 commit comments

Comments
 (0)