]>
Commit | Line | Data |
---|---|---|
230809ef C |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated'; | |
dc8bc31b | 3 | |
32294074 C |
4 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; |
5 | ||
1553e15d | 6 | import { AuthService } from '../../../users/services/auth.service'; |
32294074 | 7 | import { Pagination } from '../../pagination'; |
1553e15d | 8 | import { User } from '../../../users/models/user'; |
501bc6c2 C |
9 | import { VideosService } from '../../videos.service'; |
10 | import { Video } from '../../video'; | |
11 | import { VideoMiniatureComponent } from './video-miniature.component'; | |
471bc22f | 12 | import { Search, SearchField } from '../../../app/search'; |
dc8bc31b C |
13 | |
14 | @Component({ | |
15 | selector: 'my-videos-list', | |
16 | styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ], | |
17 | templateUrl: 'app/angular/videos/components/list/videos-list.component.html', | |
32294074 | 18 | directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent ] |
dc8bc31b C |
19 | }) |
20 | ||
21 | export class VideosListComponent implements OnInit { | |
1553e15d | 22 | user: User = null; |
d908a155 | 23 | videos: Video[] = []; |
32294074 C |
24 | pagination: Pagination = { |
25 | currentPage: 1, | |
26 | itemsPerPage: 9, | |
27 | total: 0 | |
28 | } | |
dc8bc31b | 29 | |
471bc22f | 30 | private search: Search; |
98b01bac | 31 | |
dc8bc31b | 32 | constructor( |
1553e15d | 33 | private _authService: AuthService, |
98b01bac | 34 | private _videosService: VideosService, |
32294074 | 35 | private _routeParams: RouteParams |
98b01bac | 36 | ) { |
471bc22f C |
37 | this.search = { |
38 | value: this._routeParams.get('search'), | |
39 | field: <SearchField>this._routeParams.get('field') | |
40 | } | |
98b01bac | 41 | } |
dc8bc31b C |
42 | |
43 | ngOnInit() { | |
1553e15d C |
44 | if (this._authService.isLoggedIn()) { |
45 | this.user = User.load(); | |
46 | } | |
47 | ||
dc8bc31b C |
48 | this.getVideos(); |
49 | } | |
50 | ||
51 | getVideos() { | |
98b01bac C |
52 | let observable = null; |
53 | ||
471bc22f | 54 | if (this.search.value !== null) { |
32294074 | 55 | observable = this._videosService.searchVideos(this.search, this.pagination); |
98b01bac | 56 | } else { |
32294074 | 57 | observable = this._videosService.getVideos(this.pagination); |
98b01bac C |
58 | } |
59 | ||
60 | observable.subscribe( | |
32294074 C |
61 | ({ videos, totalVideos }) => { |
62 | this.videos = videos; | |
63 | this.pagination.total = totalVideos; | |
64 | }, | |
dc8bc31b C |
65 | error => alert(error) |
66 | ); | |
67 | } | |
68 | ||
501bc6c2 C |
69 | onRemoved(video: Video): void { |
70 | this.videos.splice(this.videos.indexOf(video), 1); | |
dc8bc31b C |
71 | } |
72 | ||
73 | } |