From 9a045fb77cd52ecad143afa4cd0cf3b030b9702b Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Fri, 26 Jun 2015 11:55:31 -0700 Subject: [PATCH] feat(virtualRepeat): add md-start-index attribute. --- .../virtualRepeat/virtualRepeater.js | 37 +++++++++++++++++-- .../virtualRepeat/virtualRepeater.spec.js | 12 ++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/components/virtualRepeat/virtualRepeater.js b/src/components/virtualRepeat/virtualRepeater.js index c058773179e..d5cf7ab91f5 100644 --- a/src/components/virtualRepeat/virtualRepeater.js +++ b/src/components/virtualRepeat/virtualRepeater.js @@ -111,7 +111,7 @@ function VirtualRepeatContainerController($$rAF, $scope, $element, $attrs) { /** Called by the md-virtual-repeat inside of the container at startup. */ VirtualRepeatContainerController.prototype.register = function(repeaterCtrl) { this.repeater = repeaterCtrl; - + angular.element(this.scroller) .on('scroll wheel touchmove touchend', angular.bind(this, this.handleScroll_)); }; @@ -192,10 +192,17 @@ VirtualRepeatContainerController.prototype.getScrollOffset = function() { return this.scrollOffset; }; +/** + * Scrolls to a given scrollTop position. + * @param {number} position + */ +VirtualRepeatContainerController.prototype.scrollTo = function(position) { + this.scroller[this.isHorizontal() ? 'scrollLeft' : 'scrollTop'] = position; + this.handleScroll_(); +}; VirtualRepeatContainerController.prototype.resetScroll = function() { - this.scroller[this.isHorizontal() ? 'scrollLeft' : 'scrollTop'] = 0; - this.handleScroll_(); + this.scrollTo(0); }; @@ -293,6 +300,13 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document) // getComputedStyle? /** @type {number} Height/width of repeated elements. */ this.itemSize = $scope.$eval($attrs.mdItemSize); + + /** @type {boolean} Whether this is the first time that items are rendered. */ + this.isFirstRender = true; + + /** @type {number} Most recently seen length of items. */ + this.itemsLength = 0; + /** * Presently rendered blocks by repeat index. * @type {Object', function() { var REPEATER_HTML = ''+ '
' + ' {{i}} {{$index}}' + '
'; @@ -28,6 +29,7 @@ describe('', function() { $compile = _$compile_; $document = _$document_; scope = $rootScope.$new(); + scope.startIndex = 0; scroller = null; sizer = null; offsetter = null; @@ -249,6 +251,16 @@ describe('', function() { } }); + it('should start at the given scroll position', function() { + scope.startIndex = 10; + scope.items = createItems(200); + createRepeater(); + scope.$apply(); + $$rAF.flush(); + + expect(scroller[0].scrollTop).toBe(10 * ITEM_SIZE); + }); + /** * Facade to access transform properly even when jQuery is used; * since jQuery's css function is obtaining the computed style (not wanted)