]>
Commit | Line | Data |
---|---|---|
0629423c C |
1 | import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; |
2 | import { AsyncPipe } from '@angular/common'; | |
3 | import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router'; | |
4 | import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
dc8bc31b | 5 | |
32294074 C |
6 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; |
7 | ||
41a2aee3 C |
8 | import { |
9 | LoaderComponent, | |
10 | Pagination, | |
11 | SortField, | |
12 | Video, | |
13 | VideoService | |
4a6995be C |
14 | } from '../shared'; |
15 | import { AuthService, Search, SearchField, User } from '../../shared'; | |
501bc6c2 | 16 | import { VideoMiniatureComponent } from './video-miniature.component'; |
cf20596c | 17 | import { VideoSortComponent } from './video-sort.component'; |
00a44645 | 18 | import { SearchService } from '../../shared'; |
dc8bc31b C |
19 | |
20 | @Component({ | |
21 | selector: 'my-videos-list', | |
4a6995be | 22 | styles: [ require('./video-list.component.scss') ], |
0629423c | 23 | pipes: [ AsyncPipe ], |
4a6995be | 24 | template: require('./video-list.component.html'), |
f0a397ee | 25 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] |
dc8bc31b C |
26 | }) |
27 | ||
0629423c C |
28 | export class VideoListComponent implements OnInit, OnDestroy { |
29 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); | |
32294074 C |
30 | pagination: Pagination = { |
31 | currentPage: 1, | |
32 | itemsPerPage: 9, | |
0629423c | 33 | totalItems: null |
aff038cd | 34 | }; |
cf20596c | 35 | sort: SortField; |
4fd8aa32 C |
36 | user: User = null; |
37 | videos: Video[] = []; | |
dc8bc31b | 38 | |
471bc22f | 39 | private search: Search; |
bddab65a C |
40 | private subActivatedRoute: any; |
41 | private subSearch: any; | |
98b01bac | 42 | |
dc8bc31b | 43 | constructor( |
ccf6ed16 | 44 | private authService: AuthService, |
0629423c | 45 | private changeDetector: ChangeDetectorRef, |
4fd8aa32 | 46 | private router: Router, |
0629423c | 47 | private route: ActivatedRoute, |
00a44645 | 48 | private videoService: VideoService, |
0629423c | 49 | private searchService: SearchService |
00a44645 | 50 | ) {} |
dc8bc31b C |
51 | |
52 | ngOnInit() { | |
bddab65a C |
53 | if (this.authService.isLoggedIn()) { |
54 | this.user = User.load(); | |
55 | } | |
1553e15d | 56 | |
bddab65a C |
57 | // Subscribe to route changes |
58 | this.subActivatedRoute = this.route.params.subscribe(routeParams => { | |
59 | this.loadRouteParams(routeParams); | |
00a44645 | 60 | |
0629423c | 61 | // Update the search service component |
bddab65a C |
62 | this.searchService.updateSearch.next(this.search); |
63 | this.getVideos(); | |
64 | }); | |
00a44645 | 65 | |
bddab65a C |
66 | // Subscribe to search changes |
67 | this.subSearch = this.searchService.searchUpdated.subscribe(search => { | |
68 | this.search = search; | |
0629423c | 69 | |
bddab65a | 70 | this.navigateToNewParams(); |
0629423c C |
71 | }); |
72 | } | |
00a44645 | 73 | |
0629423c | 74 | ngOnDestroy() { |
bddab65a C |
75 | this.subActivatedRoute.unsubscribe(); |
76 | this.subSearch.unsubscribe(); | |
dc8bc31b C |
77 | } |
78 | ||
0629423c C |
79 | getVideos(detectChanges = true) { |
80 | this.loading.next(true); | |
157cb9c9 | 81 | this.videos = []; |
0629423c C |
82 | |
83 | this.changeDetector.detectChanges(); | |
157cb9c9 | 84 | |
98b01bac C |
85 | let observable = null; |
86 | ||
00a44645 | 87 | if (this.search.value) { |
ccf6ed16 | 88 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); |
98b01bac | 89 | } else { |
ccf6ed16 | 90 | observable = this.videoService.getVideos(this.pagination, this.sort); |
98b01bac C |
91 | } |
92 | ||
93 | observable.subscribe( | |
32294074 C |
94 | ({ videos, totalVideos }) => { |
95 | this.videos = videos; | |
0629423c | 96 | this.pagination.totalItems = totalVideos; |
4fd8aa32 | 97 | |
0629423c | 98 | this.loading.next(false); |
32294074 | 99 | }, |
dc8bc31b C |
100 | error => alert(error) |
101 | ); | |
102 | } | |
103 | ||
bddab65a C |
104 | isThereNoVideo() { |
105 | return !this.loading.getValue() && this.videos.length === 0; | |
106 | } | |
107 | ||
108 | onPageChanged(event: any) { | |
109 | // Be sure the current page is set | |
110 | this.pagination.currentPage = event.page; | |
111 | ||
112 | this.navigateToNewParams(); | |
9bfe96e1 C |
113 | } |
114 | ||
ccf6ed16 | 115 | onRemoved(video: Video) { |
bddab65a | 116 | this.getVideos(); |
dc8bc31b C |
117 | } |
118 | ||
cf20596c C |
119 | onSort(sort: SortField) { |
120 | this.sort = sort; | |
a99593ed | 121 | |
bddab65a C |
122 | this.navigateToNewParams(); |
123 | } | |
124 | ||
125 | private buildRouteParams() { | |
126 | // There is always a sort and a current page | |
a99593ed | 127 | const params: any = { |
bddab65a C |
128 | sort: this.sort, |
129 | page: this.pagination.currentPage | |
a99593ed C |
130 | }; |
131 | ||
bddab65a | 132 | // Maybe there is a search |
a99593ed | 133 | if (this.search.value) { |
a99593ed | 134 | params.field = this.search.field; |
4fd8aa32 | 135 | params.search = this.search.value; |
a99593ed C |
136 | } |
137 | ||
bddab65a C |
138 | return params; |
139 | } | |
140 | ||
141 | private loadRouteParams(routeParams) { | |
142 | if (routeParams['search'] !== undefined) { | |
143 | this.search = { | |
144 | value: routeParams['search'], | |
145 | field: <SearchField>routeParams['field'] | |
146 | }; | |
147 | } else { | |
148 | this.search = { | |
149 | value: '', | |
150 | field: 'name' | |
151 | }; | |
152 | } | |
153 | ||
154 | this.sort = <SortField>routeParams['sort'] || '-createdDate'; | |
155 | ||
156 | this.pagination.currentPage = parseInt(routeParams['page']) || 1; | |
157 | ||
158 | this.changeDetector.detectChanges(); | |
159 | } | |
160 | ||
161 | private navigateToNewParams() { | |
162 | const routeParams = this.buildRouteParams(); | |
163 | this.router.navigate(['/videos/list', routeParams]); | |
00a44645 | 164 | } |
dc8bc31b | 165 | } |