]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-list/video-list.component.ts
46263eb659a96d9e217f8d69a32fc9db58a7b708
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-list / video-list.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { Router, ROUTER_DIRECTIVES, RouteSegment } from '@angular/router';
3
4 import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
5
6 import {
7 LoaderComponent,
8 Pagination,
9 SortField,
10 Video,
11 VideoService
12 } from '../shared';
13 import { AuthService, Search, SearchField, User } from '../../shared';
14 import { VideoMiniatureComponent } from './video-miniature.component';
15 import { VideoSortComponent } from './video-sort.component';
16 import { SearchService } from '../../shared';
17
18 @Component({
19 selector: 'my-videos-list',
20 styles: [ require('./video-list.component.scss') ],
21 template: require('./video-list.component.html'),
22 directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ]
23 })
24
25 export class VideoListComponent implements OnInit {
26 loading = false;
27 pagination: Pagination = {
28 currentPage: 1,
29 itemsPerPage: 9,
30 total: 0
31 };
32 sort: SortField;
33 user: User = null;
34 videos: Video[] = [];
35
36 private search: Search;
37
38 constructor(
39 private authService: AuthService,
40 private router: Router,
41 private routeSegment: RouteSegment,
42 private videoService: VideoService,
43 private searchService: SearchService // Temporary
44 ) {}
45
46 ngOnInit() {
47 if (this.authService.isLoggedIn()) {
48 this.user = User.load();
49 }
50
51 this.search = {
52 value: this.routeSegment.getParam('search'),
53 field: <SearchField>this.routeSegment.getParam('field')
54 };
55
56 // Temporary
57 this.searchChanged(this.search);
58
59 this.sort = <SortField>this.routeSegment.getParam('sort') || '-createdDate';
60
61 this.getVideos();
62 }
63
64 getVideos() {
65 this.loading = true;
66 this.videos = [];
67
68 let observable = null;
69
70 if (this.search.value) {
71 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
72 } else {
73 observable = this.videoService.getVideos(this.pagination, this.sort);
74 }
75
76 observable.subscribe(
77 ({ videos, totalVideos }) => {
78 this.videos = videos;
79 this.pagination.total = totalVideos;
80
81 this.loading = false;
82 },
83 error => alert(error)
84 );
85 }
86
87 noVideo() {
88 return !this.loading && this.videos.length === 0;
89 }
90
91 onRemoved(video: Video) {
92 this.getVideos();
93 }
94
95 onSort(sort: SortField) {
96 this.sort = sort;
97
98 const params: any = {
99 sort: this.sort
100 };
101
102 if (this.search.value) {
103 params.field = this.search.field;
104 params.search = this.search.value;
105 }
106
107 this.router.navigate(['/videos/list', params]);
108 }
109
110 searchChanged(search: Search) {
111 this.searchService.searchChanged.next(search);
112 }
113 }