]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/infinite-scroller.directive.ts
Playlist support in watch page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / infinite-scroller.directive.ts
CommitLineData
db400f44 1import { distinct, distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators'
9af61e84 2import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
db400f44 3import { fromEvent, Subscription } from 'rxjs'
0cd4344f
C
4
5@Directive({
6 selector: '[myInfiniteScroller]'
7})
9af61e84 8export class InfiniteScrollerDirective implements OnInit, OnDestroy {
0cd4344f
C
9 @Input() containerHeight: number
10 @Input() pageHeight: number
a8ecc6f6 11 @Input() firstLoadedPage = 1
0cd4344f 12 @Input() percentLimit = 70
aa55a4da 13 @Input() autoInit = false
e2f01c47 14 @Input() container = document.body
0cd4344f
C
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
9af61e84
C
23 private scrollDownSub: Subscription
24 private scrollUpSub: Subscription
25 private pageChangeSub: Subscription
a8ecc6f6 26 private middleScreen: number
0cd4344f
C
27
28 constructor () {
29 this.decimalLimit = this.percentLimit / 100
30 }
31
32 ngOnInit () {
aa55a4da 33 if (this.autoInit === true) return this.initialize()
0cd4344f
C
34 }
35
9af61e84
C
36 ngOnDestroy () {
37 if (this.scrollDownSub) this.scrollDownSub.unsubscribe()
38 if (this.scrollUpSub) this.scrollUpSub.unsubscribe()
39 if (this.pageChangeSub) this.pageChangeSub.unsubscribe()
40 }
41
0cd4344f 42 initialize () {
a8ecc6f6
C
43 this.middleScreen = window.innerHeight / 2
44
6a6d92b1 45 // Emit the last value
2c6bbd97 46 const throttleOptions = { leading: true, trailing: true }
6a6d92b1 47
0cd4344f 48 const scrollObservable = fromEvent(window, 'scroll')
db400f44
C
49 .pipe(
50 startWith(null),
51 throttleTime(200, undefined, throttleOptions),
e2f01c47 52 map(() => ({ current: window.scrollY, maximumScroll: this.container.clientHeight - window.innerHeight })),
db400f44
C
53 distinctUntilChanged((o1, o2) => o1.current === o2.current),
54 share()
55 )
0cd4344f
C
56
57 // Scroll Down
9af61e84 58 this.scrollDownSub = scrollObservable
db400f44
C
59 .pipe(
60 // Check we scroll down
61 filter(({ current }) => {
62 const res = this.lastCurrentBottom < current
0cd4344f 63
db400f44
C
64 this.lastCurrentBottom = current
65 return res
66 }),
67 filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
68 )
0cd4344f
C
69 .subscribe(() => this.nearOfBottom.emit())
70
71 // Scroll up
9af61e84 72 this.scrollUpSub = scrollObservable
db400f44
C
73 .pipe(
74 // Check we scroll up
75 filter(({ current }) => {
76 const res = this.lastCurrentTop > current
0cd4344f 77
db400f44
C
78 this.lastCurrentTop = current
79 return res
80 }),
81 filter(({ current, maximumScroll }) => {
82 return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
83 })
84 )
0cd4344f
C
85 .subscribe(() => this.nearOfTop.emit())
86
87 // Page change
9af61e84 88 this.pageChangeSub = scrollObservable
db400f44
C
89 .pipe(
90 distinct(),
91 map(({ current }) => this.calculateCurrentPage(current)),
92 distinctUntilChanged()
93 )
0cd4344f
C
94 .subscribe(res => this.pageChanged.emit(res))
95 }
96
6a6d92b1 97 private calculateCurrentPage (current: number) {
a8ecc6f6
C
98 const scrollY = current + this.middleScreen
99
100 const page = Math.max(1, Math.ceil(scrollY / this.pageHeight))
101
102 // Offset page
103 return page + (this.firstLoadedPage - 1)
6a6d92b1 104 }
0cd4344f 105}