]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts
Fix live/upload redirection
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / angular / infinite-scroller.directive.ts
CommitLineData
d142c7b9 1import { fromEvent, Observable, Subscription } from 'rxjs'
14aa8556 2import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators'
617f4f5c 3import { AfterViewChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
0cd4344f
C
4
5@Directive({
6 selector: '[myInfiniteScroller]'
7})
617f4f5c 8export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterViewChecked {
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
617f4f5c 27 ngAfterViewChecked () {
ad453580
C
28 if (this.checkScroll) {
29 this.checkScroll = false
30
617f4f5c
C
31 // Wait HTML update
32 setTimeout(() => {
33 if (this.hasScroll() === false) this.nearOfBottom.emit()
34 })
ad453580
C
35 }
36 }
37
0cd4344f 38 ngOnInit () {
aa55a4da 39 if (this.autoInit === true) return this.initialize()
0cd4344f
C
40 }
41
9af61e84
C
42 ngOnDestroy () {
43 if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
9af61e84
C
44 }
45
0cd4344f 46 initialize () {
ad453580
C
47 this.container = this.onItself
48 ? this.el.nativeElement
49 : document.documentElement
bce47964 50
6a6d92b1 51 // Emit the last value
2c6bbd97 52 const throttleOptions = { leading: true, trailing: true }
6a6d92b1 53
ad453580
C
54 const scrollableElement = this.onItself ? this.container : window
55 const scrollObservable = fromEvent(scrollableElement, 'scroll')
db400f44 56 .pipe(
14aa8556 57 startWith(true),
db400f44 58 throttleTime(200, undefined, throttleOptions),
bce47964 59 map(() => this.getScrollInfo()),
db400f44
C
60 distinctUntilChanged((o1, o2) => o1.current === o2.current),
61 share()
62 )
0cd4344f
C
63
64 // Scroll Down
9af61e84 65 this.scrollDownSub = scrollObservable
db400f44 66 .pipe(
ad453580
C
67 filter(({ current }) => this.isScrollingDown(current)),
68 filter(({ current, maximumScroll }) => (current / maximumScroll) > this.decimalLimit)
db400f44 69 )
0cd4344f 70 .subscribe(() => this.nearOfBottom.emit())
ad453580
C
71
72 if (this.dataObservable) {
73 this.dataObservable
74 .pipe(filter(d => d.length !== 0))
75 .subscribe(() => this.checkScroll = true)
76 }
6a6d92b1 77 }
bce47964
C
78
79 private getScrollInfo () {
ad453580
C
80 return { current: this.container.scrollTop, maximumScroll: this.getMaximumScroll() }
81 }
82
83 private getMaximumScroll () {
d142c7b9
C
84 const elementHeight = this.onItself ? this.container.clientHeight : window.innerHeight
85
86 return this.container.scrollHeight - elementHeight
ad453580
C
87 }
88
89 private hasScroll () {
90 return this.getMaximumScroll() > 0
91 }
92
93 private isScrollingDown (current: number) {
94 const result = this.lastCurrentBottom < current
bce47964 95
ad453580
C
96 this.lastCurrentBottom = current
97 return result
bce47964 98 }
0cd4344f 99}