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

97.8% Statements 223/228
91.11% Branches 41/45
100% Functions 14/14
97.8% Lines 223/228

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 2291x 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 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1178x 1178x 1178x 1178x 1178x 9x 9x 1178x 1178x 1178x 1178x 1178x 10x 10x 3x 3x 3x 3x 3x 3x 1x 2x 2x 3x 3x   3x 3x 10x 10x 3x 7x 5x 2x 2x 2x 2x 2x 10x 10x 10x 10x 10x 10x 1178x 1178x 1178x 1178x 1178x 572x 572x 572x 572x 572x     572x 572x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 19x 19x 19x 19x 19x 19x 19x 10x 10x 9x 9x 19x 19x   19x 19x 1178x 1178x 1178x 1178x 1178x 286x 286x 1178x 1178x 1178x 1178x 1178x 1178x 1178x 21x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 9x 9x 9x 9x 9x 21x 21x   21x 21x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ContentChild,
  Directive,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  ViewChild,
  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'],
})
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;
 
  /**
   * Adapter that manages the conversion of item types.
   */
  @Input()
  public itemAdapter?: Converter<T, I>;
 
  /**
   * 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
   */
  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.focusChild();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  public _onAddButtonPress(): void {
    this.onAddButtonPress.emit();
  }
 
  /**
   * @ignore
   */
  public _onDeleteButtonPress(deletedItem: T): void {
    let focusTarget;
    if (this.items.length > 1) {
      const elements = this.el.nativeElement.querySelectorAll(
        'div.gc-container [data-gc-focus-target]',
      );
      if (elements.length === this.items.length) {
        const idx = this.items.indexOf(deletedItem);
        if (idx + 1 < this.items.length) {
          focusTarget = elements[idx + 1];
        } else {
          focusTarget = elements[idx - 1];
        }
      } else {
        console.warn('Unable to restore focus');
      }
    }
 
    if (focusTarget) {
      (focusTarget as HTMLElement).focus();
    } 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 = (_index: number, 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]',
})
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');
    }
  }
}