X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fvideo%2Fabstract-video-list.ts;h=c18ebcf5463d5c893b853c337ba67aa8ff79df00;hb=d414207f07f47307947d86d5bc5c5fc0ae73be18;hp=2b6870a789b2b06c5c0101df5ae8477604203ff0;hpb=b2731bff2834fb6aacf166cf435030bf96eb12f3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/video/abstract-video-list.ts b/client/src/app/shared/video/abstract-video-list.ts index 2b6870a78..c18ebcf54 100644 --- a/client/src/app/shared/video/abstract-video-list.ts +++ b/client/src/app/shared/video/abstract-video-list.ts @@ -1,34 +1,52 @@ -import { OnInit } from '@angular/core' +import { ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' +import { isInMobileView } from '@app/shared/misc/utils' +import { InfiniteScrollerDirective } from '@app/shared/video/infinite-scroller.directive' import { NotificationsService } from 'angular2-notifications' +import 'rxjs/add/operator/debounceTime' import { Observable } from 'rxjs/Observable' +import { fromEvent } from 'rxjs/observable/fromEvent' +import { Subscription } from 'rxjs/Subscription' import { AuthService } from '../../core/auth' +import { ComponentPagination } from '../rest/component-pagination.model' import { SortField } from './sort-field.type' -import { VideoPagination } from './video-pagination.model' import { Video } from './video.model' -export abstract class AbstractVideoList implements OnInit { - pagination: VideoPagination = { +export abstract class AbstractVideoList implements OnInit, OnDestroy { + private static LINES_PER_PAGE = 4 + + @ViewChild('videosElement') videosElement: ElementRef + @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective + + pagination: ComponentPagination = { currentPage: 1, - itemsPerPage: 25, + itemsPerPage: 10, totalItems: null } sort: SortField = '-createdAt' defaultSort: SortField = '-createdAt' - videos: Video[] = [] loadOnInit = true + pageHeight: number + videoWidth: number + videoHeight: number + videoPages: Video[][] = [] + + protected baseVideoWidth = 215 + protected baseVideoHeight = 230 protected abstract notificationsService: NotificationsService protected abstract authService: AuthService protected abstract router: Router protected abstract route: ActivatedRoute - protected abstract currentRoute: string - abstract titlePage: string - private loadedPages: { [ id: number ]: boolean } = {} - abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}> + protected loadedPages: { [ id: number ]: Video[] } = {} + protected otherRouteParams = {} + + private resizeSubscription: Subscription + + abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}> get user () { return this.authService.getUser() @@ -39,13 +57,20 @@ export abstract class AbstractVideoList implements OnInit { const routeParams = this.route.snapshot.params this.loadRouteParams(routeParams) - if (this.loadOnInit === true) this.loadMoreVideos('after') + this.resizeSubscription = fromEvent(window, 'resize') + .debounceTime(500) + .subscribe(() => this.calcPageSizes()) + + this.calcPageSizes() + if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage) + } + + ngOnDestroy () { + if (this.resizeSubscription) this.resizeSubscription.unsubscribe() } onNearOfTop () { - if (this.pagination.currentPage > 1) { - this.previousPage() - } + this.previousPage() } onNearOfBottom () { @@ -54,16 +79,20 @@ export abstract class AbstractVideoList implements OnInit { } } + onPageChanged (page: number) { + this.pagination.currentPage = page + this.setNewRouteParams() + } + reloadVideos () { - this.videos = [] this.loadedPages = {} - this.loadMoreVideos('before') + this.loadMoreVideos(this.pagination.currentPage) } - loadMoreVideos (where: 'before' | 'after') { - if (this.loadedPages[this.pagination.currentPage] === true) return + loadMoreVideos (page: number) { + if (this.loadedPages[page] !== undefined) return - const observable = this.getVideosObservable() + const observable = this.getVideosObservable(page) observable.subscribe( ({ videos, totalVideos }) => { @@ -74,16 +103,17 @@ export abstract class AbstractVideoList implements OnInit { return this.reloadVideos() } - this.loadedPages[this.pagination.currentPage] = true + this.loadedPages[page] = videos + this.buildVideoPages() this.pagination.totalItems = totalVideos - if (where === 'before') { - this.videos = videos.concat(this.videos) - } else { - this.videos = this.videos.concat(videos) + // Initialize infinite scroller now we loaded the first page + if (Object.keys(this.loadedPages).length === 1) { + // Wait elements creation + setTimeout(() => this.infiniteScroller.initialize(), 500) } }, - error => this.notificationsService.error('Error', error.text) + error => this.notificationsService.error('Error', error.message) ) } @@ -95,21 +125,19 @@ 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 () { - this.pagination.currentPage-- + const min = this.minPageLoaded() - this.setNewRouteParams() - this.loadMoreVideos('before') + if (min > 1) { + this.loadMoreVideos(min - 1) + } } protected nextPage () { - this.pagination.currentPage++ - - this.setNewRouteParams() - this.loadMoreVideos('after') + this.loadMoreVideos(this.maxPageLoaded() + 1) } protected buildRouteParams () { @@ -119,7 +147,7 @@ export abstract class AbstractVideoList implements OnInit { page: this.pagination.currentPage } - return params + return Object.assign(params, this.otherRouteParams) } protected loadRouteParams (routeParams: { [ key: string ]: any }) { @@ -136,4 +164,55 @@ export abstract class AbstractVideoList implements OnInit { const routeParams = this.buildRouteParams() this.router.navigate([ this.currentRoute, routeParams ]) } + + protected buildVideoPages () { + this.videoPages = Object.values(this.loadedPages) + } + + private minPageLoaded () { + return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) + } + + private maxPageLoaded () { + return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) + } + + private calcPageSizes () { + if (isInMobileView() || this.baseVideoWidth === -1) { + this.pagination.itemsPerPage = 5 + + // Video takes all the width + this.videoWidth = -1 + // Same ratios than base width/height + this.videoHeight = this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth) + this.pageHeight = this.pagination.itemsPerPage * this.videoHeight + } else { + this.videoWidth = this.baseVideoWidth + this.videoHeight = this.baseVideoHeight + + const videosWidth = this.videosElement.nativeElement.offsetWidth + this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE + this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE + } + + // Rebuild pages because maybe we modified the number of items per page + const videos = [].concat(...this.videoPages) + this.loadedPages = {} + + let i = 1 + // Don't include the last page if it not complete + while (videos.length >= this.pagination.itemsPerPage && i < 10000) { // 10000 -> Hard limit in case of infinite loop + this.loadedPages[i] = videos.splice(0, this.pagination.itemsPerPage) + i++ + } + + // Re fetch the last page + if (videos.length !== 0) { + this.loadMoreVideos(i) + } else { + this.buildVideoPages() + } + + console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage) + } }