aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/video-list/video-list.component.ts
blob: 784162679320dd48b04833e5ed2e2f40584d6154 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Component, OnDestroy, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { Subscription } from 'rxjs/Subscription'

import { NotificationsService } from 'angular2-notifications'

import { VideoService } from '../shared'
import { Search, SearchField, SearchService } from '../../shared'
import { AbstractVideoList } from './shared'

@Component({
  selector: 'my-videos-list',
  styleUrls: [ './shared/abstract-video-list.scss' ],
  templateUrl: './shared/abstract-video-list.html'
})
export class VideoListComponent extends AbstractVideoList implements OnInit, OnDestroy {
  private search: Search
  private subSearch: Subscription

  constructor (
    protected router: Router,
    protected route: ActivatedRoute,
    protected notificationsService: NotificationsService,
    private videoService: VideoService,
    private searchService: SearchService
  ) {
    super()
  }

  ngOnInit () {
    // Subscribe to route changes
    this.subActivatedRoute = this.route.params.subscribe(routeParams => {
      this.loadRouteParams(routeParams)

      // Update the search service component
      this.searchService.updateSearch.next(this.search)
      this.getVideos()
    })

    // Subscribe to search changes
    this.subSearch = this.searchService.searchUpdated.subscribe(search => {
      this.search = search
      // Reset pagination
      this.pagination.currentPage = 1

      this.navigateToNewParams()
    })
  }

  ngOnDestroy () {
    super.ngOnDestroy()

    this.subSearch.unsubscribe()
  }

  getVideosObservable () {
    let observable = null
    if (this.search.value) {
      observable = this.videoService.searchVideos(this.search, this.pagination, this.sort)
    } else {
      observable = this.videoService.getVideos(this.pagination, this.sort)
    }

    return observable
  }

  protected buildRouteParams () {
    const params = super.buildRouteParams()

    // Maybe there is a search
    if (this.search.value) {
      params['field'] = this.search.field
      params['search'] = this.search.value
    }

    return params
  }

  protected loadRouteParams (routeParams: { [ key: string ]: any }) {
    super.loadRouteParams(routeParams)

    if (routeParams['search'] !== undefined) {
      this.search = {
        value: routeParams['search'],
        field: routeParams['field'] as SearchField
      }
    } else {
      this.search = {
        value: '',
        field: 'name'
      }
    }
  }
}