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

83.17% Statements 173/208
85.71% Branches 12/14
60% Functions 6/10
83.17% Lines 173/208

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 2091x 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 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x                       1x 1x                         1x 1x 1x               1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 4x 4x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x       5x 5x 5x 5x 5x 5x 5x 2x 3x 3x 5x 5x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  NgZone,
  OnDestroy,
  TemplateRef,
  Type,
  computed,
  input,
  output,
  viewChild,
  viewChildren,
} from '@angular/core';
 
import { observeElementSize, subscribeWindow } from '../utils/internal-utils';
import {
  BadgeItem,
  Callback,
  FocusableElementOwner,
  Item,
  Key,
} from '../utils/util.types';
import { itemTrack } from '../utils/utilities';
 
import { ComponentMapperDirective } from '../internal/component-mapper.directive';
 
import { BadgeItemTemplateContext } from '../badge-list/badge-list.component';
import { ButtonComponent } from '../button/button.component';
 
/**
 * Context Header is a responsive, context-specific, horizontal user interface element that contains badges and links that lead to other websites or parts of the page.
 */
@Component({
  selector: 'gc-context-header',
  templateUrl: './context-header.component.html',
  styleUrls: ['./context-header.component.css'],
  changeDetection: ChangeDetectionStrategy.Eager,
  standalone: false,
})
export class ContextHeaderComponent
  implements OnDestroy, FocusableElementOwner
{
  /**
   * Label to show in the header.
   */
  public readonly label = input.required<string>();
 
  /**
   * Items that will be rendered as badges in the header.
   */
  public readonly badges = input<readonly BadgeItem[]>([]);
 
  /**
   * Icon template that will be used for the badges.
   */
  public readonly iconProvider = input<TemplateRef<BadgeItemTemplateContext>>();
 
  /** Emitted when an action item is pressed. */
  public readonly onAction = output<Item>();
 
  /**
   * Items that will be rendered as buttons in the header.
   */
  public readonly actions = input<Item[]>([]);
 
  /**
   * @ignore
   */
  public _mode: 'small' | 'medium' | 'default' = 'default';
 
  /** @ignore */
  protected readonly _popItems = computed<readonly Item[]>(() => {
    return this.actions().map(a => ({
      key: a.key,
      label: `${a.label} öffnen`,
    }));
  });
 
  /** @ignore */
  protected inSmallText = false;
 
  /**
   * @ignore
   */
  private readonly actionsContainerElement =
    viewChild.required<ElementRef<HTMLDivElement>>('actionsContainer');
 
  /** @ignore */
  private readonly menuButton = viewChild('menuButton', {
    read: ButtonComponent,
  });
 
  /** @ignore */
  private readonly actionButtons = viewChildren('actionButton', {
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- required otherwise we get an error at the callsite
    read: ComponentMapperDirective as Type<
      ComponentMapperDirective<ButtonComponent>
    >,
  });
 
  /**
   * @ignore
   */
  private observerCallbacks: Callback[] = [];
 
  constructor(zone: NgZone, el: ElementRef<HTMLElement>) {
    this.observerCallbacks.push(
      observeElementSize(el.nativeElement, zone, this.handleResize.bind(this)),
    );
 
    this.observerCallbacks.push(
      subscribeWindow('resize', this.computeWindowRelatedProps.bind(this)),
    );
    this.computeWindowRelatedProps();
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.observerCallbacks.forEach(cb => cb());
  }
 
  /**
   * Focus action in the header with the given key.
   * @param key the key of the `action` to focus
   * @returns `true` if focus has been transferred
   */
  public focusAction(key: Key): boolean {
    if (this._mode === 'default') {
      const button = this.actionButtons().find(i => i.key === key)?.component;
      if (button) {
        return button.focusChild();
      }
    } else {
      const menuButton = this.menuButton();
      return menuButton ? menuButton.focusChild() : false;
    }
    return false;
  }
 
  public focusChild(): boolean {
    if (this._mode !== 'default') {
      const menuButton = this.menuButton();
      return menuButton ? menuButton.focusChild() : false;
    } else {
      for (const action of this.actions()) {
        if (this.focusAction(action.key)) {
          return true;
        }
      }
    }
    return false;
  }
 
  /** @ignore */
  protected handleMenuAction(item: Item) {
    const action = this.actions().find(a => a.key === item.key);
    if (action) {
      this.onAction.emit(action);
    } else {
      console.error(`Unable to find action ${item.key}`);
    }
  }
 
  /**
   * @ignore
   */
  protected _itemTrackBy(item: Item): string {
    return itemTrack(item);
  }
 
  /** @ignore */
  private computeWindowRelatedProps() {
    this.inSmallText = window.innerWidth <= 480 || window.innerHeight <= 1024;
  }
 
  /** @ignore */
  private handleResize(entry: ResizeObserverEntry): void {
    // padding 64px
    // gap = 24px
    const elementWidth = entry.contentRect.width - 64 - 24;
 
    const mediumButtonExists = this.actions().length > 0;
    // medium button is 148px
    if (elementWidth - (mediumButtonExists ? 148 : 0) < 400) {
      this._mode = 'small';
      return;
    }
 
    const actionsContainerWidth =
      this.actionsContainerElement().nativeElement.getBoundingClientRect()
        .width;
    const spaceInHeader = elementWidth - actionsContainerWidth;
 
    if (spaceInHeader < 720) {
      this._mode = 'medium';
    } else {
      this._mode = 'default';
    }
  }
}