]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/infinite-scroller.directive.ts
Fix typo in embed
[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 40 // Emit the last value
2c6bbd97 41 const throttleOptions = { leading: true, trailing: true }
6a6d92b1 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 }))
2c6bbd97 47 .distinctUntilChanged((o1, o2) => o1.current === o2.current)
6a6d92b1 48 .share()
0cd4344f
C
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)
0cd4344f
C
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 })
0cd4344f
C
74 .subscribe(() => this.nearOfTop.emit())
75
76 // Page change
77 scrollObservable
0cd4344f 78 .distinct()
6a6d92b1 79 .map(({ current }) => this.calculateCurrentPage(current))
0cd4344f
C
80 .distinctUntilChanged()
81 .subscribe(res => this.pageChanged.emit(res))
82 }
83
6a6d92b1
C
84 private calculateCurrentPage (current: number) {
85 return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
86 }
0cd4344f 87}