More efficient advanced input filter
This commit is contained in:
parent
1a7d0887b6
commit
f676e0e321
1 changed files with 38 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
||||||
import * as debug from 'debug'
|
import * as debug from 'debug'
|
||||||
import { Subject } from 'rxjs'
|
import { Subject } from 'rxjs'
|
||||||
import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
|
import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
|
||||||
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
|
import { AfterViewInit, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
|
||||||
import { ActivatedRoute, Params, Router } from '@angular/router'
|
import { ActivatedRoute, Params, Router } from '@angular/router'
|
||||||
|
|
||||||
export type AdvancedInputFilter = {
|
export type AdvancedInputFilter = {
|
||||||
|
@ -16,7 +16,7 @@ const logger = debug('peertube:AdvancedInputFilterComponent')
|
||||||
templateUrl: './advanced-input-filter.component.html',
|
templateUrl: './advanced-input-filter.component.html',
|
||||||
styleUrls: [ './advanced-input-filter.component.scss' ]
|
styleUrls: [ './advanced-input-filter.component.scss' ]
|
||||||
})
|
})
|
||||||
export class AdvancedInputFilterComponent implements OnInit {
|
export class AdvancedInputFilterComponent implements OnInit, AfterViewInit {
|
||||||
@Input() filters: AdvancedInputFilter[] = []
|
@Input() filters: AdvancedInputFilter[] = []
|
||||||
|
|
||||||
@Output() search = new EventEmitter<string>()
|
@Output() search = new EventEmitter<string>()
|
||||||
|
@ -25,6 +25,9 @@ export class AdvancedInputFilterComponent implements OnInit {
|
||||||
|
|
||||||
private searchStream: Subject<string>
|
private searchStream: Subject<string>
|
||||||
|
|
||||||
|
private viewInitialized = false
|
||||||
|
private emitSearchAfterViewInit = false
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
private route: ActivatedRoute,
|
private route: ActivatedRoute,
|
||||||
private router: Router
|
private router: Router
|
||||||
|
@ -35,23 +38,37 @@ export class AdvancedInputFilterComponent implements OnInit {
|
||||||
this.listenToRouteSearchChange()
|
this.listenToRouteSearchChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngAfterViewInit () {
|
||||||
|
this.viewInitialized = true
|
||||||
|
|
||||||
|
// Init after view init to not send an event too early
|
||||||
|
if (this.emitSearchAfterViewInit) this.emitSearch()
|
||||||
|
}
|
||||||
|
|
||||||
onInputSearch (event: Event) {
|
onInputSearch (event: Event) {
|
||||||
this.updateSearch((event.target as HTMLInputElement).value)
|
this.scheduleSearchUpdate((event.target as HTMLInputElement).value)
|
||||||
}
|
}
|
||||||
|
|
||||||
onResetTableFilter () {
|
onResetTableFilter () {
|
||||||
this.updateSearch('')
|
this.immediateSearchUpdate('')
|
||||||
}
|
}
|
||||||
|
|
||||||
hasFilters () {
|
hasFilters () {
|
||||||
return this.filters.length !== 0
|
return this.filters.length !== 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateSearch (value: string) {
|
private scheduleSearchUpdate (value: string) {
|
||||||
this.searchValue = value
|
this.searchValue = value
|
||||||
this.searchStream.next(this.searchValue)
|
this.searchStream.next(this.searchValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private immediateSearchUpdate (value: string) {
|
||||||
|
this.searchValue = value
|
||||||
|
|
||||||
|
this.setQueryParams(this.searchValue)
|
||||||
|
this.emitSearch()
|
||||||
|
}
|
||||||
|
|
||||||
private listenToRouteSearchChange () {
|
private listenToRouteSearchChange () {
|
||||||
this.route.queryParams
|
this.route.queryParams
|
||||||
.subscribe(params => {
|
.subscribe(params => {
|
||||||
|
@ -59,7 +76,8 @@ export class AdvancedInputFilterComponent implements OnInit {
|
||||||
|
|
||||||
logger('On route search change "%s".', search)
|
logger('On route search change "%s".', search)
|
||||||
|
|
||||||
this.updateSearch(search)
|
this.searchValue = search
|
||||||
|
this.emitSearch()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,17 +86,27 @@ export class AdvancedInputFilterComponent implements OnInit {
|
||||||
|
|
||||||
this.searchStream
|
this.searchStream
|
||||||
.pipe(
|
.pipe(
|
||||||
debounceTime(200),
|
debounceTime(300),
|
||||||
distinctUntilChanged()
|
distinctUntilChanged()
|
||||||
)
|
)
|
||||||
.subscribe(() => {
|
.subscribe(() => {
|
||||||
logger('On search "%s".', this.searchValue)
|
|
||||||
|
|
||||||
this.setQueryParams(this.searchValue)
|
this.setQueryParams(this.searchValue)
|
||||||
this.search.emit(this.searchValue)
|
|
||||||
|
this.emitSearch()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private emitSearch () {
|
||||||
|
if (!this.viewInitialized) {
|
||||||
|
this.emitSearchAfterViewInit = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger('On search "%s".', this.searchValue)
|
||||||
|
|
||||||
|
this.search.emit(this.searchValue)
|
||||||
|
}
|
||||||
|
|
||||||
private setQueryParams (search: string) {
|
private setQueryParams (search: string) {
|
||||||
const queryParams: Params = {}
|
const queryParams: Params = {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue