File
Methods
open
|
open(options: ModalOptions)
|
|
Type parameters :
|
Returns : Observable<T>
|
Private
instances
|
instances: []
|
Type : []
|
Default value : []
|
|
import { Injectable, Injector, ComponentFactoryResolver, ComponentRef } from '@angular/core';
import { ModalComponent } from './modal.component';
import { ModalOptions } from './modal-options.model';
import { RebirthNGConfig } from '../rebirth-ng.config';
import { tap, catchError } from 'rxjs/operators';
import { throwError, Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class ModalService {
private instances = [];
constructor(private rebirthNGConfig: RebirthNGConfig, private componentFactoryResolver: ComponentFactoryResolver,
private injector: Injector) {
}
open<T>(options: ModalOptions): Observable<T> {
const rootContainer = options.rootContainer || this.rebirthNGConfig.rootContainer;
if (!rootContainer) {
throw new Error('Should setup ViewContainerRef on modal options or rebirth config service!');
}
if (options.animation === undefined || options.animation === null) {
options.animation = this.rebirthNGConfig.modal.animation;
}
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
const injector = options.injector || this.injector;
const modalRef = <ComponentRef<ModalComponent>>rootContainer
.createComponent(componentFactory, rootContainer.length, injector);
this.instances.push(modalRef);
const instance: ModalComponent = modalRef.instance;
const dismissResult = instance.addContent(options, this.instances.length)
.pipe(
tap(() => this.close(modalRef)),
catchError(error => {
this.close(modalRef);
return throwError(error);
})
);
instance.open();
return dismissResult as any;
}
closeAll(): void {
this.instances.forEach(modalRef => this.close(modalRef));
}
private close(modalRef: ComponentRef<ModalComponent>): void {
this.instances.splice(this.instances.indexOf(modalRef), 1);
const subscriber = modalRef.instance.close()
.subscribe(_ => {
subscriber.unsubscribe();
if (!this.instances.length) {
modalRef.instance.cleanup();
}
modalRef.destroy();
});
}
}