]>
Commit | Line | Data |
---|---|---|
d142c7b9 | 1 | import { fromEvent, Observable, Subscription } from 'rxjs' |
14aa8556 | 2 | import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators' |
617f4f5c | 3 | import { AfterViewChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core' |
dd24f1bb | 4 | import { PeerTubeRouterService, RouterSetting } from '@app/core' |
0cd4344f C |
5 | |
6 | @Directive({ | |
7 | selector: '[myInfiniteScroller]' | |
8 | }) | |
617f4f5c | 9 | export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterViewChecked { |
0cd4344f | 10 | @Input() percentLimit = 70 |
bce47964 | 11 | @Input() onItself = false |
ad453580 | 12 | @Input() dataObservable: Observable<any[]> |
0cd4344f | 13 | |
dd24f1bb C |
14 | // Add angular state in query params to reuse the routed component |
15 | @Input() setAngularState: boolean | |
16 | ||
0cd4344f | 17 | @Output() nearOfBottom = new EventEmitter<void>() |
0cd4344f C |
18 | |
19 | private decimalLimit = 0 | |
20 | private lastCurrentBottom = -1 | |
9af61e84 | 21 | private scrollDownSub: Subscription |
bce47964 | 22 | private container: HTMLElement |
0cd4344f | 23 | |
ad453580 C |
24 | private checkScroll = false |
25 | ||
dd24f1bb C |
26 | constructor ( |
27 | private peertubeRouter: PeerTubeRouterService, | |
28 | private el: ElementRef | |
29 | ) { | |
0cd4344f C |
30 | this.decimalLimit = this.percentLimit / 100 |
31 | } | |
32 | ||
617f4f5c | 33 | ngAfterViewChecked () { |
ad453580 C |
34 | if (this.checkScroll) { |
35 | this.checkScroll = false | |
36 | ||
617f4f5c C |
37 | // Wait HTML update |
38 | setTimeout(() => { | |
39 | if (this.hasScroll() === false) this.nearOfBottom.emit() | |
40 | }) | |
ad453580 C |
41 | } |
42 | } | |
43 | ||
0cd4344f | 44 | ngOnInit () { |
dd24f1bb | 45 | this.initialize() |
0cd4344f C |
46 | } |
47 | ||
9af61e84 C |
48 | ngOnDestroy () { |
49 | if (this.scrollDownSub) this.scrollDownSub.unsubscribe() | |
9af61e84 C |
50 | } |
51 | ||
0cd4344f | 52 | initialize () { |
ad453580 C |
53 | this.container = this.onItself |
54 | ? this.el.nativeElement | |
55 | : document.documentElement | |
bce47964 | 56 | |
6a6d92b1 | 57 | // Emit the last value |
2c6bbd97 | 58 | const throttleOptions = { leading: true, trailing: true } |
6a6d92b1 | 59 | |
ad453580 C |
60 | const scrollableElement = this.onItself ? this.container : window |
61 | const scrollObservable = fromEvent(scrollableElement, 'scroll') | |
db400f44 | 62 | .pipe( |
14aa8556 | 63 | startWith(true), |
db400f44 | 64 | throttleTime(200, undefined, throttleOptions), |
bce47964 | 65 | map(() => this.getScrollInfo()), |
db400f44 C |
66 | distinctUntilChanged((o1, o2) => o1.current === o2.current), |
67 | share() | |
68 | ) | |
0cd4344f C |
69 | |
70 | // Scroll Down | |
9af61e84 | 71 | this.scrollDownSub = scrollObservable |
db400f44 | 72 | .pipe( |
ad453580 C |
73 | filter(({ current }) => this.isScrollingDown(current)), |
74 | filter(({ current, maximumScroll }) => (current / maximumScroll) > this.decimalLimit) | |
db400f44 | 75 | ) |
dd24f1bb C |
76 | .subscribe(() => { |
77 | if (this.setAngularState) this.setScrollRouteParams() | |
78 | ||
79 | this.nearOfBottom.emit() | |
80 | }) | |
ad453580 C |
81 | |
82 | if (this.dataObservable) { | |
83 | this.dataObservable | |
84 | .pipe(filter(d => d.length !== 0)) | |
85 | .subscribe(() => this.checkScroll = true) | |
86 | } | |
6a6d92b1 | 87 | } |
bce47964 C |
88 | |
89 | private getScrollInfo () { | |
ad453580 C |
90 | return { current: this.container.scrollTop, maximumScroll: this.getMaximumScroll() } |
91 | } | |
92 | ||
93 | private getMaximumScroll () { | |
d142c7b9 C |
94 | const elementHeight = this.onItself ? this.container.clientHeight : window.innerHeight |
95 | ||
96 | return this.container.scrollHeight - elementHeight | |
ad453580 C |
97 | } |
98 | ||
99 | private hasScroll () { | |
100 | return this.getMaximumScroll() > 0 | |
101 | } | |
102 | ||
103 | private isScrollingDown (current: number) { | |
104 | const result = this.lastCurrentBottom < current | |
bce47964 | 105 | |
ad453580 C |
106 | this.lastCurrentBottom = current |
107 | return result | |
bce47964 | 108 | } |
dd24f1bb C |
109 | |
110 | private setScrollRouteParams () { | |
111 | this.peertubeRouter.addRouteSetting(RouterSetting.REUSE_COMPONENT) | |
112 | } | |
0cd4344f | 113 | } |