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

On collection change update position of upward dropdown (fixes #1167) #1168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ body > .select2-container.open {

box-shadow: 0 -4px 8px rgba(0, 0, 0, 0.25);

margin-top: -4px; /* FIXME hardcoded value :-/ */
margin-top: 1px; /* FIXME hardcoded value :-/ */
border-bottom-width: 0;
}
.ui-select-container[theme="select2"].direction-up .ui-select-dropdown .select2-search {
margin-top: 4px; /* FIXME hardcoded value :-/ */
Expand Down
22 changes: 21 additions & 1 deletion src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ uis.directive('uiSelect',
var dropdown = null,
directionUpClassName = 'direction-up';

var repositionUpwardDropdown = function () {
if (!$select.open || !element.hasClass(directionUpClassName)) {
return;
}

if ($select.open) {
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
if (dropdown === null) {
return;
}
var offsetDropdown = uisOffset(dropdown);
dropdown[0].style.top = offsetDropdown.height * -1 + 'px';
}
};

// Support changing the direction of the dropdown if there isn't enough space to render it.
scope.$watch('$select.open', function(isOpen) {
if (isOpen) {
Expand All @@ -274,9 +289,14 @@ uis.directive('uiSelect',

// 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) {
element.addClass(directionUpClassName);
offsetDropdown = uisOffset(dropdown); // recalculate after class changed
dropdown[0].style.position = 'absolute';
dropdown[0].style.top = (offsetDropdown.height * -1) + 'px';
element.addClass(directionUpClassName);
// Reposition upward dropdown each time the collection of items changes
scope.$watchCollection('$select.items', function() {
$timeout(repositionUpwardDropdown);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a big optimization that can be done here. Instead of doing a $timeout everytime it changes, scheduling an operation with scope.$$postDigest would batch it. This requires the addition of a boolean check inside the $watchCollection callback, and resetting the flag when the function is executed.

Something like

var foo = false;
scope.$watchCollection('$select.items', function() {
  if (!foo) {
    scope.$$postDigest(function() {
      foo = false;
      repositionUpwardDropdown();
    });
  }
});

});
}

// Display the dropdown once it has been positioned.
Expand Down