X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-main%2Fangular%2Finfinite-scroller.directive.ts;h=c247cfde2d92f39d0a1a7dced6728a188960d5cf;hb=e56ce494c8c55e7f653991ffc7d7398ca875e768;hp=f09c3d1fc62742244f934b3383d3a67a85a93e3c;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts b/client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts index f09c3d1fc..c247cfde2 100644 --- a/client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts +++ b/client/src/app/shared/shared-main/angular/infinite-scroller.directive.ts @@ -1,16 +1,20 @@ -import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators' -import { AfterContentChecked, Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core' import { fromEvent, Observable, Subscription } from 'rxjs' +import { distinctUntilChanged, filter, map, share, startWith, throttleTime } from 'rxjs/operators' +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 + // Add angular state in query params to reuse the routed component + @Input() setAngularState: boolean + @Input() parentDisabled = false + @Output() nearOfBottom = new EventEmitter() 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 @@ -80,7 +92,9 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterConten } private getMaximumScroll () { - return this.container.scrollHeight - window.innerHeight + const elementHeight = this.onItself ? this.container.clientHeight : window.innerHeight + + return this.container.scrollHeight - elementHeight } private hasScroll () { @@ -93,4 +107,8 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy, AfterConten this.lastCurrentBottom = current return result } + + private setScrollRouteParams () { + this.peertubeRouter.addRouteSetting(RouterSetting.REUSE_COMPONENT) + } }