All files / lib/app-header app-header.component.ts

96.52% Statements 222/230
85.71% Branches 30/35
91.66% Functions 11/12
96.52% Lines 222/230

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 2311x 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 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 1x 1x 28x 28x 28x 1x 1x 1x 55x   55x 55x 55x 55x 55x 55x 55x 1x 1x 1x 55x   55x 55x 55x 55x 55x 55x 55x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 6x 9x 9x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 43x 43x 43x 43x 43x 43x 9x 9x 34x 19x 43x 43x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnDestroy,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { observeSize } from '../utils/internal-utils';
import { Callback, FocusableElementOwner } from '../utils/util.types';
 
import { TooltipComponent } from '../internal/tooltip/tooltip.component';
 
import {
  AppHeaderButtonComponent,
  AppHeaderButtonType,
} from './app-header-button/app-header-button.component';
 
export { AppHeaderButtonType }; // re-export to ensure it's part of the public API (moving it completely to this location would create an import cycle in Angular)
 
/**
 * `gc-app-header` is a horizontal user interface element that contains buttons to show tools and subcontent related to the page
 */
@Component({
  selector: 'gc-app-header',
  templateUrl: './app-header.component.html',
  styleUrls: ['./app-header.component.css'],
})
export class AppHeaderComponent
  implements AfterViewInit, OnDestroy, FocusableElementOwner
{
  /**
   * Label that will show on the header.
   */
  @Input()
  public label = '';
 
  /**
   * Name of the person that will be used for the 'Person' button label;
   */
  @Input()
  public personName = '';
 
  /**
   * Number of new appointment notifications.
   */
  @Input()
  public newAppointments?: number;
 
  /**
   * Number of new message notifications.
   */
  @Input()
  public newMessages?: number;
 
  /**
   * The method that gets fired when a button is pressed. Emits `undefined` after a button is deselected and no other
   * button is selected instead.
   */
  @Output()
  public readonly selectedButtonChange: EventEmitter<
    AppHeaderButtonType | undefined
  > = new EventEmitter<AppHeaderButtonType | undefined>();
 
  /**
   * @ignore
   */
  @ViewChildren(AppHeaderButtonComponent)
  private appHeaderButtons?: QueryList<AppHeaderButtonComponent>;
 
  /**
   * @ignore
   */
  @ViewChild('header')
  private headerElement!: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChild('tooltip')
  private tooltip?: TooltipComponent;
 
  /** @ignore */
  @ViewChild('labelContainer')
  private labelContainer!: ElementRef<HTMLSpanElement>;
 
  /**
   * Currently selected button.
   * When `undefined`, no button is selected.
   */
  @Input()
  public get selectedButton(): AppHeaderButtonType | undefined {
    return this._selectedButton;
  }
 
  public set selectedButton(value: AppHeaderButtonType | undefined) {
    this.handleButtonComponentSelection(value);
    this._selectedButton = value;
  }
 
  /** @ignore */
  public get newAppointmentsDescription(): string {
    if (this.newAppointments === undefined || this.newAppointments === 0) {
      return '';
    } else {
      return (
        this.newAppointments.toString() +
        (this.newAppointments == 1 ? ' neuer Termin' : ' neue Termine')
      );
    }
  }
 
  /** @ignore */
  public get newMessagesDescription(): string {
    if (this.newMessages === undefined || this.newMessages === 0) {
      return '';
    } else {
      return (
        this.newMessages.toString() +
        (this.newMessages == 1 ? ' neue Meldung' : ' neue Meldungen')
      );
    }
  }
 
  /**
   * @ignore
   */
  _labelHidden = false;
 
  /** @ignore */
  private _selectedButton?: AppHeaderButtonType;
 
  /**
   * @ignore
   */
  private resizeObserver?: Callback;
 
  constructor(private zone: NgZone) {}
 
  /**
   * @ignore
   */
  ngAfterViewInit(): void {
    this.resizeObserver = observeSize(
      this.headerElement,
      this.zone,
      this.resizeObserverCallback.bind(this),
    );
    if (this.tooltip && this.tooltip.tooltipOwner !== this.labelContainer) {
      this.tooltip.tooltipOwner = this.labelContainer;
    }
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    if (this.resizeObserver) {
      this.resizeObserver();
    }
  }
 
  public focusChild(): boolean {
    const foundButton = this.appHeaderButtons?.find(
      btn => btn.type === this.selectedButton,
    );
 
    if (foundButton) {
      foundButton.focusChild();
      return true;
    } else if (this.appHeaderButtons?.first.button) {
      this.appHeaderButtons.first.focusChild();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  public _onSelect(button: AppHeaderButtonComponent): void {
    if (this.selectedButton === button.type) {
      this._selectedButton = undefined;
    } else {
      this._selectedButton = button.type;
    }
 
    this.handleButtonComponentSelection(button.type);
    this.selectedButtonChange.emit(this._selectedButton);
  }
 
  /**
   * @ignore
   * handles visual button selection
   */
  private handleButtonComponentSelection(
    buttonType?: AppHeaderButtonType,
  ): void {
    const foundButton = this.appHeaderButtons?.find(
      btn => btn.type === buttonType,
    );
    if (foundButton && !foundButton.selected) {
      this.appHeaderButtons?.forEach(btn => (btn.selected = false));
      foundButton.selected = true;
    } else if (buttonType === undefined) {
      this.appHeaderButtons?.forEach(btn => (btn.selected = false));
    }
  }
 
  /**
   * @ignore
   */
  private resizeObserverCallback(): void {
    if (this.headerElement.nativeElement.clientWidth < 480) {
      this._labelHidden = true;
    } else {
      this._labelHidden = false;
    }
  }
}