Last active
December 24, 2016 23:49
-
-
Save shumbo/48b25ff44279e13014b108985f022637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| Injectable, | |
| ViewContainerRef, | |
| ComponentFactoryResolver, | |
| ReflectiveInjector, | |
| ComponentRef, | |
| } from '@angular/core'; | |
| @Injectable() | |
| export class ModalContext { | |
| constructor( | |
| private _resolve: Function, | |
| private _reject: Function | |
| ) { | |
| } | |
| resolve(val?: any) { | |
| this._resolve(val); | |
| } | |
| reject(reason?: any) { | |
| this._reject(reason); | |
| } | |
| } | |
| @Injectable() | |
| export class Modal { | |
| public vcr: ViewContainerRef; | |
| public count = 0; | |
| constructor( | |
| private cfr: ComponentFactoryResolver | |
| ) { | |
| } | |
| isShow() { | |
| return this.count > 0; | |
| } | |
| open<T>(comp: any) { | |
| let cr: ComponentRef<any>; | |
| return new Promise<T>((resolve, reject) => { | |
| const cf = this.cfr.resolveComponentFactory(comp); | |
| const _resolve = (val: T) => { | |
| if (cr) { | |
| cr.destroy(); | |
| resolve(val); | |
| this.count--; | |
| } | |
| }; | |
| const _reject = (reason?: any) => { | |
| if (cr) { | |
| cr.destroy(); | |
| reject(reason); | |
| this.count--; | |
| } | |
| }; | |
| const bindings = ReflectiveInjector.resolve([ | |
| { provide: ModalContext, useValue: new ModalContext(_resolve, _reject) } | |
| ]); | |
| const ctxInjector = this.vcr.parentInjector; | |
| const injector = ReflectiveInjector.fromResolvedProviders(bindings, ctxInjector); | |
| cr = this.vcr.createComponent(cf, this.vcr.length, injector); | |
| this.vcr.element.nativeElement.appendChild(cr.location.nativeElement); | |
| this.count++; | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment