All files / application-header si-header-action-item.base.ts

84.61% Statements 11/13
87.5% Branches 7/8
75% Functions 3/4
90.9% Lines 10/11

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                        1x   22x       22x 22x   22x     22x 40x           5x   3x 2x            
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Directive, HostListener, inject, OnInit } from '@angular/core';
import { SiHeaderDropdownTriggerDirective } from '@siemens/element-ng/header-dropdown';
import { addIcons, elementDown2 } from '@siemens/element-ng/icon';
 
import { SiApplicationHeaderComponent } from './si-application-header.component';
import { SiHeaderCollapsibleActionsComponent } from './si-header-collapsible-actions.component';
 
@Directive({})
export abstract class SiHeaderActionItemBase implements OnInit {
  /** @internal */
  dropdownTrigger = inject(SiHeaderDropdownTriggerDirective, {
    self: true,
    optional: true
  });
  protected collapsibleActions = inject(SiHeaderCollapsibleActionsComponent, { optional: true });
  protected readonly icons = addIcons({ elementDown2 });
 
  private header = inject(SiApplicationHeaderComponent);
 
  ngOnInit(): void {
    if (this.dropdownTrigger) {
      this.header.closeMobileMenus.subscribe(() => this.dropdownTrigger!.close());
    }
  }
 
  @HostListener('click')
  protected click(): void {
    if (!this.dropdownTrigger?.isOpen && !this.collapsibleActions?.mobileExpanded()) {
      // we must close other immediately as we would close the dropdown else wise immediately after opening.
      this.header.closeMobileMenus.next();
    } else Iif (!this.dropdownTrigger || !this.collapsibleActions?.mobileExpanded()) {
      // we must use queueMicrotask, otherwise the dropdown gets re-opened immediately.
      queueMicrotask(() => this.header.closeMobileMenus.next());
    }
  }
}