]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Client: save page params as well
authorChocobozzz <florian.bigard@gmail.com>
Mon, 18 Jul 2016 13:39:10 +0000 (15:39 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Mon, 18 Jul 2016 13:39:10 +0000 (15:39 +0200)
client/src/app/app.component.html
client/src/app/app.component.ts
client/src/app/shared/search/search.component.ts
client/src/app/shared/search/search.service.ts
client/src/app/videos/video-list/video-list.component.html
client/src/app/videos/video-list/video-list.component.ts
client/src/app/videos/video-list/video-miniature.component.html
client/src/app/videos/video-list/video-miniature.component.ts
client/tsconfig.json

index 8c5285e4bc9087652a27a3a2579c4a12998bfad7..ab8e0c2836ffc7adeccd258f6ed2a6ca228a752d 100644 (file)
@@ -6,7 +6,7 @@
     </div>
 
     <div class="col-md-9">
-      <my-search (search)="onSearch($event)"></my-search>
+      <my-search></my-search>
     </div>
   </header>
 
index 88b181f9ce885c3bf19f0dd889fc85cc74d5d649..354d00a7a78479598662767e70b39098518d9959 100644 (file)
@@ -1,12 +1,11 @@
 import { Component } from '@angular/core';
 import { HTTP_PROVIDERS } from '@angular/http';
-import { Router, ROUTER_DIRECTIVES } from '@angular/router';
+import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router';
 
 import { FriendService } from './friends';
 import {
   AuthService,
   AuthStatus,
-  Search,
   SearchComponent,
   SearchService
 } from './shared';
@@ -27,6 +26,7 @@ export class AppComponent {
   constructor(
     private authService: AuthService,
     private friendService: FriendService,
+    private route: ActivatedRoute,
     private router: Router
   ) {
     this.isLoggedIn = this.authService.isLoggedIn();
@@ -40,19 +40,6 @@ export class AppComponent {
     );
   }
 
-  onSearch(search: Search) {
-    if (search.value !== '') {
-      const params = {
-        field: search.field,
-        search: search.value
-      };
-
-      this.router.navigate(['/videos/list', params]);
-    } else {
-      this.router.navigate(['/videos/list']);
-    }
-  }
-
   // FIXME
   logout() {
     // this._authService.logout();
index d33701bc81336dbc9b017d57499162a9b0f95d08..e864fbc172350776b38ae8784cae22e4959721e3 100644 (file)
@@ -1,4 +1,4 @@
-import { Component, EventEmitter, Output, OnInit } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
 
 import { DROPDOWN_DIRECTIVES} from  'ng2-bootstrap/components/dropdown';
 
@@ -13,8 +13,6 @@ import { SearchService } from './search.service';
 })
 
 export class SearchComponent implements OnInit {
-  @Output() search = new EventEmitter<Search>();
-
   fieldChoices = {
     name: 'Name',
     author: 'Author',
@@ -30,7 +28,9 @@ export class SearchComponent implements OnInit {
   constructor(private searchService: SearchService) {}
 
   ngOnInit() {
-    this.searchService.searchChanged.subscribe(
+    // Subscribe is the search changed
+    // Usually changed by videos list component
+    this.searchService.updateSearch.subscribe(
       newSearchCriterias => {
         // Put a field by default
         if (!newSearchCriterias.field) {
@@ -58,7 +58,7 @@ export class SearchComponent implements OnInit {
   }
 
   doSearch() {
-    this.search.emit(this.searchCriterias);
+    this.searchService.searchUpdated.next(this.searchCriterias);
   }
 
   getStringChoice(choiceKey: SearchField) {
index 0e41cdd34c862e75c2715ce19d733c41f678dfa0..c7993db3dd05df9ba299dbb372e3300a0d398f42 100644 (file)
@@ -7,9 +7,11 @@ import { Search } from './search.model';
 // Remove it when we'll be able to subscribe to router changes
 @Injectable()
 export class SearchService {
-  searchChanged: Subject<Search>;
+  searchUpdated: Subject<Search>;
+  updateSearch: Subject<Search>;
 
   constructor() {
-    this.searchChanged = new Subject<Search>();
+    this.updateSearch = new Subject<Search>();
+    this.searchUpdated = new Subject<Search>();
   }
 }
index 0e17ef2c43f4abaf3850c0947cc65b27e3bb79a2..e119517a804083ffc057892e7a8f680dc04a9576 100644 (file)
@@ -8,13 +8,16 @@
 </div>
 
 <div class="videos-miniatures">
-  <div class="col-md-12 no-video" *ngIf="noVideo()">There is no video.</div>
+  <div class="col-md-12 no-video" *ngIf="isThereNoVideo()">There is no video.</div>
 
-  <my-video-miniature class="ng-animate "*ngFor="let video of videos" [video]="video" [user]="user" (removed)="onRemoved(video)">
+  <my-video-miniature
+    class="ng-animate"
+    *ngFor="let video of videos" [video]="video" [user]="user" [currentSort]="sort" (removed)="onRemoved(video)"
+  >
   </my-video-miniature>
 </div>
 
 <pagination *ngIf="pagination.totalItems !== null"
   [totalItems]="pagination.totalItems" [itemsPerPage]="pagination.itemsPerPage" [maxSize]="6" [boundaryLinks]="true" [rotate]="false"
-  [(ngModel)]="pagination.currentPage" (pageChanged)="getVideos()"
+  [(ngModel)]="pagination.currentPage" (pageChanged)="onPageChanged($event)"
 ></pagination>
index 0ebf0ef5c459016e9a2bc9edafe7f25366eb311f..5691d684eee9cb5ab6ebaff98f5810d7ab150893 100644 (file)
@@ -37,7 +37,8 @@ export class VideoListComponent implements OnInit, OnDestroy {
   videos: Video[] = [];
 
   private search: Search;
-  private sub: any;
+  private subActivatedRoute: any;
+  private subSearch: any;
 
   constructor(
     private authService: AuthService,
@@ -49,33 +50,35 @@ export class VideoListComponent implements OnInit, OnDestroy {
   ) {}
 
   ngOnInit() {
-    this.sub = this.route.params.subscribe(routeParams => {
-      if (this.authService.isLoggedIn()) {
-        this.user = User.load();
-      }
+    if (this.authService.isLoggedIn()) {
+      this.user = User.load();
+    }
 
-      this.search = {
-        value: routeParams['search'],
-        field: <SearchField>routeParams['field']
-      };
+    // Subscribe to route changes
+    this.subActivatedRoute = this.route.params.subscribe(routeParams => {
+      this.loadRouteParams(routeParams);
 
       // Update the search service component
-      this.searchService.searchChanged.next(this.search);
+      this.searchService.updateSearch.next(this.search);
+      this.getVideos();
+    });
 
-      this.sort = <SortField>routeParams['sort'] || '-createdDate';
+    // Subscribe to search changes
+    this.subSearch = this.searchService.searchUpdated.subscribe(search => {
+      this.search = search;
 
-      this.getVideos();
+      this.navigateToNewParams();
     });
   }
 
   ngOnDestroy() {
-    this.sub.unsubscribe();
+    this.subActivatedRoute.unsubscribe();
+    this.subSearch.unsubscribe();
   }
 
   getVideos(detectChanges = true) {
     this.loading.next(true);
     this.videos = [];
-    this.pagination.currentPage = 1;
 
     this.changeDetector.detectChanges();
 
@@ -98,26 +101,65 @@ export class VideoListComponent implements OnInit, OnDestroy {
     );
   }
 
-  noVideo() {
-    return !this.loading && this.videos.length === 0;
+  isThereNoVideo() {
+    return !this.loading.getValue() && this.videos.length === 0;
+  }
+
+  onPageChanged(event: any) {
+    // Be sure the current page is set
+    this.pagination.currentPage = event.page;
+
+    this.navigateToNewParams();
   }
 
   onRemoved(video: Video) {
-    this.getVideos(false);
+    this.getVideos();
   }
 
   onSort(sort: SortField) {
     this.sort = sort;
 
+    this.navigateToNewParams();
+  }
+
+  private buildRouteParams() {
+    // There is always a sort and a current page
     const params: any = {
-      sort: this.sort
+      sort: this.sort,
+      page: this.pagination.currentPage
     };
 
+    // Maybe there is a search
     if (this.search.value) {
       params.field = this.search.field;
       params.search = this.search.value;
     }
 
-    this.router.navigate(['/videos/list', params]);
+    return params;
+  }
+
+  private loadRouteParams(routeParams) {
+    if (routeParams['search'] !== undefined) {
+      this.search = {
+        value: routeParams['search'],
+        field: <SearchField>routeParams['field']
+      };
+    } else {
+      this.search = {
+        value: '',
+        field: 'name'
+      };
+    }
+
+    this.sort = <SortField>routeParams['sort'] || '-createdDate';
+
+    this.pagination.currentPage = parseInt(routeParams['page']) || 1;
+
+    this.changeDetector.detectChanges();
+  }
+
+  private navigateToNewParams() {
+    const routeParams = this.buildRouteParams();
+    this.router.navigate(['/videos/list', routeParams]);
   }
 }
index 3cf28620e0d0f56913d53601786767276bb2548c..373ff6bfbd86b9d67e1edcbbadf3bf37418becd9 100644 (file)
 
       <div class="video-miniature-tags">
         <span *ngFor="let tag of video.tags" class="video-miniature-tag">
-          <a [routerLink]="['/videos/list', { field: 'tags', search: tag }]" class="label label-primary">{{ tag }}</a>
+          <a [routerLink]="['/videos/list', { field: 'tags', search: tag, sort: currentSort }]" class="label label-primary">{{ tag }}</a>
         </span>
       </div>
     </span>
 
-    <a [routerLink]="['/videos/list', { field: 'author', search: video.author }]" class="video-miniature-author">{{ video.by }}</a>
+    <a [routerLink]="['/videos/list', { field: 'author', search: video.author, sort: currentSort }]" class="video-miniature-author">{{ video.by }}</a>
     <span class="video-miniature-created-date">{{ video.createdDate | date:'short' }}</span>
   </div>
 </div>
index 90645d67ff36c79cc3cff257a2f64a7b59e00e22..84bab950e450d295b69dd74ca3728fcd3076a9ad 100644 (file)
@@ -2,7 +2,7 @@ import { DatePipe } from '@angular/common';
 import { Component, Input, Output, EventEmitter } from '@angular/core';
 import { ROUTER_DIRECTIVES } from '@angular/router';
 
-import { Video, VideoService } from '../shared';
+import { SortField, Video, VideoService } from '../shared';
 import { User } from '../../shared';
 
 @Component({
@@ -16,6 +16,7 @@ import { User } from '../../shared';
 export class VideoMiniatureComponent {
   @Output() removed = new EventEmitter<any>();
 
+  @Input() currentSort: SortField;
   @Input() user: User;
   @Input() video: Video;
 
index c7f61902c1ea3c7877c3b953b1143eac949e5dd8..79f889c3dd921b40667e1a9c5f9ca940487ffff1 100644 (file)
@@ -45,6 +45,7 @@
     "src/app/shared/users/index.ts",
     "src/app/shared/users/token.model.ts",
     "src/app/shared/users/user.model.ts",
+    "src/app/shared/videos-params.ts",
     "src/app/videos/index.ts",
     "src/app/videos/shared/index.ts",
     "src/app/videos/shared/loader/index.ts",