]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix infinite scroll
authorChocobozzz <me@florianbigard.com>
Thu, 8 Mar 2018 09:46:12 +0000 (10:46 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 8 Mar 2018 09:46:12 +0000 (10:46 +0100)
client/src/app/shared/video/abstract-video-list.ts
client/src/app/shared/video/infinite-scroller.directive.ts

index 16ff38558ef8415ec6e7fcc71229649ce73a3b63..abc9feb062030470e243eddb5b0e2be43139da3a 100644 (file)
@@ -125,7 +125,7 @@ export abstract class AbstractVideoList implements OnInit {
     if (!this.pagination.totalItems) return true
 
     const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
-    return maxPage > this.pagination.currentPage
+    return maxPage > this.maxPageLoaded()
   }
 
   protected previousPage () {
index 16e901c34f1208d7d978ffd9efbf7f6129547e4d..ed49a9e81e63dfa89a6fd2f2fd89af76f7ca6c37 100644 (file)
@@ -7,6 +7,7 @@ import 'rxjs/add/operator/map'
 import 'rxjs/add/operator/startWith'
 import 'rxjs/add/operator/throttleTime'
 import { fromEvent } from 'rxjs/observable/fromEvent'
+import 'rxjs/add/operator/share'
 
 @Directive({
   selector: '[myInfiniteScroller]'
@@ -36,10 +37,14 @@ export class InfiniteScrollerDirective implements OnInit {
   }
 
   initialize () {
+    // Emit the last value
+    const throttleOptions = { leading: false, trailing: true }
+
     const scrollObservable = fromEvent(window, 'scroll')
       .startWith(true)
-      .throttleTime(200)
+      .throttleTime(200, undefined, throttleOptions)
       .map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight }))
+      .share()
 
     // Scroll Down
     scrollObservable
@@ -51,7 +56,6 @@ export class InfiniteScrollerDirective implements OnInit {
         return res
       })
       .filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit)
-      .distinct()
       .subscribe(() => this.nearOfBottom.emit())
 
     // Scroll up
@@ -66,15 +70,17 @@ export class InfiniteScrollerDirective implements OnInit {
       .filter(({ current, maximumScroll }) => {
         return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit
       })
-      .distinct()
       .subscribe(() => this.nearOfTop.emit())
 
     // Page change
     scrollObservable
       .distinct()
-      .map(({ current }) => Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight)))
+      .map(({ current }) => this.calculateCurrentPage(current))
       .distinctUntilChanged()
       .subscribe(res => this.pageChanged.emit(res))
   }
 
+  private calculateCurrentPage (current: number) {
+    return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight))
+  }
 }