]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { distinct, distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators' |
bce47964 | 2 | import { Directive, ElementRef, 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 | @Input() containerHeight: number |
10 | @Input() pageHeight: number | |
a8ecc6f6 | 11 | @Input() firstLoadedPage = 1 |
0cd4344f | 12 | @Input() percentLimit = 70 |
aa55a4da | 13 | @Input() autoInit = false |
bce47964 | 14 | @Input() onItself = false |
0cd4344f C |
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 | |
a8ecc6f6 | 26 | private middleScreen: number |
bce47964 | 27 | private container: HTMLElement |
0cd4344f | 28 | |
bce47964 | 29 | constructor (private el: ElementRef) { |
0cd4344f C |
30 | this.decimalLimit = this.percentLimit / 100 |
31 | } | |
32 | ||
33 | ngOnInit () { | |
aa55a4da | 34 | if (this.autoInit === true) return this.initialize() |
0cd4344f C |
35 | } |
36 | ||
9af61e84 C |
37 | ngOnDestroy () { |
38 | if (this.scrollDownSub) this.scrollDownSub.unsubscribe() | |
39 | if (this.scrollUpSub) this.scrollUpSub.unsubscribe() | |
40 | if (this.pageChangeSub) this.pageChangeSub.unsubscribe() | |
41 | } | |
42 | ||
0cd4344f | 43 | initialize () { |
bce47964 C |
44 | if (this.onItself) { |
45 | this.container = this.el.nativeElement | |
46 | } | |
47 | ||
a8ecc6f6 C |
48 | this.middleScreen = window.innerHeight / 2 |
49 | ||
6a6d92b1 | 50 | // Emit the last value |
2c6bbd97 | 51 | const throttleOptions = { leading: true, trailing: true } |
6a6d92b1 | 52 | |
bce47964 | 53 | const scrollObservable = fromEvent(this.container || window, 'scroll') |
db400f44 C |
54 | .pipe( |
55 | startWith(null), | |
56 | throttleTime(200, undefined, throttleOptions), | |
bce47964 | 57 | map(() => this.getScrollInfo()), |
db400f44 C |
58 | distinctUntilChanged((o1, o2) => o1.current === o2.current), |
59 | share() | |
60 | ) | |
0cd4344f C |
61 | |
62 | // Scroll Down | |
9af61e84 | 63 | this.scrollDownSub = scrollObservable |
db400f44 C |
64 | .pipe( |
65 | // Check we scroll down | |
66 | filter(({ current }) => { | |
67 | const res = this.lastCurrentBottom < current | |
0cd4344f | 68 | |
db400f44 C |
69 | this.lastCurrentBottom = current |
70 | return res | |
71 | }), | |
72 | filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit) | |
73 | ) | |
0cd4344f C |
74 | .subscribe(() => this.nearOfBottom.emit()) |
75 | ||
76 | // Scroll up | |
9af61e84 | 77 | this.scrollUpSub = scrollObservable |
db400f44 C |
78 | .pipe( |
79 | // Check we scroll up | |
80 | filter(({ current }) => { | |
81 | const res = this.lastCurrentTop > current | |
0cd4344f | 82 | |
db400f44 C |
83 | this.lastCurrentTop = current |
84 | return res | |
85 | }), | |
86 | filter(({ current, maximumScroll }) => { | |
87 | return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit | |
88 | }) | |
89 | ) | |
0cd4344f C |
90 | .subscribe(() => this.nearOfTop.emit()) |
91 | ||
92 | // Page change | |
9af61e84 | 93 | this.pageChangeSub = scrollObservable |
db400f44 C |
94 | .pipe( |
95 | distinct(), | |
96 | map(({ current }) => this.calculateCurrentPage(current)), | |
97 | distinctUntilChanged() | |
98 | ) | |
0cd4344f C |
99 | .subscribe(res => this.pageChanged.emit(res)) |
100 | } | |
101 | ||
6a6d92b1 | 102 | private calculateCurrentPage (current: number) { |
a8ecc6f6 C |
103 | const scrollY = current + this.middleScreen |
104 | ||
105 | const page = Math.max(1, Math.ceil(scrollY / this.pageHeight)) | |
106 | ||
107 | // Offset page | |
108 | return page + (this.firstLoadedPage - 1) | |
6a6d92b1 | 109 | } |
bce47964 C |
110 | |
111 | private getScrollInfo () { | |
112 | if (this.container) { | |
113 | return { current: this.container.scrollTop, maximumScroll: this.container.scrollHeight } | |
114 | } | |
115 | ||
116 | return { current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight } | |
117 | } | |
0cd4344f | 118 | } |