X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fvideos%2Fvideo-list%2Fvideo-list.component.ts;h=7c6d4b992d387463136502c05b85a81e51fe73c9;hb=def16d33d19153c6583fa8a30634760b3d64d34c;hp=46263eb659a96d9e217f8d69a32fc9db58a7b708;hpb=00a446454d4721fc49517815655f6b4f8a17b554;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index 46263eb65..7c6d4b992 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts @@ -1,5 +1,7 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ROUTER_DIRECTIVES, RouteSegment } from '@angular/router'; +import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; +import { AsyncPipe } from '@angular/common'; +import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router'; +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; @@ -10,7 +12,7 @@ import { Video, VideoService } from '../shared'; -import { AuthService, Search, SearchField, User } from '../../shared'; +import { AuthService, AuthUser, Search, SearchField } from '../../shared'; import { VideoMiniatureComponent } from './video-miniature.component'; import { VideoSortComponent } from './video-sort.component'; import { SearchService } from '../../shared'; @@ -18,53 +20,70 @@ import { SearchService } from '../../shared'; @Component({ selector: 'my-videos-list', styles: [ require('./video-list.component.scss') ], + pipes: [ AsyncPipe ], template: require('./video-list.component.html'), directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] }) -export class VideoListComponent implements OnInit { - loading = false; +export class VideoListComponent implements OnInit, OnDestroy { + loading: BehaviorSubject = new BehaviorSubject(false); pagination: Pagination = { currentPage: 1, itemsPerPage: 9, - total: 0 + totalItems: null }; sort: SortField; - user: User = null; + user: AuthUser = null; videos: Video[] = []; private search: Search; + private subActivatedRoute: any; + private subSearch: any; constructor( private authService: AuthService, + private changeDetector: ChangeDetectorRef, private router: Router, - private routeSegment: RouteSegment, + private route: ActivatedRoute, private videoService: VideoService, - private searchService: SearchService // Temporary + private searchService: SearchService ) {} ngOnInit() { if (this.authService.isLoggedIn()) { - this.user = User.load(); + this.user = AuthUser.load(); } - this.search = { - value: this.routeSegment.getParam('search'), - field: this.routeSegment.getParam('field') - }; + // Subscribe to route changes + this.subActivatedRoute = this.route.params.subscribe(routeParams => { + this.loadRouteParams(routeParams); - // Temporary - this.searchChanged(this.search); + // Update the search service component + this.searchService.updateSearch.next(this.search); + this.getVideos(); + }); - this.sort = this.routeSegment.getParam('sort') || '-createdDate'; + // Subscribe to search changes + this.subSearch = this.searchService.searchUpdated.subscribe(search => { + this.search = search; + // Reset pagination + this.pagination.currentPage = 1; - this.getVideos(); + this.navigateToNewParams(); + }); + } + + ngOnDestroy() { + this.subActivatedRoute.unsubscribe(); + this.subSearch.unsubscribe(); } getVideos() { - this.loading = true; + this.loading.next(true); this.videos = []; + this.changeDetector.detectChanges(); + let observable = null; if (this.search.value) { @@ -76,16 +95,23 @@ export class VideoListComponent implements OnInit { observable.subscribe( ({ videos, totalVideos }) => { this.videos = videos; - this.pagination.total = totalVideos; + this.pagination.totalItems = totalVideos; - this.loading = false; + this.loading.next(false); }, error => alert(error) ); } - 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) { @@ -95,19 +121,51 @@ export class VideoListComponent implements OnInit { 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: routeParams['field'] + }; + } else { + this.search = { + value: '', + field: 'name' + }; + } + + this.sort = routeParams['sort'] || '-createdDate'; + + if (routeParams['page'] !== undefined) { + this.pagination.currentPage = parseInt(routeParams['page']); + } else { + this.pagination.currentPage = 1; + } + + this.changeDetector.detectChanges(); } - searchChanged(search: Search) { - this.searchService.searchChanged.next(search); + private navigateToNewParams() { + const routeParams = this.buildRouteParams(); + this.router.navigate(['/videos/list', routeParams]); } }