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