]>
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', | |
157cb9c9 | 22 | directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent, LoaderComponent ] |
dc8bc31b C |
23 | }) |
24 | ||
41a2aee3 | 25 | export class VideoListComponent implements OnInit { |
1553e15d | 26 | user: User = null; |
d908a155 | 27 | videos: Video[] = []; |
32294074 C |
28 | pagination: Pagination = { |
29 | currentPage: 1, | |
30 | itemsPerPage: 9, | |
31 | total: 0 | |
aff038cd | 32 | }; |
cf20596c | 33 | sort: SortField; |
ccf6ed16 | 34 | loading = false; |
dc8bc31b | 35 | |
471bc22f | 36 | private search: Search; |
98b01bac | 37 | |
dc8bc31b | 38 | constructor( |
ccf6ed16 C |
39 | private authService: AuthService, |
40 | private videoService: VideoService, | |
41 | private routeParams: RouteParams, | |
42 | private router: Router | |
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; | |
157cb9c9 | 76 | this.loading = false; |
32294074 | 77 | }, |
dc8bc31b C |
78 | error => alert(error) |
79 | ); | |
80 | } | |
81 | ||
ccf6ed16 | 82 | onRemoved(video: Video) { |
501bc6c2 | 83 | this.videos.splice(this.videos.indexOf(video), 1); |
dc8bc31b C |
84 | } |
85 | ||
cf20596c C |
86 | onSort(sort: SortField) { |
87 | this.sort = sort; | |
a99593ed C |
88 | |
89 | const params: any = { | |
90 | sort: this.sort | |
91 | }; | |
92 | ||
93 | if (this.search.value) { | |
94 | params.search = this.search.value; | |
95 | params.field = this.search.field; | |
96 | } | |
97 | ||
ccf6ed16 | 98 | this.router.navigate(['VideosList', params]); |
cf20596c C |
99 | this.getVideos(); |
100 | } | |
dc8bc31b | 101 | } |