All files / lib/internal/wizard-stepper wizard-stepper.component.ts

99.09% Statements 218/220
95.65% Branches 44/46
100% Functions 11/11
99.09% Lines 218/220

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 2211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 1x 1x 1x 202x 202x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 11x 11x 11x 11x 11x 5x 5x 5x 5x 11x 11x 11x 1x 1x 1x 1x 11x 11x 11x 5x 5x 5x 5x 11x 11x 13x 13x 1x 1x 3x 2x 2x 2x 2x 2x 2x 2x     2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1180x 236x 944x 236x 708x 708x 1180x 1180x 1x 1x 1x 1x 1x 1180x 1180x 1180x 1180x 1180x 1180x 1180x 1180x 1x 1x 1x 18x 13x 13x 18x 18x 18x 18x 8x 18x 8x 8x 6x 6x 2x 2x 2x 2x 2x 2x 1x 8x 8x 8x 10x 10x 10x 10x 18x 18x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  EventEmitter,
  HostListener,
  Input,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { FocusableElementOwner, Item, Key } from '../../utils/util.types';
 
import { WizardStepperButtonComponent } from './wizard-stepper-button/wizard-stepper-button.component';
import { WizardStepperHorizontalComponent } from './wizard-stepper-horizontal/wizard-stepper-horizontal.component';
 
/**
 * Wizard Stepper provides a wizard-like workflow by dividing content into logical steps.
 */
@Component({
  selector: 'gc-wizard-stepper',
  templateUrl: './wizard-stepper.component.html',
  styleUrls: ['./wizard-stepper.component.css'],
})
export class WizardStepperComponent implements FocusableElementOwner {
  /**
   * Items to render as steps.
   */
  @Input()
  public items: readonly Item[] = [];
 
  /**
   * List of item keys that are marked as validated.
   */
  @Input()
  public validatedItemKeys: ReadonlySet<Key> = new Set();
 
  /**
   * List of item keys that are inactive.
   */
  @Input()
  public inactiveItemKeys: ReadonlySet<Key> = new Set();
 
  /**
   * Currently selected step item.
   */
  @Input()
  public activeItemKey?: Key;
 
  /**
   * Decides if the steps will be shown with a label or as an icon alone.
   */
  @Input()
  public contentDisplay: 'icon-only' | 'text-right' = 'text-right';
 
  /**
   * Orientation of the stepper.
   */
  @Input()
  public orientation: 'vertical' | 'horizontal' = 'vertical';
 
  /**
   * ID of the element whose content is controlled by the selection of the stepper items, used for accessibility markup.
   */
  @Input()
  public controlledId = '';
 
  /**
   * User-initiated active step key switch will be only performed when the callback evaluates with value `true`. Leaving this
   * empty (`undefined`) is equivalent to always resolving to `true`.
   */
  @Input()
  public permitActiveItemKeyChange?: (key: Key) => Promise<boolean> | boolean;
 
  /**
   * Fires upon the change of the selected step item.
   */
  @Output()
  public readonly activeItemKeyChange: EventEmitter<Key> =
    new EventEmitter<Key>();
 
  /**
   * @ignore
   */
  @ViewChildren('stepperButton')
  private stepperButtons?: QueryList<WizardStepperButtonComponent>;
 
  /**
   * @ignore
   */
  @ViewChild('horizontalStepper', { read: WizardStepperHorizontalComponent })
  private horizontalStepperElement?: WizardStepperHorizontalComponent;
 
  protected get _activeItem() {
    return this.items.find(i => this.activeItemKey === i.key);
  }
 
  /**
   * @ignore
   */
  @HostListener('keydown', ['$event'])
  onKeyPress(event: KeyboardEvent) {
    const key = event.key;
 
    if (this.stepperButtons) {
      const currentlyFocusedButton = this.stepperButtons.find(i => i._hasFocus);
      if (currentlyFocusedButton) {
        const currentlyFocusedIndex = this.stepperButtons
          .toArray()
          .indexOf(currentlyFocusedButton);
 
        if (key === 'ArrowDown' || key === 'ArrowRight') {
          this.stepperButtons
            .get(currentlyFocusedIndex + 1)
            ?.button.nativeElement.focus();
          event.preventDefault();
        }
 
        if (key === 'ArrowUp' || key === 'ArrowLeft') {
          this.stepperButtons
            .get(currentlyFocusedIndex - 1)
            ?.button.nativeElement.focus();
          event.preventDefault();
        }
 
        if (key === ' ' || key === 'Enter') {
          this.stepperButtons
            .get(currentlyFocusedIndex)
            ?.button.nativeElement.click();
          event.preventDefault();
        }
      }
    }
  }
 
  focusChild(): boolean {
    if (this.orientation === 'vertical' && this.stepperButtons) {
      const selectedButton = this.stepperButtons.find(btn => btn.selected);
      const firstButton = this.stepperButtons.get(0);
 
      if (selectedButton) {
        selectedButton.button.nativeElement.focus();
        return true;
      } else if (firstButton) {
        firstButton.button.nativeElement.focus();
        return true;
      }
    } else {
      if (this.horizontalStepperElement) {
        this.horizontalStepperElement.button.nativeElement.focus();
        return true;
      }
    }
 
    return false;
  }
 
  /**
   * @ignore
   */
  public _getItemVariant(item: Item): 'first' | 'middle' | 'last' {
    if (this.items.indexOf(item) === 0) {
      return 'first';
    } else if (this.items.indexOf(item) === this.items.length - 1) {
      return 'last';
    } else {
      return 'middle';
    }
  }
 
  /**
   * @ignore
   */
  public _isLastActiveStep(item: Item): boolean {
    const activeItems = this.items.filter(
      i => !this.inactiveItemKeys.has(i.key),
    );
 
    return (
      activeItems.findIndex(i => i.key === item.key) === activeItems.length - 1
    );
  }
 
  /** @ignore */
  protected async _onButtonAction(key: Key) {
    const doChange = () => {
      this.activeItemKey = key;
      this.activeItemKeyChange.emit(key);
    };
 
    if (
      this.permitActiveItemKeyChange !== undefined &&
      this.activeItemKey !== key
    ) {
      const permit = this.permitActiveItemKeyChange(key);
      if (typeof permit !== 'boolean') {
        // permit is of type Promise<boolean>
        const proceed = await permit;
        if (proceed) {
          doChange();
        }
      } else {
        // permit is of type boolean
        if (permit) {
          doChange();
        }
      }
    } else if (
      this.permitActiveItemKeyChange === undefined &&
      this.activeItemKey !== key
    ) {
      doChange();
    }
  }
}