Skip to content

Commit f59658d

Browse files
authored
virtual-scroll: merge into master under cdk-experimental (#11354)
* feat(virtual-scroll): fixed size virtual scroll (#9316) * feat(virtual-scroll): fixed size virtual scroll * address some comments * change VirtualScrollStrategy interface a bit * address some more comments * fix lint & build * chore: move virtual-scroll to cdk-experimental (#9974) * virtual-scroll: simplify scroll listener logic (#10102) * virtual-scroll: only move views that need to be moved (#10099) * virtual-scroll: only move views that need to be moved * address comments * virtual-scroll: switch `throttleTime` to `sampleTime` (#10179) * virtual-scroll: switch throttleTime to sampleTime * add comment * virtual-scroll: allow user to pass `Observable<T[]>` (#10158) * virtual-scroll: rename `Range` to `ListRange` to avoid confusion with native `Range` (#10220) * virtual-scroll: add autosize scroll strategy (#10219) * rename fixed size virtual scroll directive * add autosize virtual scroll strategy * add item size estimator class * add logic for jumping rendered content based on scroll position * address comments * virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy` (#10290) * virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy` * address comemnts * virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy` (#10504) * virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy`. This still has a couple issues that need to be ironed out and it doesn't have the code for correcting the error between the predicted and actual scroll position. (See various TODOs for additional things that need work). * fix lint * address comments * address comments * fix bazel * fix devapp * fix lint * cleanup * virtual-scroll: rewrite offset in terms of "to-top" and fix a bug where items were removed too soon (#10986) * rewrite offsets to the end of the rendered content as offsets to the start * add some more autosize demos for testing * make sure not to remove too many items * address comments * virtual-scroll: address amcdnl's feedback (#10988) * rewrite offsets to the end of the rendered content as offsets to the start * add some more autosize demos for testing * make sure not to remove too many items * virtual-scroll: address amcdnl's feedback * virtual-scroll: fix updating when data changes and add trackBy demos (#11085) * virtual-scroll: add logic to correct the scroll position as user move… (#11137) * virtual-scroll: add logic to correct the scroll position as user moves toward the top * address comments * fix(scrolling): adds right to fix pushed content (#11192) * fix(scrolling): adds right to fix pushed content * chore: address feedback * chore: address pr feedback * virtual-scroll: add tests for fixed size items (#11255) * virtual-scroll: add tests for fixed size items * address comments * fix bug in fixed size strategy * fix bazel build * virtual-scroll: add tests for `cdkVirtualFor` logic (#11275) * merge fixed size test components into one * add tests for cdkVirtualFor logic * allow undefined to be explicitly passed as trackBy * fix bazel build * address comments * add some basic tests (#11295) * fix lint * virtual-scroll: add e2e tests for autosize scroll strategy (#11345) * set up virtual scroll page in e2e app * add gulp task for e2e:watch * add e2e tests for autosize * address comments * address comments * fix lint and tests * fix bad rebase * fix(scrolling): scrollbar jump when data change on scroll (#11193) * fix(scrolling): data change not updating size * chore: remove todo * chore: more performant fix for jump
1 parent 72f8d9d commit f59658d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2695
-70
lines changed

.github/CODEOWNERS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
# CDK experimental package
8787
/src/cdk-experimental/** @jelbourn
8888
/src/cdk-experimental/dialog/** @jelbourn @josephperrott @crisbeto
89+
/src/cdk-experimental/scrolling/** @mmalerba
8990

9091
# Docs examples & guides
9192
/guides/** @amcdnl @jelbourn
@@ -141,6 +142,7 @@
141142
/src/demo-app/tooltip/** @andrewseguin
142143
/src/demo-app/tree/** @tinayuangao
143144
/src/demo-app/typography/** @crisbeto
145+
/src/demo-app/virtual-scroll/** @mmalerba
144146

145147
# E2E app
146148
/e2e/* @jelbourn
@@ -165,6 +167,7 @@
165167
/e2e/components/stepper-e2e.spec.ts @mmalerba
166168
/e2e/components/tabs-e2e.spec.ts @andrewseguin
167169
/e2e/components/toolbar-e2e.spec.ts @devversion
170+
/e2e/components/virtual-scroll-e2e.spec.ts @mmalerba
168171
/e2e/util/** @jelbourn
169172
/src/e2e-app/* @jelbourn
170173
/src/e2e-app/block-scroll-strategy/** @andrewseguin @crisbeto
@@ -183,6 +186,7 @@
183186
/src/e2e-app/sidenav/** @mmalerba
184187
/src/e2e-app/slide-toggle/** @devversion
185188
/src/e2e-app/tabs/** @andrewseguin
189+
/src/e2e-app/virtual-scroll/** @mmalerba
186190

187191
# Universal app
188192
/src/universal-app/** @jelbourn
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import {browser, by, element, ElementFinder} from 'protractor';
2+
import {ILocation, ISize} from 'selenium-webdriver';
3+
4+
declare var window: any;
5+
6+
7+
describe('autosize cdk-virtual-scroll', () => {
8+
let viewport: ElementFinder;
9+
10+
describe('with uniform items', () => {
11+
beforeEach(() => {
12+
browser.get('/virtual-scroll');
13+
viewport = element(by.css('.demo-virtual-scroll-uniform-size cdk-virtual-scroll-viewport'));
14+
});
15+
16+
it('should scroll down slowly', async () => {
17+
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000);
18+
const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]'));
19+
const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]'));
20+
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
21+
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
22+
});
23+
24+
it('should jump scroll position down and slowly scroll back up', async () => {
25+
// The estimate of the total content size is exactly correct, so we wind up scrolled to the
26+
// same place as if we slowly scrolled down.
27+
await browser.executeAsyncScript(scrollViewportTo, viewport, 2000);
28+
const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]'));
29+
const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]'));
30+
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
31+
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
32+
33+
// As we slowly scroll back up we should wind up back at the start of the content.
34+
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0);
35+
const first = element(by.css('.demo-virtual-scroll-uniform-size [data-index="0"]'));
36+
expect(await isVisibleInViewport(first, viewport)).toBe(true);
37+
});
38+
});
39+
40+
describe('with variable size', () => {
41+
beforeEach(() => {
42+
browser.get('/virtual-scroll');
43+
viewport = element(by.css('.demo-virtual-scroll-variable-size cdk-virtual-scroll-viewport'));
44+
});
45+
46+
it('should scroll down slowly', async () => {
47+
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000);
48+
const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]'));
49+
const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="20"]'));
50+
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
51+
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
52+
});
53+
54+
it('should jump scroll position down and slowly scroll back up', async () => {
55+
// The estimate of the total content size is slightly different than the actual, so we don't
56+
// wind up in the same spot as if we scrolled slowly down.
57+
await browser.executeAsyncScript(scrollViewportTo, viewport, 2000);
58+
const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="18"]'));
59+
const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]'));
60+
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
61+
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
62+
63+
// As we slowly scroll back up we should wind up back at the start of the content. As we
64+
// scroll the error from when we jumped the scroll position should be slowly corrected.
65+
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0);
66+
const first = element(by.css('.demo-virtual-scroll-variable-size [data-index="0"]'));
67+
expect(await isVisibleInViewport(first, viewport)).toBe(true);
68+
});
69+
});
70+
});
71+
72+
73+
/** Checks if the given element is visible in the given viewport. */
74+
async function isVisibleInViewport(el: ElementFinder, viewport: ElementFinder): Promise<boolean> {
75+
if (!await el.isPresent() || !await el.isDisplayed() || !await viewport.isPresent() ||
76+
!await viewport.isDisplayed()) {
77+
return false;
78+
}
79+
const viewportRect = getRect(await viewport.getLocation(), await viewport.getSize());
80+
const elRect = getRect(await el.getLocation(), await el.getSize());
81+
return elRect.left < viewportRect.right && elRect.right > viewportRect.left &&
82+
elRect.top < viewportRect.bottom && elRect.bottom > viewportRect.top;
83+
}
84+
85+
86+
/** Gets the rect for an element given its location ans size. */
87+
function getRect(location: ILocation, size: ISize):
88+
{top: number, left: number, bottom: number, right: number} {
89+
return {
90+
top: location.y,
91+
left: location.x,
92+
bottom: location.y + size.height,
93+
right: location.x + size.width
94+
};
95+
}
96+
97+
98+
/** Immediately scrolls the viewport to the given offset. */
99+
function scrollViewportTo(viewportEl: any, offset: number, done: () => void) {
100+
viewportEl.scrollTop = offset;
101+
window.requestAnimationFrame(() => done());
102+
}
103+
104+
105+
/** Smoothly scrolls the viewport to the given offset, 25px at a time. */
106+
function smoothScrollViewportTo(viewportEl: any, offset: number, done: () => void) {
107+
let promise = Promise.resolve();
108+
let curOffset = viewportEl.scrollTop;
109+
do {
110+
const co = curOffset += Math.min(25, Math.max(-25, offset - curOffset));
111+
promise = promise.then(() => new Promise<void>(resolve => {
112+
viewportEl.scrollTop = co;
113+
window.requestAnimationFrame(() => resolve());
114+
}));
115+
} while (curOffset != offset);
116+
promise.then(() => done());
117+
}

packages.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ CDK_PACKAGES = [
2121

2222
CDK_TARGETS = ["//src/cdk"] + ["//src/cdk/%s" % p for p in CDK_PACKAGES]
2323

24+
CDK_EXPERIMENTAL_PACKAGES = [
25+
# "dialog", # Disabled because BUILD.bazel doesn't exist yet
26+
"scrolling",
27+
]
28+
29+
CDK_EXPERIMENTAL_TARGETS = ["//src/cdk-experimental"] + [
30+
"//src/cdk-experimental/%s" % p for p in CDK_EXPERIMENTAL_PACKAGES
31+
]
32+
2433
MATERIAL_PACKAGES = [
2534
"autocomplete",
2635
"badge",
@@ -84,6 +93,12 @@ ROLLUP_GLOBALS.update({
8493
"@angular/cdk/%s" % p: "ng.cdk.%s" % p for p in CDK_PACKAGES
8594
})
8695

96+
# Rollup globals for cdk subpackages in the form of, e.g.,
97+
# {"@angular/cdk-experimental/scrolling": "ng.cdkExperimental.scrolling"}
98+
ROLLUP_GLOBALS.update({
99+
"@angular/cdk-experimental/%s" % p: "ng.cdkExperimental.%s" % p for p in CDK_EXPERIMENTAL_PACKAGES
100+
})
101+
87102
# Rollup globals for material subpackages, e.g., {"@angular/material/list": "ng.material.list"}
88103
ROLLUP_GLOBALS.update({
89104
"@angular/material/%s" % p: "ng.material.%s" % p for p in MATERIAL_PACKAGES

src/cdk-experimental/BUILD.bazel

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package(default_visibility=["//visibility:public"])
22
load("@angular//:index.bzl", "ng_module", "ng_package")
3-
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
3+
load("//:packages.bzl", "CDK_EXPERIMENTAL_PACKAGES", "CDK_EXPERIMENTAL_TARGETS", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
44

5+
# Export the CDK tsconfig so that subpackages can reference it directly.
6+
exports_files(["tsconfig-build.json"])
57

68
ng_module(
79
name = "cdk-experimental",
8-
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts"]),
10+
srcs = glob(["*.ts"], exclude=["**/*.spec.ts"]),
911
module_name = "@angular/cdk-experimental",
10-
deps = CDK_TARGETS,
11-
assets = glob(["**/*.css", "**/*.html"]),
12+
deps = ["//src/cdk-experimental/%s" % p for p in CDK_EXPERIMENTAL_PACKAGES],
1213
tsconfig = "//src/lib:tsconfig-build.json",
1314
)
1415

@@ -18,6 +19,6 @@ ng_package(
1819
entry_point = "src/cdk-experimental/public_api.js",
1920
globals = ROLLUP_GLOBALS,
2021
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
21-
deps = [":cdk-experimental"],
22+
deps = CDK_EXPERIMENTAL_TARGETS,
2223
tags = ["publish"],
2324
)

src/cdk-experimental/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"homepage": "https://github.com/angular/material2#readme",
1818
"peerDependencies": {
19-
"@angular/material": "0.0.0-PLACEHOLDER",
19+
"@angular/cdk": "0.0.0-PLACEHOLDER",
2020
"@angular/core": "0.0.0-NG"
2121
},
2222
"dependencies": {

src/cdk-experimental/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*/
88

99
export * from './version';
10+
export * from '@angular/cdk-experimental/scrolling';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package(default_visibility=["//visibility:public"])
2+
load("@angular//:index.bzl", "ng_module")
3+
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test")
4+
load("@io_bazel_rules_sass//sass:sass.bzl", "sass_binary")
5+
6+
7+
ng_module(
8+
name = "scrolling",
9+
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts"]),
10+
module_name = "@angular/cdk-experimental/scrolling",
11+
assets = [":virtual_scroll_viewport_css"] + glob(["**/*.html"]),
12+
deps = [
13+
"//src/cdk/coercion",
14+
"//src/cdk/collections",
15+
"@rxjs",
16+
],
17+
tsconfig = "//src/cdk-experimental:tsconfig-build.json",
18+
)
19+
20+
sass_binary(
21+
name = "virtual_scroll_viewport_scss",
22+
src = "virtual-scroll-viewport.scss",
23+
)
24+
25+
# TODO(jelbourn): remove this when sass_binary supports specifying an output filename and dir.
26+
# Copy the output of the sass_binary such that the filename and path match what we expect.
27+
genrule(
28+
name = "virtual_scroll_viewport_css",
29+
srcs = [":virtual_scroll_viewport_scss.css"],
30+
outs = ["virtual-scroll-viewport.css"],
31+
cmd = "cp $< $@",
32+
)
33+
34+
ts_library(
35+
name = "scrolling_test_sources",
36+
testonly = 1,
37+
srcs = glob(["**/*.spec.ts"]),
38+
deps = [
39+
":scrolling",
40+
"//src/cdk/collections",
41+
"//src/cdk/testing",
42+
"@rxjs",
43+
],
44+
tsconfig = "//src/cdk-experimental:tsconfig-build.json",
45+
)
46+
47+
ts_web_test(
48+
name = "unit_tests",
49+
bootstrap = [
50+
"//:web_test_bootstrap_scripts",
51+
],
52+
tags = ["manual"],
53+
54+
# Do not sort
55+
deps = [
56+
"//:tslib_bundle",
57+
"//:angular_bundles",
58+
"//:angular_test_bundles",
59+
"//test:angular_test_init",
60+
":scrolling_test_sources",
61+
],
62+
)

0 commit comments

Comments
 (0)