All files / lib/internal/ext-search-label-container ext-search-label-container.component.ts

98.74% Statements 236/239
93.75% Branches 30/32
100% Functions 11/11
98.74% Lines 236/239

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 2401x 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 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 156x 156x 156x 156x 156x 530x 156x 156x 156x 156x 1x 1x 1x 156x 156x 156x 156x 1x 1x 1x 19x 19x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 162x 162x 162x 162x 162x 1x 1x 1x 3055x 3055x 625x 405x 3055x 3055x 1x 1x 1x 611x 611x 611x 81x 530x 611x 1x 1x 1x 530x 530x 530x 530x 530x 530x 530x 530x       530x 530x 530x 530x 530x 530x 243x 243x 243x 243x 243x 243x 287x 287x 287x 530x 530x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ContentChild,
  ContentChildren,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnDestroy,
  Output,
  QueryList,
  ViewChild,
} from '@angular/core';
 
import { Item, Key, WidgetKeySet } from '../../utils/util.types';
import { createWidgetKeySet } from '../../utils/utilities';
 
import { LabelContainerErrorMessageDirective } from '../two-slot-label-container/two-slot-label-container.component';
import {
  EndSlotContentDirective,
  StartSlotContentDirective,
} from './ext-search-label-container.directive';
 
/**
 * Component used for combining one or two input elements/components and optionally contains a settings-/remove-button.
 */
@Component({
  selector: 'gc-ext-search-label-container',
  templateUrl: './ext-search-label-container.component.html',
  styleUrls: ['./ext-search-label-container.component.css'],
})
export class ExtSearchLabelContainerComponent
  implements OnDestroy, AfterViewInit
{
  /**
   * Sets the text in the label slot
   */
  @Input()
  public label = '';
 
  /**
   * Items displayed in the settings-menu
   */
  @Input()
  public settingsMenuItems?: readonly Item[];
 
  /**
   * Add remove-button to the container
   */
  @Input()
  public showRemoveButton = false;
 
  /**
   * Decides which slots contain errors. Undefined === none
   */
  @Input()
  public errorSlots?: 'start' | 'end' | 'both';
 
  /**
   * Sets the settings-menu default item key
   */
  @Input()
  public defaultSettingsKey?: Key;
 
  /**
   * Sets the settings-menu selected item key
   */
  @Input()
  public selectedSettingsKey?: Key;
 
  /**
   * Overrides the internal vertical mode state to allow a consistent responsive view for all components inside a container/group
   */
  @Input()
  public globalVerticalMode?: boolean;
 
  /**
   * Overrides focus border visibility.
   */
  @Input()
  public hideFocusBorder = false;
 
  /**
   * Marks the input field as required.
   */
  @Input()
  public required = false;
 
  /**
   * Action that is fired when clicked on remove-button
   */
  @Output()
  public readonly onRemoveButtonAction: EventEmitter<void> =
    new EventEmitter<void>();
 
  /**
   * Action that is fired when selected settings-item changes
   */
  @Output()
  public readonly onSettingsMenuAction: EventEmitter<Item> =
    new EventEmitter<Item>();
 
  /**
   * Fires when component responsive mode changes
   */
  @Output()
  public readonly onVerticalModeChange: EventEmitter<boolean> =
    new EventEmitter<boolean>();
 
  /** @ignore */
  @ViewChild('sizeHelper')
  public sizeHelper?: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ContentChild(StartSlotContentDirective)
  public startSlotTemplate?: StartSlotContentDirective;
 
  /** @ignore */
  @ContentChild(EndSlotContentDirective)
  public endSlotTemplate?: EndSlotContentDirective;
 
  /** @ignore */
  @ContentChildren(LabelContainerErrorMessageDirective)
  protected errorMessageElements?: QueryList<LabelContainerErrorMessageDirective>;
 
  /** @ignore */
  protected verticalMode = false;
 
  /** @ignore */
  protected readonly widgetKeys: WidgetKeySet = createWidgetKeySet(
    'gc-ext-search-label-container',
  );
 
  /** @ignore */
  protected readonly settingsButtonDescriptionId =
    this.widgetKeys.baseKey + '-settings-desc';
 
  /** @ignore */
  private containerResizeObserver?: ResizeObserver;
 
  constructor(
    private el: ElementRef<HTMLElement>,
    private zone: NgZone,
  ) {
    this.zone.runOutsideAngular(() => {
      this.containerResizeObserver = new ResizeObserver(entries =>
        this.computeWidths(entries[0].target),
      );
      this.containerResizeObserver.observe(this.el.nativeElement);
    });
  }
 
  /** @ignore */
  ngAfterViewInit(): void {
    if (this.containerResizeObserver && this.sizeHelper) {
      this.containerResizeObserver.observe(this.sizeHelper.nativeElement);
    }
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this.containerResizeObserver?.disconnect();
  }
 
  /** @ignore */
  protected handleRemoveButtonAction(): void {
    this.onRemoveButtonAction.emit();
  }
 
  /** @ignore */
  protected selectSettingsMenuAction(settingsMenuAction: Item): void {
    if (this.selectedSettingsKey !== settingsMenuAction.key) {
      this.onSettingsMenuAction.emit(settingsMenuAction);
    }
  }
 
  /** @ignore */
  protected getSettingsSelectionText() {
    const selectedItem = this.settingsMenuItems?.find(
      i => i.key === this.selectedSettingsKey,
    );
    return selectedItem ? `${selectedItem.label} aktiv` : '';
  }
 
  /** @ignore */
  protected hasNonDefaultSettingsSelection() {
    return (
      this.defaultSettingsKey &&
      this.selectedSettingsKey &&
      this.selectedSettingsKey !== this.defaultSettingsKey
    );
  }
 
  /** @ignore */
  protected getTooltipText() {
    const defaultSettingsMenuTooltipText = 'Einstellungen öffnen';
 
    return this.hasNonDefaultSettingsSelection()
      ? `**${defaultSettingsMenuTooltipText}** (${this.getSettingsSelectionText()})`
      : defaultSettingsMenuTooltipText;
  }
 
  /** @ignore */
  private computeWidths(target: Element): void {
    NgZone.assertNotInAngularZone();
 
    const calcVerticalMode = () => {
      if (this.sizeHelper) {
        const verticalMode =
          this.el.nativeElement.clientWidth <
          this.sizeHelper.nativeElement.clientWidth;
        if (verticalMode !== this.verticalMode) {
          this.zone.run(() => {
            this.verticalMode = verticalMode;
            this.onVerticalModeChange.emit(verticalMode);
          });
        }
      }
    };
 
    if (target === this.el.nativeElement) {
      /**
       * setTimeout is needed, because otherwise the sizeHelper is not rendered yet with the new value,
       * which leads to an enabled verticalMode everytime a character gets removed for a short time period until the sizeHelper is finally rendered
       */
      setTimeout(() => {
        calcVerticalMode();
      });
    } else {
      calcVerticalMode();
    }
  }
}