]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add pagination support to the client
authorChocobozzz <florian.bigard@gmail.com>
Sun, 22 May 2016 08:43:06 +0000 (10:43 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Sun, 22 May 2016 08:44:35 +0000 (10:44 +0200)
client/angular/app/app.component.html
client/angular/videos/components/list/video-miniature.component.html
client/angular/videos/components/list/video-miniature.component.scss
client/angular/videos/components/list/videos-list.component.html
client/angular/videos/components/list/videos-list.component.scss
client/angular/videos/components/list/videos-list.component.ts
client/angular/videos/pagination.ts [new file with mode: 0644]
client/angular/videos/videos.service.ts
client/package.json
client/systemjs.config.js
client/tsconfig.json

index 949b375d2059a5feb9d86ceb111c9c20e7ccb945..ccbaef947b3a72433fa51662525e418b9b5a54ea 100644 (file)
@@ -5,7 +5,7 @@
       <h4>PeerTube</h4>
     </div>
 
-    <div class="col-md-8">
+    <div class="col-md-9">
       <input
         type="text" id="search_video" name="search_video" class="form-control" placeholder="Search a video..."
         #search (keyup.enter)="doSearch(search.value)"
index b88a19d5e9956d2332dfe61ac660edf650485770..244254b5a8b81aeed7df693ff577d87087ac8a77 100644 (file)
@@ -1,4 +1,4 @@
-<div class="video-miniature" (mouseenter)="onHover()" (mouseleave)="onBlur()">
+<div class="video-miniature col-md-4" (mouseenter)="onHover()" (mouseleave)="onBlur()">
   <a
     [routerLink]="['VideosWatch', { id: video.id }]" [attr.title]="video.description"
     class="video-miniature-thumbnail"
index dbcd65820950bd0c02d51a66c5b76205e64a33c7..4488abe22effb6bb5e92f4fd003fd34b511168b5 100644 (file)
@@ -1,8 +1,6 @@
 .video-miniature {
-  width: 200px;
   height: 200px;
   display: inline-block;
-  margin-right: 40px;
   position: relative;
 
   .video-miniature-thumbnail {
@@ -11,7 +9,7 @@
 
     .video-miniature-duration {
       position: absolute;
-      right: 2px;
+      right: 60px;
       bottom: 2px;
       display: inline-block;
       background-color: rgba(0, 0, 0, 0.8);
@@ -24,7 +22,7 @@
   .video-miniature-remove {
     display: inline-block;
     position: absolute;
-    left: 2px;
+    left: 16px;
     background-color: rgba(0, 0, 0, 0.8);
     color: rgba(255, 255, 255, 0.8);
     padding: 2px;
index 776339d10be21550d8b35707b7c81ffe79fec58f..39cdf29ba4b6f209ca3785c398703d23204c4ca7 100644 (file)
@@ -1,3 +1,11 @@
-<div *ngIf="videos.length === 0">There is no video.</div>
-<my-video-miniature *ngFor="let video of videos" [video]="video" [user]="user" (removed)="onRemoved(video)">
-</my-video-miniature>
+<div class="videos-miniatures">
+  <div *ngIf="videos.length === 0">There is no video.</div>
+
+  <my-video-miniature *ngFor="let video of videos" [video]="video" [user]="user" (removed)="onRemoved(video)">
+  </my-video-miniature>
+</div>
+
+<pagination
+  [totalItems]="pagination.total" [itemsPerPage]="pagination.itemsPerPage" [(ngModel)]="pagination.currentPage"
+  (ngModelChange)="getVideos()"
+></pagination>
index ac930978cf6a6131d6b89dc5f4e15cb4c9a3e087..05c38b833eb927ec044827667e46d58b9046dd12 100644 (file)
@@ -1,8 +1,12 @@
-.loading {
-  display: inline-block;
-  margin-top: 100px;
+.videos-miniatures {
+  min-height: 600px;
 }
 
 my-videos-miniature {
   display: inline-block;
 }
+
+pagination {
+  display: block;
+  text-align: center;
+}
index 6fc0c1f04b290f2f79ec271bace64ca04c59c082..341afdaa683975765070aa3fe80d661ddc52666d 100644 (file)
@@ -1,7 +1,10 @@
 import { Component, OnInit } from '@angular/core';
 import { ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated';
 
+import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
+
 import { AuthService } from '../../../users/services/auth.service';
+import { Pagination } from '../../pagination';
 import { User } from '../../../users/models/user';
 import { VideosService } from '../../videos.service';
 import { Video } from '../../video';
@@ -11,21 +14,26 @@ import { VideoMiniatureComponent } from './video-miniature.component';
   selector: 'my-videos-list',
   styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ],
   templateUrl: 'app/angular/videos/components/list/videos-list.component.html',
-  directives: [ ROUTER_DIRECTIVES, VideoMiniatureComponent ]
+  directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent ]
 })
 
 export class VideosListComponent implements OnInit {
   user: User = null;
   videos: Video[] = [];
+  pagination: Pagination = {
+    currentPage: 1,
+    itemsPerPage: 9,
+    total: 0
+  }
 
   private search: string;
 
   constructor(
     private _authService: AuthService,
     private _videosService: VideosService,
-    routeParams: RouteParams
+    private _routeParams: RouteParams
   ) {
-    this.search = routeParams.get('search');
+    this.search = this._routeParams.get('search');
   }
 
   ngOnInit() {
@@ -40,13 +48,16 @@ export class VideosListComponent implements OnInit {
     let observable = null;
 
     if (this.search !== null) {
-      observable = this._videosService.searchVideos(this.search);
+      observable = this._videosService.searchVideos(this.search, this.pagination);
     } else {
-      observable = this._videosService.getVideos();
+      observable = this._videosService.getVideos(this.pagination);
     }
 
     observable.subscribe(
-      videos => this.videos = videos,
+      ({ videos, totalVideos }) => {
+        this.videos = videos;
+        this.pagination.total = totalVideos;
+      },
       error => alert(error)
     );
   }
diff --git a/client/angular/videos/pagination.ts b/client/angular/videos/pagination.ts
new file mode 100644 (file)
index 0000000..06f7a78
--- /dev/null
@@ -0,0 +1,5 @@
+export interface Pagination {
+  currentPage: number;
+  itemsPerPage: number;
+  total: number;
+}
index f4790b5111ddbfa80c010cedfdfc31b5788ae274..94ef418ebdd886a3dd48b6988560de305e312eb5 100644 (file)
@@ -1,7 +1,8 @@
 import { Injectable } from '@angular/core';
-import { Http, Response } from '@angular/http';
+import { Http, Response, RequestOptions, URLSearchParams } from '@angular/http';
 import { Observable } from 'rxjs/Rx';
 
+import { Pagination } from './pagination';
 import { Video } from './video';
 import { AuthService } from '../users/services/auth.service';
 
@@ -11,8 +12,9 @@ export class VideosService {
 
   constructor (private http: Http, private _authService: AuthService) {}
 
-  getVideos() {
-    return this.http.get(this._baseVideoUrl)
+  getVideos(pagination: Pagination) {
+    const params = { search: this.createPaginationParams(pagination) };
+    return this.http.get(this._baseVideoUrl, params)
                     .map(res => res.json())
                     .map(this.extractVideos)
                     .catch(this.handleError);
@@ -31,24 +33,38 @@ export class VideosService {
                     .catch(this.handleError);
   }
 
-  searchVideos(search: string) {
-    return this.http.get(this._baseVideoUrl + 'search/' + search)
+  searchVideos(search: string, pagination: Pagination) {
+    const params = { search: this.createPaginationParams(pagination) };
+    return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search), params)
                     .map(res => res.json())
                     .map(this.extractVideos)
                     .catch(this.handleError);
   }
 
-  private extractVideos (body: any[]) {
+  private extractVideos (body: any) {
+    const videos_json = body.data;
+    const totalVideos = body.total;
     const videos = [];
-    for (const video_json of body) {
+    for (const video_json of videos_json) {
       videos.push(new Video(video_json));
     }
 
-    return videos;
+    return { videos, totalVideos };
   }
 
   private handleError (error: Response) {
     console.error(error);
     return Observable.throw(error.json().error || 'Server error');
   }
+
+  private createPaginationParams(pagination: Pagination) {
+    const params = new URLSearchParams();
+    const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
+    const count: number = pagination.itemsPerPage;
+
+    params.set('start', start.toString());
+    params.set('count', count.toString());
+
+    return params;
+  }
 }
index b730a1f4a64301e0110b2336f3eb88a1a0462609..b77c6e44759ff64aaf7f3ef0cc4f41a22f608157 100644 (file)
   },
   "license": "GPLv3",
   "dependencies": {
-    "angular-pipes": "^2.0.0",
     "@angular/common": "2.0.0-rc.1",
     "@angular/compiler": "2.0.0-rc.1",
     "@angular/core": "2.0.0-rc.1",
     "@angular/http": "2.0.0-rc.1",
-    "@angular/platform-browser-dynamic": "2.0.0-rc.1",
     "@angular/platform-browser": "2.0.0-rc.1",
+    "@angular/platform-browser-dynamic": "2.0.0-rc.1",
     "@angular/router-deprecated": "2.0.0-rc.1",
+    "angular-pipes": "^2.0.0",
     "blueimp-file-upload": "^9.12.1",
     "bootstrap-sass": "^3.3.6",
     "es6-promise": "^3.0.2",
     "es6-shim": "^0.35.0",
     "jquery": "^2.2.3",
     "jquery.ui.widget": "^1.10.3",
+    "ng2-bootstrap": "^1.0.16",
     "reflect-metadata": "0.1.3",
     "rxjs": "5.0.0-beta.6",
     "systemjs": "0.19.27",
index 0ac94ca445dcecf9180c651bf3fc541a6b65e49d..82ca5bb704f8cf6e6fbb22860f9de41c7e3d2cd3 100644 (file)
@@ -2,11 +2,13 @@
   var map = {
     'app': 'app/angular',
     'angular-pipes': 'app/node_modules/angular-pipes',
+    'ng2-bootstrap': 'app/node_modules/ng2-bootstrap',
     'angular-rxjs.bundle': 'app/bundles/angular-rxjs.bundle.js'
   }
 
   var packages = {
     'app': { main: 'main.js', defaultExtension: 'js' },
+    'ng2-bootstrap': { defaultExtension: 'js' },
     'rxjs': { defaultExtension: 'js' }
   }
   var packageNames = [
index fac9da116edc3811116793c9895c24d086af8dfb..1d002f7b06a6fb85a70b4d25d708b12a6a37f5db 100644 (file)
@@ -32,6 +32,7 @@
     "angular/videos/components/list/video-miniature.component.ts",
     "angular/videos/components/list/videos-list.component.ts",
     "angular/videos/components/watch/videos-watch.component.ts",
+    "angular/videos/pagination.ts",
     "angular/videos/video.ts",
     "angular/videos/videos.service.ts",
     "typings/globals/es6-shim/index.d.ts",