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

89.89% Statements 169/188
89.47% Branches 17/19
66.66% Functions 10/15
89.89% Lines 169/188

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 1891x 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 9x 9x 3x 3x 3x 3x 3x 4x 4x 4x 3x 3x 4x 4x 4x 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 6x 6x 1x 1x 1x 4x 4x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x       5x 5x 5x 5x 5x 5x 2x 3x 3x 5x 5x 1x 1x 1x 4x 4x 6x 6x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnDestroy,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { observeElementSize, subscribeWindow } from '../utils/internal-utils';
import { BadgeItem, Callback, Item, LinkItem } from '../utils/util.types';
import { itemTrack } from '../utils/utilities';
 
import { BadgeItemTemplateContext } from '../badge-list/badge-list.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'],
  standalone: false,
})
export class ContextHeaderComponent implements OnDestroy {
  /**
   * Label to show in the header.
   */
  @Input()
  public label = '';
 
  /**
   * Items that will be rendered as badges in the header.
   */
  @Input()
  public badges: readonly BadgeItem[] = [];
 
  /**
   * Icon template that will be used for the badges.
   */
  @Input()
  public iconProvider?: TemplateRef<BadgeItemTemplateContext>;
 
  /** Emitted when an action item is pressed. */
  @Output()
  public readonly onAction: EventEmitter<Item> = new EventEmitter<Item>();
 
  /**
   * @ignore
   */
  @ViewChild('actionsContainer')
  private actionsContainerElement!: ElementRef<HTMLDivElement>;
 
  /**
   * Items that will be rendered as links in the header.
   * @deprecated use 'actions' input instead
   */
  @Input()
  public set links(value: readonly LinkItem[]) {
    this._links = value;
    this.rebuildPopupItems();
    console.warn(
      'The "links" input is deprecated and should not be used. Please use "actions" input instead.',
    );
  }
 
  public get links(): readonly LinkItem[] {
    return this._links;
  }
 
  /**
   * Items that will be rendered as buttons in the header.
   */
  @Input()
  public get actions(): Item[] {
    return this._actions;
  }
 
  public set actions(value: Item[]) {
    this._actions = value;
    this.rebuildPopupItems();
  }
 
  /**
   * @ignore
   */
  public _mode: 'small' | 'medium' | 'default' = 'default';
 
  /** @ignore */
  protected _popItems: readonly Item[] = [];
  protected actionsAndLinks: readonly Item[] = [];
 
  protected inSmallText = false;
 
  /**
   * @ignore
   */
  private observerCallbacks: Callback[] = [];
 
  /** @ignore */
  private _actions: Item[] = [];
 
  /**
   * @ignore
   */
  private _links: readonly LinkItem[] = [];
 
  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());
  }
 
  /** @ignore */
  protected handleMenuAction(item: Item) {
    const action = this.actionsAndLinks.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.links.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';
    }
  }
 
  /** @ignore */
  private rebuildPopupItems() {
    this.actionsAndLinks = this.actions.concat(this.links);
    this._popItems = this.actionsAndLinks.map(a => ({
      key: a.key,
      label: `${a.label} öffnen`,
    }));
  }
}