Add video channel view
[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, ElementRef, 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   @Input() onItself = false
15
16   @Output() nearOfBottom = new EventEmitter<void>()
17   @Output() nearOfTop = new EventEmitter<void>()
18   @Output() pageChanged = new EventEmitter<number>()
19
20   private decimalLimit = 0
21   private lastCurrentBottom = -1
22   private lastCurrentTop = 0
23   private scrollDownSub: Subscription
24   private scrollUpSub: Subscription
25   private pageChangeSub: Subscription
26   private middleScreen: number
27   private container: HTMLElement
28
29   constructor (private el: ElementRef) {
30     this.decimalLimit = this.percentLimit / 100
31   }
32
33   ngOnInit () {
34     if (this.autoInit === true) return this.initialize()
35   }
36
37   ngOnDestroy () {
38     if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
39     if (this.scrollUpSub) this.scrollUpSub.unsubscribe()
40     if (this.pageChangeSub) this.pageChangeSub.unsubscribe()
41   }
42
43   initialize () {
44     if (this.onItself) {
45       this.container = this.el.nativeElement
46     }
47
48     this.middleScreen = window.innerHeight / 2
49
50     // Emit the last value
51     const throttleOptions = { leading: true, trailing: true }
52
53     const scrollObservable = fromEvent(this.container || window, 'scroll')
54       .pipe(
55         startWith(null),
56         throttleTime(200, undefined, throttleOptions),
57         map(() => this.getScrollInfo()),
58         distinctUntilChanged((o1, o2) => o1.current === o2.current),
59         share()
60       )
61
62     // Scroll Down
63     this.scrollDownSub = scrollObservable
64       .pipe(
65         // Check we scroll down
66         filter(({ current }) => {
67           const res = this.lastCurrentBottom < current
68
69           this.lastCurrentBottom = current
70           return res
71         }),
72         filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
73       )
74       .subscribe(() => this.nearOfBottom.emit())
75
76     // Scroll up
77     this.scrollUpSub = scrollObservable
78       .pipe(
79         // Check we scroll up
80         filter(({ current }) => {
81           const res = this.lastCurrentTop > current
82
83           this.lastCurrentTop = current
84           return res
85         }),
86         filter(({ current, maximumScroll }) => {
87           return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
88         })
89       )
90       .subscribe(() => this.nearOfTop.emit())
91
92     // Page change
93     this.pageChangeSub = scrollObservable
94       .pipe(
95         distinct(),
96         map(({ current }) => this.calculateCurrentPage(current)),
97         distinctUntilChanged()
98       )
99       .subscribe(res => this.pageChanged.emit(res))
100   }
101
102   private calculateCurrentPage (current: number) {
103     const scrollY = current + this.middleScreen
104
105     const page = Math.max(1, Math.ceil(scrollY / this.pageHeight))
106
107     // Offset page
108     return page + (this.firstLoadedPage - 1)
109   }
110
111   private getScrollInfo () {
112     if (this.container) {
113       return { current: this.container.scrollTop, maximumScroll: this.container.scrollHeight }
114     }
115
116     return { current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight }
117   }
118 }