]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/infinite-scroller.directive.ts
Add ability to import video with youtube-dl
[github/Chocobozzz/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, 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 private static PAGE_VIEW_TOP_MARGIN = 500
10
11 @Input() containerHeight: number
12 @Input() pageHeight: number
13 @Input() percentLimit = 70
14 @Input() autoLoading = 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
27 constructor () {
28 this.decimalLimit = this.percentLimit / 100
29 }
30
31 ngOnInit () {
32 if (this.autoLoading === true) return this.initialize()
33 }
34
35 ngOnDestroy () {
36 if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
37 if (this.scrollUpSub) this.scrollUpSub.unsubscribe()
38 if (this.pageChangeSub) this.pageChangeSub.unsubscribe()
39 }
40
41 initialize () {
42 // Emit the last value
43 const throttleOptions = { leading: true, trailing: true }
44
45 const scrollObservable = fromEvent(window, 'scroll')
46 .pipe(
47 startWith(null),
48 throttleTime(200, undefined, throttleOptions),
49 map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight })),
50 distinctUntilChanged((o1, o2) => o1.current === o2.current),
51 share()
52 )
53
54 // Scroll Down
55 this.scrollDownSub = scrollObservable
56 .pipe(
57 // Check we scroll down
58 filter(({ current }) => {
59 const res = this.lastCurrentBottom < current
60
61 this.lastCurrentBottom = current
62 return res
63 }),
64 filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
65 )
66 .subscribe(() => this.nearOfBottom.emit())
67
68 // Scroll up
69 this.scrollUpSub = scrollObservable
70 .pipe(
71 // Check we scroll up
72 filter(({ current }) => {
73 const res = this.lastCurrentTop > current
74
75 this.lastCurrentTop = current
76 return res
77 }),
78 filter(({ current, maximumScroll }) => {
79 return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
80 })
81 )
82 .subscribe(() => this.nearOfTop.emit())
83
84 // Page change
85 this.pageChangeSub = scrollObservable
86 .pipe(
87 distinct(),
88 map(({ current }) => this.calculateCurrentPage(current)),
89 distinctUntilChanged()
90 )
91 .subscribe(res => this.pageChanged.emit(res))
92 }
93
94 private calculateCurrentPage (current: number) {
95 return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
96 }
97 }