All files / lib/link link.component.ts

95.85% Statements 162/169
90% Branches 18/20
88.88% Functions 8/9
95.85% Lines 162/169

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 1701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 537x 537x 537x 537x 537x 537x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 2760x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 537x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 396x 54x 396x 396x 396x 1x 1x 1x 1x 1x 24x 12x 12x 24x 24x 24x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x           1x 1x 1x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x     2x 2x 2x 8x 8x 8x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  HostBinding,
  Input,
  ViewChild,
} from '@angular/core';
 
import { LinkEvent } from '../utils/event.types';
import {
  Consumer,
  FocusableElementOwner,
  WidgetKeySet,
} from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
/**
 * `gc-link` allows the user to visit a specified location.
 */
@Component({
  selector: 'gc-link',
  templateUrl: './link.component.html',
  styleUrls: ['./link.component.css'],
})
export class LinkComponent implements FocusableElementOwner {
  /**
   * URL that the user will be sent to on click.
   */
  @Input()
  public link?: string;
 
  /**
   * Toggles the disabled state of the link
   */
  @Input()
  @HostBinding('class.gc-disabled')
  public disabled = false;
 
  /**
   * Text that is shown as the link
   */
  @Input()
  public text = '';
 
  /**
   * Define how the content is displayed
   * - icon: icon is shown on the right of the text
   * - text-only: only text is displayed
   */
  @Input()
  public contentDisplay: 'text-only' | 'icon' = 'text-only';
 
  /**
   * Optional handler to customize handling of navigation requests
   */
  @Input()
  public linkHandler?: Consumer<LinkEvent>;
 
  /**
   * Opens the link in a new browser tab
   */
  @Input()
  public openInNewTab = false;
 
  /**
   * Optional text that is shown above the link.
   */
  @Input()
  public description?: string;
 
  /**
   * Decides if an arrow icon will be rendered, and the link will be opened in new tab or not.
   */
  @Input()
  public defaultOpenInNewTab = false;
 
  /**
   * @ignore
   */
  @ViewChild('linkElement')
  private linkElement?: ElementRef<HTMLAnchorElement>;
 
  /** @ignore */
  private readonly widgetKeySet: WidgetKeySet = createWidgetKeySet('gc-link');
 
  /**
   * @ignore
   */
  private externalDescriptionId?: string;
 
  /**
   * @ignore
   */
  private linkDescriptionIdContainerId?: string;
 
  public focusChild(): boolean {
    if (!this.disabled && this.linkElement) {
      this.linkElement.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  protected _getExternalDescriptionId() {
    if (this.externalDescriptionId === undefined) {
      this.externalDescriptionId = this.widgetKeySet.baseKey + '-description';
    }
    return this.externalDescriptionId;
  }
 
  /**
   * @ignore
   */
  protected _getOptionalDescriptionContainerId() {
    if (this.linkDescriptionIdContainerId === undefined) {
      this.linkDescriptionIdContainerId =
        this.widgetKeySet.baseKey + '-optional-description';
    }
    return this.linkDescriptionIdContainerId;
  }
 
  /**
   * @ignore
   */
  protected _handleClick() {
    return this._internalHandleClick(true);
  }
 
  /**
   * @ignore
   */
  protected _handleAuxClick(event: MouseEvent) {
    if (event.button === 1) {
      return this._internalHandleClick(false);
    }
    return true;
  }
 
  /** @ignore */
  private _internalHandleClick(replace: boolean): boolean {
    if (this.disabled) {
      return false;
    }
 
    if (this.linkHandler && this.link) {
      try {
        this.linkHandler({
          linkInfo: this.link,
          replace: this.openInNewTab ? false : replace,
        });
      } catch (e) {
        console.error('Failed to execute link handler', e);
      }
 
      return false;
    }
 
    return true;
  }
}