PeerTube/client/angular/videos/components/list/videos-list.component.ts

85 lines
2.4 KiB
TypeScript
Raw Normal View History

2016-05-13 12:18:37 +00:00
import { Component, OnInit } from '@angular/core';
2016-05-23 09:07:42 +00:00
import { ROUTER_DIRECTIVES, RouteParams, Router } from '@angular/router-deprecated';
2016-03-14 12:50:19 +00:00
2016-05-22 08:43:06 +00:00
import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
import { AuthService } from '../../../users/services/auth.service';
2016-05-22 08:43:06 +00:00
import { Pagination } from '../../pagination';
import { User } from '../../../users/models/user';
import { VideosService } from '../../videos.service';
import { Video } from '../../video';
import { VideoMiniatureComponent } from './video-miniature.component';
import { Search, SearchField } from '../../../app/search';
2016-05-23 09:07:42 +00:00
import { VideoSortComponent } from './video-sort.component';
import { SortField } from './sort';
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-list',
styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ],
templateUrl: 'app/angular/videos/components/list/videos-list.component.html',
2016-05-23 09:07:42 +00:00
directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ]
2016-03-14 12:50:19 +00:00
})
export class VideosListComponent implements OnInit {
user: User = null;
2016-04-28 17:42:57 +00:00
videos: Video[] = [];
2016-05-22 08:43:06 +00:00
pagination: Pagination = {
currentPage: 1,
itemsPerPage: 9,
total: 0
2016-05-23 10:15:03 +00:00
};
2016-05-23 09:07:42 +00:00
sort: SortField;
2016-03-14 12:50:19 +00:00
private search: Search;
2016-03-14 21:16:43 +00:00
2016-03-14 12:50:19 +00:00
constructor(
private _authService: AuthService,
2016-03-14 21:16:43 +00:00
private _videosService: VideosService,
2016-05-23 09:07:42 +00:00
private _routeParams: RouteParams,
private _router: Router
2016-03-14 21:16:43 +00:00
) {
this.search = {
value: this._routeParams.get('search'),
field: <SearchField>this._routeParams.get('field')
2016-05-23 10:15:03 +00:00
};
2016-05-23 09:07:42 +00:00
this.sort = <SortField>this._routeParams.get('sort') || '-createdDate';
2016-03-14 21:16:43 +00:00
}
2016-03-14 12:50:19 +00:00
ngOnInit() {
if (this._authService.isLoggedIn()) {
this.user = User.load();
}
2016-03-14 12:50:19 +00:00
this.getVideos();
}
getVideos() {
2016-03-14 21:16:43 +00:00
let observable = null;
if (this.search.value !== null) {
2016-05-23 09:07:42 +00:00
observable = this._videosService.searchVideos(this.search, this.pagination, this.sort);
2016-03-14 21:16:43 +00:00
} else {
2016-05-23 09:07:42 +00:00
observable = this._videosService.getVideos(this.pagination, this.sort);
2016-03-14 21:16:43 +00:00
}
observable.subscribe(
2016-05-22 08:43:06 +00:00
({ videos, totalVideos }) => {
this.videos = videos;
this.pagination.total = totalVideos;
},
2016-03-14 12:50:19 +00:00
error => alert(error)
);
}
onRemoved(video: Video): void {
this.videos.splice(this.videos.indexOf(video), 1);
2016-03-14 12:50:19 +00:00
}
2016-05-23 09:07:42 +00:00
onSort(sort: SortField) {
this.sort = sort;
this._router.navigate(['VideosList', { sort: this.sort }]);
this.getVideos();
}
2016-03-14 12:50:19 +00:00
}