ed49a9e81e63dfa89a6fd2f2fd89af76f7ca6c37
[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: false, 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       .share()
48
49     // Scroll Down
50     scrollObservable
51       // Check we scroll down
52       .filter(({ current }) => {
53         const res = this.lastCurrentBottom < current
54
55         this.lastCurrentBottom = current
56         return res
57       })
58       .filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
59       .subscribe(() => this.nearOfBottom.emit())
60
61     // Scroll up
62     scrollObservable
63       // Check we scroll up
64       .filter(({ current }) => {
65         const res = this.lastCurrentTop > current
66
67         this.lastCurrentTop = current
68         return res
69       })
70       .filter(({ current, maximumScroll }) => {
71         return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
72       })
73       .subscribe(() => this.nearOfTop.emit())
74
75     // Page change
76     scrollObservable
77       .distinct()
78       .map(({ current }) => this.calculateCurrentPage(current))
79       .distinctUntilChanged()
80       .subscribe(res => this.pageChanged.emit(res))
81   }
82
83   private calculateCurrentPage (current: number) {
84     return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
85   }
86 }