]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/infinite-scroller.directive.ts
Check activities host
[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
0cd4344f
C
14
15 @Output() nearOfBottom = new EventEmitter<void>()
16 @Output() nearOfTop = new EventEmitter<void>()
17 @Output() pageChanged = new EventEmitter<number>()
18
19 private decimalLimit = 0
20 private lastCurrentBottom = -1
21 private lastCurrentTop = 0
9af61e84
C
22 private scrollDownSub: Subscription
23 private scrollUpSub: Subscription
24 private pageChangeSub: Subscription
a8ecc6f6 25 private middleScreen: number
0cd4344f
C
26
27 constructor () {
28 this.decimalLimit = this.percentLimit / 100
29 }
30
31 ngOnInit () {
aa55a4da 32 if (this.autoInit === true) return this.initialize()
0cd4344f
C
33 }
34
9af61e84
C
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
0cd4344f 41 initialize () {
a8ecc6f6
C
42 this.middleScreen = window.innerHeight / 2
43
6a6d92b1 44 // Emit the last value
2c6bbd97 45 const throttleOptions = { leading: true, trailing: true }
6a6d92b1 46
0cd4344f 47 const scrollObservable = fromEvent(window, 'scroll')
db400f44
C
48 .pipe(
49 startWith(null),
50 throttleTime(200, undefined, throttleOptions),
51 map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight })),
52 distinctUntilChanged((o1, o2) => o1.current === o2.current),
53 share()
54 )
0cd4344f
C
55
56 // Scroll Down
9af61e84 57 this.scrollDownSub = scrollObservable
db400f44
C
58 .pipe(
59 // Check we scroll down
60 filter(({ current }) => {
61 const res = this.lastCurrentBottom < current
0cd4344f 62
db400f44
C
63 this.lastCurrentBottom = current
64 return res
65 }),
66 filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
67 )
0cd4344f
C
68 .subscribe(() => this.nearOfBottom.emit())
69
70 // Scroll up
9af61e84 71 this.scrollUpSub = scrollObservable
db400f44
C
72 .pipe(
73 // Check we scroll up
74 filter(({ current }) => {
75 const res = this.lastCurrentTop > current
0cd4344f 76
db400f44
C
77 this.lastCurrentTop = current
78 return res
79 }),
80 filter(({ current, maximumScroll }) => {
81 return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
82 })
83 )
0cd4344f
C
84 .subscribe(() => this.nearOfTop.emit())
85
86 // Page change
9af61e84 87 this.pageChangeSub = scrollObservable
db400f44
C
88 .pipe(
89 distinct(),
90 map(({ current }) => this.calculateCurrentPage(current)),
91 distinctUntilChanged()
92 )
0cd4344f
C
93 .subscribe(res => this.pageChanged.emit(res))
94 }
95
6a6d92b1 96 private calculateCurrentPage (current: number) {
a8ecc6f6
C
97 const scrollY = current + this.middleScreen
98
99 const page = Math.max(1, Math.ceil(scrollY / this.pageHeight))
100
101 // Offset page
102 return page + (this.firstLoadedPage - 1)
6a6d92b1 103 }
0cd4344f 104}