2019-12-26 11:19:43 +09:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
EventEmitter,
|
|
|
|
HostListener,
|
|
|
|
Input,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
Output
|
|
|
|
} from '@angular/core';
|
|
|
|
import { Subject, Subscription } from 'rxjs';
|
|
|
|
import { debounceTime } from 'rxjs/operators';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[ucapDebounceClick]'
|
|
|
|
})
|
|
|
|
export class ClickDebounceDirective implements OnInit, OnDestroy {
|
|
|
|
@Input() debounceTime = 500;
|
|
|
|
@Output() debounceClick = new EventEmitter();
|
|
|
|
private clicks = new Subject();
|
|
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.subscription = this.clicks
|
|
|
|
.pipe(debounceTime(this.debounceTime))
|
|
|
|
.subscribe(e => this.debounceClick.emit(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('click', ['$event'])
|
2020-01-08 14:43:43 +09:00
|
|
|
clickEvent(event: Event) {
|
2019-12-26 11:19:43 +09:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
this.clicks.next(event);
|
|
|
|
}
|
|
|
|
}
|