]>
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 | title: string | |
9 | ||
10 | children: { | |
11 | label: string | |
12 | queryParams: Params | |
13 | }[] | |
14 | } | |
15 | ||
16 | const logger = debug('peertube:AdvancedInputFilterComponent') | |
17 | ||
18 | @Component({ | |
19 | selector: 'my-advanced-input-filter', | |
20 | templateUrl: './advanced-input-filter.component.html', | |
21 | styleUrls: [ './advanced-input-filter.component.scss' ] | |
22 | }) | |
23 | export class AdvancedInputFilterComponent implements OnInit, AfterViewInit { | |
24 | @Input() filters: AdvancedInputFilter[] = [] | |
25 | @Input() emitOnInit = true | |
26 | ||
27 | @Output() search = new EventEmitter<string>() | |
28 | ||
29 | searchValue: string | |
30 | ||
31 | private searchStream: Subject<string> | |
32 | ||
33 | private viewInitialized = false | |
34 | private emitSearchAfterViewInit = false | |
35 | ||
36 | constructor ( | |
37 | private route: ActivatedRoute, | |
38 | private router: Router | |
39 | ) { } | |
40 | ||
41 | ngOnInit () { | |
42 | this.initSearchStream() | |
43 | this.listenToRouteSearchChange() | |
44 | } | |
45 | ||
46 | ngAfterViewInit () { | |
47 | this.viewInitialized = true | |
48 | ||
49 | // Init after view init to not send an event too early | |
50 | if (this.emitOnInit && this.emitSearchAfterViewInit) this.emitSearch() | |
51 | } | |
52 | ||
53 | onInputSearch (event: Event) { | |
54 | this.scheduleSearchUpdate((event.target as HTMLInputElement).value) | |
55 | } | |
56 | ||
57 | onResetTableFilter () { | |
58 | this.immediateSearchUpdate('') | |
59 | } | |
60 | ||
61 | hasFilters () { | |
62 | return this.filters && this.filters.length !== 0 | |
63 | } | |
64 | ||
65 | private scheduleSearchUpdate (value: string) { | |
66 | this.searchValue = value | |
67 | this.searchStream.next(this.searchValue) | |
68 | } | |
69 | ||
70 | private immediateSearchUpdate (value: string) { | |
71 | this.searchValue = value | |
72 | ||
73 | this.setQueryParams(this.searchValue) | |
74 | this.emitSearch() | |
75 | } | |
76 | ||
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 | ||
84 | if (this.searchValue === search) return | |
85 | ||
86 | this.searchValue = search | |
87 | this.emitSearch() | |
88 | }) | |
89 | } | |
90 | ||
91 | private initSearchStream () { | |
92 | this.searchStream = new Subject() | |
93 | ||
94 | this.searchStream | |
95 | .pipe( | |
96 | debounceTime(300), | |
97 | distinctUntilChanged() | |
98 | ) | |
99 | .subscribe(() => { | |
100 | this.setQueryParams(this.searchValue) | |
101 | ||
102 | this.emitSearch() | |
103 | }) | |
104 | } | |
105 | ||
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 | ||
117 | private setQueryParams (search: string) { | |
118 | const queryParams: Params = {} | |
119 | ||
120 | if (search) Object.assign(queryParams, { search }) | |
121 | this.router.navigate([ ], { queryParams }) | |
122 | } | |
123 | } |