import { Component, ComponentFactoryResolver, ViewChild, ViewContainerRef, AfterViewInit } from '@angular/core'; import { RdComponent } from '../../base/rdComponent'; import { PopUpOptions } from '../../base/rdPopUp'; @Component({ selector: 'rd-modal-instance', template: ` ` }) export class ModalInstance extends RdComponent implements AfterViewInit { @ViewChild('content', { read: ViewContainerRef }) view: ViewContainerRef; @ViewChild('contentStatic', { read: ViewContainerRef }) viewStatic: ViewContainerRef; @ViewChild('dataToggler') dataToggler; @ViewChild('dataTogglerStatic') dataTogglerStatic; private contentReferance; public options = new PopUpOptions(); public size; constructor(private loader: ComponentFactoryResolver) { super(); } ngAfterViewInit() { this.jQuery('#modal-default').on('hidden.bs.modal', function (e) { if (this.contentReferance) this.contentReferance.destroy(); }.bind(this)); } open(type, options: PopUpOptions, callback) { if (this.contentReferance) this.contentReferance.destroy(); this.options = options; switch (this.options.size) { case 'small': this.size = 'modal-sm'; break; case 'medium': this.size = 'modal-md'; break; case 'large': this.size = 'modal-lg'; break; case 'full': this.size = 'modal-full'; break; default: this.size = 'modal-md'; break; } let factory = this.loader.resolveComponentFactory(type); let ref = this.options.closeOnOutsideClick ? this.view.createComponent(factory) : this.viewStatic.createComponent(factory); this.contentReferance = ref; callback(ref.instance); ref.changeDetectorRef.detectChanges(); if (this.options.closeOnOutsideClick) this.dataToggler.nativeElement.click(); else this.dataTogglerStatic.nativeElement.click(); } dismiss() { if (this.options.closeOnOutsideClick) this.dataToggler.nativeElement.click(); else this.dataTogglerStatic.nativeElement.click(); } }