From 67633273228d55bbc4a2f905d278d4e136fca052 Mon Sep 17 00:00:00 2001 From: Karl Seamon Date: Tue, 14 Jul 2015 18:08:44 -0400 Subject: [PATCH] perf(autocomplete): Use virtual repeat... ...and some other optimizations review --- .../autocomplete/autocomplete-theme.scss | 2 +- src/components/autocomplete/autocomplete.js | 3 +- src/components/autocomplete/autocomplete.scss | 28 ++++---- .../autocomplete/autocomplete.spec.js | 28 ++++++-- .../autocomplete/demoBasicUsage/index.html | 2 +- .../autocomplete/js/autocompleteController.js | 57 ++++++++++------- .../autocomplete/js/autocompleteDirective.js | 53 +++++++-------- .../autocomplete/js/highlightController.js | 64 +++++++------------ .../autocomplete/js/highlightDirective.js | 15 +++-- .../autocomplete/js/listItemDirective.js | 22 ------- src/components/autocomplete/js/parentScope.js | 3 + .../virtualRepeat/virtual-repeater.js | 5 +- 12 files changed, 141 insertions(+), 141 deletions(-) delete mode 100644 src/components/autocomplete/js/listItemDirective.js diff --git a/src/components/autocomplete/autocomplete-theme.scss b/src/components/autocomplete/autocomplete-theme.scss index cee44d3baa..566048a144 100644 --- a/src/components/autocomplete/autocomplete-theme.scss +++ b/src/components/autocomplete/autocomplete-theme.scss @@ -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}}'; diff --git a/src/components/autocomplete/autocomplete.js b/src/components/autocomplete/autocomplete.js index 0b086d308e..f2c657e6d1 100644 --- a/src/components/autocomplete/autocomplete.js +++ b/src/components/autocomplete/autocomplete.js @@ -7,5 +7,6 @@ */ angular.module('material.components.autocomplete', [ 'material.core', - 'material.components.icon' + 'material.components.icon', + 'material.components.virtualRepeat' ]); diff --git a/src/components/autocomplete/autocomplete.scss b/src/components/autocomplete/autocomplete.scss index 136d334f4b..4d43d76a28 100644 --- a/src/components/autocomplete/autocomplete.scss +++ b/src/components/autocomplete/autocomplete.scss @@ -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; @@ -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 { diff --git a/src/components/autocomplete/autocomplete.spec.js b/src/components/autocomplete/autocomplete.spec.js index fd7c320e1d..1f02b102e5 100644 --- a/src/components/autocomplete/autocomplete.spec.js +++ b/src/components/autocomplete/autocomplete.spec.js @@ -36,6 +36,21 @@ describe('', 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(); + }); + } + describe('basic functionality', function () { it('should update selected item and search text', inject(function ($timeout, $mdConstant) { var scope = createScope(); @@ -57,9 +72,11 @@ describe('', 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)); @@ -165,6 +182,7 @@ describe('', function () { element.scope().searchText = 'fo'; $timeout.flush(); + waitForVirtualRepeat(element); expect(scope.searchText).toBe('fo'); expect(scope.match(scope.searchText).length).toBe(1); @@ -182,9 +200,9 @@ describe('', function () { }); describe('xss prevention', function () { - it('should not allow html to slip through', function () { - var html = 'foo '; - var scope = createScope([ { display: html } ]); + it('should not allow html to slip through', inject(function($timeout) { + var html = 'foo '; + var scope = createScope([ { display: html } ]); var template = '\ ', 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); @@ -209,7 +229,7 @@ describe('', function () { expect(ul.find('li').find('img').length).toBe(0); element.remove(); - }); + })); }); describe('API access', function () { diff --git a/src/components/autocomplete/demoBasicUsage/index.html b/src/components/autocomplete/demoBasicUsage/index.html index 7947f7491b..736a411b38 100644 --- a/src/components/autocomplete/demoBasicUsage/index.html +++ b/src/components/autocomplete/demoBasicUsage/index.html @@ -1,7 +1,7 @@
-

Use md-autocomplete to search for matches from local or remote data sources.

+

Use md-autocomplete to search for matches from local or remote data sources.

root.right - MENU_PADDING) { styles.left = (hrect.right - dropdown.width) + 'px'; } - elements.$.ul.css(styles); + elements.$.scrollContainer.css(styles); } } @@ -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); } /** @@ -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'); @@ -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. diff --git a/src/components/autocomplete/js/autocompleteDirective.js b/src/components/autocomplete/js/autocompleteDirective.js index 998591efdb..c9e9a4d2d0 100644 --- a/src/components/autocomplete/js/autocompleteDirective.js +++ b/src/components/autocomplete/js/autocompleteDirective.js @@ -155,21 +155,26 @@ function MdAutocomplete () { \ - \ + \ +
    \ +
  • \ + ' + itemTemplate + '\ +
  • ' + noItemsTemplate + '\ +
\ + \ \ {{message}}

\ '; - 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(), + template = templateTag.length ? templateTag.html() : ''; return template - ? '
  • ' + template + '
  • ' + ? '
  • ' + template + '
  • ' : ''; } diff --git a/src/components/autocomplete/js/highlightController.js b/src/components/autocomplete/js/highlightController.js index 2b4d6f5660..21ca2fbf35 100644 --- a/src/components/autocomplete/js/highlightController.js +++ b/src/components/autocomplete/js/highlightController.js @@ -2,49 +2,33 @@ angular .module('material.components.autocomplete') .controller('MdHighlightCtrl', MdHighlightCtrl); -function MdHighlightCtrl ($scope, $element, $interpolate) { - var ctrl = this; - - ctrl.term = null; - ctrl.template = null; - ctrl.watchers = []; - ctrl.init = init; - - function init (term, template) { - createWatchers(term, template); - $element.on('$destroy', cleanup); - } - - function createWatchers (term, template) { - ctrl.watchers.push($scope.$watch(term, function (term) { - ctrl.term = term; - updateHTML(term, ctrl.template); - })); - ctrl.watchers.push($scope.$watch(compileTemplate, function (template) { - ctrl.template = template; - updateHTML(ctrl.term, template); - })); - - function compileTemplate () { return $interpolate(template)($scope); } - } - - function cleanup () { - ctrl.watchers.forEach(function (watcher) { watcher(); }); - } - - function updateHTML () { - if (ctrl.term === null || ctrl.template === null) return; - var unsafeText = $interpolate(ctrl.template)($scope), - text = angular.element('
    ').text(unsafeText).html(), - flags = $element.attr('md-highlight-flags') || '', - regex = getRegExp(ctrl.term, flags), - html = text.replace(regex, '$&'); - $element.html(html); +function MdHighlightCtrl ($scope, $element, $attrs) { + this.init = init; + + function init (termExpr, unsafeTextExpr) { + var text = null, + regex = null, + flags = $attrs.mdHighlightFlags || '', + watcher = $scope.$watch(function($scope) { + return { + term: termExpr($scope), + unsafeText: unsafeTextExpr($scope) + }; + }, function (state, prevState) { + if (text === null || state.unsafeText !== prevState.unsafeText) { + text = angular.element('
    ').text(state.unsafeText).html() + } + if (regex === null || state.term !== prevState.term) { + regex = getRegExp(state.term, flags); + } + + $element.html(text.replace(regex, '$&')); + }, true); + $element.on('$destroy', function () { watcher(); }); } function sanitize (term) { - if (!term) return term; - return term.replace(/[\\\^\$\*\+\?\.\(\)\|\{}\[\]]/g, '\\$&'); + return term && term.replace(/[\\\^\$\*\+\?\.\(\)\|\{}\[\]]/g, '\\$&'); } function getRegExp (text, flags) { diff --git a/src/components/autocomplete/js/highlightDirective.js b/src/components/autocomplete/js/highlightDirective.js index f40ecf81d0..bb263d8a99 100644 --- a/src/components/autocomplete/js/highlightDirective.js +++ b/src/components/autocomplete/js/highlightDirective.js @@ -31,15 +31,16 @@ angular * */ -function MdHighlight () { +function MdHighlight ($interpolate, $parse) { return { - terminal: true, - scope: false, + terminal: true, controller: 'MdHighlightCtrl', - compile: function (element, attr) { - var template = element.html(); - return function (scope, element, attr, ctrl) { - ctrl.init(attr.mdHighlightText, template); + compile: function mdHighlightCompile(tElement, tAttr) { + var termExpr = $parse(tAttr.mdHighlightText); + var unsafeTextExpr = $interpolate(tElement.html()); + + return function mdHighlightLink(scope, element, attr, ctrl) { + ctrl.init(termExpr, unsafeTextExpr); }; } }; diff --git a/src/components/autocomplete/js/listItemDirective.js b/src/components/autocomplete/js/listItemDirective.js deleted file mode 100644 index af595c0e5f..0000000000 --- a/src/components/autocomplete/js/listItemDirective.js +++ /dev/null @@ -1,22 +0,0 @@ -angular - .module('material.components.autocomplete') - .directive('mdAutocompleteListItem', MdAutocompleteListItem); - -function MdAutocompleteListItem ($compile, $mdUtil) { - return { - terminal: true, - link: postLink, - scope: false - }; - function postLink (scope, element, attr) { - var ctrl = scope.$parent.$mdAutocompleteCtrl, - newScope = ctrl.parent.$new(false, ctrl.parent), - itemName = ctrl.scope.$eval(attr.mdAutocompleteListItem); - newScope[ itemName ] = scope.item; - $compile(element.contents())(newScope); - element.attr({ - role: 'option', - id: 'item_' + $mdUtil.nextUid() - }); - } -} diff --git a/src/components/autocomplete/js/parentScope.js b/src/components/autocomplete/js/parentScope.js index 1894458801..de0c525859 100644 --- a/src/components/autocomplete/js/parentScope.js +++ b/src/components/autocomplete/js/parentScope.js @@ -11,6 +11,9 @@ function MdAutocompleteParentScope ($compile) { }; function postLink (scope, element, attr) { var ctrl = scope.$parent.$mdAutocompleteCtrl; + + // TODO: transclude self might make it possible to do this without + // re-compiling, which is slow. $compile(element.contents())(ctrl.parent); if (attr.hasOwnProperty('mdAutocompleteReplace')) { element.after(element.contents()); diff --git a/src/components/virtualRepeat/virtual-repeater.js b/src/components/virtualRepeat/virtual-repeater.js index 167f62352f..8f79d38eb4 100644 --- a/src/components/virtualRepeat/virtual-repeater.js +++ b/src/components/virtualRepeat/virtual-repeater.js @@ -436,6 +436,9 @@ VirtualRepeatController.prototype.readItemSize_ = function() { this.items = this.repeatListExpression(this.$scope); this.parentNode = this.$element[0].parentNode; var block = this.getBlock_(0); + if (!block.element[0].parentNode) { + this.parentNode.appendChild(block.element[0]); + } this.itemSize = block.element[0][ this.container.isHorizontal() ? 'offsetWidth' : 'offsetHeight'] || null; @@ -475,7 +478,7 @@ VirtualRepeatController.prototype.repeatListExpression_ = function(scope) { VirtualRepeatController.prototype.containerUpdated = function() { // If itemSize is unknown, attempt to measure it. if (!this.itemSize) { - this.unwatchItemSize_ = this.$scope.$watch( + this.unwatchItemSize_ = this.$scope.$watchCollection( this.repeatListExpression, angular.bind(this, function(items) { if (items && items.length) {