]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/infinite-scroller.directive.ts
Fix infinite scroll
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / infinite-scroller.directive.ts
CommitLineData
0cd4344f 1import { Directive, EventEmitter, Input, OnInit, Output } from '@angular/core'
8cac1b64 2import 'rxjs/add/operator/debounceTime'
0cd4344f 3import 'rxjs/add/operator/distinct'
3bcfff7f 4import 'rxjs/add/operator/distinctUntilChanged'
8cac1b64
C
5import 'rxjs/add/operator/filter'
6import 'rxjs/add/operator/map'
0cd4344f 7import 'rxjs/add/operator/startWith'
a9ca764e 8import 'rxjs/add/operator/throttleTime'
0cd4344f 9import { fromEvent } from 'rxjs/observable/fromEvent'
6a6d92b1 10import 'rxjs/add/operator/share'
0cd4344f
C
11
12@Directive({
13 selector: '[myInfiniteScroller]'
14})
15export 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 () {
6a6d92b1
C
40 // Emit the last value
41 const throttleOptions = { leading: false, trailing: true }
42
0cd4344f
C
43 const scrollObservable = fromEvent(window, 'scroll')
44 .startWith(true)
6a6d92b1 45 .throttleTime(200, undefined, throttleOptions)
0cd4344f 46 .map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight }))
6a6d92b1 47 .share()
0cd4344f
C
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)
0cd4344f
C
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 })
0cd4344f
C
73 .subscribe(() => this.nearOfTop.emit())
74
75 // Page change
76 scrollObservable
0cd4344f 77 .distinct()
6a6d92b1 78 .map(({ current }) => this.calculateCurrentPage(current))
0cd4344f
C
79 .distinctUntilChanged()
80 .subscribe(res => this.pageChanged.emit(res))
81 }
82
6a6d92b1
C
83 private calculateCurrentPage (current: number) {
84 return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
85 }
0cd4344f 86}