Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

feat(typeahead): anchor popup left or right of input #1378

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 27 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap

var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined;

//anchoring typeahead to side of input (left, right or auto)
var anchor = attrs.typeaheadAnchor ? attrs.typeaheadAnchor : 'left';

//INTERNAL VARIABLES

//model setter executed upon match selection
Expand All @@ -74,7 +77,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
active: 'activeIdx',
select: 'select(activeIdx)',
query: 'query',
position: 'position'
position: 'position',
anchor: anchor
});
//custom item template
if (angular.isDefined(attrs.typeaheadTemplateUrl)) {
Expand Down Expand Up @@ -289,6 +293,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
query:'=',
active:'=',
position:'=',
anchor:'@',
select:'&'
},
replace:true,
Expand All @@ -312,6 +317,27 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
scope.selectMatch = function (activeIdx) {
scope.select({activeIdx:activeIdx});
};

scope.displayStyle = function() {
var style = {display: scope.isOpen()&&'block' || 'none'};
if (!angular.isUndefined(scope.position)) {
style.top = scope.position.top+'px';
if (scope.anchor === 'right' || (scope.anchor === 'auto' && element.width() + scope.position.left > document.documentElement.clientWidth &&
element.width() < scope.position.left + scope.position.width)) {
//anchor on the right if typeahead-anchor is set to right, or if set to auto and it would
//overflow if anchored to left without overflowing when anchored to right
style.left = 'auto';
style.right = document.documentElement.clientWidth - scope.position.left - scope.position.width;
} else if (scope.anchor === 'left') {
//if typeahead-anchor is set to left
style.left = scope.position.left+'px';
} else {
//if typeahead-anchor is set to auto and it overflows whatever the side it is anchored to.
style.left = 0;
}
}
return style;
};
}
};
})
Expand Down
2 changes: 1 addition & 1 deletion template/typeahead/typeahead-popup.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ul class="typeahead dropdown-menu" ng-style="{display: isOpen()&&'block' || 'none', top: position.top+'px', left: position.left+'px'}">
<ul class="typeahead dropdown-menu" ng-style="displayStyle()">
<li ng-repeat="match in matches" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index)">
<div typeahead-match index="$index" match="match" query="query" template-url="templateUrl"></div>
</li>
Expand Down