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

Commit d68b4f6

Browse files
kseamonThomasBurleson
authored andcommitted
feat(virtualRepeat): Infinite scroll and deferred data loading
includes demos for both infinite scroll and deferred loading. Closes #4002.
1 parent 814cc97 commit d68b4f6

File tree

7 files changed

+296
-3
lines changed

7 files changed

+296
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div ng-controller="AppCtrl as ctrl">
2+
<md-content layout="column">
3+
<p>
4+
Display a list of 50,000 items that load on demand in a viewport of only 7 rows (height=40px).
5+
<br/><br/>
6+
This demo shows scroll and rendering performance gains when using <code>md-virtual-repeat</code>;
7+
achieved with the dynamic reuse of rows visible in the viewport area. Developers are required to
8+
explicitly use <code>md-virtual-repeat-container</code> as a wrapping parent container.
9+
<br/><br/>
10+
To enable load-on-demand behavior, developers must pass in a custom instance of
11+
mdVirtualRepeatModel (see the example's source for more info).
12+
</p>
13+
14+
<md-virtual-repeat-container id="vertical-container">
15+
<div md-virtual-repeat="item in ctrl.dynamicItems" md-on-demand
16+
class="repeated-item" flex>
17+
{{item}}
18+
</div>
19+
</md-virtual-repeat-container>
20+
</md-content>
21+
22+
</div>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(function () {
2+
'use strict';
3+
4+
angular
5+
.module('virtualRepeatDeferredLoadingDemo', ['ngMaterial'])
6+
.controller('AppCtrl', function($timeout) {
7+
8+
// In this example, we set up our model using a class.
9+
// Using a plain object works too. All that matters
10+
// is that we implement getItemAtIndex and getLength.
11+
var DynamicItems = function() {
12+
/**
13+
* @type {!Object<?Array>} Data pages, keyed by page number (0-index).
14+
*/
15+
this.loadedPages = {};
16+
17+
/** @type {number} Total number of items. */
18+
this.numItems = 0;
19+
20+
/** @const {number} Number of items to fetch per request. */
21+
this.PAGE_SIZE = 50;
22+
23+
this.fetchNumItems_();
24+
};
25+
26+
// Required.
27+
DynamicItems.prototype.getItemAtIndex = function(index) {
28+
var pageNumber = Math.floor(index / this.PAGE_SIZE);
29+
var page = this.loadedPages[pageNumber];
30+
31+
if (page) {
32+
return page[index % this.PAGE_SIZE];
33+
} else if (page !== null) {
34+
this.fetchPage_(pageNumber);
35+
}
36+
};
37+
38+
// Required.
39+
DynamicItems.prototype.getLength = function() {
40+
return this.numItems;
41+
};
42+
43+
DynamicItems.prototype.fetchPage_ = function(pageNumber) {
44+
// Set the page to null so we know it is already being fetched.
45+
this.loadedPages[pageNumber] = null;
46+
47+
// For demo purposes, we simulate loading more items with a timed
48+
// promise. In real code, this function would likely contain an
49+
// $http request.
50+
$timeout(angular.noop, 300).then(angular.bind(this, function() {
51+
this.loadedPages[pageNumber] = [];
52+
var pageOffset = pageNumber * this.PAGE_SIZE;
53+
for (var i = pageOffset; i < pageOffset + this.PAGE_SIZE; i++) {
54+
this.loadedPages[pageNumber].push(i);
55+
}
56+
}));
57+
};
58+
59+
DynamicItems.prototype.fetchNumItems_ = function() {
60+
// For demo purposes, we simulate loading the item count with a timed
61+
// promise. In real code, this function would likely contain an
62+
// $http request.
63+
$timeout(angular.noop, 300).then(angular.bind(this, function() {
64+
this.numItems = 50000;
65+
}));
66+
};
67+
68+
this.dynamicItems = new DynamicItems();
69+
});
70+
})();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#vertical-container {
2+
height: 292px;
3+
width: 400px;
4+
}
5+
6+
.repeated-item {
7+
border-bottom: 1px solid #ddd;
8+
box-sizing: border-box;
9+
height: 40px;
10+
padding-top: 10px;
11+
}
12+
13+
md-content {
14+
margin: 16px;
15+
}
16+
17+
md-virtual-repeat-container {
18+
border: solid 1px grey;
19+
}
20+
21+
.md-virtual-repeat-container .md-virtual-repeat-offsetter {
22+
padding-left: 16px;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div ng-controller="AppCtrl as ctrl">
2+
<md-content layout="column">
3+
<p>
4+
Display an infinitely growing list of items in a viewport of only 7 rows (height=40px).
5+
<br/><br/>
6+
This demo shows scroll and rendering performance gains when using <code>md-virtual-repeat</code>;
7+
achieved with the dynamic reuse of rows visible in the viewport area. Developers are required to
8+
explicitly use <code>md-virtual-repeat-container</code> as a wrapping parent container.
9+
<br/><br/>
10+
To enable infinite scroll behavior, developers must pass in a custom instance of
11+
mdVirtualRepeatModel (see the example's source for more info).
12+
</p>
13+
14+
<md-virtual-repeat-container id="vertical-container">
15+
<div md-virtual-repeat="item in ctrl.infiniteItems" md-on-demand
16+
class="repeated-item" flex>
17+
{{item}}
18+
</div>
19+
</md-virtual-repeat-container>
20+
</md-content>
21+
22+
</div>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(function () {
2+
'use strict';
3+
4+
angular
5+
.module('virtualRepeatInfiniteScrollDemo', ['ngMaterial'])
6+
.controller('AppCtrl', function($timeout) {
7+
8+
// In this example, we set up our model using a plain object.
9+
// Using a class works too. All that matters is that we implement
10+
// getItemAtIndex and getLength.
11+
this.infiniteItems = {
12+
numLoaded_: 0,
13+
toLoad_: 0,
14+
15+
// Required.
16+
getItemAtIndex: function(index) {
17+
if (index > this.numLoaded_) {
18+
this.fetchMoreItems_(index);
19+
return null;
20+
}
21+
22+
return index;
23+
},
24+
25+
// Required.
26+
// For infinite scroll behavior, we always return a slightly higher
27+
// number than the previously loaded items.
28+
getLength: function() {
29+
return this.numLoaded_ + 5;
30+
},
31+
32+
fetchMoreItems_: function(index) {
33+
// For demo purposes, we simulate loading more items with a timed
34+
// promise. In real code, this function would likely contain an
35+
// $http request.
36+
37+
if (this.toLoad_ < index) {
38+
this.toLoad_ += 20;
39+
$timeout(angular.noop, 300).then(angular.bind(this, function() {
40+
this.numLoaded_ = this.toLoad_;
41+
}));
42+
}
43+
}
44+
};
45+
});
46+
47+
})();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#vertical-container {
2+
height: 292px;
3+
width: 400px;
4+
}
5+
6+
.repeated-item {
7+
border-bottom: 1px solid #ddd;
8+
box-sizing: border-box;
9+
height: 40px;
10+
padding-top: 10px;
11+
}
12+
13+
md-content {
14+
margin: 16px;
15+
}
16+
17+
md-virtual-repeat-container {
18+
border: solid 1px grey;
19+
}
20+
21+
.md-virtual-repeat-container .md-virtual-repeat-offsetter {
22+
padding-left: 16px;
23+
}

src/components/virtualRepeat/virtualRepeater.js

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ VirtualRepeatContainerController.prototype.handleScroll_ = function() {
311311
* @param {string=} md-extra-name Evaluates to an additional name to which
312312
* the current iterated item can be assigned on the repeated scope. (Needed
313313
* for use in md-autocomplete).
314+
* @param {boolean=} md-on-demand When present, treats the md-virtual-repeat argument
315+
* as an object that can fetch rows rather than an array.
316+
* NOTE: This object must implement the following interface with two (2) methods:
317+
* getItemAtIndex: function(index) -> item at that index or null if it is not yet
318+
* loaded (It should start downloading the item in that case).
319+
* getLength: function() -> number The data legnth to which the repeater container
320+
* should be sized. Ideally, when the count is known, this method should return it.
321+
* Otherwise, return a higher number than the currently loaded items to produce an
322+
* infinite-scroll behavior.
314323
*/
315324
function VirtualRepeatDirective($parse) {
316325
return {
@@ -344,12 +353,16 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
344353
this.$document = $document;
345354
this.$$rAF = $$rAF;
346355

356+
/** @type {boolean} Whether we are in on-demand mode. */
357+
this.onDemand = $attrs.hasOwnProperty('mdOnDemand');
347358
/** @type {!Function} Backup reference to $browser.$$checkUrlChange */
348359
this.browserCheckUrlChange = $browser.$$checkUrlChange;
349360
/** @type {number} Most recent starting repeat index (based on scroll offset) */
350361
this.newStartIndex = 0;
351362
/** @type {number} Most recent ending repeat index (based on scroll offset) */
352363
this.newEndIndex = 0;
364+
/** @type {number} Most recent end visible index (based on scroll offset) */
365+
this.newVisibleEnd = 0;
353366
/** @type {number} Previous starting repeat index (based on scroll offset) */
354367
this.startIndex = 0;
355368
/** @type {number} Previous ending repeat index (based on scroll offset) */
@@ -403,10 +416,12 @@ VirtualRepeatController.prototype.link_ =
403416
this.container = container;
404417
this.transclude = transclude;
405418
this.repeatName = repeatName;
406-
this.repeatListExpression = repeatListExpression;
419+
this.rawRepeatListExpression = repeatListExpression;
407420
this.extraName = extraName;
408421
this.sized = false;
409422

423+
this.repeatListExpression = angular.bind(this, this.repeatListExpression_);
424+
410425
this.container.register(this);
411426
};
412427

@@ -434,6 +449,25 @@ VirtualRepeatController.prototype.readItemSize_ = function() {
434449
};
435450

436451

452+
/**
453+
* Returns the user-specified repeat list, transforming it into an array-like
454+
* object in the case of infinite scroll/dynamic load mode.
455+
* @param {!angular.Scope} The scope.
456+
* @return {!Array|!Object} An array or array-like object for iteration.
457+
*/
458+
VirtualRepeatController.prototype.repeatListExpression_ = function(scope) {
459+
var repeatList = this.rawRepeatListExpression(scope);
460+
461+
if (this.onDemand && repeatList) {
462+
var virtualList = new VirtualRepeatModelArrayLike(repeatList);
463+
virtualList.$$includeIndexes(this.newStartIndex, this.newVisibleEnd);
464+
return virtualList;
465+
} else {
466+
return repeatList;
467+
}
468+
};
469+
470+
437471
/**
438472
* Called by the container. Informs us that the containers scroll or size has
439473
* changed.
@@ -467,6 +501,9 @@ VirtualRepeatController.prototype.containerUpdated = function() {
467501
if (this.newStartIndex !== this.startIndex ||
468502
this.newEndIndex !== this.endIndex ||
469503
this.container.getScrollOffset() > this.container.getScrollSize()) {
504+
if (this.items instanceof VirtualRepeatModelArrayLike) {
505+
this.items.$$includeIndexes(this.newStartIndex, this.newEndIndex);
506+
}
470507
this.virtualRepeatUpdate_(this.items, this.items);
471508
}
472509
};
@@ -503,7 +540,7 @@ VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItem
503540
}
504541

505542
this.items = items;
506-
if (items !== oldItems) {
543+
if (items !== oldItems || lengthChanged) {
507544
this.updateIndexes_();
508545
}
509546

@@ -684,6 +721,55 @@ VirtualRepeatController.prototype.updateIndexes_ = function() {
684721
this.newStartIndex = Math.max(0, Math.min(
685722
itemsLength - containerLength,
686723
Math.floor(this.container.getScrollOffset() / this.itemSize)));
687-
this.newEndIndex = Math.min(itemsLength, this.newStartIndex + containerLength + NUM_EXTRA);
724+
this.newVisibleEnd = this.newStartIndex + containerLength + NUM_EXTRA;
725+
this.newEndIndex = Math.min(itemsLength, this.newVisibleEnd);
688726
this.newStartIndex = Math.max(0, this.newStartIndex - NUM_EXTRA);
689727
};
728+
729+
/**
730+
* This VirtualRepeatModelArrayLike class enforces the interface requirements
731+
* for infinite scrolling within a mdVirtualRepeatContainer. An object with this
732+
* interface must implement the following interface with two (2) methods:
733+
*
734+
* getItemAtIndex: function(index) -> item at that index or null if it is not yet
735+
* loaded (It should start downloading the item in that case).
736+
*
737+
* getLength: function() -> number The data legnth to which the repeater container
738+
* should be sized. Ideally, when the count is known, this method should return it.
739+
* Otherwise, return a higher number than the currently loaded items to produce an
740+
* infinite-scroll behavior.
741+
*
742+
* @usage
743+
* <hljs lang="html">
744+
* <md-virtual-repeat-container md-orient-horizontal>
745+
* <div md-virtual-repeat="i in items" md-on-demand>
746+
* Hello {{i}}!
747+
* </div>
748+
* </md-virtual-repeat-container>
749+
* </hljs>
750+
*
751+
*/
752+
function VirtualRepeatModelArrayLike(model) {
753+
if (!angular.isFunction(model.getItemAtIndex) ||
754+
!angular.isFunction(model.getLength)) {
755+
throw Error('When md-on-demand is enabled, the Object passed to md-virtual-repeat must implement ' +
756+
'functions getItemAtIndex() and getLength() ');
757+
}
758+
759+
this.model = model;
760+
}
761+
762+
763+
VirtualRepeatModelArrayLike.prototype.$$includeIndexes = function(start, end) {
764+
for (var i = start; i < end; i++) {
765+
if (!this.hasOwnProperty(i)) {
766+
this[i] = this.model.getItemAtIndex(i);
767+
}
768+
}
769+
this.length = this.model.getLength();
770+
};
771+
772+
773+
function abstractMethod() {
774+
throw Error('Non-overridden abstract method called.');
775+
}

0 commit comments

Comments
 (0)