]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/components/list/videos-list.component.ts
Lint the client
[github/Chocobozzz/PeerTube.git] / client / angular / videos / components / list / videos-list.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { ROUTER_DIRECTIVES, RouteParams, Router } from '@angular/router-deprecated';
3
4 import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
5
6 import { AuthService } from '../../../users/services/auth.service';
7 import { Pagination } from '../../pagination';
8 import { User } from '../../../users/models/user';
9 import { VideosService } from '../../videos.service';
10 import { Video } from '../../video';
11 import { VideoMiniatureComponent } from './video-miniature.component';
12 import { Search, SearchField } from '../../../app/search';
13 import { VideoSortComponent } from './video-sort.component';
14 import { SortField } from './sort';
15
16 @Component({
17 selector: 'my-videos-list',
18 styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ],
19 templateUrl: 'app/angular/videos/components/list/videos-list.component.html',
20 directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ]
21 })
22
23 export class VideosListComponent implements OnInit {
24 user: User = null;
25 videos: Video[] = [];
26 pagination: Pagination = {
27 currentPage: 1,
28 itemsPerPage: 9,
29 total: 0
30 };
31 sort: SortField;
32
33 private search: Search;
34
35 constructor(
36 private _authService: AuthService,
37 private _videosService: VideosService,
38 private _routeParams: RouteParams,
39 private _router: Router
40 ) {
41 this.search = {
42 value: this._routeParams.get('search'),
43 field: <SearchField>this._routeParams.get('field')
44 };
45
46 this.sort = <SortField>this._routeParams.get('sort') || '-createdDate';
47 }
48
49 ngOnInit() {
50 if (this._authService.isLoggedIn()) {
51 this.user = User.load();
52 }
53
54 this.getVideos();
55 }
56
57 getVideos() {
58 let observable = null;
59
60 if (this.search.value !== null) {
61 observable = this._videosService.searchVideos(this.search, this.pagination, this.sort);
62 } else {
63 observable = this._videosService.getVideos(this.pagination, this.sort);
64 }
65
66 observable.subscribe(
67 ({ videos, totalVideos }) => {
68 this.videos = videos;
69 this.pagination.total = totalVideos;
70 },
71 error => alert(error)
72 );
73 }
74
75 onRemoved(video: Video): void {
76 this.videos.splice(this.videos.indexOf(video), 1);
77 }
78
79 onSort(sort: SortField) {
80 this.sort = sort;
81 this._router.navigate(['VideosList', { sort: this.sort }]);
82 this.getVideos();
83 }
84 }