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

99.26% Statements 271/273
84% Branches 21/25
100% Functions 8/8
99.26% Lines 271/273

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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 2741x 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 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 1x 1x 1x 1x 1x 306x 306x 306x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 1x 1x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 9x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 9x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 57x 57x 57x 57x 57x 87x 87x 87x 87x 87x 87x 69x 69x 69x 69x 69x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x   5x 5x 5x 5x 5x 5x 7x 7x 7x 7x 1x  
import {
  ApplicationRef,
  ComponentRef,
  ElementRef,
  EmbeddedViewRef,
  Injectable,
  Injector,
  StaticProvider,
  Type,
  createComponent,
} from '@angular/core';
 
import { AlertDialogComponent } from '../../alert-dialog/alert-dialog.component';
import { DynamicHeadingLevelService } from '../../dynamic-heading/dynamic-heading-level.service';
import { ElementInsertionContextService } from '../element-insertion-context.service';
import {
  DialogModalOverlayContainerComponent,
  DialogOverlayRef,
} from './dialog-modal-overlay-container/dialog-modal-overlay-container';
import {
  SidebarOverlayContainerComponent,
  SidebarOverlayRef,
} from './sidebar-overlay-container/sidebar-overlay-container.component';
 
const MODAL_STACK: SidebarOverlayContainerComponent[] = [];
 
export type ModalType = 'default' | 'error';
 
export class ModalConfig {
  public readonly type: ModalType;
  public readonly offsetTop?: number;
  public readonly label?: string;
  public readonly variant?: string;
 
  constructor(init?: {
    type?: ModalType;
    offsetTop?: number;
    variant?: 'dialog' | 'sidebar' | 'alertdialog';
    label?: string;
  }) {
    this.type = init?.type ?? 'default';
    this.offsetTop = init?.offsetTop;
    this.label = init?.label;
    this.variant = init?.variant;
  }
}
 
@Injectable()
export class ModalOverlayContainerService {
  constructor(
    private appRef: ApplicationRef,
    private injector: Injector,
  ) {}
 
  public openSidebar<T>(
    component: Type<T>,
    config?: ModalConfig,
    additionalProviders?: StaticProvider[],
  ): {
    overlayRef: SidebarOverlayRef;
    componentRef: ComponentRef<T>;
    instance: T;
  } {
    const ref = this.initSidebarOverlay<T>(
      component,
      config,
      additionalProviders,
    );
 
    return {
      overlayRef: ref.overlayRef,
      componentRef: ref.componentRef,
      instance: ref.componentRef.instance,
    };
  }
 
  public openDialog<T>(
    component: Type<T>,
    config?: ModalConfig,
    additionalProviders?: StaticProvider[],
  ): {
    overlayRef: DialogOverlayRef;
    componentRef: ComponentRef<T>;
    instance: T;
  } {
    const ref = this.initDialogOverlay<T>(
      component,
      config,
      additionalProviders,
    );
 
    return {
      overlayRef: ref.overlayRef,
      componentRef: ref.componentRef,
      instance: ref.componentRef.instance,
    };
  }
 
  private initDialogOverlay<T>(
    component: Type<T>,
    config?: ModalConfig,
    additionalProviders?: StaticProvider[],
  ): { componentRef: ComponentRef<T>; overlayRef: DialogOverlayRef } {
    const dialogOverlayRef: DialogOverlayRef = new DialogOverlayRef();
    const modalProviders: StaticProvider[] = [
      { provide: ModalConfig, useValue: config ?? new ModalConfig() },
    ];
 
    if (additionalProviders !== undefined) {
      modalProviders.push(additionalProviders);
    }
    const modalComponentInjector = Injector.create({
      parent: this.injector,
      providers: modalProviders,
    });
    const createModalComponentOptions = {
      environmentInjector: this.appRef.injector,
      elementInjector: modalComponentInjector,
    };
    const modalRef = createComponent(
      DialogModalOverlayContainerComponent,
      createModalComponentOptions,
    );
 
    const dialogElement = modalRef.injector
      .get<ElementRef<HTMLElement>>(ElementRef)
      .nativeElement.querySelector('dialog');
 
    if (dialogElement === null) {
      // HTMLDialogElement always exists in the DialogModalOverlayContainerComponent
      throw new Error('unable to find dialog element');
    }
 
    const componentProviders: StaticProvider[] = [
      { provide: DialogOverlayRef, useValue: dialogOverlayRef },
      {
        provide: DynamicHeadingLevelService,
        useValue: new DynamicHeadingLevelService(null, {
          kind: 'absolute',
          start: 2,
        }),
      },
      {
        provide: ElementInsertionContextService,
        useValue: new ElementInsertionContextService(dialogElement),
      },
    ];
 
    if (additionalProviders !== undefined) {
      componentProviders.push(additionalProviders);
    }
    const componentInjector = Injector.create({
      parent: this.injector,
      providers: componentProviders,
    });
    const createComponentOptions = {
      environmentInjector: this.appRef.injector,
      elementInjector: componentInjector,
    };
    const componentRef = createComponent(component, createComponentOptions);
 
    modalRef.setInput('component', componentRef);
    modalRef.setInput('label', config?.label);
 
    if (componentRef.componentType === AlertDialogComponent) {
      const alertDialogComponent =
        componentRef.instance as AlertDialogComponent;
      modalRef.setInput('describedby', alertDialogComponent._descId);
      modalRef.setInput('labelledby', alertDialogComponent._labelId);
    }
 
    this.appRef.attachView(modalRef.hostView);
 
    const modalDomElement = (modalRef.hostView as EmbeddedViewRef<never>)
      .rootNodes[0] as Node; // NOSONAR - false positive typescript:S4325
    const destroy = () => {
      this.appRef.detachView(componentRef.hostView);
      this.appRef.detachView(modalRef.hostView);
 
      componentRef.destroy();
      modalRef.destroy();
    };
 
    // Force blurring so that even if nothing is focused later
    // the opening control does not have focus anymore
    if (document.activeElement) {
      (document.activeElement as HTMLElement).blur();
    }
 
    document.body.appendChild(modalDomElement);
 
    // listens for elements' native close events
    modalRef.instance.onClose.subscribe(destroy);
 
    dialogOverlayRef.onClosed.subscribe(destroy);
 
    return { componentRef, overlayRef: dialogOverlayRef };
  }
 
  private initSidebarOverlay<T>(
    component: Type<T>,
    config?: ModalConfig,
    additionalProviders?: StaticProvider[],
  ): { componentRef: ComponentRef<T>; overlayRef: SidebarOverlayRef } {
    const sidebarOverlayRef: SidebarOverlayRef = new SidebarOverlayRef();
 
    const providers: StaticProvider[] = [
      { provide: SidebarOverlayRef, useValue: sidebarOverlayRef },
      { provide: ModalConfig, useValue: config ?? new ModalConfig() },
      {
        provide: DynamicHeadingLevelService,
        useValue: new DynamicHeadingLevelService(null, {
          kind: 'absolute',
          start: 2,
        }),
      },
    ];
 
    if (additionalProviders !== undefined) {
      providers.push(additionalProviders);
    }
    const componentInjector = Injector.create({
      parent: this.injector,
      providers,
    });
    const createComponentOptions = {
      environmentInjector: this.appRef.injector,
      elementInjector: componentInjector,
    };
    const modalRef = createComponent(
      SidebarOverlayContainerComponent,
      createComponentOptions,
    );
 
    if (MODAL_STACK.length > 0) {
      MODAL_STACK[MODAL_STACK.length - 1]._inactive = true;
    }
    MODAL_STACK.push(modalRef.instance);
 
    const componentRef = createComponent(component, createComponentOptions);
 
    this.appRef.attachView(modalRef.hostView);
    this.appRef.attachView(componentRef.hostView);
 
    const modalDomElement = (modalRef.hostView as EmbeddedViewRef<never>)
      .rootNodes[0] as Node; // NOSONAR - false positive typescript:S4325
    const domElem = (componentRef.hostView as EmbeddedViewRef<never>)
      .rootNodes[0] as Node; // NOSONAR - false positive typescript:S4325
 
    // Force blurring so that even if nothing is focused later
    // the opening control does not have focus anymore
    if (document.activeElement) {
      (document.activeElement as HTMLElement).blur();
    }
 
    modalDomElement.appendChild(domElem);
    document.body.appendChild(modalDomElement);
 
    sidebarOverlayRef.onClosed.subscribe(() => {
      MODAL_STACK.pop();
      if (MODAL_STACK.length > 0) {
        MODAL_STACK[MODAL_STACK.length - 1]._inactive = false;
      }
      this.appRef.detachView(componentRef.hostView);
      componentRef.destroy();
 
      this.appRef.detachView(modalRef.hostView);
      modalRef.destroy();
    });
 
    return { componentRef, overlayRef: sidebarOverlayRef };
  }
}