All files / lib/button button.component.ts

98.91% Statements 274/277
96.66% Branches 29/30
93.75% Functions 15/16
98.91% Lines 274/277

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 2781x 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 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 37224x 37224x 37224x 2862x 2862x 778x 778x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 2862x 1x 1x 24x 24x 24x 24x 24x 24x 1x 1x 1x 40060x 40060x 13932x 1010x 1010x 40060x 40060x 1x 1x 1x 37198x 37198x 1x 1x 1x 186x 186x 186x 186x 186x 186x 1x 1x 1x 1x 1x 1x     1x 1x 1x 107x 107x 7x 7x 107x 107x 1x 1x 1x 113x 7x 7x 7x 113x 113x 113x 1x 1x 1x 395x 395x 395x 395x 1x 1x 1x 241x 241x 241x 241x 241x 1x 1x 1x 37198x 12555x 12555x 12555x 14137x 37198x 1x 1x 1x 5x 5x 1x 1x 1x 636x 636x 636x 636x   636x 636x 636x 636x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewChecked,
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { ActionEvent, HasFocusEvent } from '../utils/event.types';
import { isNotEmpty, isUndefOrEmpty } from '../utils/internal-utils';
import { FocusableElementOwner, Item } from '../utils/util.types';
 
import { TooltipComponent } from '../internal/tooltip/tooltip.component';
 
export type ButtonContentDisplay =
  | 'icon-only'
  | 'icon-only-circle'
  | 'icon-left'
  | 'icon-right'
  | 'text-only'
  | 'status';
 
const ARMED_EMULATION = 'gc-input-active';
 
/**
 * A Button element that allows users to fire an action on a button press.
 */
@Component({
  selector: 'gc-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.css'],
  host: {
    // host element generates an extra text node, which causes Firefox + Jaws to read out the button
    // text twice when it's used in a list -> remove it from the a11y tree
    role: 'none',
  },
})
export class ButtonComponent<MenuItem extends Item = Item>
  implements AfterViewInit, AfterViewChecked, FocusableElementOwner
{
  /**
   * The label that shows on the button.
   */
  @Input()
  public label = '';
 
  /**
   * The visual type of the button.
   */
  @Input()
  public variant: 'primary' | 'secondary' | 'tertiary' = 'primary';
 
  /**
   * Define how the content is displayed
   * - icon-only: only the icon is shown
   * - icon-only-circle: only the icon is shown in a circular shape
   * - icon-left: icon is shown on the left of the text
   * - icon-right: icon is shown on the right of the text
   * - text-only: only text is displayed
   * - status: only the icon is shown but using the width of the label
   */
  @Input()
  public contentDisplay: ButtonContentDisplay = 'text-only';
 
  /**
   * Text displayed as a tooltip to the user
   */
  @Input()
  public tooltipText?: string;
 
  /**
   * @ignore
   */
  @Input()
  public _classes = '';
 
  /**
   * @ignore
   */
  @Input()
  public _supportLiveTextChange = false;
 
  /**
   * Automatically focus the control if it is contained in an area tagged with data-gc-auto-focus-area
   */
  @Input()
  public autoFocus = false;
 
  /**
   * If not empty, the button will open a menu on click and render these as items of the menu.
   */
  @Input()
  public menuItems: readonly MenuItem[] = [];
 
  /**
   * Decides on which side of the menu button the menu is going to appear.
   */
  @Input()
  public menuAlignment: 'start' | 'end' | 'auto' = 'end';
 
  /** Optional icon provider to use for menu-items */
  @Input()
  public menuItemIconProvider?: TemplateRef<unknown>;
 
  /**
   * Menu items that should be disabled in the menu.
   */
  @Input()
  public disabledMenuItems: ReadonlySet<string> = new Set();
 
  /**
   * @ignore
   * Overrides the default tabindex value.
   */
  @Input()
  public _tabIndexOverride?: number;
 
  /**
   * Event which is fired when an menu item is activated.
   */
  @Output()
  public readonly onMenuAction: EventEmitter<MenuItem> =
    new EventEmitter<MenuItem>();
 
  /**
   * The method that gets fired when the button is pressed.
   */
  @Output()
  public readonly onAction: EventEmitter<ActionEvent> =
    new EventEmitter<ActionEvent>();
 
  /**
   * @ignore
   */
  @Output()
  public readonly _onHasFocus: EventEmitter<HasFocusEvent> =
    new EventEmitter<HasFocusEvent>();
 
  /** @ignore */
  @ViewChild('button')
  private button!: ElementRef<HTMLButtonElement>;
 
  /** @ignore */
  @ViewChild('tooltip')
  private tooltip?: TooltipComponent;
 
  /**
   * If set to true the button state is set to disabled which means that it is no longer clickable.
   */
  @Input()
  public get disabled(): boolean {
    return this._disabled;
  }
 
  public set disabled(value: boolean) {
    this._disabled = value;
  }
 
  /**
   * @ignore
   */
  public hasFocus = false;
 
  /**
   * @ignore
   */
  private _disabled = false;
 
  focusChild(): boolean {
    if (!this.disabled) {
      this.button.nativeElement.focus();
      return true;
    }
    return false;
  }
 
  /** @ignore */
  ngAfterViewInit(): void {
    if (
      this.tooltip !== undefined &&
      this.tooltip.tooltipOwner !== this.button
    ) {
      this.tooltip.tooltipOwner = this.button;
    }
  }
 
  /** @ignore */
  ngAfterViewChecked(): void {
    this.ngAfterViewInit();
  }
 
  /** @ignore */
  _handleOnClick(event: MouseEvent): void {
    this.onAction.emit({
      ctrlKey: event.ctrlKey,
      metaKey: event.metaKey,
      shiftKey: event.shiftKey,
    });
  }
 
  /**
   * @ignore
   * @deprecated use moveFocus
   */
  _focus(): void {
    this.button.nativeElement.focus();
  }
 
  /** @ignore */
  _onKeyDown(event: KeyboardEvent): void {
    // We need to emulate the armed state for FF
    if (event.key == ' ') {
      // only emulate for space - ENTER executes the action on key-down already and so native buttons also do not show that
      (event.currentTarget as HTMLButtonElement).classList.add(ARMED_EMULATION);
    }
  }
 
  /** @ignore */
  _onKeyUp(event: KeyboardEvent): void {
    if (event.key == ' ') {
      // only emulate for space - ENTER executes the action on key-down already and so native buttons also do not show that
      (event.currentTarget as HTMLButtonElement).classList.remove(
        ARMED_EMULATION,
      );
    }
  }
 
  /** @ignore */
  _onFocus(): void {
    this.hasFocus = true;
    this.refreshAriaLive();
    this._onHasFocus.emit({ hasFocus: true });
  }
 
  /** @ignore */
  _onBlur(): void {
    this.hasFocus = false;
    this.refreshAriaLive();
    this.button.nativeElement.classList.remove(ARMED_EMULATION); // Safe guard to clean up if the key-up is not received
    this._onHasFocus.emit({ hasFocus: false });
  }
 
  /** @ignore */
  _hasGraphicTooltip(): boolean {
    if (!this.contentDisplay.includes('icon-only')) {
      return false;
    }
 
    return isUndefOrEmpty(this.tooltipText) && isNotEmpty(this.label);
  }
 
  /** @ignore */
  _hideTooltip(): void {
    this.tooltip?.hide();
  }
 
  /** @ignore */
  private refreshAriaLive() {
    // directly interface with the element befor the other DOM-Syncs happen
    const el = this.button.nativeElement;
 
    if (this._supportLiveTextChange && !this.disabled && this.hasFocus) {
      el.setAttribute('aria-live', 'polite');
    } else {
      el.removeAttribute('aria-live');
    }
  }
}