]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / angular / infinite-scroller.directive.ts
index d2cf532277c41547cf2aa30061878e8dfa28a862..c247cfde2d92f39d0a1a7dced6728a188960d5cf 100644 (file)
@@ -1,16 +1,20 @@
 import { fromEvent, Observable, Subscription } from 'rxjs'
 import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators'
-import { AfterContentChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
+import { AfterViewChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
+import { PeerTubeRouterService, RouterSetting } from '@app/core'
 
 @Directive({
   selector: '[myInfiniteScroller]'
 })
-export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterContentChecked {
+export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterViewChecked {
   @Input() percentLimit = 70
-  @Input() autoInit = false
   @Input() onItself = false
   @Input() dataObservable: Observable<any[]>
 
+  // Add angular state in query params to reuse the routed component
+  @Input() setAngularState: boolean
+  @Input() parentDisabled = false
+
   @Output() nearOfBottom = new EventEmitter<void>()
 
   private decimalLimit = 0
@@ -20,22 +24,26 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterConten
 
   private checkScroll = false
 
-  constructor (private el: ElementRef) {
+  constructor (
+    private peertubeRouter: PeerTubeRouterService,
+    private el: ElementRef
+  ) {
     this.decimalLimit = this.percentLimit / 100
   }
 
-  ngAfterContentChecked () {
+  ngAfterViewChecked () {
     if (this.checkScroll) {
       this.checkScroll = false
 
-      console.log('Checking if the initial state has a scroll.')
-
-      if (this.hasScroll() === false) this.nearOfBottom.emit()
+      // Wait HTML update
+      setTimeout(() => {
+        if (this.hasScroll() === false) this.nearOfBottom.emit()
+      })
     }
   }
 
   ngOnInit () {
-    if (this.autoInit === true) return this.initialize()
+    this.initialize()
   }
 
   ngOnDestroy () {
@@ -66,7 +74,11 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterConten
         filter(({ current }) => this.isScrollingDown(current)),
         filter(({ current, maximumScroll }) => (current / maximumScroll) > this.decimalLimit)
       )
-      .subscribe(() => this.nearOfBottom.emit())
+      .subscribe(() => {
+        if (this.setAngularState && !this.parentDisabled) this.setScrollRouteParams()
+
+        this.nearOfBottom.emit()
+      })
 
     if (this.dataObservable) {
       this.dataObservable
@@ -95,4 +107,8 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterConten
     this.lastCurrentBottom = current
     return result
   }
+
+  private setScrollRouteParams () {
+    this.peertubeRouter.addRouteSetting(RouterSetting.REUSE_COMPONENT)
+  }
 }