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