]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-list/video-list.component.ts
Client: use templateUrl/styleUrls instead of require
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-list / video-list.component.ts
CommitLineData
0629423c 1import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
ab32b0fc 2import { ActivatedRoute, Router } from '@angular/router';
0629423c 3import { BehaviorSubject } from 'rxjs/BehaviorSubject';
dc8bc31b 4
41a2aee3 5import {
41a2aee3
C
6 SortField,
7 Video,
8 VideoService
4a6995be 9} from '../shared';
de59c48f 10import { AuthService, AuthUser, RestPagination, Search, SearchField } from '../../shared';
00a44645 11import { SearchService } from '../../shared';
dc8bc31b
C
12
13@Component({
14 selector: 'my-videos-list',
ec8d8440
C
15 styleUrls: [ './video-list.component.scss' ],
16 templateUrl: './video-list.component.html'
dc8bc31b
C
17})
18
0629423c
C
19export class VideoListComponent implements OnInit, OnDestroy {
20 loading: BehaviorSubject<boolean> = new BehaviorSubject(false);
de59c48f 21 pagination: RestPagination = {
32294074
C
22 currentPage: 1,
23 itemsPerPage: 9,
0629423c 24 totalItems: null
aff038cd 25 };
cf20596c 26 sort: SortField;
7da18e44 27 user: AuthUser = null;
4fd8aa32 28 videos: Video[] = [];
dc8bc31b 29
471bc22f 30 private search: Search;
bddab65a
C
31 private subActivatedRoute: any;
32 private subSearch: any;
98b01bac 33
dc8bc31b 34 constructor(
ccf6ed16 35 private authService: AuthService,
0629423c 36 private changeDetector: ChangeDetectorRef,
4fd8aa32 37 private router: Router,
0629423c 38 private route: ActivatedRoute,
00a44645 39 private videoService: VideoService,
0629423c 40 private searchService: SearchService
00a44645 41 ) {}
dc8bc31b
C
42
43 ngOnInit() {
bddab65a 44 if (this.authService.isLoggedIn()) {
7da18e44 45 this.user = AuthUser.load();
bddab65a 46 }
1553e15d 47
bddab65a
C
48 // Subscribe to route changes
49 this.subActivatedRoute = this.route.params.subscribe(routeParams => {
50 this.loadRouteParams(routeParams);
00a44645 51
0629423c 52 // Update the search service component
bddab65a
C
53 this.searchService.updateSearch.next(this.search);
54 this.getVideos();
55 });
00a44645 56
bddab65a
C
57 // Subscribe to search changes
58 this.subSearch = this.searchService.searchUpdated.subscribe(search => {
59 this.search = search;
7eef9535
C
60 // Reset pagination
61 this.pagination.currentPage = 1;
0629423c 62
bddab65a 63 this.navigateToNewParams();
0629423c
C
64 });
65 }
00a44645 66
0629423c 67 ngOnDestroy() {
bddab65a
C
68 this.subActivatedRoute.unsubscribe();
69 this.subSearch.unsubscribe();
dc8bc31b
C
70 }
71
7eef9535 72 getVideos() {
0629423c 73 this.loading.next(true);
157cb9c9 74 this.videos = [];
0629423c
C
75
76 this.changeDetector.detectChanges();
157cb9c9 77
98b01bac
C
78 let observable = null;
79
00a44645 80 if (this.search.value) {
ccf6ed16 81 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
98b01bac 82 } else {
ccf6ed16 83 observable = this.videoService.getVideos(this.pagination, this.sort);
98b01bac
C
84 }
85
86 observable.subscribe(
32294074
C
87 ({ videos, totalVideos }) => {
88 this.videos = videos;
0629423c 89 this.pagination.totalItems = totalVideos;
4fd8aa32 90
0629423c 91 this.loading.next(false);
32294074 92 },
bf68dd75 93 error => alert(error.text)
dc8bc31b
C
94 );
95 }
96
bddab65a
C
97 isThereNoVideo() {
98 return !this.loading.getValue() && this.videos.length === 0;
99 }
100
101 onPageChanged(event: any) {
102 // Be sure the current page is set
103 this.pagination.currentPage = event.page;
104
105 this.navigateToNewParams();
9bfe96e1
C
106 }
107
ccf6ed16 108 onRemoved(video: Video) {
bddab65a 109 this.getVideos();
dc8bc31b
C
110 }
111
cf20596c
C
112 onSort(sort: SortField) {
113 this.sort = sort;
a99593ed 114
bddab65a
C
115 this.navigateToNewParams();
116 }
117
118 private buildRouteParams() {
119 // There is always a sort and a current page
a99593ed 120 const params: any = {
bddab65a
C
121 sort: this.sort,
122 page: this.pagination.currentPage
a99593ed
C
123 };
124
bddab65a 125 // Maybe there is a search
a99593ed 126 if (this.search.value) {
a99593ed 127 params.field = this.search.field;
4fd8aa32 128 params.search = this.search.value;
a99593ed
C
129 }
130
bddab65a
C
131 return params;
132 }
133
134 private loadRouteParams(routeParams) {
135 if (routeParams['search'] !== undefined) {
136 this.search = {
137 value: routeParams['search'],
138 field: <SearchField>routeParams['field']
139 };
140 } else {
141 this.search = {
142 value: '',
143 field: 'name'
144 };
145 }
146
147 this.sort = <SortField>routeParams['sort'] || '-createdDate';
148
7eef9535
C
149 if (routeParams['page'] !== undefined) {
150 this.pagination.currentPage = parseInt(routeParams['page']);
151 } else {
152 this.pagination.currentPage = 1;
153 }
bddab65a
C
154
155 this.changeDetector.detectChanges();
156 }
157
158 private navigateToNewParams() {
159 const routeParams = this.buildRouteParams();
160 this.router.navigate(['/videos/list', routeParams]);
00a44645 161 }
dc8bc31b 162}