All files / lib/infobox/infobox-container infobox-container.component.ts

96.64% Statements 144/149
100% Branches 19/19
90.9% Functions 10/11
96.64% Lines 144/149

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 1501x 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 165x 165x 165x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 8x 108x 108x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x           1x 1x 1x 20x 20x 1x 1x 1x 33x 26x 33x 33x 1x 1x 1x 36x 36x 1x 1x 1x 20x 20x 1x 1x 1x 4x 49x 46x 3x 3x 49x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  EventEmitter,
  HostListener,
  Input,
  OnDestroy,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { getKeyboardFocusableElements } from '../../utils/internal-utils';
import {
  BadgeItem,
  FocusableElementOwner,
  WidgetKeySet,
} from '../../utils/util.types';
import { createWidgetKeySet } from '../../utils/utilities';
 
import { AutoFocusAreaDirective } from '../../internal/auto-focus-area.directive';
 
import { BadgeItemTemplateContext } from '../../badge-list/badge-list.component';
 
function isCritical(item: BadgeItem) {
  return item.color === 'critical';
}
 
/**
 * Infobox Container component that is a part of the gc-infobox component. Acts as a dropdown container.
 */
@Component({
  selector: 'gc-infobox-container',
  templateUrl: './infobox-container.component.html',
  styleUrls: ['./infobox-container.component.css'],
  standalone: false,
})
export class InfoboxContainerComponent
  implements FocusableElementOwner, OnDestroy
{
  /**
   * Label of the infobox container.
   */
  @Input({ required: true })
  public label = '';
 
  /**
   * Items that will be rendered as badges in the header.
   */
  @Input()
  public badges: readonly BadgeItem[] = [];
 
  /**
   * A template that will be used to render icons within badges.
   */
  @Input()
  public badgeIconProvider?: TemplateRef<BadgeItemTemplateContext>;
 
  /**
   * Hint message that appears within the header.
   */
  @Input()
  public hintTitle = '';
 
  /**
   * Event emitted on close button press.
   */
  @Output()
  public readonly onClose: EventEmitter<void> = new EventEmitter<void>();
 
  /** @ignore */
  @ViewChild(AutoFocusAreaDirective)
  private contentAutoFocusArea?: AutoFocusAreaDirective;
 
  /** @ignore */
  protected readonly _widgetKeys: WidgetKeySet = createWidgetKeySet(
    'gc-infobox-container',
  );
 
  /** @ignore */
  private _disabledElements: Map<Element, string | null> = new Map<
    Element,
    string | null
  >();
 
  constructor() {
    getKeyboardFocusableElements(document.body).forEach(el => {
      this._disabledElements.set(el, el.getAttribute('tabindex'));
      el.setAttribute('tabindex', '-1');
    });
  }
 
  /** @ignore */
  @HostListener('keydown', ['$event'])
  protected onKeyDown(e: KeyboardEvent): void {
    if (e.key === 'Escape') {
      this.onClose.emit();
    }
  }
 
  ngOnDestroy(): void {
    this.resetTabIndex();
  }
 
  /** @ignore */
  public focusChild(): boolean {
    if (!this.contentAutoFocusArea) {
      return false;
    }
    return this.contentAutoFocusArea.focusInitialElement();
  }
 
  /** @ignore */
  protected _hasCriticalBadge() {
    return this._criticalBadgeItems().length > 0;
  }
 
  /** @ignore */
  protected _criticalBadgeItems(): BadgeItem[] {
    return this.badges.filter(isCritical).map(b => {
      return { ...b, showIcon: true } satisfies BadgeItem;
    });
  }
 
  /** @ignore */
  protected _defaultBadgeItems() {
    return this.badges.filter(b => !isCritical(b));
  }
 
  /** @ignore */
  protected _hasDefaultBadgeItems() {
    return this._defaultBadgeItems().length > 0;
  }
 
  /** @ignore */
  private resetTabIndex() {
    this._disabledElements.forEach((v, k) => {
      if (v === null) {
        k.removeAttribute('tabindex');
      } else {
        k.setAttribute('tabindex', v);
      }
    });
  }
}