All files / lib/internal/modal-overlay/dialog-modal-overlay-container dialog-modal-overlay-container.ts

94.62% Statements 176/186
89.65% Branches 26/29
83.33% Functions 10/12
94.62% Lines 176/186

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 1871x 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 108x 108x 108x 108x 108x 108x 108x 108x 2333x 2333x 108x 108x 1x 1x 79x 79x 79x 79x 79x 79x 79x 79x 1x 1x 79x 1752x 1299x 453x 1752x 79x 79x 79x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 1x 1x 1x 1x 1x 1x       1x 1x 87x 87x 87x 87x 87x 87x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 87x 87x 87x 87x 1x 1x 87x 87x 87x 87x 1x 1x       1x 1x 1x 87x 87x 87x 87x         87x 87x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 13x 13x 13x 13x 13x 17x 17x 17x 1x  
import {
  AfterViewInit,
  Component,
  ComponentRef,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  Output,
  ViewChild,
  ViewContainerRef,
  ViewEncapsulation,
} from '@angular/core';
 
import { getKeyboardFocusableElements } from '../../../utils/internal-utils';
 
import { ModalConfig } from '../modal-overlay-container.service';
 
export class DialogOverlayRef {
  public readonly onClosed: EventEmitter<void> = new EventEmitter<void>();
  private focusNode?: HTMLElement;
  private _disabledElements: Map<Element, string | null> = new Map<
    Element,
    string | null
  >();
 
  constructor() {
    if (
      document.activeElement !== null &&
      document.activeElement instanceof HTMLElement
    ) {
      this.focusNode = document.activeElement;
    }
 
    getKeyboardFocusableElements(document.body).forEach(el => {
      this._disabledElements.set(el, el.getAttribute('tabindex'));
      el.setAttribute('tabindex', '-1');
    });
  }
 
  public close(): void {
    this.resetTabIndex();
 
    this.onClosed.next();
 
    setTimeout(() => {
      this.focusNode?.focus();
    });
  }
 
  private resetTabIndex() {
    this._disabledElements.forEach((v, k) => {
      if (v === null) {
        k.removeAttribute('tabindex');
      } else {
        k.setAttribute('tabindex', v);
      }
    });
  }
}
 
@Component({
  selector: 'gc-dialog-modal-overlay-container',
  template: ` <dialog
    #dialog
    [attr.aria-label]="labelledby ? null : label"
    [attr.aria-labelledby]="labelledby"
    [attr.aria-describedby]="describedby"
    [attr.role]="_role"
    class="gc-dialog-modal-overlay-container"
    (close)="handleDialogClose()"
    (click)="handleDialogClick($event)"
    (focus)="handleDialogFocus($event)"
  >
    <ng-container #container />
  </dialog>`,
  styleUrls: ['./dialog-modal-overlay-container.css'],
  // eslint-disable-next-line @angular-eslint/use-component-view-encapsulation
  encapsulation: ViewEncapsulation.None,
})
export class DialogModalOverlayContainerComponent implements AfterViewInit {
  @HostBinding('class.gc-modal-error')
  public _errorType = false;
 
  @HostBinding('class.gc-modal-inactive')
  public _inactive = false;
 
  @Input()
  public label?: string;
 
  @Input()
  public describedby: string | null = null;
 
  @Input()
  public labelledby: string | null = null;
 
  @Output()
  public readonly onClose: EventEmitter<void> = new EventEmitter<void>();
 
  @ViewChild('container', { read: ViewContainerRef })
  private container?: ViewContainerRef;
 
  @ViewChild('dialog')
  private dialog?: ElementRef<HTMLDialogElement>;
 
  @Input()
  public get component(): ComponentRef<unknown> | undefined {
    return this._component;
  }
 
  public set component(c: ComponentRef<unknown>) {
    this.container?.clear();
    this._component = c;
    setTimeout(() => {
      this.container?.insert(c.hostView);
    });
  }
 
  protected _role: 'alertdialog' | null = null;
 
  private _component?: ComponentRef<unknown>;
 
  private lastFocusedElement?: HTMLElement;
 
  private focusNode?: HTMLElement;
 
  constructor(config: ModalConfig) {
    this._errorType = config.type === 'error';
    this._role = config.variant === 'alertdialog' ? 'alertdialog' : null;
    this.focusNode = document.activeElement as HTMLElement;
  }
 
  ngAfterViewInit(): void {
    if (this.dialog) {
      this.dialog.nativeElement.showModal();
    }
  }
 
  protected handleDialogClose(): void {
    this.onClose.emit();
    this.focusNode?.focus();
  }
 
  // workaround for inconsistent browser focus event behavior (HTMLDialogElement receives focus on Chromium but not on FF)
  protected handleDialogFocus(event: FocusEvent) {
    if (!this.dialog) {
      return;
    }
    if (this.lastFocusedElement) {
      if (event.target === this.dialog.nativeElement) {
        event.preventDefault();
      }
      this.lastFocusedElement.focus();
    }
  }
 
  // workaround for inconsistent browser focus event behavior (HTMLDialogElement receives focus on Chromium but not on FF)
  protected handleDialogClick(e: MouseEvent): void {
    if (!this.dialog) {
      return;
    }
 
    const target = e.target;
    const focusableElements = getKeyboardFocusableElements(
      this.dialog.nativeElement,
    );
 
    if (
      target &&
      target instanceof HTMLElement &&
      target !== this.dialog.nativeElement &&
      focusableElements.includes(target)
    ) {
      this.lastFocusedElement = target;
    }
 
    const focusListenerCallback = () => {
      this.lastFocusedElement?.removeEventListener(
        'focus',
        focusListenerCallback,
      );
      this.lastFocusedElement = undefined;
    };
    this.lastFocusedElement?.addEventListener('focus', focusListenerCallback);
  }
}