]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-list/video-list.component.ts
Implement header design
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-list / video-list.component.ts
CommitLineData
bcd9f81e 1import { Component, OnDestroy, OnInit } from '@angular/core'
df98563e 2import { ActivatedRoute, Router } from '@angular/router'
33c4972d 3import { Subscription } from 'rxjs/Subscription'
dc8bc31b 4
df98563e 5import { NotificationsService } from 'angular2-notifications'
7ddd02c9 6
fd45e8f4
C
7import { VideoService } from '../shared'
8import { Search, SearchField, SearchService } from '../../shared'
9import { AbstractVideoList } from './shared'
dc8bc31b
C
10
11@Component({
12 selector: 'my-videos-list',
fd45e8f4
C
13 styleUrls: [ './shared/abstract-video-list.scss' ],
14 templateUrl: './shared/abstract-video-list.html'
dc8bc31b 15})
fd45e8f4 16export class VideoListComponent extends AbstractVideoList implements OnInit, OnDestroy {
df98563e 17 private search: Search
33c4972d 18 private subSearch: Subscription
98b01bac 19
df98563e 20 constructor (
fd45e8f4
C
21 protected router: Router,
22 protected route: ActivatedRoute,
23 protected notificationsService: NotificationsService,
00a44645 24 private videoService: VideoService,
0629423c 25 private searchService: SearchService
fd45e8f4
C
26 ) {
27 super()
28 }
dc8bc31b 29
df98563e 30 ngOnInit () {
bddab65a
C
31 // Subscribe to route changes
32 this.subActivatedRoute = this.route.params.subscribe(routeParams => {
df98563e 33 this.loadRouteParams(routeParams)
00a44645 34
0629423c 35 // Update the search service component
df98563e
C
36 this.searchService.updateSearch.next(this.search)
37 this.getVideos()
38 })
00a44645 39
bddab65a
C
40 // Subscribe to search changes
41 this.subSearch = this.searchService.searchUpdated.subscribe(search => {
df98563e 42 this.search = search
7eef9535 43 // Reset pagination
df98563e 44 this.pagination.currentPage = 1
0629423c 45
df98563e
C
46 this.navigateToNewParams()
47 })
0629423c 48 }
00a44645 49
df98563e 50 ngOnDestroy () {
fd45e8f4
C
51 super.ngOnDestroy()
52
df98563e 53 this.subSearch.unsubscribe()
dc8bc31b
C
54 }
55
fd45e8f4 56 getVideosObservable () {
df98563e 57 let observable = null
00a44645 58 if (this.search.value) {
df98563e 59 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort)
98b01bac 60 } else {
df98563e 61 observable = this.videoService.getVideos(this.pagination, this.sort)
98b01bac
C
62 }
63
fd45e8f4 64 return observable
9bfe96e1
C
65 }
66
fd45e8f4
C
67 protected buildRouteParams () {
68 const params = super.buildRouteParams()
a99593ed 69
bddab65a 70 // Maybe there is a search
a99593ed 71 if (this.search.value) {
33c4972d
C
72 params['field'] = this.search.field
73 params['search'] = this.search.value
a99593ed
C
74 }
75
df98563e 76 return params
bddab65a
C
77 }
78
fd45e8f4
C
79 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
80 super.loadRouteParams(routeParams)
81
bddab65a
C
82 if (routeParams['search'] !== undefined) {
83 this.search = {
84 value: routeParams['search'],
df98563e
C
85 field: routeParams['field'] as SearchField
86 }
bddab65a
C
87 } else {
88 this.search = {
89 value: '',
90 field: 'name'
df98563e 91 }
bddab65a 92 }
00a44645 93 }
dc8bc31b 94}