All files / src/app/directives scrollable.directive.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 441x               1x     25x           25x             1x 2x 2x 2x 2x   2x 1x     2x 1x                
import { Directive, ElementRef, EventEmitter, HostListener, Output } from '@angular/core';
 
/**
 * Scrollable Directive
 */
@Directive({
    selector: '[appScrollable]'
})
export class ScrollableDirective {
 
    /** scroll position event emitter */
    @Output() readonly scrollPosition = new EventEmitter();
 
    /**
     * constructor of ScrollableDirective
     * @param el: ElementRef
     */
    constructor(public el: ElementRef) {
    }
 
    /**
     * onScroll event listener
     * @param event: scroll event
     */
    @HostListener('scroll', ['$event']) onScroll(event): void {
        try {
            const top = event.target.scrollTop;
            const height = this.el.nativeElement.scrollHeight;
            const offset = this.el.nativeElement.offsetHeight;
 
            if (top > height - offset - 1) {
                this.scrollPosition.emit('bottom');
            }
 
            if (top <= 0) {
                this.scrollPosition.emit('top');
            }
 
        } catch (err) {
            // console.error('Error within ScrollableDirective : ', err);
        }
    }
}