Guest User

Untitled

a guest
Dec 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import { Directive, ElementRef, AfterViewInit, Input, HostListener, OnDestroy, QueryList, ContentChildren } from '@angular/core';
  2. import { debounceTime } from 'rxjs/operators';
  3. import { fromEvent, Subscription } from 'rxjs';
  4.  
  5. @Directive({
  6. selector: '[pyb-button-group]'
  7. })
  8. export class ButtonGroupDirective implements AfterViewInit, OnDestroy {
  9. @Input() className: string;
  10. @ContentChildren('header', { read: ElementRef }) headers: QueryList<ElementRef>
  11.  
  12. private eventSubscription: Subscription
  13.  
  14. @HostListener('window:resize', ['$event'])
  15. onResize() {
  16. this.eventSubscription = fromEvent(window, 'resize')
  17. .pipe(debounceTime(500))
  18. .subscribe(() => this.resize());
  19. }
  20.  
  21. ngAfterViewInit() {
  22. this.resize();
  23. }
  24.  
  25. ngOnDestroy() {
  26. if (this.eventSubscription) this.eventSubscription.unsubscribe();
  27. }
  28.  
  29. resize(): void {
  30. let headerHeight = 0;
  31.  
  32. this.headers.forEach(header => {
  33. let element = header.nativeElement;
  34. element.style.height = 'auto'; // Reset when resizing the window
  35.  
  36. let height = element.offsetHeight;
  37. if (height > headerHeight) headerHeight = height;
  38. });
  39.  
  40. this.headers.forEach(header => {
  41. let element = header.nativeElement;
  42. element.style.height = headerHeight ? headerHeight + 'px' : 'auto';
  43. });
  44. }
  45. }
  46.  
  47. <div class="container" *ngIf="categories">
  48. <div class="row" pyb-button-group>
  49. <div class="col-md-4 col-6" *ngFor="let category of categories">
  50. <a class="btn-choice" [routerLink]="['/' + category.id]">
  51. <div class="header" #header>
  52. <span class="title">{{ category.name }}</span>
  53. </div>
  54. </a>
  55. </div>
  56. </div>
  57. </div>
Add Comment
Please, Sign In to add comment