Try to improve infinite pagination
[oweals/peertube.git] / client / src / app / shared / video / infinite-scroller.directive.ts
1 import { distinct, distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators'
2 import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
3 import { fromEvent, Subscription } from 'rxjs'
4
5 @Directive({
6   selector: '[myInfiniteScroller]'
7 })
8 export class InfiniteScrollerDirective implements OnInit, OnDestroy {
9   @Input() containerHeight: number
10   @Input() pageHeight: number
11   @Input() firstLoadedPage = 1
12   @Input() percentLimit = 70
13   @Input() autoInit = false
14
15   @Output() nearOfBottom = new EventEmitter<void>()
16   @Output() nearOfTop = new EventEmitter<void>()
17   @Output() pageChanged = new EventEmitter<number>()
18
19   private decimalLimit = 0
20   private lastCurrentBottom = -1
21   private lastCurrentTop = 0
22   private scrollDownSub: Subscription
23   private scrollUpSub: Subscription
24   private pageChangeSub: Subscription
25   private middleScreen: number
26
27   constructor () {
28     this.decimalLimit = this.percentLimit / 100
29   }
30
31   ngOnInit () {
32     if (this.autoInit === true) return this.initialize()
33   }
34
35   ngOnDestroy () {
36     if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
37     if (this.scrollUpSub) this.scrollUpSub.unsubscribe()
38     if (this.pageChangeSub) this.pageChangeSub.unsubscribe()
39   }
40
41   initialize () {
42     this.middleScreen = window.innerHeight / 2
43
44     // Emit the last value
45     const throttleOptions = { leading: true, trailing: true }
46
47     const scrollObservable = fromEvent(window, 'scroll')
48       .pipe(
49         startWith(null),
50         throttleTime(200, undefined, throttleOptions),
51         map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight })),
52         distinctUntilChanged((o1, o2) => o1.current === o2.current),
53         share()
54       )
55
56     // Scroll Down
57     this.scrollDownSub = scrollObservable
58       .pipe(
59         // Check we scroll down
60         filter(({ current }) => {
61           const res = this.lastCurrentBottom < current
62
63           this.lastCurrentBottom = current
64           return res
65         }),
66         filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
67       )
68       .subscribe(() => this.nearOfBottom.emit())
69
70     // Scroll up
71     this.scrollUpSub = scrollObservable
72       .pipe(
73         // Check we scroll up
74         filter(({ current }) => {
75           const res = this.lastCurrentTop > current
76
77           this.lastCurrentTop = current
78           return res
79         }),
80         filter(({ current, maximumScroll }) => {
81           return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
82         })
83       )
84       .subscribe(() => this.nearOfTop.emit())
85
86     // Page change
87     this.pageChangeSub = scrollObservable
88       .pipe(
89         distinct(),
90         map(({ current }) => this.calculateCurrentPage(current)),
91         distinctUntilChanged()
92       )
93       .subscribe(res => this.pageChanged.emit(res))
94   }
95
96   private calculateCurrentPage (current: number) {
97     const scrollY = current + this.middleScreen
98
99     const page = Math.max(1, Math.ceil(scrollY / this.pageHeight))
100
101     // Offset page
102     return page + (this.firstLoadedPage - 1)
103   }
104 }