]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { distinct, distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators' |
9af61e84 | 2 | import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core' |
db400f44 | 3 | import { fromEvent, Subscription } from 'rxjs' |
0cd4344f C |
4 | |
5 | @Directive({ | |
6 | selector: '[myInfiniteScroller]' | |
7 | }) | |
9af61e84 | 8 | export class InfiniteScrollerDirective implements OnInit, OnDestroy { |
0cd4344f C |
9 | private static PAGE_VIEW_TOP_MARGIN = 500 |
10 | ||
11 | @Input() containerHeight: number | |
12 | @Input() pageHeight: number | |
13 | @Input() percentLimit = 70 | |
14 | @Input() autoLoading = false | |
15 | ||
16 | @Output() nearOfBottom = new EventEmitter<void>() | |
17 | @Output() nearOfTop = new EventEmitter<void>() | |
18 | @Output() pageChanged = new EventEmitter<number>() | |
19 | ||
20 | private decimalLimit = 0 | |
21 | private lastCurrentBottom = -1 | |
22 | private lastCurrentTop = 0 | |
9af61e84 C |
23 | private scrollDownSub: Subscription |
24 | private scrollUpSub: Subscription | |
25 | private pageChangeSub: Subscription | |
0cd4344f C |
26 | |
27 | constructor () { | |
28 | this.decimalLimit = this.percentLimit / 100 | |
29 | } | |
30 | ||
31 | ngOnInit () { | |
32 | if (this.autoLoading === true) return this.initialize() | |
33 | } | |
34 | ||
9af61e84 C |
35 | ngOnDestroy () { |
36 | if (this.scrollDownSub) this.scrollDownSub.unsubscribe() | |
37 | if (this.scrollUpSub) this.scrollUpSub.unsubscribe() | |
38 | if (this.pageChangeSub) this.pageChangeSub.unsubscribe() | |
39 | } | |
40 | ||
0cd4344f | 41 | initialize () { |
6a6d92b1 | 42 | // Emit the last value |
2c6bbd97 | 43 | const throttleOptions = { leading: true, trailing: true } |
6a6d92b1 | 44 | |
0cd4344f | 45 | const scrollObservable = fromEvent(window, 'scroll') |
db400f44 C |
46 | .pipe( |
47 | startWith(null), | |
48 | throttleTime(200, undefined, throttleOptions), | |
49 | map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight })), | |
50 | distinctUntilChanged((o1, o2) => o1.current === o2.current), | |
51 | share() | |
52 | ) | |
0cd4344f C |
53 | |
54 | // Scroll Down | |
9af61e84 | 55 | this.scrollDownSub = scrollObservable |
db400f44 C |
56 | .pipe( |
57 | // Check we scroll down | |
58 | filter(({ current }) => { | |
59 | const res = this.lastCurrentBottom < current | |
0cd4344f | 60 | |
db400f44 C |
61 | this.lastCurrentBottom = current |
62 | return res | |
63 | }), | |
64 | filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit) | |
65 | ) | |
0cd4344f C |
66 | .subscribe(() => this.nearOfBottom.emit()) |
67 | ||
68 | // Scroll up | |
9af61e84 | 69 | this.scrollUpSub = scrollObservable |
db400f44 C |
70 | .pipe( |
71 | // Check we scroll up | |
72 | filter(({ current }) => { | |
73 | const res = this.lastCurrentTop > current | |
0cd4344f | 74 | |
db400f44 C |
75 | this.lastCurrentTop = current |
76 | return res | |
77 | }), | |
78 | filter(({ current, maximumScroll }) => { | |
79 | return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit | |
80 | }) | |
81 | ) | |
0cd4344f C |
82 | .subscribe(() => this.nearOfTop.emit()) |
83 | ||
84 | // Page change | |
9af61e84 | 85 | this.pageChangeSub = scrollObservable |
db400f44 C |
86 | .pipe( |
87 | distinct(), | |
88 | map(({ current }) => this.calculateCurrentPage(current)), | |
89 | distinctUntilChanged() | |
90 | ) | |
0cd4344f C |
91 | .subscribe(res => this.pageChanged.emit(res)) |
92 | } | |
93 | ||
6a6d92b1 C |
94 | private calculateCurrentPage (current: number) { |
95 | return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight)) | |
96 | } | |
0cd4344f | 97 | } |