All files / application-header si-application-header.component.ts

91.54% Statements 65/71
81.81% Branches 9/11
85.71% Functions 18/21
91.17% Lines 62/68

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                                                                                        1x 1x             18x 18x             18x     18x     18x   18x   18x 18x 18x 18x 18x   18x 18x   18x 18x 18x   18x 23x 23x         18x 31x 31x 27x   4x           6x       18x 18x 18x                   16x 16x 13x           23x 23x 23x         46x 1x 1x   1x     1x             4x 4x 4x 4x 4x             6x     6x         4x     4x                   3x       6x 6x 6x 6x 6x   1x 6x         23x 4x 4x 4x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { A11yModule, CdkTrapFocus } from '@angular/cdk/a11y';
import { BreakpointObserver } from '@angular/cdk/layout';
import { NgClass, NgTemplateOutlet } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  inject,
  Injector,
  input,
  OnDestroy,
  signal,
  TemplateRef,
  viewChild
} from '@angular/core';
import {
  HeaderWithDropdowns,
  SI_HEADER_WITH_DROPDOWNS,
  SiHeaderDropdownTriggerDirective
} from '@siemens/element-ng/header-dropdown';
import {
  addIcons,
  elementMenu,
  elementThumbnails,
  SiIconComponent
} from '@siemens/element-ng/icon';
import { BOOTSTRAP_BREAKPOINTS, Breakpoints } from '@siemens/element-ng/resize-observer';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
import { defer, of, Subject } from 'rxjs';
import { map, skip, takeUntil } from 'rxjs/operators';
 
/** Root component for the application header. */
@Component({
  selector: 'si-application-header',
  imports: [SiTranslatePipe, NgClass, A11yModule, NgTemplateOutlet, SiIconComponent],
  templateUrl: './si-application-header.component.html',
  styleUrl: './si-application-header.component.scss',
  providers: [{ provide: SI_HEADER_WITH_DROPDOWNS, useExisting: SiApplicationHeaderComponent }],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiApplicationHeaderComponent implements HeaderWithDropdowns, OnDestroy {
  private static idCounter = 0;
 
  /**
   * Defines the minimum breakpoint which must be matched to expand the header / switch to desktop mode.
   *
   * @defaultValue 'sm'
   */
  readonly expandBreakpoint = input<'sm' | 'md' | 'lg' | 'xl' | 'xxl' | 'never'>('sm');
  readonly launchpad = input<TemplateRef<void>>();
  /**
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_APPLICATION_HEADER.LAUNCHPAD:Launchpad`)
   * ```
   */
  readonly launchpadLabel = input(t(() => $localize`:@@SI_APPLICATION_HEADER.LAUNCHPAD:Launchpad`));
 
  /** @internal */
  readonly closeMobileMenus = new Subject<void>();
 
  /** @internal */
  readonly mobileNavigationExpanded = signal(false);
  /** @internal */
  readonly hasNavigation = signal(false);
 
  protected readonly openDropdownCount = signal(0);
  protected readonly launchpadOpen = signal(false);
  protected readonly id = `__si-application-header-${SiApplicationHeaderComponent.idCounter++}`;
  protected toggleNavigation = t(
    () => $localize`:@@SI_APPLICATION_HEADER.TOGGLE_NAVIGATION:Toggle navigation`
  );
  protected injector = inject(Injector);
  protected readonly icons = addIcons({ elementThumbnails, elementMenu });
 
  private readonly navigationToggle = viewChild<ElementRef<HTMLDivElement>>('navigationToggle');
  private readonly focusTrap = viewChild.required(CdkTrapFocus);
  private breakpointObserver = inject(BreakpointObserver);
  private openDropdown?: SiHeaderDropdownTriggerDirective;
  private closeMobileSub = this.closeMobileMenus.subscribe(() => {
    this.closeMobileNavigation();
    this.closeLaunchpad();
  });
 
  /** @internal */
  // defer is required to re-check the current breakpoint as it may change.
  readonly inlineDropdown = defer(() => {
    const expandBreakpoint = this.expandBreakpoint();
    if (expandBreakpoint === 'never') {
      return of(true);
    }
    return this.breakpointObserver
      .observe(
        `(min-width: ${
          BOOTSTRAP_BREAKPOINTS[(expandBreakpoint + 'Minimum') as keyof Breakpoints]
        }px)`
      )
      .pipe(map(({ matches }) => !matches));
  });
 
  ngOnDestroy(): void {
    this.closeMobileSub.unsubscribe();
    this.closeMobileMenus.next();
    this.closeMobileMenus.complete();
  }
 
  /** @internal */
  onDropdownItemTriggered(): void {
    this.closeMobileMenus.next();
  }
 
  /** @internal */
  dropdownClosed(trigger?: SiHeaderDropdownTriggerDirective): void {
    this.openDropdownCount.update(v => v - 1);
    if (trigger === this.openDropdown) {
      this.openDropdown = undefined;
    }
  }
 
  /** @internal */
  dropdownOpened(trigger?: SiHeaderDropdownTriggerDirective): void {
    this.openDropdown ??= trigger;
    this.openDropdownCount.update(v => v + 1);
    this.closeLaunchpad();
  }
 
  /** @internal */
  closeLaunchpad(): void {
    if (this.launchpadOpen()) {
      this.launchpadOpen.set(false);
      this.dropdownClosed();
 
      if (this.hasNavigation()) {
        // This is also fine in expanded view when the mobile toggle is not visible.
        // Then the focus call will be ignored.
        this.navigationToggle()?.nativeElement.focus();
      }
    }
  }
 
  /** @internal */
  openLaunchpad(): void {
    if (!this.launchpadOpen()) {
      this.dropdownOpened();
      this.closeMobileMenus.next();
      this.launchpadOpen.set(true);
      this.inlineDropdown
        .pipe(skip(1), takeUntil(this.closeMobileMenus))
        .subscribe(() => this.closeMobileMenus.next());
    }
  }
 
  protected toggleMobileNavigationExpanded(): void {
    Iif (this.mobileNavigationExpanded()) {
      this.closeMobileNavigation();
    } else {
      this.openMobileNavigation();
    }
  }
 
  protected toggleLaunchpad(): void {
    Iif (this.launchpadOpen()) {
      this.closeLaunchpad();
    } else {
      this.openLaunchpad();
    }
  }
 
  protected navigationEscapePressed(): void {
    this.closeMobileNavigation();
    this.navigationToggle()?.nativeElement.focus();
  }
 
  protected backdropClicked(): void {
    this.closeMobileMenus.next();
  }
 
  private openMobileNavigation(): void {
    if (!this.mobileNavigationExpanded()) {
      this.closeMobileMenus.next();
      this.mobileNavigationExpanded.set(true);
      this.dropdownOpened();
      this.inlineDropdown
        .pipe(skip(1), takeUntil(this.closeMobileMenus))
        .subscribe(() => this.closeMobileMenus.next());
      this.focusTrap().focusTrap.focusFirstTabbableElementWhenReady();
    }
  }
 
  private closeMobileNavigation(): void {
    if (this.mobileNavigationExpanded()) {
      this.mobileNavigationExpanded.set(false);
      this.openDropdown?.close();
      this.dropdownClosed();
    }
  }
}