All files / lib/card card.component.ts

94.61% Statements 158/167
90% Branches 18/20
90% Functions 9/10
94.61% Lines 158/167

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 1681x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 1x 1x 1x 17x 17x 19x 19x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 2x 2x 1x 1x 1x 28x 28x 28x 28x 28x 1x 1x 1x 64x 64x 64x           64x 64x 64x 1x 1x 1x 64x 64x     64x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x     1x 1x 1x 66x 6x 60x 60x 66x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnDestroy,
  Optional,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { ActionEvent } from '../utils/event.types';
import { badgeListAsString } from '../utils/internal-utils';
import { BadgeItem, FocusableElementOwner } from '../utils/util.types';
 
import { BadgeItemTemplateContext } from '../badge-list/badge-list.component';
import { ButtonComponent } from '../button/button.component';
import {
  DynamicHeadingLevelPassthroughService,
  DynamicHeadingLevelService,
} from '../dynamic-heading/dynamic-heading-level.service';
import { CardLayoutComponent } from './card-layout.component';
 
/**
 * `gc-card` is used in forms to summarize data from subforms.
 */
@Component({
  selector: 'gc-card',
  templateUrl: './card.component.html',
  styleUrls: ['../utils/styles/reset-form.css', './card.component.css'],
  providers: [DynamicHeadingLevelService],
  viewProviders: [
    {
      provide: DynamicHeadingLevelService,
      useClass: DynamicHeadingLevelPassthroughService,
    },
  ],
})
export class CardComponent
  implements FocusableElementOwner, AfterViewInit, OnDestroy
{
  /**
   * Label of the card.
   */
  @Input()
  public label = '';
 
  /**
   * Badge items to display in the header
   */
  @Input()
  public badgeItems: readonly BadgeItem[] = [];
 
  /**
   * Icon provider for badge
   */
  @Input()
  public badgeIconProvider?: TemplateRef<BadgeItemTemplateContext>;
 
  /**
   * State of button
   */
  @Input()
  public state: 'edit' | 'view' | 'inactive' = 'edit';
 
  /**
   * The method that gets fired when the edit button is pressed.
   */
  @Output()
  public readonly onAction: EventEmitter<ActionEvent> =
    new EventEmitter<ActionEvent>();
 
  /** @ignore */
  @ViewChild('statusButton', { read: ButtonComponent })
  private statusButton?: ButtonComponent;
 
  /** @ignore */
  @ViewChild('headerTitle')
  private headerTitleElement?: ElementRef<HTMLDivElement>;
 
  /** @ignore */
  private sizeObserver?: ResizeObserver;
 
  constructor(
    @Optional() private cardLayout: CardLayoutComponent | null,
    private elementRef: ElementRef<HTMLElement>,
    private zone: NgZone,
  ) {}
 
  /** @ignore */
  public ngAfterViewInit(): void {
    this.zone.runOutsideAngular(() => {
      this.sizeObserver = new ResizeObserver(() => {
        if (this.cardLayout) {
          this.cardLayout._recomputeHeaders();
        }
      });
      if (this.headerTitleElement) {
        this.sizeObserver.observe(this.headerTitleElement.nativeElement);
      }
    });
  }
 
  /** @ignore */
  public ngOnDestroy(): void {
    this.sizeObserver?.disconnect();
  }
 
  /** @ignore */
  public _updateColumnConfig(startColumn: number, endColumn: number): void {
    this.elementRef.nativeElement.style.setProperty(
      '--gc-card-layout-column',
      `${startColumn.toFixed(0)}/${endColumn.toFixed(0)}`,
    );
  }
 
  /** @ignore */
  public _updateMinTitleHeight(minHeight: number): void {
    if (minHeight <= 56) {
      this.elementRef.nativeElement.style.removeProperty(
        '--gc-card-header-height',
      );
    } else {
      this.elementRef.nativeElement.style.setProperty(
        '--gc-card-header-height',
        `${minHeight.toFixed(0)}px`,
      );
    }
  }
 
  /** @ignore */
  public _headerTitleHeight(): number {
    if (this.headerTitleElement) {
      return this.headerTitleElement.nativeElement.clientHeight;
    }
    return 56;
  }
 
  public focusChild(): boolean {
    if (this.statusButton) {
      return this.statusButton.focusChild();
    } else {
      return false;
    }
  }
 
  /** @ignore */
  protected onButtonPress(event: ActionEvent): void {
    this.onAction.emit(event);
  }
 
  /** @ignore */
  protected badgeLegendText() {
    if (this.badgeItems.length > 0) {
      return '- ' + badgeListAsString(this.badgeItems);
    }
    return '';
  }
}