All files / lib/ext-search/ext-search-group ext-search-group.component.ts

98.06% Statements 254/259
97.22% Branches 35/36
94.11% Functions 16/17
98.06% Lines 254/259

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 254 255 256 257 258 259 2601x 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 19x 19x 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 27x 27x 27x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 218x 218x 218x 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 27x 27x 27x 27x 1x 1x 1x 27x 27x 1x 1x 1x 27x 27x 27x 100x 100x 28x 28x 28x 28x 28x 100x 100x 27x 27x 27x 27x 1x 1x 1x 8x 8x 1x 1x 4x 4x 3x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 86x 86x 40x 40x 40x 40x 40x 86x 1x 1x 1x 1x 1x 1x 80x 80x 80x 80x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  ChangeDetectorRef,
  Component,
  ContentChildren,
  Directive,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  OnDestroy,
  OnInit,
  Optional,
  Output,
  QueryList,
  TemplateRef,
  ViewChild,
  forwardRef,
} from '@angular/core';
import { Subscription } from 'rxjs';
 
import {
  Callback,
  FilterSlots,
  FocusableElementOwner,
  SlotItem,
  WidgetKeySet,
} from '../../utils/util.types';
import { createWidgetKeySet } from '../../utils/utilities';
 
import { ButtonComponent } from '../../button/button.component';
import {
  BaseExtSearchComponent,
  GlobalVerticalModeChange,
} from '../base-ext-search/base-ext-search-field.component';
import { ExtSearchGroupContainerComponent } from '../ext-search-group-container/ext-search-group-container.component';
 
interface ExtSearchGroupContainerTemplateContext {
  $implicit: Callback;
}
 
/**
 * A directive allowing to tag the container content template of the extended search group, where
 * the $implicit value is of type Callback
 */
@Directive({
  selector: 'ng-template[data-gc-ext-search-container]',
})
export class ExtSearchGroupContainerTemplateDirective {
  constructor(
    public readonly template: TemplateRef<ExtSearchGroupContainerTemplateContext>,
  ) {}
 
  /**
   * @ignore
   */
  static ngTemplateContextGuard(
    dir: ExtSearchGroupContainerTemplateDirective,
    ctx: unknown,
  ): ctx is ExtSearchGroupContainerTemplateContext {
    return true;
  }
}
/**
 * `gc-ext-search-group` is used to make a group of one or more extended search components (i.e. `gc-ext-search-text`)
 */
@Component({
  selector: 'gc-ext-search-group',
  templateUrl: './ext-search-group.component.html',
  styleUrls: ['./ext-search-group-component.css'],
  host: {
    role: 'group',
  },
})
export class ExtSearchGroupComponent
  implements FocusableElementOwner, OnDestroy, OnInit, AfterViewInit
{
  /**
   * Shows the remove-button if enabled
   */
  @Input()
  public showRemoveButton = false;
 
  /**
   * Content of the filter container that shows up, when the "add-to-group-button" is pressed.
   */
  @Input({ required: true })
  public filterContainerTemplate?: TemplateRef<ExtSearchGroupContainerTemplateContext>;
 
  /**
   * Action that is fired when clicked on remove-button
   */
  @Output()
  public readonly onRemoveButtonAction: EventEmitter<void> =
    new EventEmitter<void>();
 
  /**
   * Sets the header text of the filter group
   */
  @Input({ required: true })
  public label = '';
 
  /**
   * @ignore
   */
  @ContentChildren(forwardRef(() => BaseExtSearchComponent))
  protected content?: QueryList<BaseExtSearchComponent>;
 
  /**
   * @ignore
   */
  @ViewChild('addToGroupButton', { read: ElementRef })
  protected addToGroupButtonElement?: ElementRef<HTMLElement>;
 
  /** @ignore */
  @ViewChild('addToGroupButton')
  protected addToGroupButtonComponent?: ButtonComponent;
 
  /** @ignore */
  @HostBinding('attr.aria-labelledby')
  protected get _ariaLabelledby(): string {
    return this._widgetKeys.labelKey;
  }
 
  /**
   * @ignore
   * (Internal) Event is fired, when vertical-mode of group changes
   */
  public readonly _onVerticalModeChange: EventEmitter<GlobalVerticalModeChange> =
    new EventEmitter<GlobalVerticalModeChange>();
 
  /** @ignore */
  protected readonly _widgetKeys: WidgetKeySet = createWidgetKeySet(
    'gc-ext-search-group',
  );
 
  /** @ignore */
  protected _filterContainerVisible = false;
 
  /** @ignore */
  protected containerAnker?: ElementRef<HTMLElement>;
 
  /** @ignore */
  protected readonly containerActions: SlotItem<FilterSlots>[] = [
    {
      key: 'action-1',
      label: 'Schließen',
      slot: 'footer',
    },
  ];
 
  /** @ignore */
  protected verticalMode = false;
 
  /** @ignore */
  private vertModeSubscription?: Subscription;
 
  constructor(
    @Optional()
    protected extSearchContainer: ExtSearchGroupContainerComponent | null,
    private cdr: ChangeDetectorRef,
  ) {}
 
  /** @ignore */
  public ngAfterViewInit(): void {
    this.containerAnker = this.addToGroupButtonElement;
  }
 
  /** @ignore */
  public ngOnInit(): void {
    this.vertModeSubscription =
      this.extSearchContainer?._onVerticalModeChange.subscribe(
        verticalModeUpdate => {
          this.verticalMode = verticalModeUpdate.newState;
          if (verticalModeUpdate.onDestroy) {
            /**
             * Manually trigger change detection if vertical-mode changes, because an element got removed.
             * Otherwise would result in an NG0100 error, because change-detection could already run before `_onVerticalModeChange()` is emitted
             */
            this.cdr.detectChanges();
          }
          this._onVerticalModeChange.emit(verticalModeUpdate);
        },
      );
    this.extSearchContainer?._updateGlobalVertState(false);
  }
 
  /** @ignore */
  public ngOnDestroy(): void {
    this.vertModeSubscription?.unsubscribe();
  }
 
  public focusChild(): boolean {
    if (this.content) {
      const el = this.content.get(0);
      if (el?.focusChild()) {
        return true;
      }
    }
    return this.moveFocusToAddButton();
  }
 
  /**
   * Try to move the focus to the Add-button
   */
  public moveFocusToAddButton(): boolean {
    return this.addToGroupButtonComponent?.focusChild() ?? false;
  }
 
  /**
   * @ignore
   * Refresh and update the global vertical state for all components
   */
  public _updateGlobalVertState(onDestroy: boolean) {
    if (this.extSearchContainer) {
      this.extSearchContainer._updateGlobalVertState(onDestroy);
    } else {
      const newState: boolean = this._getInternalVerticalMode();
      this.verticalMode = newState;
      this._onVerticalModeChange.emit({ newState, onDestroy });
    }
  }
 
  /**
   * @ignore
   * Returns the vertical mode state of the internal group
   */
  public _getInternalVerticalMode(): boolean {
    return (
      this.content?.some(entry => entry._getInternalVerticalMode()) ?? false
    );
  }
 
  /**
   * @ignore
   */
  protected filterContainerCallback: Callback = () => {
    this.closeFilterContainer();
  };
 
  /**
   * @ignore
   */
  protected closeFilterContainer(): void {
    this._filterContainerVisible = false;
  }
 
  /**
   * @ignore
   */
  protected openFilterContainer() {
    this._filterContainerVisible = true;
  }
}