all files / src/ scroller.ts

100% Statements 24/24
100% Branches 0/0
100% Functions 9/9
100% Lines 20/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                   
import { Descriptor } from './descriptor';
import { ScrollDetector } from './scroll-detector';
import { IElementsManager } from './elements-manager';
 
export class Scroller {
    private get scope(): ng.IScope {
        return this.descriptor.Scope;
    }
 
    public constructor(private descriptor: Descriptor, private scrollDetector: ScrollDetector, private elementsManager: IElementsManager) {
        this.scrollDetector.OnScrollDown = this.onScrollDown;
        this.scrollDetector.OnScrollUp = this.onScrollUp;
    }
 
    public Subscribe() {
        this.scrollDetector.SubscribeToElement();
        this.scope.$watchCollection(this.descriptor.CollectionExpression, this.onCollectionUpdated);
    }
 
    private onCollectionUpdated = (newCollection: any[]): void => {
        this.elementsManager.UpdateCollection(newCollection);
    }
 
    private onScrollDown = (): void => {
        this.scope.$apply(() => {
            this.elementsManager.AddBottom();
            this.elementsManager.RemoveTop();
        });
    }
 
    private onScrollUp = (): void => {
        this.scope.$apply(() => {
            this.elementsManager.AddTop();
            this.elementsManager.RemoveBottom();
        });
    }
}