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