Skip to content

Commit dd7cc5f

Browse files
committed
refactor to use CdkScrollable
1 parent e3acc1f commit dd7cc5f

File tree

3 files changed

+78
-83
lines changed

3 files changed

+78
-83
lines changed

src/cdk/scrolling/scrollable.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ export class CdkScrollable implements OnInit, OnDestroy {
4848
private _destroyed = new Subject();
4949

5050
private _elementScrolled: Observable<Event> = Observable.create(observer =>
51-
this._ngZone.runOutsideAngular(() =>
52-
fromEvent(this._elementRef.nativeElement, 'scroll').pipe(takeUntil(this._destroyed))
51+
this.ngZone.runOutsideAngular(() =>
52+
fromEvent(this.elementRef.nativeElement, 'scroll').pipe(takeUntil(this._destroyed))
5353
.subscribe(observer)));
5454

55-
constructor(private _elementRef: ElementRef<HTMLElement>,
56-
private _scroll: ScrollDispatcher,
57-
private _ngZone: NgZone,
58-
@Optional() private _dir?: Directionality) {}
55+
constructor(protected elementRef: ElementRef<HTMLElement>,
56+
protected scrollDispatcher: ScrollDispatcher,
57+
protected ngZone: NgZone,
58+
@Optional() protected dir?: Directionality) {}
5959

6060
ngOnInit() {
61-
this._scroll.register(this);
61+
this.scrollDispatcher.register(this);
6262
}
6363

6464
ngOnDestroy() {
65-
this._scroll.deregister(this);
65+
this.scrollDispatcher.deregister(this);
6666
this._destroyed.next();
6767
this._destroyed.complete();
6868
}
@@ -74,7 +74,7 @@ export class CdkScrollable implements OnInit, OnDestroy {
7474

7575
/** Gets the ElementRef for the viewport. */
7676
getElementRef(): ElementRef<HTMLElement> {
77-
return this._elementRef;
77+
return this.elementRef;
7878
}
7979

8080
/**
@@ -86,8 +86,8 @@ export class CdkScrollable implements OnInit, OnDestroy {
8686
* @param options specified the offsets to scroll to.
8787
*/
8888
scrollTo(options: ExtendedScrollToOptions): void {
89-
const el = this._elementRef.nativeElement;
90-
const isRtl = this._dir && this._dir.value == 'rtl';
89+
const el = this.elementRef.nativeElement;
90+
const isRtl = this.dir && this.dir.value == 'rtl';
9191

9292
// Rewrite start & end offsets as right or left offsets.
9393
options.left = options.left == null ? (isRtl ? options.end : options.start) : options.left;
@@ -119,7 +119,7 @@ export class CdkScrollable implements OnInit, OnDestroy {
119119
}
120120

121121
private _applyScrollToOptions(options: ScrollToOptions): void {
122-
const el = this._elementRef.nativeElement;
122+
const el = this.elementRef.nativeElement;
123123

124124
if (supportsScrollBehavior()) {
125125
el.scrollTo(options);
@@ -145,7 +145,7 @@ export class CdkScrollable implements OnInit, OnDestroy {
145145
measureScrollOffset(from: 'top' | 'left' | 'right' | 'bottom' | 'start' | 'end'): number {
146146
const LEFT = 'left';
147147
const RIGHT = 'right';
148-
const el = this._elementRef.nativeElement;
148+
const el = this.elementRef.nativeElement;
149149
if (from == 'top') {
150150
return el.scrollTop;
151151
}
@@ -154,7 +154,7 @@ export class CdkScrollable implements OnInit, OnDestroy {
154154
}
155155

156156
// Rewrite start & end as left or right offsets.
157-
const isRtl = this._dir && this._dir.value == 'rtl';
157+
const isRtl = this.dir && this.dir.value == 'rtl';
158158
if (from == 'start') {
159159
from = isRtl ? RIGHT : LEFT;
160160
} else if (from == 'end') {

src/cdk/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {ArrayDataSource} from '@angular/cdk/collections';
2-
import {CdkVirtualForOf, CdkVirtualScrollViewport, ScrollingModule} from '@angular/cdk/scrolling';
2+
import {
3+
CdkVirtualForOf,
4+
CdkVirtualScrollViewport,
5+
ScrollDispatcher,
6+
ScrollingModule
7+
} from '@angular/cdk/scrolling';
38
import {dispatchFakeEvent} from '@angular/cdk/testing';
49
import {
510
Component,
@@ -9,7 +14,7 @@ import {
914
ViewContainerRef,
1015
ViewEncapsulation
1116
} from '@angular/core';
12-
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
17+
import {ComponentFixture, fakeAsync, flush, inject, TestBed} from '@angular/core/testing';
1318
import {animationFrameScheduler, Subject} from 'rxjs';
1419

1520

@@ -157,7 +162,7 @@ describe('CdkVirtualScrollViewport', () => {
157162
fixture.detectChanges();
158163
flush();
159164

160-
expect(viewport.elementRef.nativeElement.scrollTop).toBe(testComponent.itemSize * 2);
165+
expect(viewport.measureScrollOffset()).toBe(testComponent.itemSize * 2);
161166
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6});
162167
}));
163168

@@ -169,7 +174,7 @@ describe('CdkVirtualScrollViewport', () => {
169174
fixture.detectChanges();
170175
flush();
171176

172-
expect(viewport.elementRef.nativeElement.scrollTop).toBe(testComponent.itemSize * 2);
177+
expect(viewport.measureScrollOffset()).toBe(testComponent.itemSize * 2);
173178
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6});
174179
}));
175180

@@ -182,7 +187,7 @@ describe('CdkVirtualScrollViewport', () => {
182187
fixture.detectChanges();
183188
flush();
184189

185-
expect(viewport.elementRef.nativeElement.scrollLeft).toBe(testComponent.itemSize * 2);
190+
expect(viewport.measureScrollOffset()).toBe(testComponent.itemSize * 2);
186191
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6});
187192
}));
188193

@@ -195,7 +200,7 @@ describe('CdkVirtualScrollViewport', () => {
195200
fixture.detectChanges();
196201
flush();
197202

198-
expect(viewport.elementRef.nativeElement.scrollLeft).toBe(testComponent.itemSize * 2);
203+
expect(viewport.measureScrollOffset()).toBe(testComponent.itemSize * 2);
199204
expect(viewport.getRenderedRange()).toEqual({start: 2, end: 6});
200205
}));
201206

@@ -577,6 +582,16 @@ describe('CdkVirtualScrollViewport', () => {
577582
testComponent.maxBufferPx = 99;
578583
expect(() => finishInit(fixture)).toThrow();
579584
}));
585+
586+
it('should register and degregister with ScrollDispatcher',
587+
fakeAsync(inject([ScrollDispatcher], (dispatcher: ScrollDispatcher) => {
588+
spyOn(dispatcher, 'register').and.callThrough();
589+
spyOn(dispatcher, 'deregister').and.callThrough();
590+
finishInit(fixture);
591+
expect(dispatcher.register).toHaveBeenCalledWith(testComponent.viewport);
592+
fixture.destroy();
593+
expect(dispatcher.deregister).toHaveBeenCalledWith(testComponent.viewport);
594+
})));
580595
});
581596

582597
describe('with RTL direction', () => {
@@ -605,8 +620,7 @@ describe('CdkVirtualScrollViewport', () => {
605620
testComponent.orientation = 'horizontal';
606621
finishInit(fixture);
607622

608-
expect(viewportEl.scrollLeft).toBe(
609-
testComponent.itemSize * testComponent.items.length - testComponent.viewportSize);
623+
expect(viewport.measureScrollOffset()).toBe(0);
610624
expect(contentWrapperEl.style.transform).toMatch(/translateX\(0(px)?\)/);
611625
expect((contentWrapperEl.children[0] as HTMLElement).innerText).toBe('0 - 0');
612626
}));
@@ -616,7 +630,7 @@ describe('CdkVirtualScrollViewport', () => {
616630
testComponent.orientation = 'horizontal';
617631
finishInit(fixture);
618632

619-
triggerScroll(viewport, 0);
633+
triggerScroll(viewport, 300);
620634
fixture.detectChanges();
621635
flush();
622636

@@ -634,8 +648,7 @@ describe('CdkVirtualScrollViewport', () => {
634648
fixture.detectChanges();
635649
flush();
636650

637-
expect(viewportEl.scrollLeft).toBe(testComponent.itemSize * testComponent.items.length -
638-
testComponent.viewportSize - 100);
651+
expect(viewport.measureScrollOffset('right')).toBe(100);
639652
}));
640653

641654
it('should scroll to the correct index in horizontal mode', fakeAsync(() => {
@@ -686,11 +699,7 @@ function finishInit(fixture: ComponentFixture<any>) {
686699
/** Trigger a scroll event on the viewport (optionally setting a new scroll offset). */
687700
function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
688701
if (offset !== undefined) {
689-
if (viewport.orientation == 'horizontal') {
690-
viewport.elementRef.nativeElement.scrollLeft = offset;
691-
} else {
692-
viewport.elementRef.nativeElement.scrollTop = offset;
693-
}
702+
viewport.scrollToOffset(offset);
694703
}
695704
dispatchFakeEvent(viewport.elementRef.nativeElement, 'scroll');
696705
animationFrameScheduler.flush();
@@ -751,9 +760,9 @@ class FixedSizeVirtualScroll {
751760
@Component({
752761
template: `
753762
<cdk-virtual-scroll-viewport dir="rtl"
754-
[itemSize]="itemSize" [bufferSize]="bufferSize" [orientation]="orientation"
755-
[style.height.px]="viewportHeight" [style.width.px]="viewportWidth"
756-
(scrolledIndexChange)="scrolledToIndex = $event">
763+
[itemSize]="itemSize" [minBufferPx]="minBufferPx" [maxBufferPx]="maxBufferPx"
764+
[orientation]="orientation" [style.height.px]="viewportHeight"
765+
[style.width.px]="viewportWidth" (scrolledIndexChange)="scrolledToIndex = $event">
757766
<div class="item"
758767
*cdkVirtualFor="let item of items; let i = index; trackBy: trackBy; \
759768
templateCacheSize: templateCacheSize"
@@ -782,7 +791,8 @@ class FixedSizeVirtualScrollWithRtlDirection {
782791
@Input() viewportSize = 200;
783792
@Input() viewportCrossSize = 100;
784793
@Input() itemSize = 50;
785-
@Input() bufferSize = 0;
794+
@Input() minBufferPx = 0;
795+
@Input() maxBufferPx = 0;
786796
@Input() items = Array(10).fill(0).map((_, i) => i);
787797
@Input() trackBy: TrackByFunction<number>;
788798
@Input() templateCacheSize = 20;

0 commit comments

Comments
 (0)