]>
Commit | Line | Data |
---|---|---|
2e46eb97 C |
1 | import * as debug from 'debug' |
2 | import { Subject } from 'rxjs' | |
3 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' | |
f676e0e3 | 4 | import { AfterViewInit, Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
2e46eb97 | 5 | import { ActivatedRoute, Params, Router } from '@angular/router' |
1fd61899 C |
6 | |
7 | export type AdvancedInputFilter = { | |
978c87e7 C |
8 | title: string |
9 | ||
10 | children: { | |
11 | label: string | |
12 | queryParams: Params | |
13 | }[] | |
1fd61899 C |
14 | } |
15 | ||
2e46eb97 C |
16 | const logger = debug('peertube:AdvancedInputFilterComponent') |
17 | ||
1fd61899 C |
18 | @Component({ |
19 | selector: 'my-advanced-input-filter', | |
20 | templateUrl: './advanced-input-filter.component.html', | |
21 | styleUrls: [ './advanced-input-filter.component.scss' ] | |
22 | }) | |
f676e0e3 | 23 | export class AdvancedInputFilterComponent implements OnInit, AfterViewInit { |
1fd61899 | 24 | @Input() filters: AdvancedInputFilter[] = [] |
dd24f1bb | 25 | @Input() emitOnInit = true |
1fd61899 | 26 | |
2e46eb97 C |
27 | @Output() search = new EventEmitter<string>() |
28 | ||
29 | searchValue: string | |
30 | ||
31 | private searchStream: Subject<string> | |
32 | ||
f676e0e3 C |
33 | private viewInitialized = false |
34 | private emitSearchAfterViewInit = false | |
35 | ||
2e46eb97 C |
36 | constructor ( |
37 | private route: ActivatedRoute, | |
38 | private router: Router | |
39 | ) { } | |
40 | ||
41 | ngOnInit () { | |
42 | this.initSearchStream() | |
43 | this.listenToRouteSearchChange() | |
44 | } | |
1fd61899 | 45 | |
f676e0e3 C |
46 | ngAfterViewInit () { |
47 | this.viewInitialized = true | |
48 | ||
49 | // Init after view init to not send an event too early | |
dd24f1bb | 50 | if (this.emitOnInit && this.emitSearchAfterViewInit) this.emitSearch() |
f676e0e3 C |
51 | } |
52 | ||
2e46eb97 | 53 | onInputSearch (event: Event) { |
f676e0e3 | 54 | this.scheduleSearchUpdate((event.target as HTMLInputElement).value) |
1fd61899 C |
55 | } |
56 | ||
57 | onResetTableFilter () { | |
f676e0e3 | 58 | this.immediateSearchUpdate('') |
2e46eb97 C |
59 | } |
60 | ||
61 | hasFilters () { | |
7e76cc38 | 62 | return this.filters && this.filters.length !== 0 |
2e46eb97 C |
63 | } |
64 | ||
f676e0e3 | 65 | private scheduleSearchUpdate (value: string) { |
2e46eb97 C |
66 | this.searchValue = value |
67 | this.searchStream.next(this.searchValue) | |
68 | } | |
69 | ||
f676e0e3 C |
70 | private immediateSearchUpdate (value: string) { |
71 | this.searchValue = value | |
72 | ||
73 | this.setQueryParams(this.searchValue) | |
74 | this.emitSearch() | |
75 | } | |
76 | ||
2e46eb97 C |
77 | private listenToRouteSearchChange () { |
78 | this.route.queryParams | |
79 | .subscribe(params => { | |
80 | const search = params.search || '' | |
81 | ||
82 | logger('On route search change "%s".', search) | |
83 | ||
4beda9e1 C |
84 | if (this.searchValue === search) return |
85 | ||
f676e0e3 C |
86 | this.searchValue = search |
87 | this.emitSearch() | |
2e46eb97 C |
88 | }) |
89 | } | |
90 | ||
91 | private initSearchStream () { | |
92 | this.searchStream = new Subject() | |
93 | ||
94 | this.searchStream | |
95 | .pipe( | |
f676e0e3 | 96 | debounceTime(300), |
2e46eb97 C |
97 | distinctUntilChanged() |
98 | ) | |
99 | .subscribe(() => { | |
2e46eb97 | 100 | this.setQueryParams(this.searchValue) |
f676e0e3 C |
101 | |
102 | this.emitSearch() | |
2e46eb97 C |
103 | }) |
104 | } | |
105 | ||
f676e0e3 C |
106 | private emitSearch () { |
107 | if (!this.viewInitialized) { | |
108 | this.emitSearchAfterViewInit = true | |
109 | return | |
110 | } | |
111 | ||
112 | logger('On search "%s".', this.searchValue) | |
113 | ||
114 | this.search.emit(this.searchValue) | |
115 | } | |
116 | ||
2e46eb97 C |
117 | private setQueryParams (search: string) { |
118 | const queryParams: Params = {} | |
119 | ||
120 | if (search) Object.assign(queryParams, { search }) | |
121 | this.router.navigate([ ], { queryParams }) | |
1fd61899 C |
122 | } |
123 | } |