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

96.48% Statements 192/199
94.11% Branches 32/34
86.66% Functions 13/15
96.48% Lines 192/199

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 2001x 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 1x 1x 1x         1x 1x 1x 387x 387x 1x 1x 1x 261x 261x 261x 261x 261x 261x 261x 261x 261x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 219x 219x 219x 219x 219x 219x 219x 219x 219x 219x 219x 21x 21x 21x 23x 23x 23x 46x 23x 23x 46x 46x 23x 23x 23x 23x 23x   46x 23x 23x 23x 21x 21x 21x 21x 1x 1x 21x 21x 21x 21x 21x 21x 26x 26x 26x 21x 21x 1x 1x 1x 1x 1x     1x 1x 21x 21x 1x 1x 21x 21x 1x 1x 2349x 2349x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x 5x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 315x 315x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  computed,
  effect,
  input,
  model,
  viewChild,
} from '@angular/core';
 
import { IndexedItem, textToLine } from '../../../utils/internal-utils';
import { Item, Key, WidgetKeySet } from '../../../utils/util.types';
import { createWidgetKeySet, itemTrack } from '../../../utils/utilities';
 
import {
  KeyFocusTarget,
  LegacyBasePickerComponent,
} from '../../../picker/legacy-base-picker.component';
import { SelectionEvent } from '../../listbox.directive';
 
@Component({
  selector: 'gc-wizard-stepper-horizontal',
  templateUrl: './wizard-stepper-horizontal.component.html',
  styleUrls: [
    './../../../utils/styles/reset-button.css',
    './wizard-stepper-horizontal.component.css',
  ],
  changeDetection: ChangeDetectionStrategy.Eager,
  standalone: false,
})
export class WizardStepperHorizontalComponent extends LegacyBasePickerComponent<
  Item,
  Item
> {
  public override set items(items: readonly Item[]) {
    throw new Error(
      'invalid usage of component input, use `items_` input instead',
    );
  }
 
  /** @ignore */
  public get _listboxId(): string {
    return this._widgetKeys.baseKey + '-lb';
  }
 
  /** @ignore */
  public get _ariaControls(): string | undefined {
    const idList = [
      this._impl_drawerOpen ? this._listboxId : '',
      this.controlledId(),
    ]
      .join(' ')
      .trim();
 
    return idList ? idList : undefined;
  }
 
  // for transition to signals only (legacy base class is not updated and does not use signals, but singal API desired in this component)
  // -> temporary alternative input, mapped to super input via effect. internal component, hence possible to easily switch around without external API breakage.
  public readonly items_ = input.required<readonly Item[]>();
 
  public readonly controlledId = input('');
 
  public readonly invalidItemKeys = input<ReadonlySet<Key>>(new Set());
 
  /**
   * List of item keys that are marked as validated.
   */
  public readonly validatedItemKeys = input<ReadonlySet<Key>>(new Set());
 
  public readonly button =
    viewChild.required<ElementRef<HTMLButtonElement>>('button');
 
  /**
   * The key of the selected value
   */
  public readonly selectedItemKey = model.required<Key>();
 
  public readonly _widgetKeys: WidgetKeySet = createWidgetKeySet(
    'gc-wizard-stepper-horizontal',
  );
 
  public _hasFocus = false;
 
  /** @ignore */
  protected readonly ariaMessage = computed<string>(() => {
    const itemIndex = this.items_().findIndex(
      i => i.key === this.selectedItemKey(),
    );
    return (
      'Arbeitsschritt ' +
      (itemIndex !== -1 ? itemIndex + 1 : 1).toString() +
      ' von ' +
      this.items_().length.toString() +
      (this.invalidItemKeys().size > 0 ? this.invalidStepIndexes() : '')
    );
  });
 
  /** @ignore */
  private readonly invalidStepIndexes = computed(() => {
    let invalidItemsAriaText = ' - Fehlerhafte Eingaben in Schritt ';
    const invalidStepIndexes: number[] = [];
    this.invalidItemKeys().forEach(value => {
      invalidStepIndexes.push(this.items_().findIndex(i => i.key === value));
    });
    invalidStepIndexes.forEach((stepIndex, idx) => {
      const stringifiedIndex = (stepIndex + 1).toString();
      if (idx === 0) {
        invalidItemsAriaText += stringifiedIndex;
      } else {
        invalidItemsAriaText +=
          idx === invalidStepIndexes.length - 1
            ? ` und ${stringifiedIndex}`
            : `, ${stringifiedIndex}`;
      }
    });
    return invalidItemsAriaText;
  });
 
  private readonly containerElement = viewChild.required<
    ElementRef<HTMLDivElement>
  >('wizardStepperContainer');
 
  public constructor() {
    super();
 
    effect(() => {
      super.items = this.items_();
    });
    effect(() => {
      this.storage.selected = this.items_().find(
        i => this.selectedItemKey() === i.key,
      );
    });
  }
 
  /**
   * @ignore
   */
  public _processItemText(text: string) {
    return textToLine(text, 32, '<br>');
  }
 
  public getAnker(): ElementRef<HTMLElement> {
    return this.button();
  }
 
  public getScrollTarget(): ElementRef<HTMLElement> | undefined {
    return this.containerElement();
  }
 
  public _isSelected(item: IndexedItem<Item>): boolean {
    return item.value.key === this.selectedItemKey();
  }
 
  public _onSelect(event: SelectionEvent): void {
    const item = this.storage.indexItems.find(i => i.key === event.key);
    if (item && !this.disabledItems.has(item.key)) {
      this._impl_onSelect(item.key);
    }
  }
 
  /** @ignore */
  public _impl_onSelect(key: Key): void {
    if (!this._isDisabled(key)) {
      const value = this.storage.getValueByKey(key);
      if (value !== undefined) {
        this._impl_handleChange(value);
        this._impl_close(true);
      }
    }
  }
 
  /** @ignore */
  public _impl_handleChange(value: Item): void {
    this.selectedItemKey.set(value.key);
  }
 
  /** @ignore */
  protected override _impl_onOpen(focusItem: KeyFocusTarget): KeyFocusTarget {
    // always open selected value if it exists
    const key = this.storage.getItemByKey(this.selectedItemKey())?.key;
    if (key !== undefined) {
      focusItem = key;
    }
    return focusItem;
  }
 
  /** @ignore */
  protected _itemTrackBy(item: Item): string {
    return itemTrack(item);
  }
}