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

94.75% Statements 217/229
82.92% Branches 34/41
92.85% Functions 13/14
94.75% Lines 217/229

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 2301x 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 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 68x 68x 68x 16x 16x 19x 19x 19x 19x 3x 3x 1x 3x 16x   19x 19x 16x 16x 16x 16x 16x 16x 21x 21x 21x 16x 16x 31x 31x 31x 16x 16x 16x 68x   68x 68x 68x 68x 68x 68x 68x 16x 16x 16x 68x   68x 68x 68x 68x 68x 68x 68x 16x 16x 16x 68x 68x 68x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 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 46x 46x 46x 46x 46x 46x 9x 9x 37x 22x 46x 46x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  EventEmitter,
  HostBinding,
  Input,
  OnDestroy,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
 
import { AriaLiveReaderComponent } from '../internal/aria-live-reader/aria-live-reader.component';
 
import {
  AppHeaderButtonComponent,
  AppHeaderButtonType,
} from './app-header-button/app-header-button.component';
 
export type { 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'],
  standalone: false,
})
export class AppHeaderComponent implements FocusableElementOwner, OnDestroy {
  /**
   * Label that will show on the header.
   */
  @Input()
  public label = '';
 
  /**
   * Number of new appointment notifications.
   */
  @Input()
  public newAppointments?: number;
 
  /**
   * Number of new message notifications.
   */
  @Input()
  public newMessages?: number;
 
  /**
   * Prefix text shown in loading tooltip.
   */
  @Input()
  public loadingTextPrefix = 'Behördenname';
 
  /**
   * Buttons showing up on the header right side.
   */
  @Input()
  public shownHeaderButtons: ReadonlySet<AppHeaderButtonType> = new Set([
    'search',
    'calendar',
    'notifications',
    'person',
    'help',
  ]);
 
  /**
   * 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('ariaLiveRegion')
  private ariaLiveRegion?: AriaLiveReaderComponent;
 
  /**
   * Indicates that the app-header is in a loading state.
   */
  @Input()
  public get isLoading() {
    return this._isLoading;
  }
 
  public set isLoading(value: boolean) {
    this._isLoading = value;
 
    clearTimeout(this.clearTimer);
    if (this._isLoading) {
      this.loadedAfterTwoSeconds = false;
      this.clearTimer = setTimeout(() => {
        this.loadedAfterTwoSeconds = true;
      }, 2000);
    } else if (this.loadedAfterTwoSeconds) {
      this.ariaLiveRegion?.readText(`${this.loadingTextPrefix} geladen`);
    }
  }
 
  /**
   * 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 */
  @HostBinding('style.--gc-right-buttons-count')
  protected get rightHeaderButtonsCount(): number {
    return this.shownHeaderButtons.size;
  }
 
  /** @ignore */
  private _selectedButton?: AppHeaderButtonType;
 
  /** @ignore */
  private clearTimer?: ReturnType<typeof setTimeout>;
 
  /** @ignore */
  private _isLoading = false;
 
  /** @ignore */
  private loadedAfterTwoSeconds = false;
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    if (this.clearTimer) {
      clearTimeout(this.clearTimer);
      this.clearTimer = undefined;
    }
  }
 
  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));
    }
  }
}