]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/advanced-input-filter.component.ts
Add quick filter for followers
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / advanced-input-filter.component.ts
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 && 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 if (this.searchValue === search) return
81
82 this.searchValue = search
83 this.emitSearch()
84 })
85 }
86
87 private initSearchStream () {
88 this.searchStream = new Subject()
89
90 this.searchStream
91 .pipe(
92 debounceTime(300),
93 distinctUntilChanged()
94 )
95 .subscribe(() => {
96 this.setQueryParams(this.searchValue)
97
98 this.emitSearch()
99 })
100 }
101
102 private emitSearch () {
103 if (!this.viewInitialized) {
104 this.emitSearchAfterViewInit = true
105 return
106 }
107
108 logger('On search "%s".', this.searchValue)
109
110 this.search.emit(this.searchValue)
111 }
112
113 private setQueryParams (search: string) {
114 const queryParams: Params = {}
115
116 if (search) Object.assign(queryParams, { search })
117 this.router.navigate([ ], { queryParams })
118 }
119 }