]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/infinite-scroller.directive.ts
Merge branch 'develop' into pr/1217
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / infinite-scroller.directive.ts
index 0448e2c2309942f3dc900e1eb8a191f6c0f850ae..a02e9444a72cd91f26d892d7a436b9534906b8e5 100644 (file)
@@ -6,12 +6,11 @@ import { fromEvent, Subscription } from 'rxjs'
   selector: '[myInfiniteScroller]'
 })
 export class InfiniteScrollerDirective implements OnInit, OnDestroy {
-  private static PAGE_VIEW_TOP_MARGIN = 500
-
   @Input() containerHeight: number
   @Input() pageHeight: number
+  @Input() firstLoadedPage = 1
   @Input() percentLimit = 70
-  @Input() autoLoading = false
+  @Input() autoInit = false
 
   @Output() nearOfBottom = new EventEmitter<void>()
   @Output() nearOfTop = new EventEmitter<void>()
@@ -23,13 +22,14 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy {
   private scrollDownSub: Subscription
   private scrollUpSub: Subscription
   private pageChangeSub: Subscription
+  private middleScreen: number
 
   constructor () {
     this.decimalLimit = this.percentLimit / 100
   }
 
   ngOnInit () {
-    if (this.autoLoading === true) return this.initialize()
+    if (this.autoInit === true) return this.initialize()
   }
 
   ngOnDestroy () {
@@ -39,6 +39,8 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy {
   }
 
   initialize () {
+    this.middleScreen = window.innerHeight / 2
+
     // Emit the last value
     const throttleOptions = { leading: true, trailing: true }
 
@@ -92,6 +94,11 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy {
   }
 
   private calculateCurrentPage (current: number) {
-    return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
+    const scrollY = current + this.middleScreen
+
+    const page = Math.max(1, Math.ceil(scrollY / this.pageHeight))
+
+    // Offset page
+    return page + (this.firstLoadedPage - 1)
   }
 }