File

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

Index

Properties
Methods

Constructor

constructor(rebirthNGConfig: RebirthNGConfig, componentFactoryResolver: ComponentFactoryResolver)
Parameters :
Name Type Optional
rebirthNGConfig RebirthNGConfig no
componentFactoryResolver ComponentFactoryResolver no

Methods

close
close(alertModel: NotifyModel)
Parameters :
Name Type Optional
alertModel NotifyModel no
Returns : void
closeAll
closeAll()
Returns : void
open
open(notify: NotifyModel, duration?: number)
Parameters :
Name Type Optional
notify NotifyModel no
duration number yes
Returns : void
placement
placement(placement: "top" | "top-right" | "bottom" | "bottom-right" | "center")
Parameters :
Name Type Optional
placement "top" | "top-right" | "bottom" | "bottom-right" | "center" no
Returns : void

Properties

Private _placement
_placement: "top" | "top-right" | "bottom" | "bottom-right" | "center"
Type : "top" | "top-right" | "bottom" | "bottom-right" | "center"
Private panelRef
panelRef: ComponentRef<NotifyComponent>
Type : ComponentRef<NotifyComponent>
import { Injectable, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { RebirthNGConfig } from '../rebirth-ng.config';
import { NotifyComponent } from './notify.component';
import { NotifyModel } from './notify.model';

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

  private panelRef: ComponentRef<NotifyComponent>;
  private _placement: 'top' | 'top-right' | 'bottom' | 'bottom-right' | 'center';

  constructor(private rebirthNGConfig: RebirthNGConfig,
              private componentFactoryResolver: ComponentFactoryResolver) {

  }

  placement(placement: 'top' | 'top-right' | 'bottom' | 'bottom-right' | 'center') {
    if (this.panelRef) {
      this.panelRef.instance.placement = placement || 'top-right';
    }
    this._placement = placement;
  }

  open(notify: NotifyModel, duration?: number) {
    if (!this.panelRef) {
      const rootContainer = this.rebirthNGConfig.rootContainer;
      const componentFactory = this.componentFactoryResolver.resolveComponentFactory(NotifyComponent);
      this.panelRef = rootContainer.createComponent(componentFactory, rootContainer.length);
      if (this.placement) {
        this.panelRef.instance.placement = this._placement;
      }
    }

    this.panelRef.instance.notifies = [...this.panelRef.instance.notifies, notify];
    if (duration) {
      const timeout = setTimeout(() => {
        clearTimeout(timeout);
        this.close(notify);
      }, duration);
    }
  }

  close(alertModel: NotifyModel) {
    this.panelRef.instance.notifies = this.panelRef.instance.notifies.filter((alert) => alert !== alertModel);
    this.panelRef.changeDetectorRef.markForCheck();
  }

  closeAll() {
    this.panelRef.instance.notifies = [];
    this.panelRef.changeDetectorRef.markForCheck();
  }
}

results matching ""

    No results matching ""