File

projects/rebirth-ng/src/lib/overlay/overlay.service.ts

Index

Properties
Methods

Constructor

constructor(rebirthNGConfig: RebirthNGConfig, componentFactoryResolver: ComponentFactoryResolver, injector: Injector, domSanitizer: DomSanitizer)
Parameters :
Name Type Optional
rebirthNGConfig RebirthNGConfig no
componentFactoryResolver ComponentFactoryResolver no
injector Injector no
domSanitizer DomSanitizer no

Methods

close
close()
Returns : void
Private createOverlay
createOverlay(options: OverlayOptions)
Parameters :
Name Type Optional
options OverlayOptions no
Returns : void
open
open(options: OverlayOptions)
Parameters :
Name Type Optional
options OverlayOptions no
Returns : void

Properties

Private overlayCount
overlayCount: number
Type : number
Default value : 0
Private overlayRef
overlayRef: ComponentRef<OverlayComponent>
Type : ComponentRef<OverlayComponent>
import { ComponentFactoryResolver, ComponentRef, Injectable, Injector } from '@angular/core';
import { RebirthNGConfig } from '../rebirth-ng.config';
import { OverlayComponent } from './overlay.component';
import { OverlayOptions } from './overlay-options.model';
import { DomSanitizer } from '@angular/platform-browser';

@Injectable({ providedIn: 'root' })
export class OverlayService {

  private overlayRef: ComponentRef<OverlayComponent>;
  private overlayCount = 0;

  constructor(private rebirthNGConfig: RebirthNGConfig, private componentFactoryResolver: ComponentFactoryResolver,
              private injector: Injector, private domSanitizer: DomSanitizer) {
  }

  open(options: OverlayOptions): void {
    if (!this.overlayCount++) {
      this.createOverlay(options);
    }
  }

  private createOverlay(options: OverlayOptions) {
    const rootContainer = options.rootContainer || this.rebirthNGConfig.rootContainer;
    if (!rootContainer) {
      throw new Error('Should setup ViewContainerRef on modal overlay or rebirth config service!');
    }

    if (options.html && (typeof options.html === 'string')) {
      options.html = this.domSanitizer.bypassSecurityTrustHtml(options.html as string);
    }
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(OverlayComponent);
    const injector = options.injector || this.injector;
    this.overlayRef = rootContainer.createComponent(componentFactory, rootContainer.length, injector);
    const cmp = this.overlayRef.instance;
    cmp.addContent(options);
  }

  close(): void {
    this.overlayCount = this.overlayCount > 0 ? this.overlayCount - 1 : 0;
    if (this.overlayCount < 1 && this.overlayRef) {
      this.overlayRef.destroy();
    }
  }
}

results matching ""

    No results matching ""