All files / lib/card card-layout.component.ts

88.52% Statements 108/122
88.88% Branches 16/18
71.42% Functions 5/7
88.52% Lines 108/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x           1x 1x 1x 3x 3x 3x 3x 1x 1x           1x 1x 1x 19x 19x 19x 19x 19x 19x 19x 24x 24x 24x 24x 24x 19x 1x 1x 1x 27x 27x 27x         27x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 11x 28x 28x 28x 28x 28x 8x 8x 8x 8x 1x  
import {
  AfterContentInit,
  Component,
  ContentChildren,
  ElementRef,
  NgZone,
  OnDestroy,
  QueryList,
  forwardRef,
} from '@angular/core';
import { Subscription } from 'rxjs';
 
import { FocusableElementOwner } from '../utils/util.types';
 
import { CardComponent } from './card.component';
 
/**
 * `gc-card-layout` is used to group `gc-card` components
 */
@Component({
  selector: 'gc-card-layout',
  templateUrl: './card-layout.component.html',
  styleUrls: ['./card-layout.component.css'],
})
export class CardLayoutComponent
  implements AfterContentInit, OnDestroy, FocusableElementOwner
{
  /** @ignore */
  @ContentChildren(forwardRef(() => CardComponent))
  private cards?: QueryList<CardComponent>;
 
  /** @ignore */
  private cardSubscription?: Subscription;
 
  /** @ignore */
  private resizeObserver?: ResizeObserver;
 
  constructor(
    private readonly elementRef: ElementRef<HTMLElement>,
    private readonly zone: NgZone,
  ) {
    zone.runOutsideAngular(() => {
      this.resizeObserver = new ResizeObserver(() => this.handleResize());
      this.resizeObserver.observe(elementRef.nativeElement);
    });
  }
 
  /** @ignore */
  public ngOnDestroy(): void {
    if (this.cardSubscription) {
      this.cardSubscription.unsubscribe();
    }
    this.resizeObserver?.disconnect();
  }
 
  /** @ignore */
  public ngAfterContentInit(): void {
    this.cardSubscription = this.cards?.changes.subscribe(() => {
      this.zone.runOutsideAngular(() => this.handleResize());
    });
  }
 
  public focusChild(): boolean {
    if (this.cards?.first) {
      return this.cards.first.focusChild();
    }
    return false;
  }
 
  /** @ignore */
  public _recomputeHeaders(): void {
    NgZone.assertNotInAngularZone();
 
    if (this.cards) {
      const cards = this.cards.toArray();
 
      const itemsPerRow = this.computeRowItems();
      for (let i = 0; i < cards.length; i += itemsPerRow) {
        const row = cards.slice(i, Math.min(i + itemsPerRow, cards.length));
        const maxHeight = Math.max(...row.map(c => c._headerTitleHeight()));
        row.forEach(c => c._updateMinTitleHeight(maxHeight));
      }
    }
  }
 
  /** @ignore */
  private computeRowItems() {
    const rect = this.elementRef.nativeElement.getBoundingClientRect();
    if (rect.width >= 1400) {
      return 3;
    } else if (rect.width >= 1025) {
      return 2;
    }
    return 1;
  }
 
  /** @ignore */
  private handleResize(): void {
    if (!this.cards) {
      return;
    }
 
    NgZone.assertNotInAngularZone();
 
    const itemsPerRow = this.computeRowItems();
    const columnSpan = 12 / itemsPerRow;
 
    const cards = this.cards.toArray();
    let columnIndex = 1;
    for (let i = 0; i < cards.length; i++) {
      if (i % itemsPerRow === 0) {
        columnIndex = 1;
      }
 
      cards[i]._updateColumnConfig(columnIndex, columnIndex + columnSpan);
 
      columnIndex += columnSpan;
    }
 
    this._recomputeHeaders();
  }
}