aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/search/search.component.ts
blob: 6e2827fe3a8b9f1b62ee6c865dfadb0f0a43eead (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
import { Component, OnInit } from '@angular/core'
import { Router } from '@angular/router'

import { Search } from './search.model'
import { SearchField } from './search-field.type'
import { SearchService } from './search.service'

@Component({
  selector: 'my-search',
  templateUrl: './search.component.html',
  styleUrls: [ './search.component.scss' ]
})

export class SearchComponent implements OnInit {
  fieldChoices = {
    name: 'Name',
    author: 'Author',
    host: 'Pod Host',
    tags: 'Tags'
  }
  searchCriteria: Search = {
    field: 'name',
    value: ''
  }

  constructor (private searchService: SearchService, private router: Router) {}

  ngOnInit () {
    // Subscribe if the search changed
    // Usually changed by videos list component
    this.searchService.updateSearch.subscribe(
      newSearchCriteria => {
        // Put a field by default
        if (!newSearchCriteria.field) {
          newSearchCriteria.field = 'name'
        }

        this.searchCriteria = newSearchCriteria
      }
    )
  }

  get choiceKeys () {
    return Object.keys(this.fieldChoices)
  }

  choose ($event: MouseEvent, choice: SearchField) {
    $event.preventDefault()
    $event.stopPropagation()

    this.searchCriteria.field = choice

    if (this.searchCriteria.value) {
      this.doSearch()
    }
  }

  doSearch () {
    if (this.router.url.indexOf('/videos/list') === -1) {
      this.router.navigate([ '/videos/list' ])
    }

    this.searchService.searchUpdated.next(this.searchCriteria)
  }

  getStringChoice (choiceKey: SearchField) {
    return this.fieldChoices[choiceKey]
  }
}