aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/video-list/video-list.component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/videos/video-list/video-list.component.ts')
-rw-r--r--client/src/app/videos/video-list/video-list.component.ts105
1 files changed, 105 insertions, 0 deletions
diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts
new file mode 100644
index 000000000..b1ce55845
--- /dev/null
+++ b/client/src/app/videos/video-list/video-list.component.ts
@@ -0,0 +1,105 @@
1import { Component, OnInit } from '@angular/core';
2import { Router, ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated';
3
4import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
5
6import {
7 LoaderComponent,
8 Pagination,
9 SortField,
10 Video,
11 VideoService
12} from '../shared';
13import { AuthService, Search, SearchField, User } from '../../shared';
14import { VideoMiniatureComponent } from './video-miniature.component';
15import { VideoSortComponent } from './video-sort.component';
16
17@Component({
18 selector: 'my-videos-list',
19 styles: [ require('./video-list.component.scss') ],
20 template: require('./video-list.component.html'),
21 directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ]
22})
23
24export class VideoListComponent implements OnInit {
25 loading = false;
26 pagination: Pagination = {
27 currentPage: 1,
28 itemsPerPage: 9,
29 total: 0
30 };
31 sort: SortField;
32 user: User = null;
33 videos: Video[] = [];
34
35 private search: Search;
36
37 constructor(
38 private authService: AuthService,
39 private router: Router,
40 private routeParams: RouteParams,
41 private videoService: VideoService
42 ) {
43 this.search = {
44 value: this.routeParams.get('search'),
45 field: <SearchField>this.routeParams.get('field')
46 };
47
48 this.sort = <SortField>this.routeParams.get('sort') || '-createdDate';
49 }
50
51 ngOnInit() {
52 if (this.authService.isLoggedIn()) {
53 this.user = User.load();
54 }
55
56 this.getVideos();
57 }
58
59 getVideos() {
60 this.loading = true;
61 this.videos = [];
62
63 let observable = null;
64
65 if (this.search.value !== null) {
66 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
67 } else {
68 observable = this.videoService.getVideos(this.pagination, this.sort);
69 }
70
71 observable.subscribe(
72 ({ videos, totalVideos }) => {
73 this.videos = videos;
74 this.pagination.total = totalVideos;
75
76 this.loading = false;
77 },
78 error => alert(error)
79 );
80 }
81
82 noVideo() {
83 return !this.loading && this.videos.length === 0;
84 }
85
86 onRemoved(video: Video) {
87 this.videos.splice(this.videos.indexOf(video), 1);
88 }
89
90 onSort(sort: SortField) {
91 this.sort = sort;
92
93 const params: any = {
94 sort: this.sort
95 };
96
97 if (this.search.value) {
98 params.field = this.search.field;
99 params.search = this.search.value;
100 }
101
102 this.router.navigate(['VideosList', params]);
103 this.getVideos();
104 }
105}