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