]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/angular/videos/components/list/videos-list.component.ts
341afdaa683975765070aa3fe80d661ddc52666d
[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 } 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
13 @Component({
14 selector: 'my-videos-list',
15 styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ],
16 templateUrl: 'app/angular/videos/components/list/videos-list.component.html',
17 directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent ]
18 })
19
20 export class VideosListComponent implements OnInit {
21 user: User = null;
22 videos: Video[] = [];
23 pagination: Pagination = {
24 currentPage: 1,
25 itemsPerPage: 9,
26 total: 0
27 }
28
29 private search: string;
30
31 constructor(
32 private _authService: AuthService,
33 private _videosService: VideosService,
34 private _routeParams: RouteParams
35 ) {
36 this.search = this._routeParams.get('search');
37 }
38
39 ngOnInit() {
40 if (this._authService.isLoggedIn()) {
41 this.user = User.load();
42 }
43
44 this.getVideos();
45 }
46
47 getVideos() {
48 let observable = null;
49
50 if (this.search !== null) {
51 observable = this._videosService.searchVideos(this.search, this.pagination);
52 } else {
53 observable = this._videosService.getVideos(this.pagination);
54 }
55
56 observable.subscribe(
57 ({ videos, totalVideos }) => {
58 this.videos = videos;
59 this.pagination.total = totalVideos;
60 },
61 error => alert(error)
62 );
63 }
64
65 onRemoved(video: Video): void {
66 this.videos.splice(this.videos.indexOf(video), 1);
67 }
68
69 }