Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 07c488c

Browse files
devversionThomasBurleson
authored andcommitted
feat(autocomplete): expose position dropdown function to controller
* Exposes the `positionDropdown` function to the autocompletes controller. * Adds test for the dropdown positioning (including the new exposed function) Closes #9085. Closes #9180
1 parent d9bd266 commit 07c488c

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,116 @@ describe('<md-autocomplete>', function() {
15621562
});
15631563
});
15641564

1565+
describe('dropdown position', function() {
1566+
1567+
it('should adjust the width when the window resizes', inject(function($timeout, $window) {
1568+
var scope = createScope();
1569+
1570+
var template =
1571+
'<div style="width: 400px">' +
1572+
'<md-autocomplete ' +
1573+
'md-search-text="searchText" ' +
1574+
'md-items="item in match(searchText)" ' +
1575+
'md-item-text="item.display" ' +
1576+
'md-min-length="0" ' +
1577+
'placeholder="placeholder">' +
1578+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
1579+
'</md-autocomplete>' +
1580+
'</div>';
1581+
1582+
var parent = compile(template, scope);
1583+
var element = parent.find('md-autocomplete');
1584+
var ctrl = element.controller('mdAutocomplete');
1585+
1586+
// Add container to the DOM to be able to test the rect calculations.
1587+
document.body.appendChild(parent[0]);
1588+
1589+
$timeout.flush();
1590+
1591+
expect(ctrl.positionDropdown).toBeTruthy();
1592+
1593+
// Focus the Autocomplete to open the dropdown.
1594+
ctrl.focus();
1595+
1596+
scope.$apply('searchText = "fo"');
1597+
waitForVirtualRepeat(element);
1598+
1599+
// The scroll repeat container has been moved to the body element to avoid
1600+
// z-index / overflow issues.
1601+
var scrollContainer = document.body.querySelector('.md-virtual-repeat-container');
1602+
expect(scrollContainer).toBeTruthy();
1603+
1604+
// Expect the current width of the scrollContainer to be the same as of the parent element
1605+
// at initialization.
1606+
expect(scrollContainer.style.minWidth).toBe('400px');
1607+
1608+
// Change the parents width, to be shrink the scrollContainers width.
1609+
parent.css('width', '200px');
1610+
1611+
// Update the scrollContainers rectangle, by triggering a reposition of the dropdown.
1612+
angular.element($window).triggerHandler('resize');
1613+
1614+
// The scroll container should have a width of 200px, since we changed the parents width.
1615+
expect(scrollContainer.style.minWidth).toBe('200px');
1616+
1617+
document.body.removeChild(parent[0]);
1618+
}));
1619+
1620+
it('should adjust the width when manually repositioning', inject(function($timeout) {
1621+
var scope = createScope();
1622+
1623+
var template =
1624+
'<div style="width: 400px">' +
1625+
'<md-autocomplete ' +
1626+
'md-search-text="searchText" ' +
1627+
'md-items="item in match(searchText)" ' +
1628+
'md-item-text="item.display" ' +
1629+
'md-min-length="0" ' +
1630+
'placeholder="placeholder">' +
1631+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
1632+
'</md-autocomplete>' +
1633+
'</div>';
1634+
1635+
var parent = compile(template, scope);
1636+
var element = parent.find('md-autocomplete');
1637+
var ctrl = element.controller('mdAutocomplete');
1638+
1639+
// Add container to the DOM to be able to test the rect calculations.
1640+
document.body.appendChild(parent[0]);
1641+
1642+
$timeout.flush();
1643+
1644+
expect(ctrl.positionDropdown).toBeTruthy();
1645+
1646+
// Focus the Autocomplete to open the dropdown.
1647+
ctrl.focus();
1648+
1649+
scope.$apply('searchText = "fo"');
1650+
waitForVirtualRepeat(element);
1651+
1652+
// The scroll repeat container has been moved to the body element to avoid
1653+
// z-index / overflow issues.
1654+
var scrollContainer = document.body.querySelector('.md-virtual-repeat-container');
1655+
expect(scrollContainer).toBeTruthy();
1656+
1657+
// Expect the current width of the scrollContainer to be the same as of the parent element
1658+
// at initialization.
1659+
expect(scrollContainer.style.minWidth).toBe('400px');
1660+
1661+
// Change the parents width, to be shrink the scrollContainers width.
1662+
parent.css('width', '200px');
1663+
1664+
// Update the scrollContainers rectangle, by triggering a reposition of the dropdown.
1665+
ctrl.positionDropdown();
1666+
1667+
// The scroll container should have a width of 200px, since we changed the parents width.
1668+
expect(scrollContainer.style.minWidth).toBe('200px');
1669+
1670+
document.body.removeChild(parent[0]);
1671+
}));
1672+
1673+
});
1674+
15651675
describe('md-highlight-text', function() {
15661676
it('updates when content is modified', inject(function() {
15671677
var template = '<div md-highlight-text="query">{{message}}</div>';

src/components/autocomplete/js/autocompleteController.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var ITEM_HEIGHT = 41,
99

1010
function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, $window,
1111
$animate, $rootElement, $attrs, $q) {
12-
//-- private variables
12+
13+
// Internal Variables.
1314
var ctrl = this,
1415
itemParts = $scope.itemsExpr.split(/ in /i),
1516
itemExpr = itemParts[ 1 ],
@@ -23,10 +24,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
2324
enableWrapScroll = null,
2425
inputModelCtrl = null;
2526

26-
//-- public variables with handlers
27+
// Public Exported Variables with handlers
2728
defineProperty('hidden', handleHiddenChange, true);
2829

29-
//-- public variables
30+
// Public Exported Variables
3031
ctrl.scope = $scope;
3132
ctrl.parent = $scope.$parent;
3233
ctrl.itemName = itemParts[ 0 ];
@@ -41,7 +42,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
4142
ctrl.isReadonly = null;
4243
ctrl.hasNotFound = false;
4344

44-
//-- public methods
45+
// Public Exported Methods
4546
ctrl.keydown = keydown;
4647
ctrl.blur = blur;
4748
ctrl.focus = focus;
@@ -55,6 +56,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
5556
ctrl.unregisterSelectedItemWatcher = unregisterSelectedItemWatcher;
5657
ctrl.notFoundVisible = notFoundVisible;
5758
ctrl.loadingIsVisible = loadingIsVisible;
59+
ctrl.positionDropdown = positionDropdown;
5860

5961
return init();
6062

0 commit comments

Comments
 (0)