]>
Commit | Line | Data |
---|---|---|
00a44645 | 1 | import { Component, EventEmitter, Output, OnInit } from '@angular/core'; |
471bc22f C |
2 | |
3 | import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown'; | |
4 | ||
41a2aee3 C |
5 | import { Search } from './search.model'; |
6 | import { SearchField } from './search-field.type'; | |
00a44645 | 7 | import { SearchService } from './search.service'; // Temporary |
471bc22f C |
8 | |
9 | @Component({ | |
10 | selector: 'my-search', | |
4a6995be | 11 | template: require('./search.component.html'), |
471bc22f C |
12 | directives: [ DROPDOWN_DIRECTIVES ] |
13 | }) | |
14 | ||
00a44645 | 15 | export class SearchComponent implements OnInit { |
ccf6ed16 | 16 | @Output() search = new EventEmitter<Search>(); |
471bc22f | 17 | |
471bc22f | 18 | fieldChoices = { |
aff038cd C |
19 | name: 'Name', |
20 | author: 'Author', | |
21 | podUrl: 'Pod Url', | |
e822fdae C |
22 | magnetUri: 'Magnet Uri', |
23 | tags: 'Tags' | |
aff038cd | 24 | }; |
4fd8aa32 C |
25 | searchCriterias: Search = { |
26 | field: 'name', | |
27 | value: '' | |
28 | }; | |
471bc22f | 29 | |
00a44645 C |
30 | constructor(private searchService: SearchService) {} |
31 | ||
32 | ngOnInit() { | |
33 | this.searchService.searchChanged.subscribe( | |
34 | newSearchCriterias => { | |
35 | // Put a field by default | |
36 | if (!newSearchCriterias.field) { | |
37 | newSearchCriterias.field = 'name'; | |
38 | } | |
39 | ||
40 | this.searchCriterias = newSearchCriterias; | |
41 | } | |
42 | ); | |
43 | } | |
44 | ||
471bc22f C |
45 | get choiceKeys() { |
46 | return Object.keys(this.fieldChoices); | |
47 | } | |
48 | ||
ccf6ed16 | 49 | choose($event: MouseEvent, choice: SearchField) { |
471bc22f C |
50 | $event.preventDefault(); |
51 | $event.stopPropagation(); | |
52 | ||
53 | this.searchCriterias.field = choice; | |
00a44645 | 54 | this.doSearch(); |
471bc22f C |
55 | } |
56 | ||
ccf6ed16 | 57 | doSearch() { |
471bc22f C |
58 | this.search.emit(this.searchCriterias); |
59 | } | |
60 | ||
4fd8aa32 C |
61 | getStringChoice(choiceKey: SearchField) { |
62 | return this.fieldChoices[choiceKey]; | |
63 | } | |
471bc22f | 64 | } |