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

fix(dropdown): cross browser dropdown position #1212

Merged
merged 1 commit into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ uis.controller('uiSelectCtrl',
ctrl.itemProperty = ctrl.parserResult.itemName;

ctrl.refreshItems = function (data){
$scope.calculateDropdownPos();
data = data || ctrl.parserResult.source($scope);
var selectedItems = ctrl.selected;
//TODO should implement for single mode removeSelected
Expand Down
23 changes: 19 additions & 4 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,12 @@ uis.directive('uiSelect',
directionUpClassName = 'direction-up';

// Support changing the direction of the dropdown if there isn't enough space to render it.
scope.$watch('$select.open', function(isOpen) {
if (isOpen) {
scope.$watch('$select.open', function() {
scope.calculateDropdownPos();
});

scope.calculateDropdownPos = function(){
if ($select.open) {
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
if (dropdown === null) {
return;
Expand All @@ -269,14 +273,25 @@ uis.directive('uiSelect',

// Delay positioning the dropdown until all choices have been added so its height is correct.
$timeout(function(){

element.removeClass(directionUpClassName);

var offset = uisOffset(element);
var offsetDropdown = uisOffset(dropdown);

//https://code.google.com/p/chromium/issues/detail?id=342307#c4
var scrollTop = $document[0].documentElement.scrollTop || $document[0].body.scrollTop; //To make it cross browser (blink, webkit, IE, Firefox).

// Determine if the direction of the dropdown needs to be changed.
if (offset.top + offset.height + offsetDropdown.height > $document[0].documentElement.scrollTop + $document[0].documentElement.clientHeight) {
if (offset.top + offset.height + offsetDropdown.height > scrollTop + $document[0].documentElement.clientHeight) {
//Go UP
dropdown[0].style.position = 'absolute';
dropdown[0].style.top = (offsetDropdown.height * -1) + 'px';
element.addClass(directionUpClassName);
}else{
//Go DOWN
dropdown[0].style.position = '';
dropdown[0].style.top = '';
}

// Display the dropdown once it has been positioned.
Expand All @@ -292,7 +307,7 @@ uis.directive('uiSelect',
dropdown[0].style.top = '';
element.removeClass(directionUpClassName);
}
});
};
};
}
};
Expand Down