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