PeerTube/client/app/videos/video-list/video-list.component.ts

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2016-05-13 07:18:37 -05:00
import { Component, OnInit } from '@angular/core';
import { Router, ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated';
2016-03-14 07:50:19 -05:00
2016-05-22 03:43:06 -05:00
import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
import {
LoaderComponent,
Pagination,
SortField,
Video,
VideoService
} from '../shared/index';
import { Search, SearchField } from '../../shared/index';
import { AuthService, User } from '../../users/index';
import { VideoMiniatureComponent } from './video-miniature.component';
2016-05-23 04:07:42 -05:00
import { VideoSortComponent } from './video-sort.component';
2016-03-14 07:50:19 -05:00
@Component({
selector: 'my-videos-list',
styleUrls: [ 'client/app/videos/video-list/video-list.component.css' ],
templateUrl: 'client/app/videos/video-list/video-list.component.html',
directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent, LoaderComponent ]
2016-03-14 07:50:19 -05:00
})
export class VideoListComponent implements OnInit {
2016-05-27 10:49:18 -05:00
loading = false;
2016-05-22 03:43:06 -05:00
pagination: Pagination = {
currentPage: 1,
itemsPerPage: 9,
total: 0
2016-05-23 05:15:03 -05:00
};
2016-05-23 04:07:42 -05:00
sort: SortField;
2016-05-27 10:49:18 -05:00
user: User = null;
videos: Video[] = [];
2016-03-14 07:50:19 -05:00
private search: Search;
2016-03-14 16:16:43 -05:00
2016-03-14 07:50:19 -05:00
constructor(
2016-05-27 10:25:52 -05:00
private authService: AuthService,
2016-05-27 10:49:18 -05:00
private router: Router,
2016-05-27 10:25:52 -05:00
private routeParams: RouteParams,
2016-05-27 10:49:18 -05:00
private videoService: VideoService
2016-03-14 16:16:43 -05:00
) {
this.search = {
2016-05-27 10:25:52 -05:00
value: this.routeParams.get('search'),
field: <SearchField>this.routeParams.get('field')
2016-05-23 05:15:03 -05:00
};
2016-05-23 04:07:42 -05:00
2016-05-27 10:25:52 -05:00
this.sort = <SortField>this.routeParams.get('sort') || '-createdDate';
2016-03-14 16:16:43 -05:00
}
2016-03-14 07:50:19 -05:00
ngOnInit() {
2016-05-27 10:25:52 -05:00
if (this.authService.isLoggedIn()) {
this.user = User.load();
}
2016-03-14 07:50:19 -05:00
this.getVideos();
}
getVideos() {
this.loading = true;
this.videos = [];
2016-03-14 16:16:43 -05:00
let observable = null;
if (this.search.value !== null) {
2016-05-27 10:25:52 -05:00
observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
2016-03-14 16:16:43 -05:00
} else {
2016-05-27 10:25:52 -05:00
observable = this.videoService.getVideos(this.pagination, this.sort);
2016-03-14 16:16:43 -05:00
}
observable.subscribe(
2016-05-22 03:43:06 -05:00
({ videos, totalVideos }) => {
this.videos = videos;
this.pagination.total = totalVideos;
2016-05-27 10:49:18 -05:00
this.loading = false;
2016-05-22 03:43:06 -05:00
},
2016-03-14 07:50:19 -05:00
error => alert(error)
);
}
2016-05-27 10:25:52 -05:00
onRemoved(video: Video) {
this.videos.splice(this.videos.indexOf(video), 1);
2016-03-14 07:50:19 -05:00
}
2016-05-23 04:07:42 -05:00
onSort(sort: SortField) {
this.sort = sort;
2016-05-24 16:00:58 -05:00
const params: any = {
sort: this.sort
};
if (this.search.value) {
params.field = this.search.field;
2016-05-27 10:49:18 -05:00
params.search = this.search.value;
2016-05-24 16:00:58 -05:00
}
2016-05-27 10:25:52 -05:00
this.router.navigate(['VideosList', params]);
2016-05-23 04:07:42 -05:00
this.getVideos();
}
2016-03-14 07:50:19 -05:00
}