]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/abstract-video-list.ts
Handle resizes on videos list
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
index 034d0d879a9e4372e2e9047b5353c2a7073169cd..7235b34255e3860aa51ce926787a40103c3b0392 100644 (file)
@@ -1,9 +1,10 @@
-import { ElementRef, OnInit, ViewChild, ViewChildren } from '@angular/core'
+import { ElementRef, 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 { Observable } from 'rxjs/Observable'
+import { fromEvent } from 'rxjs/observable/fromEvent'
 import { AuthService } from '../../core/auth'
 import { ComponentPagination } from '../rest/component-pagination.model'
 import { SortField } from './sort-field.type'
@@ -49,23 +50,11 @@ export abstract class AbstractVideoList implements OnInit {
     const routeParams = this.route.snapshot.params
     this.loadRouteParams(routeParams)
 
-    if (isInMobileView()) {
-      this.pagination.itemsPerPage = 5
-      this.videoWidth = -1
-    }
-
-    if (this.videoWidth !== -1) {
-      const videosWidth = this.videosElement.nativeElement.offsetWidth
-      this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE
-    }
-
-    // Video takes all the width
-    if (this.videoWidth === -1) {
-      this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
-    } else {
-      this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE
-    }
+    fromEvent(window, 'resize')
+      .debounceTime(500)
+      .subscribe(() => this.calcPageSizes())
 
+    this.calcPageSizes()
     if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
   }
 
@@ -125,7 +114,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 () {
@@ -176,4 +165,32 @@ export abstract class AbstractVideoList implements OnInit {
   private maxPageLoaded () {
     return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
   }
+
+  private calcPageSizes () {
+    if (isInMobileView()) {
+      this.pagination.itemsPerPage = 5
+
+      // Video takes all the width
+      this.videoWidth = -1
+      this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
+    } else {
+      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
+    let videos: Video[] = []
+    Object.values(this.loadedPages)
+      .forEach(videosPage => videos = videos.concat(videosPage))
+    this.loadedPages = {}
+
+    for (let i = 1; (i * this.pagination.itemsPerPage) <= videos.length; i++) {
+      this.loadedPages[i] = videos.slice((i - 1) * this.pagination.itemsPerPage, this.pagination.itemsPerPage * i)
+    }
+
+    this.buildVideoPages()
+
+    console.log('Re calculated pages after a resize!')
+  }
 }