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