All files / lib/input-list input-list.component.ts

97.23% Statements 246/253
91.22% Branches 52/57
95.83% Functions 23/24
97.23% Lines 246/253

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 2541x 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 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 2x 1x 1x 1x 1x 1x     2x 2x 42x 42x 42x 42x 42x 11x 11x 42x 42x 42x 42x 42x 11x 11x 11x 3x 3x 3x 3x 3x 3x 3x   11x 11x 11x 11x 3x 8x 6x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 11x 42x 42x 42x 42x 42x 405x 405x 405x 405x 405x     405x 405x 42x 42x 42x 42x 42x 42x 42x 23x 23x 23x 23x 23x 23x 23x 12x 12x 11x 11x 23x 23x   23x 23x 42x 42x 42x 42x 42x 29x 29x 1284x 1284x 1284x 1284x 1284x 1284x 1284x 1284x 25x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 11x 11x 11x 11x 11x 25x 25x   25x 25x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ContentChild,
  Directive,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
  ViewContainerRef,
} from '@angular/core';
 
import { Converter, FocusableElementOwner, Item } from '../utils/util.types';
import { isItem, itemTrack } from '../utils/utilities';
 
import { TemplateDirective } from '../internal/angular-directives';
 
import { ButtonComponent } from '../button/button.component';
 
/**
 * Input list component that allows adding
 * one or more input fields of a desired type, rendered
 * by using a passed template.
 */
@Component({
  selector: 'gc-input-list',
  templateUrl: './input-list.component.html',
  styleUrls: ['./input-list.component.css'],
  standalone: false,
})
export class InputListComponent<T, I extends Item = Item>
  implements FocusableElementOwner
{
  /** @ignore */
  @ViewChild('gcAddButton')
  public addButton?: ButtonComponent;
 
  /**
   * Items that will render as input fields.
   */
  @Input()
  public items: readonly T[] = [];
 
  /**
   * Label that will show on the button that adds items.
   */
  @Input()
  public buttonLabel = '';
 
  /**
   * Maximum number of items that can be added.
   * A zero (0) means that an endless number of items can be added.
   */
  @Input()
  public maxItems = 0;
 
  /**
   * Minimum number of items.
   */
  @Input()
  public minItems = 0;
 
  /**
   * Adapter that manages the conversion of item types.
   */
  @Input()
  public itemAdapter?: Converter<T, I>;
 
  /**
   * Decides if the control itself (not its child controls) is rendered readonly
   */
  @Input()
  public readonly = false;
 
  /**
   * Decides if the add/remove buttons are hidden.
   */
  @Input()
  public hideAddRemove = false;
 
  /**
   * Fired when the add button is pressed.
   */
  @Output()
  public readonly onAddButtonPress: EventEmitter<void> =
    new EventEmitter<void>();
 
  /**
   * Fired when the delete button is pressed.
   */
  @Output()
  public readonly onDeleteButtonPress: EventEmitter<T> = new EventEmitter<T>();
 
  /**
   * @ignore
   */
  @ContentChild(TemplateDirective)
  public inputTemplate?: TemplateDirective;
 
  /**
   * @ignore
   */
  @ViewChildren('deleteButton')
  protected deleteButtons?: QueryList<ButtonComponent>;
 
  /**
   * @ignore
   */
  public deferredFocus?: T;
 
  constructor(private el: ElementRef<HTMLElement>) {}
 
  public focusChild(): boolean {
    if (this.items.length > 0) {
      this.focusItem(this.items[0]);
      return true;
    } else if (this.addButton && !this.addButton.disabled) {
      this.addButton.focusChild();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  public _onAddButtonPress(): void {
    this.onAddButtonPress.emit();
  }
 
  /**
   * @ignore
   */
  public _onDeleteButtonPress(deletedItem: T): void {
    let deleteButton;
    if (this.items.length > 1 && this.items.length - 1 > this.minItems) {
      if (
        this.deleteButtons &&
        this.deleteButtons.length === this.items.length
      ) {
        const idx = this.items.indexOf(deletedItem);
        const targetIdx = idx + 1 < this.items.length ? idx + 1 : idx - 1;
        deleteButton = this.deleteButtons.get(targetIdx);
      } else {
        console.warn('Unable to restore focus');
      }
    }
 
    if (deleteButton) {
      deleteButton.focusChild();
    } else if (this.addButton) {
      this.addButton.focusChild();
    } else {
      // The add button might not be visible so wait a tick
      setTimeout(() => {
        if (this.addButton) {
          this.addButton.focusChild();
        }
      });
    }
 
    this.onDeleteButtonPress.emit(deletedItem);
  }
 
  /**
   * @ignore
   */
  public getAsItem(item: T): Item {
    if (this.itemAdapter === undefined) {
      if (isItem(item)) {
        return item;
      }
      throw new Error('missing converter');
    } else {
      return this.itemAdapter(item);
    }
  }
 
  /**
   * Move the input focus to the UI-Element representing the provided item
   *
   * @param item the item to focus
   */
  public focusItem(item: T) {
    const elements = this.el.nativeElement.querySelectorAll(
      'div.gc-container [data-gc-focus-target]',
    );
    if (elements.length === this.items.length) {
      const idx = this.items.indexOf(item);
      const element = elements[idx];
      if (idx !== -1) {
        (element as HTMLElement).focus();
        this.deferredFocus = undefined;
      } else {
        this.deferredFocus = item;
      }
    } else {
      this.deferredFocus = item;
    }
  }
 
  /**
   * @ignore
   */
  public _itemTrackBy = (item: T) => {
    // this makes sure we are bound to this instead of the differ
    return itemTrack(this.getAsItem(item));
  };
}
 
@Directive({
  selector: '[data-gc-input-list-item]',
  standalone: false,
})
export class InputListItemDirective<T> implements AfterViewInit {
  @ContentChild('vc', { read: ViewContainerRef })
  public viewContainer!: ViewContainerRef;
 
  @Input('data-gc-input-list-item')
  public gcInputListItem!: T;
 
  constructor(private list: InputListComponent<T>) {}
 
  ngAfterViewInit(): void {
    if (this.list.inputTemplate?.template) {
      const template = this.list.inputTemplate.template;
      setTimeout(() => {
        this.viewContainer.createEmbeddedView(template, {
          item: this.gcInputListItem,
        });
 
        // If there's a deferred focus item we should try to focus it
        if (this.list.deferredFocus) {
          const item = this.list.deferredFocus;
          setTimeout(() => {
            this.list.focusItem(item);
          });
        }
      });
    } else {
      console.error('It is mandatory to provide a template');
    }
  }
}