]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Component, OnInit } from '@angular/core'; |
41a2aee3 | 2 | import { Router, ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated'; |
dc8bc31b | 3 | |
32294074 C |
4 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; |
5 | ||
41a2aee3 C |
6 | import { |
7 | LoaderComponent, | |
8 | Pagination, | |
9 | SortField, | |
10 | Video, | |
11 | VideoService | |
12 | } from '../shared/index'; | |
13 | import { Search, SearchField } from '../../shared/index'; | |
14 | import { AuthService, User } from '../../users/index'; | |
501bc6c2 | 15 | import { VideoMiniatureComponent } from './video-miniature.component'; |
cf20596c | 16 | import { VideoSortComponent } from './video-sort.component'; |
dc8bc31b C |
17 | |
18 | @Component({ | |
19 | selector: 'my-videos-list', | |
41a2aee3 C |
20 | styleUrls: [ 'client/app/videos/video-list/video-list.component.css' ], |
21 | templateUrl: 'client/app/videos/video-list/video-list.component.html', | |
f0a397ee | 22 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] |
dc8bc31b C |
23 | }) |
24 | ||
41a2aee3 | 25 | export class VideoListComponent implements OnInit { |
4fd8aa32 | 26 | loading = false; |
32294074 C |
27 | pagination: Pagination = { |
28 | currentPage: 1, | |
29 | itemsPerPage: 9, | |
30 | total: 0 | |
aff038cd | 31 | }; |
cf20596c | 32 | sort: SortField; |
4fd8aa32 C |
33 | user: User = null; |
34 | videos: Video[] = []; | |
dc8bc31b | 35 | |
471bc22f | 36 | private search: Search; |
98b01bac | 37 | |
dc8bc31b | 38 | constructor( |
ccf6ed16 | 39 | private authService: AuthService, |
4fd8aa32 | 40 | private router: Router, |
ccf6ed16 | 41 | private routeParams: RouteParams, |
4fd8aa32 | 42 | private videoService: VideoService |
98b01bac | 43 | ) { |
471bc22f | 44 | this.search = { |
ccf6ed16 C |
45 | value: this.routeParams.get('search'), |
46 | field: <SearchField>this.routeParams.get('field') | |
aff038cd | 47 | }; |
cf20596c | 48 | |
ccf6ed16 | 49 | this.sort = <SortField>this.routeParams.get('sort') || '-createdDate'; |
98b01bac | 50 | } |
dc8bc31b C |
51 | |
52 | ngOnInit() { | |
ccf6ed16 | 53 | if (this.authService.isLoggedIn()) { |
1553e15d C |
54 | this.user = User.load(); |
55 | } | |
56 | ||
dc8bc31b C |
57 | this.getVideos(); |
58 | } | |
59 | ||
60 | getVideos() { | |
157cb9c9 C |
61 | this.loading = true; |
62 | this.videos = []; | |
63 | ||
98b01bac C |
64 | let observable = null; |
65 | ||
471bc22f | 66 | if (this.search.value !== null) { |
ccf6ed16 | 67 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); |
98b01bac | 68 | } else { |
ccf6ed16 | 69 | observable = this.videoService.getVideos(this.pagination, this.sort); |
98b01bac C |
70 | } |
71 | ||
72 | observable.subscribe( | |
32294074 C |
73 | ({ videos, totalVideos }) => { |
74 | this.videos = videos; | |
75 | this.pagination.total = totalVideos; | |
4fd8aa32 | 76 | |
157cb9c9 | 77 | this.loading = false; |
32294074 | 78 | }, |
dc8bc31b C |
79 | error => alert(error) |
80 | ); | |
81 | } | |
82 | ||
9bfe96e1 C |
83 | noVideo() { |
84 | return !this.loading && this.videos.length === 0; | |
85 | } | |
86 | ||
ccf6ed16 | 87 | onRemoved(video: Video) { |
501bc6c2 | 88 | this.videos.splice(this.videos.indexOf(video), 1); |
dc8bc31b C |
89 | } |
90 | ||
cf20596c C |
91 | onSort(sort: SortField) { |
92 | this.sort = sort; | |
a99593ed C |
93 | |
94 | const params: any = { | |
95 | sort: this.sort | |
96 | }; | |
97 | ||
98 | if (this.search.value) { | |
a99593ed | 99 | params.field = this.search.field; |
4fd8aa32 | 100 | params.search = this.search.value; |
a99593ed C |
101 | } |
102 | ||
ccf6ed16 | 103 | this.router.navigate(['VideosList', params]); |
cf20596c C |
104 | this.getVideos(); |
105 | } | |
dc8bc31b | 106 | } |