]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/infinite-scroller.directive.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / infinite-scroller.directive.ts
CommitLineData
14aa8556 1import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators'
ad453580
C
2import { AfterContentChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
3import { fromEvent, Observable, Subscription } from 'rxjs'
0cd4344f
C
4
5@Directive({
6 selector: '[myInfiniteScroller]'
7})
ad453580 8export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterContentChecked {
0cd4344f 9 @Input() percentLimit = 70
aa55a4da 10 @Input() autoInit = false
bce47964 11 @Input() onItself = false
ad453580 12 @Input() dataObservable: Observable<any[]>
0cd4344f
C
13
14 @Output() nearOfBottom = new EventEmitter<void>()
0cd4344f
C
15
16 private decimalLimit = 0
17 private lastCurrentBottom = -1
9af61e84 18 private scrollDownSub: Subscription
bce47964 19 private container: HTMLElement
0cd4344f 20
ad453580
C
21 private checkScroll = false
22
bce47964 23 constructor (private el: ElementRef) {
0cd4344f
C
24 this.decimalLimit = this.percentLimit / 100
25 }
26
ad453580
C
27 ngAfterContentChecked () {
28 if (this.checkScroll) {
29 this.checkScroll = false
30
31 console.log('Checking if the initial state has a scroll.')
32
33 if (this.hasScroll() === false) this.nearOfBottom.emit()
34 }
35 }
36
0cd4344f 37 ngOnInit () {
aa55a4da 38 if (this.autoInit === true) return this.initialize()
0cd4344f
C
39 }
40
9af61e84
C
41 ngOnDestroy () {
42 if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
9af61e84
C
43 }
44
0cd4344f 45 initialize () {
ad453580
C
46 this.container = this.onItself
47 ? this.el.nativeElement
48 : document.documentElement
bce47964 49
6a6d92b1 50 // Emit the last value
2c6bbd97 51 const throttleOptions = { leading: true, trailing: true }
6a6d92b1 52
ad453580
C
53 const scrollableElement = this.onItself ? this.container : window
54 const scrollObservable = fromEvent(scrollableElement, 'scroll')
db400f44 55 .pipe(
14aa8556 56 startWith(true),
db400f44 57 throttleTime(200, undefined, throttleOptions),
bce47964 58 map(() => this.getScrollInfo()),
db400f44
C
59 distinctUntilChanged((o1, o2) => o1.current === o2.current),
60 share()
61 )
0cd4344f
C
62
63 // Scroll Down
9af61e84 64 this.scrollDownSub = scrollObservable
db400f44 65 .pipe(
ad453580
C
66 filter(({ current }) => this.isScrollingDown(current)),
67 filter(({ current, maximumScroll }) => (current / maximumScroll) > this.decimalLimit)
db400f44 68 )
0cd4344f 69 .subscribe(() => this.nearOfBottom.emit())
ad453580
C
70
71 if (this.dataObservable) {
72 this.dataObservable
73 .pipe(filter(d => d.length !== 0))
74 .subscribe(() => this.checkScroll = true)
75 }
6a6d92b1 76 }
bce47964
C
77
78 private getScrollInfo () {
ad453580
C
79 return { current: this.container.scrollTop, maximumScroll: this.getMaximumScroll() }
80 }
81
82 private getMaximumScroll () {
83 return this.container.scrollHeight - window.innerHeight
84 }
85
86 private hasScroll () {
87 return this.getMaximumScroll() > 0
88 }
89
90 private isScrollingDown (current: number) {
91 const result = this.lastCurrentBottom < current
bce47964 92
ad453580
C
93 this.lastCurrentBottom = current
94 return result
bce47964 95 }
0cd4344f 96}