Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion src/components/autocomplete/autocomplete-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ md-autocomplete.md-THEME_NAME-theme {
}
}
}
.md-autocomplete-suggestions.md-THEME_NAME-theme {
.md-autocomplete-suggestions-container.md-THEME_NAME-theme, {
background: '{{background-50}}';
li {
color: '{{background-900}}';
Expand Down
3 changes: 2 additions & 1 deletion src/components/autocomplete/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
*/
angular.module('material.components.autocomplete', [
'material.core',
'material.components.icon'
'material.components.icon',
'material.components.virtualRepeat'
]);
28 changes: 11 additions & 17 deletions src/components/autocomplete/autocomplete.scss
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,18 @@ md-autocomplete {
}
}
}
.md-autocomplete-suggestions {

.md-virtual-repeat-container.md-autocomplete-suggestions-container {
position: absolute;
box-shadow: 0 2px 5px rgba(black, 0.25);
height: 41px * 5.5;
max-height: 41px * 5.5;
z-index: $z-index-tooltip;
}
.md-autocomplete-suggestions {
margin: 0;
list-style: none;
padding: 0;
overflow: auto;
max-height: 41px * 5.5;
z-index: $z-index-tooltip;
li {
cursor: pointer;
font-size: 14px;
Expand All @@ -189,23 +193,13 @@ md-autocomplete {
margin: 0;
white-space: nowrap;
text-overflow: ellipsis;
&.ng-enter,
&.ng-hide-remove {
transition: none;
animation: md-autocomplete-list-in 0.2s;
}
&.ng-leave,
&.ng-hide-add {
transition: none;
animation: md-autocomplete-list-out 0.2s;
}

&:focus {
outline: none;
}

outline: none;
}
}
}

@media screen and (-ms-high-contrast: active) {
md-autocomplete,
.md-autocomplete-suggestions {
Expand Down
28 changes: 24 additions & 4 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ describe('<md-autocomplete>', function () {
};
}

function waitForVirtualRepeat(element) {
// Because the autocomplete does not make the suggestions menu visible
// off the bat, the virtual repeat needs a couple more iterations to
// figure out how tall it is and then how tall the repeated items are.

// Using md-item-size would reduce this to a single flush, but given that
// autocomplete allows for custom row templates, it's better to measure
// rather than assuming a given size.
inject(function ($$rAF) {
$$rAF.flush();
element.scope().$apply();
$$rAF.flush();
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, I think I get it: You just have to scope.$apply(), then $timeout.flush(), then $$rAF.flush(), then again scope.$apply(), then finally $$rAF.flush() again and...BAM...virtual-repeat ready !!!
Nice !

(Some comments would be also nice here, but I guess that's not very straightforward.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LOL - completely obvious.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

as @gkalpak says, the code is 100% self-documenting and not confusing at all.

However, I've added some comments as well.

}

describe('basic functionality', function () {
it('should update selected item and search text', inject(function ($timeout, $mdConstant) {
var scope = createScope();
Expand All @@ -57,9 +72,11 @@ describe('<md-autocomplete>', function () {

element.scope().searchText = 'fo';
$timeout.flush();
waitForVirtualRepeat(element);

expect(scope.searchText).toBe('fo');
expect(scope.match(scope.searchText).length).toBe(1);

expect(ul.find('li').length).toBe(1);

ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.DOWN_ARROW));
Expand Down Expand Up @@ -165,6 +182,7 @@ describe('<md-autocomplete>', function () {

element.scope().searchText = 'fo';
$timeout.flush();
waitForVirtualRepeat(element);

expect(scope.searchText).toBe('fo');
expect(scope.match(scope.searchText).length).toBe(1);
Expand All @@ -182,9 +200,9 @@ describe('<md-autocomplete>', function () {
});

describe('xss prevention', function () {
it('should not allow html to slip through', function () {
var html = 'foo <img src="img" onerror="alert(1)" />';
var scope = createScope([ { display: html } ]);
it('should not allow html to slip through', inject(function($timeout) {
var html = 'foo <img src="img" onerror="alert(1)" />';
var scope = createScope([ { display: html } ]);
var template = '\
<md-autocomplete\
md-selected-item="selectedItem"\
Expand All @@ -202,14 +220,16 @@ describe('<md-autocomplete>', function () {
expect(scope.selectedItem).toBe(null);

scope.$apply('searchText = "fo"');
$timeout.flush();
waitForVirtualRepeat(element);

expect(scope.searchText).toBe('fo');
expect(scope.match(scope.searchText).length).toBe(1);
expect(ul.find('li').length).toBe(1);
expect(ul.find('li').find('img').length).toBe(0);

element.remove();
});
}));
});

describe('API access', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/components/autocomplete/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div ng-controller="DemoCtrl as ctrl" layout="column">
<md-content class="md-padding">
<form ng-submit="$event.preventDefault()">
<p>Use <code>md-autocomplete</code> to search for matches from local or remote data sources.</p>
<p>Use <code>md-autocomplete</code> to search for matches from local or remote data sources.</p>
<md-autocomplete
ng-disabled="ctrl.isDisabled"
md-no-cache="ctrl.noCache"
Expand Down
57 changes: 35 additions & 22 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
ctrl.getCurrentDisplayValue = getCurrentDisplayValue;
ctrl.registerSelectedItemWatcher = registerSelectedItemWatcher;
ctrl.unregisterSelectedItemWatcher = unregisterSelectedItemWatcher;
ctrl.notFoundVisible = notFoundVisible;

return init();

Expand Down Expand Up @@ -94,19 +95,20 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
styles.bottom = 'auto';
styles.maxHeight = Math.min(MAX_HEIGHT, root.bottom - hrect.bottom - MENU_PADDING) + 'px';
}
elements.$.ul.css(styles);

elements.$.scrollContainer.css(styles);
$mdUtil.nextTick(correctHorizontalAlignment, false);

/**
* Makes sure that the menu doesn't go off of the screen on either side.
*/
function correctHorizontalAlignment () {
var dropdown = elements.ul.getBoundingClientRect(),
var dropdown = elements.scrollContainer.getBoundingClientRect(),
styles = {};
if (dropdown.right > root.right - MENU_PADDING) {
styles.left = (hrect.right - dropdown.width) + 'px';
}
elements.$.ul.css(styles);
elements.$.scrollContainer.css(styles);
}
}

Expand All @@ -115,10 +117,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
*/
function moveDropdown () {
if (!elements.$.root.length) return;
$mdTheming(elements.$.ul);
elements.$.ul.detach();
elements.$.root.append(elements.$.ul);
if ($animate.pin) $animate.pin(elements.$.ul, $rootElement);
$mdTheming(elements.$.scrollContainer);
elements.$.scrollContainer.detach();
elements.$.root.append(elements.$.scrollContainer);
if ($animate.pin) $animate.pin(elements.$.scrollContainer, $rootElement);
}

/**
Expand Down Expand Up @@ -146,18 +148,20 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
*/
function cleanup () {
angular.element($window).off('resize', positionDropdown);
elements.$.ul.remove();
elements.$.scrollContainer.remove();
}

/**
* Gathers all of the elements needed for this controller
*/
function gatherElements () {
elements = {
main: $element[ 0 ],
ul: $element.find('ul')[ 0 ],
input: $element.find('input')[ 0 ],
wrap: $element.find('md-autocomplete-wrap')[ 0 ],
elements = {
main: $element[0],
scrollContainer: $element[0].getElementsByClassName('md-virtual-repeat-container')[0],
scroller: $element[0].getElementsByClassName('md-virtual-repeat-scroller')[0],
ul: $element.find('ul')[0],
input: $element.find('input')[0],
wrap: $element.find('md-autocomplete-wrap')[0],
root: document.body
};
elements.li = elements.ul.getElementsByTagName('li');
Expand Down Expand Up @@ -567,18 +571,27 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
* Makes sure that the focused element is within view.
*/
function updateScroll () {
if (!elements.li[ ctrl.index ]) return;
var li = elements.li[ ctrl.index ],
top = li.offsetTop,
bot = top + li.offsetHeight,
hgt = elements.ul.clientHeight;
if (top < elements.ul.scrollTop) {
elements.ul.scrollTop = top;
} else if (bot > elements.ul.scrollTop + hgt) {
elements.ul.scrollTop = bot - hgt;
if (!elements.li[0]) return;
var height = elements.li[0].offsetHeight,
top = height * ctrl.index,
bot = top + height,
hgt = elements.scroller.clientHeight,
scrollTop = elements.scroller.scrollTop;
if (top < scrollTop) {
scrollTo(top);
} else if (bot > scrollTop + hgt) {
scrollTo(bot - hgt);
}
}

function scrollTo (offset) {
elements.$.scrollContainer.controller('mdVirtualRepeatContainer').scrollTo(offset);
}

function notFoundVisible () {
return !ctrl.matches.length && !ctrl.loading && ctrl.scope.searchText;
}

/**
* Starts the query to gather the results for the current searchText. Attempts to return cached
* results first, then forwards the process to `fetchResults` if necessary.
Expand Down
53 changes: 28 additions & 25 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,26 @@ function MdAutocomplete () {
<md-progress-linear\
ng-if="$mdAutocompleteCtrl.loading && !$mdAutocompleteCtrl.hidden"\
md-mode="indeterminate"></md-progress-linear>\
<ul role="presentation"\
class="md-autocomplete-suggestions md-whiteframe-z1 {{menuClass || \'\'}}"\
id="ul-{{$mdAutocompleteCtrl.id}}"\
ng-hide="$mdAutocompleteCtrl.hidden"\
ng-mouseenter="$mdAutocompleteCtrl.listEnter()"\
ng-mouseleave="$mdAutocompleteCtrl.listLeave()"\
ng-mouseup="$mdAutocompleteCtrl.mouseUp()">\
<li ng-repeat="(index, item) in $mdAutocompleteCtrl.matches"\
ng-class="{ selected: index === $mdAutocompleteCtrl.index }"\
ng-click="$mdAutocompleteCtrl.select(index)"\
md-autocomplete-list-item="$mdAutocompleteCtrl.itemName">\
' + itemTemplate + '\
</li>\
' + noItemsTemplate + '\
</ul>\
<md-virtual-repeat-container\
md-auto-shrink\
md-auto-shrink-min="1"\
ng-hide="$mdAutocompleteCtrl.hidden && !$mdAutocompleteCtrl.notFoundVisible()"\
class="md-autocomplete-suggestions-container md-whiteframe-z1"\
role="presentation">\
<ul class="md-autocomplete-suggestions"\
ng-class="::menuClass"\
id="ul-{{$mdAutocompleteCtrl.id}}"\
ng-mouseenter="$mdAutocompleteCtrl.listEnter()"\
ng-mouseleave="$mdAutocompleteCtrl.listLeave()"\
ng-mouseup="$mdAutocompleteCtrl.mouseUp()">\
<li md-virtual-repeat="item in $mdAutocompleteCtrl.matches"\
ng-class="{ selected: $index === $mdAutocompleteCtrl.index }"\
ng-click="$mdAutocompleteCtrl.select($index)"\
md-extra-name="$mdAutocompleteCtrl.itemName">\
' + itemTemplate + '\
</li>' + noItemsTemplate + '\
</ul>\
</md-virtual-repeat-container>\
</md-autocomplete-wrap>\
<aria-status\
class="md-visually-hidden"\
Expand All @@ -178,21 +183,19 @@ function MdAutocomplete () {
<p ng-repeat="message in $mdAutocompleteCtrl.messages track by $index" ng-if="message">{{message}}</p>\
</aria-status>';

function getItemTemplate () {
var templateTag = element.find('md-item-template').remove(),
html = templateTag.length ? templateTag.html() : element.html();
function getItemTemplate() {
var templateTag = element.find('md-item-template').detach(),
html = templateTag.length ? templateTag.html() : element.html();
if (!templateTag.length) element.empty();
return html;
}

function getNoItemsTemplate () {
var templateTag = element.find('md-not-found').remove(),
template = templateTag.length ? templateTag.html() : '';
function getNoItemsTemplate() {
var templateTag = element.find('md-not-found').detach(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe this is just being used as a template (and angular isn't actually parsing it) - does it matter if we use remove or detach?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/angular/angular.js/blob/master/src/jqLite.js

remove will call into jqLiteDealloc, which tries to find data associated with the elements to clean up. In this case we can be confident that such data does not exist, so detach is faster.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, very cool!

template = templateTag.length ? templateTag.html() : '';
return template
? '<li ng-if="!$mdAutocompleteCtrl.matches.length && !$mdAutocompleteCtrl.loading\
&& !$mdAutocompleteCtrl.hidden"\
ng-hide="$mdAutocompleteCtrl.hidden"\
md-autocomplete-parent-scope>' + template + '</li>'
? '<li ng-if="$mdAutocompleteCtrl.notFoundVisible()"\
md-autocomplete-parent-scope>' + template + '</li>'
: '';

}
Expand Down
Loading