Guest User

Untitled

a guest
Jan 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import { Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. import { buffer, debounceTime, takeUntil } from 'rxjs/operators';
  4.  
  5. @Directive({
  6. selector: '[appMultiClick]'
  7. })
  8. export class MultiClickDirective implements OnInit, OnDestroy {
  9. @Input() threshold = 250;
  10. @Output()
  11. multiClick = new EventEmitter<{ originalEvent: Event; times: number }>();
  12. private clickStream$ = new Subject<Event>();
  13. private destroy$ = new Subject();
  14. constructor() {}
  15. ngOnInit() {
  16. this.clickStream$
  17. .pipe(
  18. buffer(this.clickStream$.pipe(debounceTime(this.threshold))),
  19. takeUntil(this.destroy$)
  20. )
  21. .subscribe(clicks => {
  22. this.multiClick.emit({
  23. originalEvent: clicks[0],
  24. times: clicks.length
  25. });
  26. });
  27. }
  28. ngOnDestroy() {
  29. this.destroy$.next(true);
  30. }
  31. @HostListener('click', ['$event'])
  32. clickEvent(event) {
  33. event.preventDefault();
  34. event.stopPropagation();
  35. this.clickStream$.next(event);
  36. }
  37. }
Add Comment
Please, Sign In to add comment