All files / lib/toggle-button-group toggle-button.component.ts

98.13% Statements 158/161
93.75% Branches 15/16
100% Functions 9/9
98.13% Lines 158/161

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 1621x 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 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 1x 1x 1x 1x 1x 48x 48x 48x 1x 1x 1x 1x 1x 35x 35x 35x 1x 1x 1x 1x 1x 30x 28x 28x 28x 30x 30x 28x 28x       30x 30x 1x 1x 1x 1x 1x 20x 20x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  ViewChild,
} from '@angular/core';
 
import { WidgetKeySet } from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
/**
 * A Toggle Button component that allows the user to mark a value as selected.
 */
@Component({
  selector: 'gc-toggle-button',
  templateUrl: './toggle-button.component.html',
  styleUrls: [
    './../utils/styles/reset-button.css',
    './toggle-button.component.css',
  ],
  standalone: false,
})
export class ToggleButtonComponent {
  /**
   * Label of the toggle button component.
   */
  @Input()
  public label = '';
 
  /**
   * Name of the toggle button group.
   */
  @Input()
  public groupName = '';
 
  /**
   * Decides if the toggle button is active/selected.
   */
  @Input()
  public value = false;
 
  /**
   * Key of the toggle button. Used for selecting the toggle button.
   */
  @Input()
  public key!: string;
 
  /**
   * Inform about the change of the selection state
   */
  @Output()
  public readonly valueChange: EventEmitter<boolean> =
    new EventEmitter<boolean>();
 
  /**
   * @ignore
   */
  @ViewChild('button')
  public _buttonElement!: ElementRef<HTMLButtonElement>;
 
  /**
   * @ignore
   */
  @Input()
  public _focused = false;
 
  /**
   * @ignore
   */
  @Input()
  public contentDisplay: 'icon-left' | 'icon-only' | 'text-only' = 'icon-left';
 
  /**
   * Enables setting of the value only when the Promise resolves with value "true".
   */
  @Input()
  public permitSelection?: () => Promise<boolean> | boolean;
 
  /**
   * @ignore
   */
  @Input()
  public _isFirstInToggleButtonGroup = false;
 
  /**
   * @ignore
   */
  @Input()
  public _isLastInToggleButtonGroup = false;
 
  /**
   * @ignore
   */
  @Output()
  public readonly onFocusEntered: EventEmitter<void> = new EventEmitter<void>();
 
  /**
   * @ignore
   */
  @Output()
  public readonly onFocusLeft: EventEmitter<void> = new EventEmitter<void>();
 
  /**
   * @ignore
   */
  @Output()
  public readonly keyPressed: EventEmitter<KeyboardEvent> =
    new EventEmitter<KeyboardEvent>();
 
  /** @ignore */
  protected readonly widgetKeys: WidgetKeySet =
    createWidgetKeySet('gc-toggle-button');
 
  /**
   * @ignore
   */
  _handleFocus(): void {
    this._focused = true;
    this.onFocusEntered.emit();
  }
 
  /**
   * @ignore
   */
  _handleBlur(): void {
    this._focused = false;
    this.onFocusLeft.emit();
  }
 
  /**
   * @ignore
   */
  async _handleClick(): Promise<void> {
    const doChange = () => {
      this.value = true;
      this.valueChange.emit(this.value);
    };
    if (this.permitSelection) {
      const permit = await this.permitSelection();
      if (permit) {
        doChange();
      }
    } else {
      doChange();
    }
  }
 
  /**
   * @ignore
   */
  _handleKeyDown(event: KeyboardEvent): void {
    this.keyPressed.emit(event);
  }
}